From f620bcf1f0772b1ea686ec6e7a50cdd55e37fd56 Mon Sep 17 00:00:00 2001 From: remi Date: Sat, 7 May 2022 15:06:20 +0200 Subject: [PATCH] yaaaa --- ime.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/ime.js b/ime.js index f8643ca..f852394 100644 --- a/ime.js +++ b/ime.js @@ -2,17 +2,16 @@ // it successfully breaks everything. however, im not managing to repro it so... // (shruggie) -let h; +// let h; -window.onload = () => { - document.querySelectorAll(".ime").forEach(input => { - h = new HeonianIME(); - h.imeAttach(input); - }); -} +// window.onload = () => { + // document.querySelectorAll(".ime").forEach(input => { + // h = new HeonianIME(input); + // }); +// } class HeonianIME { - constructor() { + constructor(what) { this.input = []; this.currentWord = []; this.selected = -1; @@ -23,6 +22,7 @@ class HeonianIME { this.inputCurrent = ""; this.ourHtml; + this.oldText = []; this.hVowels = { //standalone, composing, trailing "a": ["0", "5", "A"], @@ -54,10 +54,9 @@ class HeonianIME { this.hVowelsK = Object.keys(this.hVowels) this.hConsonantsK = Object.keys(this.hConsonants); this.hConsonantsR = this.inverse(this.hConsonants); - } - imeAttach(what) { //re-attaching is unsupported :3 this.ourHtml = what; + this.oldText = [what.value.slice(0, what.selectionStart), what.value.slice(what.selectionStart)]; what.addEventListener("keydown", (key) => { this.imeDown(key, what); }); @@ -74,6 +73,21 @@ class HeonianIME { imeDetach() { //how do we detach this without . uh, + let renderText = ""; + this.input.forEach((w) => { + renderText += w.join(""); + }) + if (this.selected == -1) { + this.ourHtml.setSelectionRange(this.oldText[0].length+renderText.length, this.oldText[0].length+renderText.length); + } else { + if (this.input.join("") != "") { + let from = 0; + for (let x = 0; x < this.selected; x++) { + from += this.input[x].join("").length; + } + this.ourHtml.setSelectionRange(this.oldText[0].length+from, this.oldText[0].length+from); + } + } this.imeDown = ()=>{}; this.mouse = ()=>{}; //close enough? @@ -143,7 +157,7 @@ class HeonianIME { break; case "Escape": this.imeDetach(); - break; + return; default: this.imeInput(keyEvent.key); if (this.selected == -1) { @@ -161,9 +175,9 @@ class HeonianIME { this.input.forEach((w) => { renderText += w.join(""); }) - inputField.value = renderText + this.currentWord.join(""); + inputField.value = this.oldText[0] + renderText + this.currentWord.join("") + this.oldText[1]; if (this.selected == -1) { - inputField.setSelectionRange(renderText.length, renderText.length + this.currentWord.join("").length); + inputField.setSelectionRange(this.oldText[0].length+renderText.length, this.oldText[0].length+renderText.length + this.currentWord.join("").length); } else { if (this.input.join("") != "") { let from = 0; @@ -171,7 +185,7 @@ class HeonianIME { from += this.input[x].join("").length; } let to = from + this.input[this.selected].join("").length; - inputField.setSelectionRange(from, to); + inputField.setSelectionRange(this.oldText[0].length+from, this.oldText[0].length+to); } } }