Fix Esc not closing CB (#7800)

- 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
This commit is contained in:
somebody1234 2023-09-16 01:04:45 +10:00 committed by GitHub
parent af8a2c39d8
commit f7e079aa43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 9 deletions

View File

@ -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()
@ -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
@ -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) {
@ -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) => {

View File

@ -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}. */
@ -322,7 +324,8 @@ export class ShortcutRegistry {
if (this.matchesKeyboardShortcut(shortcut, event)) {
const handler = this.activeKeyboardHandlers[shortcut.action]
if (handler != null) {
handler(event)
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
@ -330,6 +333,7 @@ export class ShortcutRegistry {
}
}
}
}
return false
}
@ -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)) {