Skip to content

Commit

Permalink
Fix Esc not closing CB (#7800)
Browse files Browse the repository at this point in the history
- Fixes #7799

The PR that introduced this bug stopped event propagation in certain cases, in order to fix another issue.
This PR introduces a way to run an event handler, but indicate failure so further handlers (if any) are run, otherwise the event will be propagated all the way to the document root.

# Important Notes
None
  • Loading branch information
somebody1234 authored Sep 15, 2023
1 parent af8a2c3 commit f7e079a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function Dashboard(props: DashboardProps) {
const session = authProvider.useNonPartialUserSession()
const { backend } = backendProvider.useBackend()
const { setBackend } = backendProvider.useSetBackend()
const { modal } = modalProvider.useModal()
const { unsetModal } = modalProvider.useSetModal()
const { localStorage } = localStorageProvider.useLocalStorage()
const { shortcuts } = shortcutsProvider.useShortcuts()
Expand All @@ -70,6 +71,7 @@ export default function Dashboard(props: DashboardProps) {
const [assetListEvents, dispatchAssetListEvent] =
hooks.useEvent<assetListEventModule.AssetListEvent>()
const [assetEvents, dispatchAssetEvent] = hooks.useEvent<assetEventModule.AssetEvent>()
const modalRef = React.useRef<modalProvider.Modal | null>(null)

const isListingLocalDirectoryAndWillFail =
backend.type === backendModule.BackendType.local && loadingProjectManagerDidFail
Expand All @@ -84,6 +86,10 @@ export default function Dashboard(props: DashboardProps) {
setInitialized(true)
}, [])

React.useEffect(() => {
modalRef.current = modal
}, [modal])

React.useEffect(() => {
unsetModal()
if (page === pageSwitcher.Page.editor) {
Expand Down Expand Up @@ -251,9 +257,15 @@ export default function Dashboard(props: DashboardProps) {

React.useEffect(() => {
return shortcuts.registerKeyboardHandlers({
[shortcutsModule.KeyboardAction.closeModal]: unsetModal,
[shortcutsModule.KeyboardAction.closeModal]: () => {
unsetModal()
if (modalRef.current == null) {
// eslint-disable-next-line no-restricted-syntax
return false
}
},
})
}, [shortcuts, unsetModal])
}, [shortcuts, /* should never change */ unsetModal])

const setBackendType = React.useCallback(
(newBackendType: backendModule.BackendType) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,15 @@ export class ShortcutRegistry {
keyboardShortcutsByKey: Record<string, KeyboardShortcut[]> = {}
allKeyboardHandlers: Record<
KeyboardAction,
((event: KeyboardEvent | React.KeyboardEvent) => void)[]
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
((event: KeyboardEvent | React.KeyboardEvent) => boolean | void)[]
> = makeKeyboardActionMap(() => [])
/** The last handler (if any) for each action in
* {@link ShortcutRegistry.allKeyboardHandlers}. */
activeKeyboardHandlers: Record<
KeyboardAction,
((event: KeyboardEvent | React.KeyboardEvent) => void) | null
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
((event: KeyboardEvent | React.KeyboardEvent) => boolean | void) | null
> = makeKeyboardActionMap(() => null)

/** Create a {@link ShortcutRegistry}. */
Expand Down Expand Up @@ -322,10 +324,12 @@ export class ShortcutRegistry {
if (this.matchesKeyboardShortcut(shortcut, event)) {
const handler = this.activeKeyboardHandlers[shortcut.action]
if (handler != null) {
handler(event)
// The matching `false` return is immediately after this loop.
// eslint-disable-next-line no-restricted-syntax
return true
const result = handler(event)
if (result !== false) {
// The matching `false` return is immediately after this loop.
// eslint-disable-next-line no-restricted-syntax
return true
}
}
}
}
Expand Down Expand Up @@ -360,7 +364,8 @@ export class ShortcutRegistry {
* these handlers. */
registerKeyboardHandlers(
handlers: Partial<
Record<KeyboardAction, (event: KeyboardEvent | React.KeyboardEvent) => void>
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
Record<KeyboardAction, (event: KeyboardEvent | React.KeyboardEvent) => boolean | void>
>
) {
for (const action of Object.values(KeyboardAction)) {
Expand Down

0 comments on commit f7e079a

Please sign in to comment.