diff options
| author | Shulhan <ms@kilabit.info> | 2023-10-30 13:25:39 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-10-30 13:25:39 +0700 |
| commit | fde0b661b85ec2c813a806d5406a47e8023ba9a0 (patch) | |
| tree | 9c22d17bc77efdec0a4dbbe0883de0de6cb1a990 | |
| parent | 85c3fc0431e7e75a41894d4669f6a46bbda5440b (diff) | |
| download | pakakeh.ts-fde0b661b85ec2c813a806d5406a47e8023ba9a0.tar.xz | |
editor: fix handling control key
Instead of binding key up and down on each line for handling, bind it to
document.
| -rw-r--r-- | editor/editor.ts | 91 |
1 files changed, 40 insertions, 51 deletions
diff --git a/editor/editor.ts b/editor/editor.ts index 0e919ab..4ca0cce 100644 --- a/editor/editor.ts +++ b/editor/editor.ts @@ -55,6 +55,9 @@ export class WuiEditor { this.sel = sel; this.range = document.createRange(); + document.onkeydown = (ev: KeyboardEvent) => { + this.onKeydownDocument(this, ev); + }; document.onkeyup = (ev: KeyboardEvent) => { this.onKeyupDocument(this, ev); }; @@ -143,28 +146,10 @@ export class WuiEditor { this.setCaret(elTextPrev, off); return false; - case "Control": - this.is_key_control = false; - break; - case "Enter": ev.preventDefault(); break; - case "r": - if (this.is_key_control) { - ev.preventDefault(); - return; - } - break; - - case "z": - if (this.is_key_control) { - ev.preventDefault(); - return; - } - break; - default: if (this.is_key_control) { break; @@ -228,10 +213,6 @@ export class WuiEditor { } return false; - case "Control": - this.is_key_control = true; - break; - case "Delete": ev.preventDefault(); @@ -315,33 +296,6 @@ export class WuiEditor { this.setCaret(elText, off + 1); break; - - case "r": - if (this.is_key_control) { - ev.preventDefault(); - this.doRedo(); - return; - } - break; - - case "s": - if (this.is_key_control) { - ev.preventDefault(); - ev.stopPropagation(); - if (this.opts.onSave) { - this.opts.onSave(this.getContent()); - } - return false; - } - break; - - case "z": - if (this.is_key_control) { - ev.preventDefault(); - this.doUndo(); - return; - } - break; } return true; } @@ -546,14 +500,49 @@ export class WuiEditor { this.setCaret(newline.elText, 0); } + private onKeydownDocument(ed: WuiEditor, ev: KeyboardEvent) { + switch (ev.key) { + case "Control": + ed.is_key_control = true; + return; + + case "r": + if (ed.is_key_control) { + ev.preventDefault(); + ed.doRedo(); + } + return; + + case "s": + if (ed.is_key_control) { + ev.preventDefault(); + ev.stopPropagation(); + if (ed.opts.onSave) { + ed.opts.onSave(ed.getContent()); + } + } + return; + + case "z": + if (ed.is_key_control) { + ev.preventDefault(); + ed.doUndo(); + } + return; + } + } + private onKeyupDocument(ed: WuiEditor, ev: KeyboardEvent) { switch (ev.key) { + case "Control": + ed.is_key_control = false; + return; + case "Escape": ev.preventDefault(); ed.clearSelection(); - break; + return; } - return true; } private render() { |
