aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-10-20 22:09:24 +0700
committerShulhan <ms@kilabit.info>2021-10-20 22:09:24 +0700
commit9f089dfb69cff87ead23fc32ca2b6c9d681de489 (patch)
tree021d996787de0c57dfddc19736d629f1c56f8e7b
parenta7e2c7db1dec9c5760646cd4901edea4019a1b99 (diff)
downloadpakakeh.ts-9f089dfb69cff87ead23fc32ca2b6c9d681de489.tar.xz
editor: handle key Delete
When delete key pressed on line, it will delete single character or join the current line with the next line.
-rw-r--r--editor/editor.ts41
1 files changed, 40 insertions, 1 deletions
diff --git a/editor/editor.ts b/editor/editor.ts
index fe90066..919b425 100644
--- a/editor/editor.ts
+++ b/editor/editor.ts
@@ -102,7 +102,6 @@ export class WuiEditor {
case "ArrowUp":
case "CapsLock":
case "ContextMenu":
- case "Delete":
case "End":
case "Home":
case "Insert":
@@ -226,6 +225,46 @@ export class WuiEditor {
this.is_key_control = true
break
+ case "Delete":
+ ev.preventDefault()
+
+ let is_join_line_after = false
+ let el_text_current = this.lines[x].el_text
+
+ off = this.sel.focusOffset
+ text_before = el_text_current.innerText
+ text_after = ""
+
+ if (text_before.length === 0 || off === text_before.length) {
+ // Current line is empty, join the next line to current
+ // line; or
+ // Current offset is at the end of text, join the next
+ // line to current line.
+ is_join_line_after = true
+ }
+
+ if (is_join_line_after) {
+ if (x+1 < this.lines.length) {
+ let el_text_after = this.lines[x+1].el_text
+ text_after = el_text_after.innerText
+ el_text_after.innerText = ""
+
+ this.unre.DoJoin(x, text_before, text_after)
+
+ this.deleteLine(x+1)
+ text_after = text_before + text_after
+ }
+ } else {
+ text_after = text_before.slice(0, off) + text_before.slice(off+1, text_before.length)
+
+ this.unre.DoUpdate(x, text_before, text_after)
+ }
+
+ this.lines[x].el_text.innerText = text_after
+ this.raw_lines[x] = text_after
+ this.setCaret(el_text_current, off)
+ break
+
case "Enter":
ev.preventDefault()