@@ -3,39 +3,39 @@ export function wrapTextWith (editor, cm, symbol) {
33 if ( ! cm . getSelection ( ) ) {
44 return CodeMirror . Pass
55 } else {
6- var ranges = cm . listSelections ( )
7- for ( var i = 0 ; i < ranges . length ; i ++ ) {
8- var range = ranges [ i ]
6+ let ranges = cm . listSelections ( )
7+ for ( let i = 0 ; i < ranges . length ; i ++ ) {
8+ let range = ranges [ i ]
99 if ( ! range . empty ( ) ) {
1010 const from = range . from ( )
1111 const to = range . to ( )
1212
1313 if ( symbol !== 'Backspace' ) {
14- cm . replaceRange ( symbol , to , to , '+input' )
15- cm . replaceRange ( symbol , from , from , '+input' )
16- // workaround selection range not correct after add symbol
17- var _ranges = cm . listSelections ( )
18- var anchorIndex = editor . indexFromPos ( _ranges [ i ] . anchor )
19- var headIndex = editor . indexFromPos ( _ranges [ i ] . head )
14+ let selection = cm . getRange ( from , to )
15+ let anchorIndex = editor . indexFromPos ( ranges [ i ] . anchor )
16+ let headIndex = editor . indexFromPos ( ranges [ i ] . head )
17+ cm . replaceRange ( symbol + selection + symbol , from , to , '+input' )
2018 if ( anchorIndex > headIndex ) {
21- _ranges [ i ] . anchor . ch --
19+ ranges [ i ] . anchor . ch += symbol . length
20+ ranges [ i ] . head . ch += symbol . length
2221 } else {
23- _ranges [ i ] . head . ch --
22+ ranges [ i ] . head . ch += symbol . length
23+ ranges [ i ] . anchor . ch += symbol . length
2424 }
25- cm . setSelections ( _ranges )
25+ cm . setSelections ( ranges )
2626 } else {
27- var preEndPos = {
27+ let preEndPos = {
2828 line : to . line ,
29- ch : to . ch + 1
29+ ch : to . ch + symbol . length
3030 }
31- var preText = cm . getRange ( to , preEndPos )
32- var preIndex = wrapSymbols . indexOf ( preText )
33- var postEndPos = {
31+ let preText = cm . getRange ( to , preEndPos )
32+ let preIndex = wrapSymbols . indexOf ( preText )
33+ let postEndPos = {
3434 line : from . line ,
35- ch : from . ch - 1
35+ ch : from . ch - symbol . length
3636 }
37- var postText = cm . getRange ( postEndPos , from )
38- var postIndex = wrapSymbols . indexOf ( postText )
37+ let postText = cm . getRange ( postEndPos , from )
38+ let postIndex = wrapSymbols . indexOf ( postText )
3939 // check if surround symbol are list in array and matched
4040 if ( preIndex > - 1 && postIndex > - 1 && preIndex === postIndex ) {
4141 cm . replaceRange ( '' , to , preEndPos , '+delete' )
@@ -48,12 +48,44 @@ export function wrapTextWith (editor, cm, symbol) {
4848}
4949
5050export function insertText ( cm , text , cursorEnd = 0 ) {
51- var cursor = cm . getCursor ( )
51+ let cursor = cm . getCursor ( )
5252 cm . replaceSelection ( text , cursor , cursor )
5353 cm . focus ( )
5454 cm . setCursor ( { line : cursor . line , ch : cursor . ch + cursorEnd } )
5555}
5656
57+ export function insertLink ( cm , isImage ) {
58+ let cursor = cm . getCursor ( )
59+ let ranges = cm . listSelections ( )
60+ const linkEnd = '](https://)'
61+ const symbol = ( isImage ) ? '![' : '['
62+
63+ for ( let i = 0 ; i < ranges . length ; i ++ ) {
64+ let range = ranges [ i ]
65+ if ( ! range . empty ( ) ) {
66+ const from = range . from ( )
67+ const to = range . to ( )
68+ let anchorIndex = editor . indexFromPos ( ranges [ i ] . anchor )
69+ let headIndex = editor . indexFromPos ( ranges [ i ] . head )
70+ let selection = cm . getRange ( from , to )
71+ selection = symbol + selection + linkEnd
72+ cm . replaceRange ( selection , from , to )
73+ if ( anchorIndex > headIndex ) {
74+ ranges [ i ] . anchor . ch += symbol . length
75+ ranges [ i ] . head . ch += symbol . length
76+ } else {
77+ ranges [ i ] . head . ch += symbol . length
78+ ranges [ i ] . anchor . ch += symbol . length
79+ }
80+ cm . setSelections ( ranges )
81+ } else {
82+ cm . replaceRange ( symbol + linkEnd , cursor , cursor )
83+ cm . setCursor ( { line : cursor . line , ch : cursor . ch + symbol . length + linkend . length } )
84+ }
85+ }
86+ cm . focus ( )
87+ }
88+
5789export function insertHeader ( cm ) {
5890 let cursor = cm . getCursor ( )
5991 let startOfLine = { line : cursor . line , ch : 0 }
@@ -67,22 +99,23 @@ export function insertHeader (cm) {
6799 cm . focus ( )
68100}
69101
70- export function insertOnStartOfLines ( cm , symbol , cursorEnd ) {
102+ export function insertOnStartOfLines ( cm , symbol ) {
71103 let cursor = cm . getCursor ( )
72- var ranges = cm . listSelections ( )
104+ let ranges = cm . listSelections ( )
73105
74106 for ( let i = 0 ; i < ranges . length ; i ++ ) {
75- var range = ranges [ i ]
107+ let range = ranges [ i ]
76108 if ( ! range . empty ( ) ) {
77109 const from = range . from ( )
78110 const to = range . to ( )
79- for ( let j = from . line ; j <= to . line ; ++ j ) {
80- cm . replaceRange ( symbol , { line : j , ch : 0 } , { line : j , ch : 0 } )
81- }
111+ let selection = cm . getRange ( { line : from . line , ch : 0 } , to )
112+ selection = selection . replace ( / \n / g, '\n' + symbol )
113+ selection = symbol + selection
114+ cm . replaceRange ( selection , from , to )
82115 } else {
83116 cm . replaceRange ( symbol , { line : cursor . line , ch : 0 } , { line : cursor . line , ch : 0 } )
84117 }
85118 }
86- cm . setCursor ( { line : cursor . line , ch : ( cursorEnd ) ? cursorEnd : cursor . ch } )
119+ cm . setCursor ( { line : cursor . line , ch : cursor . ch + symbol . length } )
87120 cm . focus ( )
88121}
0 commit comments