Skip to content

Commit

Permalink
Add onPaste event
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jul 26, 2023
1 parent f730c38 commit 90b0b86
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions codejar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
addClosing: true,
history: true,
window: globalWindow,
...opt
...opt,
}

const window = options.window
Expand All @@ -46,7 +46,12 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
let history: HistoryRecord[] = []
let at = -1
let focus = false
let callback: (code: string) => void | undefined
const cb = {
update(code: string): void | undefined {
},
paste(data: { text: string }): void {
},
}
let prev: string // code content prior keydown event

editor.setAttribute('contenteditable', 'plaintext-only')
Expand Down Expand Up @@ -113,7 +118,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P

if (prev !== toString()) debounceHighlight()
debounceRecordHistory(event)
if (callback) callback(toString())
cb.update(toString())
})

on('focus', _event => {
Expand All @@ -128,14 +133,14 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
recordHistory()
handlePaste(event)
recordHistory()
if (callback) callback(toString())
cb.update(toString())
})

on('cut', event => {
recordHistory()
handleCut(event)
recordHistory()
if (callback) callback(toString())
cb.update(toString())
})

function save(): Position {
Expand Down Expand Up @@ -338,7 +343,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
preventDefault(event)
if (event.shiftKey) {
const before = beforeCursor()
let [padding, start,] = findPadding(before)
let [padding, start] = findPadding(before)
if (padding.length > 0) {
const pos = save()
// Remove full length tab or just remaining padding
Expand Down Expand Up @@ -404,16 +409,17 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P

function handlePaste(event: ClipboardEvent) {
preventDefault(event)
const text = ((event as any).originalEvent || event)
.clipboardData
.getData('text/plain')
.replace(/\r\n?/g, '\n')
const originalEvent = (event as any).originalEvent ?? event
const data = {
text: originalEvent.clipboardData.getData('text/plain').replace(/\r\n?/g, '\n'),
}
cb.paste(data)
const pos = save()
insert(text)
insert(data.text)
highlight(editor)
restore({
start: Math.min(pos.start, pos.end) + text.length,
end: Math.min(pos.start, pos.end) + text.length,
start: Math.min(pos.start, pos.end) + data.text.length,
end: Math.min(pos.start, pos.end) + data.text.length,
dir: '<-',
})
}
Expand Down Expand Up @@ -518,10 +524,13 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
updateCode(code: string) {
editor.textContent = code
highlight(editor)
if (callback) callback(code)
cb.update(code)
},
onUpdate(callback: (code: string) => void) {
cb.update = callback
},
onUpdate(cb: (code: string) => void) {
callback = cb
onPaste(callback: (data: { text: string }) => void) {
cb.paste = callback
},
toString,
save,
Expand Down

0 comments on commit 90b0b86

Please sign in to comment.