import * as React from 'react' ; import { deepMemo } from "../react/ReactUtils" ; import { IBaseKeyboardShortcut , IKeyboardShortcut , IKeyboardShortcutWithHandler , KeyboardShortcutEventHandler , useKeyboardShortcutsCallbacks } from "./KeyboardShortcutsStore" ; import { useComponentDidMount , useComponentWillUnmount } from "../hooks/ReactLifecycleHooks" ; export type KeyMap = { [ key ] : IKeyboardShortcut ; } ; export type HandlerMap = { [ key ] : KeyboardShortcutEventHandler | null ; } ; interface IProps { readonly keyMap : KeyMap ; readonly handlerMap : HandlerMap ; } export const GlobalKeyboardShortcuts = deepMemo ( function GlobalKeyboardShortcuts ( props ) { const { addKeyboardShortcut , removeKeyboardShortcut } = useKeyboardShortcutsCallbacks ( ) ; function toKeyboardShortcutWithHandler ( key , shortcut ) { const handler = props . handlerMap [ key ] || undefined ; if ( handler ) { return { ... shortcut , handler } ; } else { return undefined ; } } const keyboardShortcuts = Object . entries ( props . keyMap ) . map ( entry => toKeyboardShortcutWithHandler ( entry [ 0 ] , entry [ 1 ] ) ) . filter ( current => current !== undefined ) . map ( current => current ! ) ; useComponentDidMount ( ( ) => { for ( const keyboardShortcut of keyboardShortcuts ) { addKeyboardShortcut ( keyboardShortcut ) ; } } ) ; useComponentWillUnmount ( ( ) => { for ( const keyboardShortcut of keyboardShortcuts ) { removeKeyboardShortcut ( keyboardShortcut ) ; } } ) ; return null ; } ) ; export interface IKeyMapWithGroup { readonly group : string ; readonly groupPriority ? : number ; readonly keyMap : { [ key ] : IBaseKeyboardShortcut ; } ; } export function keyMapWithGroup ( opts ) { function toKeyboardShortcut ( option ) { return { sequences : option . sequences , name : option . name , icon : option . icon , description : option . description , priority : option . priority , group , groupPriority : groupPriority !== undefined ? groupPriority : 0 } ; } const result = { } ; const { group , groupPriority , keyMap } = opts ; for ( const key of Object . keys ( keyMap ) ) { result [ key ] = toKeyboardShortcut ( keyMap [ key ] ) ; } return result ; }