Skip to content

Commit

Permalink
fix: global event handlers called from windows (#2195)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianWielga committed Sep 15, 2021
1 parent d65d95c commit 81aaff4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
4 changes: 1 addition & 3 deletions ui/client/bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {WindowManagerProvider} from "@touk/window-manager"
import {defaultsDeep} from "lodash"
import React, {Suspense} from "react"
import ReactDOM from "react-dom"
import Modal from "react-modal"
import {Provider} from "react-redux"
import {Router} from "react-router-dom"
import {PersistGate} from "redux-persist/integration/react"
Expand All @@ -22,10 +21,9 @@ import {contentGetter} from "./windowManager"

const {store, persistor} = configureStore()
const rootContainer = document.createElement(`div`)
rootContainer.id = "root"
document.body.appendChild(rootContainer)

Modal.setAppElement(rootContainer)

const Root = () => (
<DragArea>
<Suspense fallback={<LoaderSpinner show/>}>
Expand Down
9 changes: 4 additions & 5 deletions ui/client/containers/BindKeyboardShortcuts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ export function BindKeyboardShortcuts({disabled}: { disabled?: boolean }): JSX.E
BACKSPACE: userActions.delete,
}), [userActions])

const copyPasteHandlers = useMemo(() => ({
keydown: event => isInputEvent(event) || !disabled && keyHandlers[event.key.toUpperCase()]?.(event),
const eventHandlers = useMemo(() => ({
keydown: event => isInputEvent(event) || keyHandlers[event.key.toUpperCase()]?.(event),
copy: event => userActions.copy ? userActions.copy(event) : null,
paste: event => userActions.paste ? userActions.paste(event) : null,
cut: event => userActions.cut ? userActions.cut(event) : null,
}), [disabled, keyHandlers, userActions])

useDocumentListeners(!disabled && copyPasteHandlers)
}), [keyHandlers, userActions])
useDocumentListeners(!disabled && eventHandlers)

return null
}
23 changes: 17 additions & 6 deletions ui/client/containers/useDocumentListeners.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import {useEffect} from "react"
import {useDispatch} from "react-redux"
import {useEffect, useMemo} from "react"

type Listener<K extends keyof DocumentEventMap> = (event: DocumentEventMap[K]) => void
export type DocumentListeners = {
[K in keyof DocumentEventMap]?: Listener<K> | false
}

const filterEventsByContainer = (container: HTMLElement) => <K extends keyof DocumentEventMap>(listener: Listener<K>): Listener<K> => (event) => {
const path = event.composedPath() || []
const rootElements = [window, document, document.documentElement, document.body]
const parents = path.filter(t => ![event.target, ...rootElements].includes(t))
// allow events from root
// ignore all events from outside of container (modals, overlays, etc)
if (!parents.length || parents.includes(container)) {
return listener(event)
}
}

export function useDocumentListeners(listeners: DocumentListeners): void {
const dispatch = useDispatch()
const filterByRoot = useMemo(() => filterEventsByContainer(document.getElementById("root")), [])
useEffect(
() => {
const entries = Object.entries(listeners).map(([type, listener]) => {
if (listener) {
document.addEventListener(type, listener)
return () => document.removeEventListener(type, listener)
const wrapped = filterByRoot(listener)
document.addEventListener(type, wrapped)
return () => document.removeEventListener(type, wrapped)
}
})
return () => entries.forEach(unbind => unbind())
},
[listeners],
[filterByRoot, listeners],
)
}
2 changes: 0 additions & 2 deletions ui/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"react-json-tree": "^0.15.0",
"react-ladda": "~6.0.0",
"react-markdown": "^5.0.3",
"react-modal": "3.14.3",
"react-notification-system": "~0.4.0",
"react-notification-system-redux": "^2.0.1",
"react-redux": "7.2.5",
Expand Down Expand Up @@ -166,7 +165,6 @@
"@types/react-dom": "^17.0.9",
"@types/react-highlight-words": "^0.16.3",
"@types/react-inspector": "^4.0.2",
"@types/react-modal": "^3.12.0",
"@types/react-redux": "7.1.18",
"@types/react-router-dom": "5.1.8",
"@types/react-select": "^3.1.2",
Expand Down

0 comments on commit 81aaff4

Please sign in to comment.