forgot file
This commit is contained in:
parent
7fafa35b4b
commit
7b6d18bc6a
1 changed files with 32 additions and 0 deletions
32
src/util/dom_trickery.ts
Normal file
32
src/util/dom_trickery.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue