Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit b5e1fe6

Browse files
Merge pull request #7 from antonmedv/master
Reintegrate 4.2.0 from Upstream
2 parents 9efe26e + 8e66e80 commit b5e1fe6

File tree

2 files changed

+24
-34
lines changed

2 files changed

+24
-34
lines changed

codejar.ts

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

55-
let listeners: [string, any][] = []
56-
let history: HistoryRecord[] = []
55+
const listeners: [string, any][] = []
56+
const history: HistoryRecord[] = []
5757
let at = -1
5858
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
6560
let prev: string // code content prior keydown event
6661

6762
editor.setAttribute('contenteditable', 'plaintext-only')
@@ -71,9 +66,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
7166
editor.style.overflowY = 'auto'
7267
editor.style.whiteSpace = 'pre-wrap'
7368

74-
let isLegacy = false // true if plaintext-only is not supported
69+
const doHighlight = (editor: HTMLElement, pos?: Position) => {
70+
highlight(editor, pos)
71+
}
7572

76-
highlight(editor)
73+
let isLegacy = false // true if plaintext-only is not supported
7774
if (editor.contentEditable !== 'plaintext-only') isLegacy = true
7875
if (isLegacy) {
7976
editor.setAttribute('contenteditable', 'true')
@@ -83,7 +80,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
8380

8481
const debounceHighlight = debounce(() => {
8582
const pos = save()
86-
highlight(editor, pos)
83+
doHighlight(editor, pos)
8784
restore(pos)
8885
}, 30)
8986

@@ -122,7 +119,6 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
122119
recording = true
123120
}
124121
}
125-
126122
if (isLegacy && !isCopy(event)) restore(save())
127123
})
128124

@@ -132,7 +128,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
132128

133129
if (prev !== toString()) debounceHighlight()
134130
debounceRecordHistory(event)
135-
cb.update(toString())
131+
onUpdate(toString())
136132
})
137133

138134
on('focus', _event => {
@@ -147,14 +143,14 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
147143
recordHistory()
148144
handlePaste(event)
149145
recordHistory()
150-
cb.update(toString())
146+
onUpdate(toString())
151147
})
152148

153149
on('cut', event => {
154150
recordHistory()
155151
handleCut(event)
156152
recordHistory()
157-
cb.update(toString())
153+
onUpdate(toString())
158154
})
159155

160156
function save(): Position {
@@ -218,9 +214,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
218214
}
219215
})
220216

221-
// collapse empty text nodes
222-
editor.normalize()
223-
217+
editor.normalize() // collapse empty text nodes
224218
return pos
225219
}
226220

@@ -287,6 +281,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
287281
}
288282

289283
s.setBaseAndExtent(startNode, startOffset, endNode, endOffset)
284+
editor.normalize() // collapse empty text nodes
290285
}
291286

292287
function uneditable(node: Node): Element | undefined {
@@ -542,18 +537,16 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
542537
}
543538

544539
function handlePaste(event: ClipboardEvent) {
540+
if (event.defaultPrevented) return
545541
preventDefault(event)
546542
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')
551544
const pos = save()
552-
insert(data.text)
553-
highlight(editor)
545+
insert(text)
546+
doHighlight(editor)
554547
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,
557550
dir: '<-',
558551
})
559552
}
@@ -564,7 +557,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
564557
const originalEvent = (event as any).originalEvent ?? event
565558
originalEvent.clipboardData.setData('text/plain', selection.toString())
566559
document.execCommand('delete')
567-
highlight(editor)
560+
doHighlight(editor)
568561
restore({
569562
start: Math.min(pos.start, pos.end),
570563
end: Math.min(pos.start, pos.end),
@@ -663,14 +656,11 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
663656
},
664657
updateCode(code: string) {
665658
editor.textContent = code
666-
highlight(editor)
667-
cb.update(code)
659+
doHighlight(editor)
660+
onUpdate(code)
668661
},
669662
onUpdate(callback: (code: string) => void) {
670-
cb.update = callback
671-
},
672-
onPaste(callback: (data: { text: string }) => void) {
673-
cb.paste = callback
663+
onUpdate = callback
674664
},
675665
toString,
676666
save,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "codejar",
33
"description": "An embeddable code editor for the browser",
4-
"version": "4.1.2",
4+
"version": "4.2.0",
55
"type": "module",
66
"main": "./dist/codejar.js",
77
"types": "./dist/codejar.d.ts",

0 commit comments

Comments
 (0)