Skip to content

Commit

Permalink
fix forbidOtherKeyboardKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin committed Sep 12, 2024
1 parent d27a963 commit 13395fd
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/mathInput/mathInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,6 @@ export const MathInput = ({
timeout.current = setTimeout(eventually, 300);
};

useEffect(() => {
if (!forbidOtherKeyboardKeys) return;
let keys: (string | undefined)[] = [...vanillaKeys];
if (numericToolbarKeys)
keys.push(
...numericToolbarKeys.map((key) => {
return typeof key === "string"
? KeysPropsMap.get(key)!.keypressId
: key.keypressId;
})
);

keys = keys.filter((e) => e !== undefined);

const exec = (event: KeyboardEvent) => {
if (!keys.includes(event.key)) event.preventDefault();
};
const inputElement = document.getElementById(
`mq-keyboard-${idCounter.current}-container`
);
inputElement?.addEventListener("keypress", exec);
return () => inputElement?.removeEventListener("keypress", exec);
}, [forbidOtherKeyboardKeys, numericToolbarKeys]);

const idCounter = useRef<number>(0);
useEffect(() => {
window.jQuery = $;
Expand Down Expand Up @@ -171,6 +147,30 @@ export const MathInput = ({
setLoaded(true);
}, []);

useEffect(() => {
if (!forbidOtherKeyboardKeys || !loaded) return;
let keys: (string | undefined)[] = [...vanillaKeys];
if (numericToolbarKeys)
keys.push(
...numericToolbarKeys.map((key) => {
return typeof key === "string"
? KeysPropsMap.get(key)!.keypressId
: key.keypressId;
})
);

keys = keys.filter((e) => e !== undefined);

const exec = (event: KeyboardEvent) => {
if (!keys.includes(event.key)) event.preventDefault();
};
const inputElement = document.getElementById(
`mq-keyboard-${idCounter.current}-container`
);
inputElement?.addEventListener("keypress", exec);
return () => inputElement?.removeEventListener("keypress", exec);
}, [forbidOtherKeyboardKeys, numericToolbarKeys, loaded]);

useEffect(() => {
const onMouseDown = (e: MouseEvent) => {
if (e.target instanceof HTMLElement) {
Expand Down

0 comments on commit 13395fd

Please sign in to comment.