ui_util: Update place_caret_at_end to also work for input fields.

Previously, `place_caret_at_end` was only used for HTMLElement with
contenteditable="true", updated it so it takes HTMLElement as
argument and use logic to place cursor at end as per type of
element passed(i.e HTMLElement or HTMLInputElement).
This commit is contained in:
Hardik Dharmani
2023-04-14 21:01:24 +05:30
committed by Tim Abbott
parent f0ef0f7157
commit 17ae99f436

View File

@@ -9,13 +9,16 @@ import * as keydown_util from "./keydown_util";
// https://stackoverflow.com/questions/4233265/contenteditable-set-caret-at-the-end-of-the-text-cross-browser
export function place_caret_at_end(el: HTMLElement): void {
el.focus();
const range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
const sel = window.getSelection();
sel?.removeAllRanges();
sel?.addRange(range);
if (el instanceof HTMLInputElement) {
el.setSelectionRange(el.value.length, el.value.length);
} else {
const range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
const sel = window.getSelection();
sel?.removeAllRanges();
sel?.addRange(range);
}
}
export function blur_active_element(): void {