TelegramPWA/src/util/focusEditableElement.ts
Alexander Zinchuk 3afcde3217 Initial commit
2021-04-09 14:11:51 +03:00

17 lines
474 B
TypeScript

export default function focusEditableElement(element: HTMLElement, force?: boolean) {
if (!force && element === document.activeElement) {
return;
}
const selection = window.getSelection()!;
const range = document.createRange();
if (!element.lastChild || !element.lastChild.nodeValue) {
element.focus();
return;
}
range.setStart(element.lastChild, element.lastChild.nodeValue.length);
selection.removeAllRanges();
selection.addRange(range);
}