From cdcd8b2d559bc367969aa531f32dd1f315d8b759 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 21 Nov 2023 13:29:02 +0700 Subject: editor: replace execCommand with Selection The execCommand has been deprecated according to Mozilla Developer Network. This changes require the tsc target set to es2019 to be able to use the string trimEnd method. [1]: https://developer.mozilla.org/en-US/docs/Web/API/document/execCommand --- editor/editor.ts | 18 ++++++++++++++++-- tsconfig.json | 3 +-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/editor/editor.ts b/editor/editor.ts index c7ce506..1ea0438 100644 --- a/editor/editor.ts +++ b/editor/editor.ts @@ -109,8 +109,22 @@ export class WuiEditor { this.elContent.addEventListener("paste", (ev: ClipboardEvent) => { ev.preventDefault(); - const text = ev.clipboardData?.getData("text/plain"); - document.execCommand("insertText", false, text); + let text: string = ev.clipboardData?.getData("text/plain") || ""; + if (!text) { + console.error(`on paste: text is ${text}`); + return; + } + const selection = window.getSelection(); + if (!selection || !selection.rangeCount) { + console.error(`on paste: failed to get selection`); + return; + } + + text = text.trimEnd(); + selection.deleteFromDocument(); + selection.getRangeAt(0).insertNode(document.createTextNode(text)); + selection.collapseToEnd(); + this.renderLineNumber(this.getContent()); }); diff --git a/tsconfig.json b/tsconfig.json index 508e44b..d7fc0f4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,6 @@ { "compilerOptions": { - /* Visit https://aka.ms/tsconfig.json to read more about this file */ - "target": "es2018", + "target": "es2019", "module": "es2020", "isolatedModules": true, "esModuleInterop": true, -- cgit v1.3