@@ -9,92 +9,88 @@ class ChineseSymbolAutoPairerPlugin extends BaseCustomPlugin {
99 selector = ( ) => this . utils . disableForeverSelector
1010
1111 init = ( ) => {
12- const reverseMap = map => {
13- const result = new Map ( ) ;
14- map . forEach ( ( v , k ) => result . set ( v , k ) ) ;
15- return result
16- }
12+ this . rangyText = ""
1713
18- this . rangyText = "" ;
19- this . pairMap = new Map ( this . config . auto_pair_symbols )
2014 this . swapMap = new Map ( this . config . auto_swap_symbols )
15+ this . pairMap = new Map ( this . config . auto_pair_symbols )
16+ this . reversePairMap = new Map ( this . config . auto_pair_symbols . map ( ( [ k , v ] ) => [ v , k ] ) )
2117 this . codeSet = new Set ( [
2218 "Digit1" , "Digit2" , "Digit3" , "Digit4" , "Digit5" , "Digit6" , "Digit7" , "Digit8" , "Digit9" , "Digit0" ,
2319 "Backquote" , "BracketLeft" , "BracketRight" , "Backslash" , "Semicolon" , "Quote" , "Comma" , "Period" , "Slash" ,
2420 ] )
25- this . reversePairMap = reverseMap ( this . pairMap )
21+
2622 const until = ( ) => File ?. editor ?. undo ?. UndoManager ?. SnapFlag
2723 const after = ( ) => this . undoSnapType = File . editor . undo . UndoManager . SnapFlag
2824 this . utils . pollUntil ( until , after )
2925 }
3026
3127 process = ( ) => {
3228 this . utils . entities . eWrite . addEventListener ( "input" , this . utils . throttle ( ev => {
33- if ( File . option . noPairingMatch || document . activeElement . tagName === "TEXTAREA" ) return ;
29+ if ( File . option . noPairingMatch || document . activeElement . tagName === "TEXTAREA" ) return
3430
35- const inputSymbol = ev . data ;
36- const pairSymbol = this . pairMap . get ( inputSymbol ) ;
31+ const inputSymbol = ev . data
32+ const pairSymbol = this . pairMap . get ( inputSymbol )
3733 if ( pairSymbol ) {
38- this . insertText ( this . rangyText + pairSymbol ) ;
39- setTimeout ( this . selectText , 50 ) ;
34+ this . insertText ( this . rangyText + pairSymbol )
35+ setTimeout ( this . selectText , 50 )
4036 } else if ( this . config . auto_skip && this . reversePairMap . get ( inputSymbol ) ) {
41- this . skipSymbol ( inputSymbol ) ;
37+ this . skipSymbol ( inputSymbol )
4238 }
4339 if ( this . config . auto_swap && this . swapMap . has ( inputSymbol ) ) {
4440 this . swapSymbol ( inputSymbol )
4541 }
46- } , 30 ) ) ;
42+ } , 30 ) )
4743
4844 if ( this . config . auto_delete_pair || this . config . auto_surround_pair ) {
4945 this . utils . entities . eWrite . addEventListener ( "keydown" , ev => {
50- if ( File . option . noPairingMatch || document . activeElement . tagName === "TEXTAREA" ) return ;
46+ if ( File . option . noPairingMatch || document . activeElement . tagName === "TEXTAREA" ) return
5147
5248 if ( this . config . auto_surround_pair && this . utils . isIMEActivated ( ev ) && this . codeSet . has ( ev . code ) ) {
53- this . rangyText = this . utils . getRangyText ( ) ;
49+ this . rangyText = this . utils . getRangyText ( )
5450 }
5551 if ( this . config . auto_delete_pair && ev . key === "Backspace" && ! ev . shiftKey && ! ev . altKey && ! this . utils . metaKeyPressed ( ev ) ) {
56- this . deletePair ( ) ;
52+ this . deletePair ( )
5753 }
58- } , true ) ;
54+ } , true )
5955 }
6056 }
6157
6258 selectText = ( ) => {
6359 if ( this . config . auto_select_after_surround || this . rangyText ) {
64- const { range, bookmark } = this . utils . getRangy ( ) ;
65- bookmark . end += this . rangyText . length ;
66- range . moveToBookmark ( bookmark ) ;
67- range . select ( ) ;
60+ const { range, bookmark } = this . utils . getRangy ( )
61+ bookmark . end += this . rangyText . length
62+ range . moveToBookmark ( bookmark )
63+ range . select ( )
6864 }
69- this . rangyText = "" ;
65+ this . rangyText = ""
7066 }
7167
7268 insertText = symbol => {
73- const { range, node } = this . utils . getRangy ( ) ;
74- const textNode = document . createTextNode ( symbol ) ;
75- range . insertNode ( textNode ) ;
76- File . editor . undo . addSnap ( node . cid , this . undoSnapType . REPLACE ) ;
69+ const { range, node } = this . utils . getRangy ( )
70+ const textNode = document . createTextNode ( symbol )
71+ range . insertNode ( textNode )
72+ File . editor . undo . addSnap ( node . cid , this . undoSnapType . REPLACE )
7773 }
7874
7975 _getRange = ( ) => {
80- const { node, bookmark } = this . utils . getRangy ( ) ;
81- if ( ! node ) return { } ;
76+ const { node, bookmark } = this . utils . getRangy ( )
77+ if ( ! node ) return { }
8278
83- File . editor . undo . endSnap ( ) ;
84- File . editor . undo . addSnap ( node . cid , this . undoSnapType . NONE ) ;
85- const ele = File . editor . findElemById ( node . cid ) ;
86- if ( ele . hasClass ( "md-fences" ) ) return { } ;
79+ File . editor . undo . endSnap ( )
80+ File . editor . undo . addSnap ( node . cid , this . undoSnapType . NONE )
81+ const ele = File . editor . findElemById ( node . cid )
82+ if ( ele . hasClass ( "md-fences" ) ) return { }
8783
88- const rawText = ele . rawText ( ) ;
89- return { rawText, bookmark } ;
84+ const rawText = ele . rawText ( )
85+ return { rawText, bookmark }
9086 }
9187
9288 skipSymbol = inputSymbol => {
93- const { rawText, bookmark } = this . _getRange ( ) ;
94- if ( ! rawText || ! bookmark ) return ;
89+ const { rawText, bookmark } = this . _getRange ( )
90+ if ( ! rawText || ! bookmark ) return
9591 if ( inputSymbol === rawText . substring ( bookmark . start , bookmark . start + 1 ) ) {
96- bookmark . end += 1 ;
97- this . deleteContent ( bookmark ) ;
92+ bookmark . end += 1
93+ this . deleteContent ( bookmark )
9894 }
9995 }
10096
@@ -144,22 +140,22 @@ class ChineseSymbolAutoPairerPlugin extends BaseCustomPlugin {
144140 }
145141
146142 deletePair = ( ) => {
147- const { rawText, bookmark } = this . _getRange ( ) ;
148- if ( ! rawText || ! bookmark ) return ;
149- const pair = rawText . substring ( bookmark . start - 1 , bookmark . start + 1 ) ;
143+ const { rawText, bookmark } = this . _getRange ( )
144+ if ( ! rawText || ! bookmark ) return
145+ const pair = rawText . substring ( bookmark . start - 1 , bookmark . start + 1 )
150146 if ( pair . length === 2 ) {
151- const [ left , right ] = pair ;
147+ const [ left , right ] = pair
152148 if ( this . reversePairMap . get ( right ) === left && this . pairMap . get ( left ) === right ) {
153- bookmark . end += 1 ;
154- this . deleteContent ( bookmark ) ;
149+ bookmark . end += 1
150+ this . deleteContent ( bookmark )
155151 }
156152 }
157153 }
158154
159155 deleteContent = bookmark => {
160- const newRange = File . editor . selection . rangy . createRange ( ) ;
161- newRange . moveToBookmark ( bookmark ) ;
162- newRange . deleteContents ( ) ;
156+ const range = File . editor . selection . rangy . createRange ( )
157+ range . moveToBookmark ( bookmark )
158+ range . deleteContents ( )
163159 }
164160}
165161
0 commit comments