Skip to content

Commit 89940be

Browse files
committed
Respect defaultPrevented in handlePaste
1 parent 2935f6a commit 89940be

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

codejar.ts

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
4242
const window = options.window
4343
const document = window.document
4444

45-
let listeners: [string, any][] = []
46-
let history: HistoryRecord[] = []
45+
const listeners: [string, any][] = []
46+
const history: HistoryRecord[] = []
4747
let at = -1
4848
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
5550
let prev: string // code content prior keydown event
5651

5752
editor.setAttribute('contenteditable', 'plaintext-only')
@@ -61,15 +56,17 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
6156
editor.style.overflowY = 'auto'
6257
editor.style.whiteSpace = 'pre-wrap'
6358

64-
let isLegacy = false // true if plaintext-only is not supported
59+
const doHighlight = (editor: HTMLElement, pos?: Position) => {
60+
highlight(editor, pos)
61+
}
6562

66-
highlight(editor)
63+
let isLegacy = false // true if plaintext-only is not supported
6764
if (editor.contentEditable !== 'plaintext-only') isLegacy = true
6865
if (isLegacy) editor.setAttribute('contenteditable', 'true')
6966

7067
const debounceHighlight = debounce(() => {
7168
const pos = save()
72-
highlight(editor, pos)
69+
doHighlight(editor, pos)
7370
restore(pos)
7471
}, 30)
7572

@@ -108,7 +105,6 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
108105
recording = true
109106
}
110107
}
111-
112108
if (isLegacy && !isCopy(event)) restore(save())
113109
})
114110

@@ -118,7 +114,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
118114

119115
if (prev !== toString()) debounceHighlight()
120116
debounceRecordHistory(event)
121-
cb.update(toString())
117+
onUpdate(toString())
122118
})
123119

124120
on('focus', _event => {
@@ -133,14 +129,14 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
133129
recordHistory()
134130
handlePaste(event)
135131
recordHistory()
136-
cb.update(toString())
132+
onUpdate(toString())
137133
})
138134

139135
on('cut', event => {
140136
recordHistory()
141137
handleCut(event)
142138
recordHistory()
143-
cb.update(toString())
139+
onUpdate(toString())
144140
})
145141

146142
function save(): Position {
@@ -204,9 +200,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
204200
}
205201
})
206202

207-
// collapse empty text nodes
208-
editor.normalize()
209-
203+
editor.normalize() // collapse empty text nodes
210204
return pos
211205
}
212206

@@ -273,6 +267,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
273267
}
274268

275269
s.setBaseAndExtent(startNode, startOffset, endNode, endOffset)
270+
editor.normalize() // collapse empty text nodes
276271
}
277272

278273
function uneditable(node: Node): Element | undefined {
@@ -438,18 +433,16 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
438433
}
439434

440435
function handlePaste(event: ClipboardEvent) {
436+
if (event.defaultPrevented) return
441437
preventDefault(event)
442438
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')
447440
const pos = save()
448-
insert(data.text)
449-
highlight(editor)
441+
insert(text)
442+
doHighlight(editor)
450443
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,
453446
dir: '<-',
454447
})
455448
}
@@ -460,7 +453,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
460453
const originalEvent = (event as any).originalEvent ?? event
461454
originalEvent.clipboardData.setData('text/plain', selection.toString())
462455
document.execCommand('delete')
463-
highlight(editor)
456+
doHighlight(editor)
464457
restore({
465458
start: Math.min(pos.start, pos.end),
466459
end: Math.min(pos.start, pos.end),
@@ -553,14 +546,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
553546
},
554547
updateCode(code: string) {
555548
editor.textContent = code
556-
highlight(editor)
557-
cb.update(code)
549+
doHighlight(editor)
550+
onUpdate(code)
558551
},
559552
onUpdate(callback: (code: string) => void) {
560-
cb.update = callback
561-
},
562-
onPaste(callback: (data: { text: string }) => void) {
563-
cb.paste = callback
553+
onUpdate = callback
564554
},
565555
toString,
566556
save,

0 commit comments

Comments
 (0)