From b2cdd441d1f0bf28f546f9051485fbf9f7734181 Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 28 Jun 2024 17:53:35 -0700 Subject: [PATCH] keydownWrapper --- frontend/app/app.tsx | 13 +++---------- frontend/app/view/term/term.tsx | 1 + frontend/util/keyutil.ts | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/frontend/app/app.tsx b/frontend/app/app.tsx index 4a7a79de4..6708ed853 100644 --- a/frontend/app/app.tsx +++ b/frontend/app/app.tsx @@ -210,15 +210,8 @@ const AppInner = () => { ); } - function handleKeyDown(ev: KeyboardEvent) { - let waveEvent = keyutil.adaptFromReactOrNativeKeyEvent(ev); - const rtn = handleKeyDownInternal(waveEvent); - if (rtn) { - ev.preventDefault(); - ev.stopPropagation(); - } - } - function handleKeyDownInternal(waveEvent: WaveKeyboardEvent): boolean { + + function handleKeyDown(waveEvent: WaveKeyboardEvent): boolean { // global key handler for now (refactor later) if (keyutil.checkKeyPressed(waveEvent, "Cmd:]")) { switchTab(1); @@ -247,7 +240,7 @@ const AppInner = () => { return false; } React.useEffect(() => { - const staticKeyDownHandler = handleKeyDown; + const staticKeyDownHandler = keyutil.keydownWrapper(handleKeyDown); document.addEventListener("keydown", staticKeyDownHandler); return () => { document.removeEventListener("keydown", staticKeyDownHandler); diff --git a/frontend/app/view/term/term.tsx b/frontend/app/view/term/term.tsx index 53efd0f26..d140eae26 100644 --- a/frontend/app/view/term/term.tsx +++ b/frontend/app/view/term/term.tsx @@ -213,6 +213,7 @@ const TerminalView = ({ blockId }: { blockId: string }) => { termWrap.initTerminal(); return () => { termWrap.dispose(); + rszObs.disconnect(); }; }, []); diff --git a/frontend/util/keyutil.ts b/frontend/util/keyutil.ts index d62cea4f4..a718bc909 100644 --- a/frontend/util/keyutil.ts +++ b/frontend/util/keyutil.ts @@ -12,6 +12,19 @@ function setKeyUtilPlatform(platform: NodeJS.Platform) { PLATFORM = platform; } +function keydownWrapper( + fn: (waveEvent: WaveKeyboardEvent) => boolean +): (event: KeyboardEvent | React.KeyboardEvent) => void { + return (event: KeyboardEvent | React.KeyboardEvent) => { + const waveEvent = adaptFromReactOrNativeKeyEvent(event); + const rtnVal = fn(waveEvent); + if (rtnVal) { + event.preventDefault(); + event.stopPropagation(); + } + }; +} + function parseKey(key: string): { key: string; type: string } { let regexMatch = key.match(KeyTypeCodeRegex); if (regexMatch != null && regexMatch.length > 1) { @@ -142,6 +155,7 @@ export { adaptFromElectronKeyEvent, adaptFromReactOrNativeKeyEvent, checkKeyPressed, + keydownWrapper, parseKeyDescription, setKeyUtilPlatform, };