fix: backspacing causing desync on some platforms

This commit is contained in:
Miodec 2025-12-02 10:26:46 +01:00
parent 8c035c1687
commit ad3b7b37d1
2 changed files with 7 additions and 10 deletions

View file

@ -1,10 +1,10 @@
const el = document.querySelector("#wordsInput") as HTMLInputElement;
const el = document.querySelector("#wordsInput") as HTMLTextAreaElement;
if (el === null) {
throw new Error("Words input element not found");
}
export function getInputElement(): HTMLInputElement {
export function getInputElement(): HTMLTextAreaElement {
return el;
}

View file

@ -20,20 +20,17 @@ inputEl.addEventListener("select selectstart", (event) => {
inputEl.addEventListener("selectionchange", (event) => {
const selection = window.getSelection();
console.debug("wordsInput event selectionchange", {
event,
selection: selection?.toString(),
isCollapsed: selection?.isCollapsed,
selectionStart: (event.target as HTMLInputElement).selectionStart,
selectionEnd: (event.target as HTMLInputElement).selectionEnd,
selectionStart: inputEl.selectionStart,
selectionEnd: inputEl.selectionEnd,
});
const el = event.target;
if (el === null || !(el instanceof HTMLInputElement)) {
return;
}
const hasSelectedText = el.selectionStart !== el.selectionEnd;
const isCursorAtEnd = el.selectionStart === el.value.length;
const hasSelectedText = inputEl.selectionStart !== inputEl.selectionEnd;
const isCursorAtEnd = inputEl.selectionStart === inputEl.value.length;
if (hasSelectedText || !isCursorAtEnd) {
moveInputElementCaretToTheEnd();
}