aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-11 13:10:06 +0700
committerShulhan <ms@kilabit.info>2023-11-11 13:10:06 +0700
commitf205d88121d1ec31a9db64a8268d5b8913380111 (patch)
tree5ac683587656a459f48b0e9422996b6ebd3b418c
parent91329828bba4f1d6aa8ddfb3e9f801f0d25a4c41 (diff)
downloadpakakeh.ts-f205d88121d1ec31a9db64a8268d5b8913380111.tar.xz
editor: handle paste event manually
On each paste event, re-render the line number in case the clipboard contains new lines.
-rw-r--r--editor/editor.ts22
1 files changed, 18 insertions, 4 deletions
diff --git a/editor/editor.ts b/editor/editor.ts
index 620d6b8..6bdbfca 100644
--- a/editor/editor.ts
+++ b/editor/editor.ts
@@ -98,10 +98,11 @@ export class WuiEditor {
this.elContent.setAttribute("contenteditable", "true");
this.elContent.setAttribute("spellcheck", "false");
- this.elContent.addEventListener("paste", () => {
- setTimeout(() => {
- this.render(this.getContent());
- }, 100);
+ this.elContent.addEventListener("paste", (ev: ClipboardEvent) => {
+ ev.preventDefault();
+ const text = ev.clipboardData?.getData("text/plain");
+ document.execCommand("insertText", false, text);
+ this.renderLineNumber(this.getContent());
});
this.elContent.onkeydown = (ev: KeyboardEvent) => {
@@ -207,4 +208,17 @@ export class WuiEditor {
this.totalLine = lines.length;
}
+
+ private renderLineNumber(content: string) {
+ const lines = content.split("\n");
+
+ this.elLineNumber.innerText = "";
+ lines.forEach((_: string, x: number) => {
+ const el = document.createElement("div");
+ el.innerText = `${x + 1}`;
+ this.elLineNumber.appendChild(el);
+ });
+
+ this.totalLine = lines.length;
+ }
}