forgot file

This commit is contained in:
Joeri Exelmans 2025-05-31 11:11:39 +02:00
parent 7fafa35b4b
commit 7b6d18bc6a

32
src/util/dom_trickery.ts Normal file
View file

@ -0,0 +1,32 @@
// Move caret all the way to the right in the currently focused element
export function setRightMostCaretPosition(elem) {
const range = document.createRange();
range.selectNode(elem);
if (elem.lastChild) { // if no text is entered, there is no lastChild
range.setStart(elem.lastChild, elem.textContent.length);
range.setEnd(elem.lastChild, elem.textContent.length);
const selection = window.getSelection();
selection?.removeAllRanges();
selection?.addRange(range);
}
}
export function focusNextElement() {
const editable = Array.from<any>(document.querySelectorAll('input.editable'));
const index = editable.indexOf(document.activeElement);
const nextElem = editable[index + 1];
if (nextElem) {
nextElem.focus();
}
}
export function focusPrevElement() {
const editable = Array.from<any>(document.querySelectorAll('input.editable'));
const index = editable.indexOf(document.activeElement);
const prevElem = editable[index - 1];
if (prevElem) {
prevElem.focus();
setRightMostCaretPosition(prevElem);
}
}