|
1 | | -export function observeInputValue(element: HTMLInputElement, callback: (value: string) => void) { |
2 | | - const elementPrototype = Object.getPrototypeOf(element); |
3 | | - const descriptor = Object.getOwnPropertyDescriptor(elementPrototype, "value"); |
4 | | - |
5 | | - Object.defineProperty(element, "value", { |
6 | | - get: function (...args) { |
7 | | - // @ts-expect-error |
8 | | - return descriptor.get.apply(this, args); |
9 | | - }, |
10 | | - set: function (...args) { |
11 | | - // @ts-expect-error |
12 | | - descriptor.set.apply(this, args); |
13 | | - const newValue = this.value; |
14 | | - |
15 | | - callback(newValue); |
16 | | - |
17 | | - return newValue; |
18 | | - } |
19 | | - }); |
| 1 | +import { assert } from "tsafe/assert"; |
| 2 | + |
| 3 | +// NOTE: The callback is called way too often, it can be called multiple times |
| 4 | +// for the same input value. Futhermore the callback is not guaranteed to be called |
| 5 | +// as soon as the input value changes. |
| 6 | +export function observeInputValue(params: { |
| 7 | + inputElement: HTMLInputElement; |
| 8 | + callback: () => void; |
| 9 | +}): { cleanup: () => void } { |
| 10 | + const { inputElement, callback: callback_params } = params; |
| 11 | + |
| 12 | + const cleanups: (() => void)[] = []; |
| 13 | + |
| 14 | + { |
| 15 | + const descriptorToRestore = Object.getOwnPropertyDescriptor(inputElement, "value"); |
| 16 | + |
| 17 | + cleanups.push(() => { |
| 18 | + assert(descriptorToRestore !== undefined); |
| 19 | + Object.defineProperty(inputElement, "value", descriptorToRestore); |
| 20 | + }); |
| 21 | + |
| 22 | + const inputElementPrototype = Object.getPrototypeOf(inputElement); |
| 23 | + const descriptor = Object.getOwnPropertyDescriptor(inputElementPrototype, "value"); |
| 24 | + |
| 25 | + Object.defineProperty(inputElement, "value", { |
| 26 | + "get": function (...args) { |
| 27 | + // @ts-expect-error |
| 28 | + return descriptor.get.apply(this, args); |
| 29 | + }, |
| 30 | + "set": function (...args) { |
| 31 | + // @ts-expect-error |
| 32 | + descriptor.set.apply(this, args); |
| 33 | + const newValue = this.value; |
| 34 | + |
| 35 | + callback_params(); |
| 36 | + |
| 37 | + return newValue; |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + inputElement.addEventListener( |
| 43 | + "input", |
| 44 | + (() => { |
| 45 | + const callback = (): void => { |
| 46 | + callback_params(); |
| 47 | + }; |
| 48 | + |
| 49 | + cleanups.push(() => inputElement.removeEventListener("input", callback)); |
| 50 | + |
| 51 | + return callback; |
| 52 | + })() |
| 53 | + ); |
| 54 | + |
| 55 | + inputElement.addEventListener( |
| 56 | + "keydown", |
| 57 | + (() => { |
| 58 | + const callback = (event: KeyboardEvent) => { |
| 59 | + if (event.key === "Escape") { |
| 60 | + const timer = setTimeout(() => { |
| 61 | + cleanups.splice(cleanups.indexOf(cleanup), 1); |
| 62 | + callback_params(); |
| 63 | + }, 50); |
| 64 | + |
| 65 | + const cleanup = () => { |
| 66 | + clearTimeout(timer); |
| 67 | + }; |
| 68 | + |
| 69 | + cleanups.push(cleanup); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + cleanups.push(() => inputElement.removeEventListener("keyup", callback)); |
| 74 | + |
| 75 | + return callback; |
| 76 | + })() |
| 77 | + ); |
| 78 | + |
| 79 | + function cleanup() { |
| 80 | + cleanups.forEach(cleanup => cleanup()); |
| 81 | + } |
| 82 | + |
| 83 | + return { cleanup }; |
20 | 84 | } |
0 commit comments