@@ -42,16 +42,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
42
42
const window = options . window
43
43
const document = window . document
44
44
45
- let listeners : [ string , any ] [ ] = [ ]
46
- let history : HistoryRecord [ ] = [ ]
45
+ const listeners : [ string , any ] [ ] = [ ]
46
+ const history : HistoryRecord [ ] = [ ]
47
47
let at = - 1
48
48
let focus = false
49
- const cb = {
50
- update ( code : string ) : void | undefined {
51
- } ,
52
- paste ( data : { text : string } ) : void {
53
- } ,
54
- }
49
+ let onUpdate : ( code : string ) => void | undefined = ( ) => void 0
55
50
let prev : string // code content prior keydown event
56
51
57
52
editor . setAttribute ( 'contenteditable' , 'plaintext-only' )
@@ -61,15 +56,17 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
61
56
editor . style . overflowY = 'auto'
62
57
editor . style . whiteSpace = 'pre-wrap'
63
58
64
- let isLegacy = false // true if plaintext-only is not supported
59
+ const doHighlight = ( editor : HTMLElement , pos ?: Position ) => {
60
+ highlight ( editor , pos )
61
+ }
65
62
66
- highlight ( editor )
63
+ let isLegacy = false // true if plaintext-only is not supported
67
64
if ( editor . contentEditable !== 'plaintext-only' ) isLegacy = true
68
65
if ( isLegacy ) editor . setAttribute ( 'contenteditable' , 'true' )
69
66
70
67
const debounceHighlight = debounce ( ( ) => {
71
68
const pos = save ( )
72
- highlight ( editor , pos )
69
+ doHighlight ( editor , pos )
73
70
restore ( pos )
74
71
} , 30 )
75
72
@@ -108,7 +105,6 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
108
105
recording = true
109
106
}
110
107
}
111
-
112
108
if ( isLegacy && ! isCopy ( event ) ) restore ( save ( ) )
113
109
} )
114
110
@@ -118,7 +114,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
118
114
119
115
if ( prev !== toString ( ) ) debounceHighlight ( )
120
116
debounceRecordHistory ( event )
121
- cb . update ( toString ( ) )
117
+ onUpdate ( toString ( ) )
122
118
} )
123
119
124
120
on ( 'focus' , _event => {
@@ -133,14 +129,14 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
133
129
recordHistory ( )
134
130
handlePaste ( event )
135
131
recordHistory ( )
136
- cb . update ( toString ( ) )
132
+ onUpdate ( toString ( ) )
137
133
} )
138
134
139
135
on ( 'cut' , event => {
140
136
recordHistory ( )
141
137
handleCut ( event )
142
138
recordHistory ( )
143
- cb . update ( toString ( ) )
139
+ onUpdate ( toString ( ) )
144
140
} )
145
141
146
142
function save ( ) : Position {
@@ -204,9 +200,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
204
200
}
205
201
} )
206
202
207
- // collapse empty text nodes
208
- editor . normalize ( )
209
-
203
+ editor . normalize ( ) // collapse empty text nodes
210
204
return pos
211
205
}
212
206
@@ -273,6 +267,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
273
267
}
274
268
275
269
s . setBaseAndExtent ( startNode , startOffset , endNode , endOffset )
270
+ editor . normalize ( ) // collapse empty text nodes
276
271
}
277
272
278
273
function uneditable ( node : Node ) : Element | undefined {
@@ -438,18 +433,16 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
438
433
}
439
434
440
435
function handlePaste ( event : ClipboardEvent ) {
436
+ if ( event . defaultPrevented ) return
441
437
preventDefault ( event )
442
438
const originalEvent = ( event as any ) . originalEvent ?? event
443
- const data = {
444
- text : originalEvent . clipboardData . getData ( 'text/plain' ) . replace ( / \r \n ? / g, '\n' ) ,
445
- }
446
- cb . paste ( data )
439
+ const text = originalEvent . clipboardData . getData ( 'text/plain' ) . replace ( / \r \n ? / g, '\n' )
447
440
const pos = save ( )
448
- insert ( data . text )
449
- highlight ( editor )
441
+ insert ( text )
442
+ doHighlight ( editor )
450
443
restore ( {
451
- start : Math . min ( pos . start , pos . end ) + data . text . length ,
452
- end : Math . min ( pos . start , pos . end ) + data . text . length ,
444
+ start : Math . min ( pos . start , pos . end ) + text . length ,
445
+ end : Math . min ( pos . start , pos . end ) + text . length ,
453
446
dir : '<-' ,
454
447
} )
455
448
}
@@ -460,7 +453,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
460
453
const originalEvent = ( event as any ) . originalEvent ?? event
461
454
originalEvent . clipboardData . setData ( 'text/plain' , selection . toString ( ) )
462
455
document . execCommand ( 'delete' )
463
- highlight ( editor )
456
+ doHighlight ( editor )
464
457
restore ( {
465
458
start : Math . min ( pos . start , pos . end ) ,
466
459
end : Math . min ( pos . start , pos . end ) ,
@@ -553,14 +546,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
553
546
} ,
554
547
updateCode ( code : string ) {
555
548
editor . textContent = code
556
- highlight ( editor )
557
- cb . update ( code )
549
+ doHighlight ( editor )
550
+ onUpdate ( code )
558
551
} ,
559
552
onUpdate ( callback : ( code : string ) => void ) {
560
- cb . update = callback
561
- } ,
562
- onPaste ( callback : ( data : { text : string } ) => void ) {
563
- cb . paste = callback
553
+ onUpdate = callback
564
554
} ,
565
555
toString,
566
556
save,
0 commit comments