From df0ad4be4bd283d26c5785ee8c4bea6eab49d0d7 Mon Sep 17 00:00:00 2001 From: Hikaru Y <47855854+hikaru-y@users.noreply.github.com> Date: Fri, 24 Dec 2021 08:12:04 +0900 Subject: [PATCH] Prevent unwanted
from being left behind when clearing field (#1565) * Prevent unwanted
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 --- ts/sveltelib/input-manager.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ts/sveltelib/input-manager.ts b/ts/sveltelib/input-manager.ts index fd9061f8b..53b8cb936 100644 --- a/ts/sveltelib/input-manager.ts +++ b/ts/sveltelib/input-manager.ts @@ -71,6 +71,25 @@ function getInputManager(): InputManager { cancelInsertText(); } + function onInput(event: Event): void { + if ( + !(event instanceof InputEvent) || + !(event.currentTarget instanceof HTMLElement) + ) { + return; + } + + // prevent unwanted
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 } { const removeBeforeInput = on(element, "beforeinput", onBeforeInput); const removePointerDown = on(element, "pointerdown", cancelInsertText); @@ -80,6 +99,7 @@ function getInputManager(): InputManager { "keydown", cancelIfInsertText as EventListener, ); + const removeInput = on(element, "input", onInput); return { destroy() { @@ -87,6 +107,7 @@ function getInputManager(): InputManager { removePointerDown(); removeBlur(); removeKeyDown(); + removeInput(); }, }; }