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...
// (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);
}
}
}