@@ -66,6 +66,8 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
66
66
if ( editor . contentEditable !== 'plaintext-only' ) isLegacy = true
67
67
if ( isLegacy ) editor . setAttribute ( 'contenteditable' , 'true' )
68
68
69
+ recordHistory ( )
70
+
69
71
const debounceHighlight = debounce ( ( ) => {
70
72
const pos = save ( )
71
73
highlight ( editor , pos )
@@ -105,10 +107,10 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
105
107
106
108
function save ( ) : Position {
107
109
const s = getSelection ( )
108
- const pos : Position = { start : 0 , end : 0 , dir : undefined }
110
+ const pos : Position = { start : 0 , end : 0 }
109
111
110
112
let { anchorNode, anchorOffset, focusNode, focusOffset} = s
111
- if ( ! anchorNode || ! focusNode ) throw 'error1'
113
+ if ( ! anchorNode || ! focusNode ) return history [ at ] ?. pos ?? pos
112
114
113
115
// Selection anchor and focus are expected to be text nodes,
114
116
// so normalize them.
@@ -285,14 +287,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
285
287
if ( isLegacy && event . key === 'Enter' ) {
286
288
event . preventDefault ( )
287
289
event . stopPropagation ( )
288
- if ( aroundCursor ( ) . after === '' ) {
289
- insert ( '\n ' )
290
- const pos = save ( )
291
- pos . start = -- pos . end
292
- restore ( pos )
293
- } else {
294
- insert ( '\n' )
295
- }
290
+ insert ( '\n' )
296
291
}
297
292
}
298
293
@@ -415,8 +410,13 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
415
410
function insert ( text : string ) {
416
411
let { start} = save ( )
417
412
const { before, after} = aroundCursor ( )
418
- editor . textContent = before + text + after
419
413
start += text . length
414
+
415
+ // the last line break isn't shown and it can cause editing issues
416
+ // so, add an extra line break in order to avoid those issues
417
+ if ( after === '' && text . endsWith ( '\n' ) ) text += '\n'
418
+
419
+ editor . textContent = before + text + after
420
420
restore ( { start, end : start } )
421
421
}
422
422
@@ -452,7 +452,9 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
452
452
Object . assign ( options , newOptions )
453
453
} ,
454
454
updateCode ( code : string , callOnUpdate : boolean = true ) {
455
- editor . textContent = code
455
+ recordHistory ( )
456
+ editor . textContent = ''
457
+ insert ( code )
456
458
highlight ( editor )
457
459
if ( callOnUpdate ) onUpdate ( code )
458
460
} ,
0 commit comments