From ed03d0f0ed8b8bc2ed2a75d4cb14b2e4e26c6f47 Mon Sep 17 00:00:00 2001 From: mike76 Date: Mon, 30 Nov 2020 23:27:31 +0100 Subject: [PATCH] fixed paste over selection issue (#39) --- codejar.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/codejar.ts b/codejar.ts index 7b99486..6a0f397 100644 --- a/codejar.ts +++ b/codejar.ts @@ -336,10 +336,16 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement) => void function handlePaste(event: ClipboardEvent) { preventDefault(event) const text = ((event as any).originalEvent || event).clipboardData.getData("text/plain") + const sel = window.getSelection() + const selection = !sel.isCollapsed const pos = save() insert(text) highlight(editor) - restore({start: pos.end + text.length, end: pos.end + text.length}) + if (selection) { + restore({start: pos.end, end: pos.end}) + } else { + restore({start: pos.end + text.length, end: pos.end + text.length}) + } }