This commit is contained in:
remi 2022-05-07 15:06:20 +02:00
parent 8095cae7cb
commit f620bcf1f0

42
ime.js
View File

@ -2,17 +2,16 @@
// it successfully breaks everything. however, im not managing to repro it so... // it successfully breaks everything. however, im not managing to repro it so...
// (shruggie) // (shruggie)
let h; // let h;
window.onload = () => { // window.onload = () => {
document.querySelectorAll(".ime").forEach(input => { // document.querySelectorAll(".ime").forEach(input => {
h = new HeonianIME(); // h = new HeonianIME(input);
h.imeAttach(input); // });
}); // }
}
class HeonianIME { class HeonianIME {
constructor() { constructor(what) {
this.input = []; this.input = [];
this.currentWord = []; this.currentWord = [];
this.selected = -1; this.selected = -1;
@ -23,6 +22,7 @@ class HeonianIME {
this.inputCurrent = ""; this.inputCurrent = "";
this.ourHtml; this.ourHtml;
this.oldText = [];
this.hVowels = { //standalone, composing, trailing this.hVowels = { //standalone, composing, trailing
"a": ["0", "5", "A"], "a": ["0", "5", "A"],
@ -54,10 +54,9 @@ class HeonianIME {
this.hVowelsK = Object.keys(this.hVowels) this.hVowelsK = Object.keys(this.hVowels)
this.hConsonantsK = Object.keys(this.hConsonants); this.hConsonantsK = Object.keys(this.hConsonants);
this.hConsonantsR = this.inverse(this.hConsonants); this.hConsonantsR = this.inverse(this.hConsonants);
}
imeAttach(what) { //re-attaching is unsupported :3
this.ourHtml = what; this.ourHtml = what;
this.oldText = [what.value.slice(0, what.selectionStart), what.value.slice(what.selectionStart)];
what.addEventListener("keydown", (key) => { what.addEventListener("keydown", (key) => {
this.imeDown(key, what); this.imeDown(key, what);
}); });
@ -74,6 +73,21 @@ class HeonianIME {
imeDetach() { imeDetach() {
//how do we detach this without . uh, //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.imeDown = ()=>{};
this.mouse = ()=>{}; this.mouse = ()=>{};
//close enough? //close enough?
@ -143,7 +157,7 @@ class HeonianIME {
break; break;
case "Escape": case "Escape":
this.imeDetach(); this.imeDetach();
break; return;
default: default:
this.imeInput(keyEvent.key); this.imeInput(keyEvent.key);
if (this.selected == -1) { if (this.selected == -1) {
@ -161,9 +175,9 @@ class HeonianIME {
this.input.forEach((w) => { this.input.forEach((w) => {
renderText += w.join(""); 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) { 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 { } else {
if (this.input.join("") != "") { if (this.input.join("") != "") {
let from = 0; let from = 0;
@ -171,7 +185,7 @@ class HeonianIME {
from += this.input[x].join("").length; from += this.input[x].join("").length;
} }
let to = from + this.input[this.selected].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);
} }
} }
} }