Prevent unwanted <div> from being left behind when clearing field (#1565)

* Prevent unwanted <div> from being left behind when clearing field

* Use event.currentTarget instead of 'this'

* Check if event.data is empty for better performance

* Change order of conditions for better performance
This commit is contained in:
Hikaru Y 2021-12-24 08:12:04 +09:00 committed by GitHub
parent 21fde1b59e
commit df0ad4be4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -71,6 +71,25 @@ function getInputManager(): InputManager {
cancelInsertText(); cancelInsertText();
} }
function onInput(event: Event): void {
if (
!(event instanceof InputEvent) ||
!(event.currentTarget instanceof HTMLElement)
) {
return;
}
// prevent unwanted <div> from being left behind when clearing field contents
if (
(event.data === null || event.data === "") &&
event.currentTarget.children.length === 1 &&
event.currentTarget.children.item(0) instanceof HTMLDivElement &&
/^\n?$/.test(event.currentTarget.innerText)
) {
event.currentTarget.innerHTML = "";
}
}
function manager(element: HTMLElement): { destroy(): void } { function manager(element: HTMLElement): { destroy(): void } {
const removeBeforeInput = on(element, "beforeinput", onBeforeInput); const removeBeforeInput = on(element, "beforeinput", onBeforeInput);
const removePointerDown = on(element, "pointerdown", cancelInsertText); const removePointerDown = on(element, "pointerdown", cancelInsertText);
@ -80,6 +99,7 @@ function getInputManager(): InputManager {
"keydown", "keydown",
cancelIfInsertText as EventListener, cancelIfInsertText as EventListener,
); );
const removeInput = on(element, "input", onInput);
return { return {
destroy() { destroy() {
@ -87,6 +107,7 @@ function getInputManager(): InputManager {
removePointerDown(); removePointerDown();
removeBlur(); removeBlur();
removeKeyDown(); removeKeyDown();
removeInput();
}, },
}; };
} }