@@ -52,16 +52,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
52
52
const window = options . window
53
53
const document = window . document
54
54
55
- let listeners : [ string , any ] [ ] = [ ]
56
- let history : HistoryRecord [ ] = [ ]
55
+ const listeners : [ string , any ] [ ] = [ ]
56
+ const history : HistoryRecord [ ] = [ ]
57
57
let at = - 1
58
58
let focus = false
59
- const cb = {
60
- update ( code : string ) : void | undefined {
61
- } ,
62
- paste ( data : { text : string } ) : void {
63
- } ,
64
- }
59
+ let onUpdate : ( code : string ) => void | undefined = ( ) => void 0
65
60
let prev : string // code content prior keydown event
66
61
67
62
editor . setAttribute ( 'contenteditable' , 'plaintext-only' )
@@ -71,9 +66,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
71
66
editor . style . overflowY = 'auto'
72
67
editor . style . whiteSpace = 'pre-wrap'
73
68
74
- let isLegacy = false // true if plaintext-only is not supported
69
+ const doHighlight = ( editor : HTMLElement , pos ?: Position ) => {
70
+ highlight ( editor , pos )
71
+ }
75
72
76
- highlight ( editor )
73
+ let isLegacy = false // true if plaintext-only is not supported
77
74
if ( editor . contentEditable !== 'plaintext-only' ) isLegacy = true
78
75
if ( isLegacy ) {
79
76
editor . setAttribute ( 'contenteditable' , 'true' )
@@ -83,7 +80,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
83
80
84
81
const debounceHighlight = debounce ( ( ) => {
85
82
const pos = save ( )
86
- highlight ( editor , pos )
83
+ doHighlight ( editor , pos )
87
84
restore ( pos )
88
85
} , 30 )
89
86
@@ -122,7 +119,6 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
122
119
recording = true
123
120
}
124
121
}
125
-
126
122
if ( isLegacy && ! isCopy ( event ) ) restore ( save ( ) )
127
123
} )
128
124
@@ -132,7 +128,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
132
128
133
129
if ( prev !== toString ( ) ) debounceHighlight ( )
134
130
debounceRecordHistory ( event )
135
- cb . update ( toString ( ) )
131
+ onUpdate ( toString ( ) )
136
132
} )
137
133
138
134
on ( 'focus' , _event => {
@@ -147,14 +143,14 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
147
143
recordHistory ( )
148
144
handlePaste ( event )
149
145
recordHistory ( )
150
- cb . update ( toString ( ) )
146
+ onUpdate ( toString ( ) )
151
147
} )
152
148
153
149
on ( 'cut' , event => {
154
150
recordHistory ( )
155
151
handleCut ( event )
156
152
recordHistory ( )
157
- cb . update ( toString ( ) )
153
+ onUpdate ( toString ( ) )
158
154
} )
159
155
160
156
function save ( ) : Position {
@@ -218,9 +214,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
218
214
}
219
215
} )
220
216
221
- // collapse empty text nodes
222
- editor . normalize ( )
223
-
217
+ editor . normalize ( ) // collapse empty text nodes
224
218
return pos
225
219
}
226
220
@@ -287,6 +281,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
287
281
}
288
282
289
283
s . setBaseAndExtent ( startNode , startOffset , endNode , endOffset )
284
+ editor . normalize ( ) // collapse empty text nodes
290
285
}
291
286
292
287
function uneditable ( node : Node ) : Element | undefined {
@@ -542,18 +537,16 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
542
537
}
543
538
544
539
function handlePaste ( event : ClipboardEvent ) {
540
+ if ( event . defaultPrevented ) return
545
541
preventDefault ( event )
546
542
const originalEvent = ( event as any ) . originalEvent ?? event
547
- const data = {
548
- text : originalEvent . clipboardData . getData ( 'text/plain' ) . replace ( / \r \n ? / g, '\n' ) ,
549
- }
550
- cb . paste ( data )
543
+ const text = originalEvent . clipboardData . getData ( 'text/plain' ) . replace ( / \r \n ? / g, '\n' )
551
544
const pos = save ( )
552
- insert ( data . text )
553
- highlight ( editor )
545
+ insert ( text )
546
+ doHighlight ( editor )
554
547
restore ( {
555
- start : Math . min ( pos . start , pos . end ) + data . text . length ,
556
- end : Math . min ( pos . start , pos . end ) + data . text . length ,
548
+ start : Math . min ( pos . start , pos . end ) + text . length ,
549
+ end : Math . min ( pos . start , pos . end ) + text . length ,
557
550
dir : '<-' ,
558
551
} )
559
552
}
@@ -564,7 +557,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
564
557
const originalEvent = ( event as any ) . originalEvent ?? event
565
558
originalEvent . clipboardData . setData ( 'text/plain' , selection . toString ( ) )
566
559
document . execCommand ( 'delete' )
567
- highlight ( editor )
560
+ doHighlight ( editor )
568
561
restore ( {
569
562
start : Math . min ( pos . start , pos . end ) ,
570
563
end : Math . min ( pos . start , pos . end ) ,
@@ -663,14 +656,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
663
656
} ,
664
657
updateCode ( code : string ) {
665
658
editor . textContent = code
666
- highlight ( editor )
667
- cb . update ( code )
659
+ doHighlight ( editor )
660
+ onUpdate ( code )
668
661
} ,
669
662
onUpdate ( callback : ( code : string ) => void ) {
670
- cb . update = callback
671
- } ,
672
- onPaste ( callback : ( data : { text : string } ) => void ) {
673
- cb . paste = callback
663
+ onUpdate = callback
674
664
} ,
675
665
toString,
676
666
save,
0 commit comments