import {html} from "lit" import {styles} from "./styles.js" import {shadow_view} from "../../context/context.js" import keyboardSvg from "../../icons/gravity-ui/keyboard.svg.js" import {ActionType} from "../../context/controllers/shortcuts/controller.js" export const ShortcutsManager = shadow_view((use) => () => { use.styles(styles) const manager = use.context.controllers.shortcuts const [isModalOpened, setIsModalOpened] = use.state(false) const [_, setPressedKeys, getPressedKeys] = use.state(new Set()) const [conflict, setConflict] = use.state<{newShortcut: string, type: ActionType} | null>(null) // Render the list of shortcuts const renderShortcutsList = () => manager.listShortcuts().map(({ shortcut, actionType }) => html` ${actionType} onPointerUpInput(e, actionType)} @keydown=${onPointerDownInput} class="shortcut-input" > ` ) const onPointerDownInput = (e: KeyboardEvent) => { e.preventDefault() const inputElement = e.target as HTMLInputElement const keyCombination = manager.getKeyCombination(e) setPressedKeys(new Set(getPressedKeys()).add(keyCombination)) inputElement.value = keyCombination } const onPointerUpInput = (e: PointerEvent, type: ActionType) => { const inputElement = e.target as HTMLInputElement const finalShortcut = inputElement.value.toLowerCase() try { manager.updateShortcut(type, finalShortcut) } catch (error) { setConflict({newShortcut: finalShortcut, type}) } setPressedKeys(new Set()) inputElement.blur() } const resolveConflict = () => { if (conflict) { manager.updateShortcut(conflict.type, conflict.newShortcut, true) setConflict(null) } } const cancelConflict = () => setConflict(null) return html` setIsModalOpened(true)}>${keyboardSvg} Customize Shortcuts Action Current Shortcut ${renderShortcutsList()} { manager.resetToDefaults() use.rerender() }} id="reset-defaults" > Reset to Defaults setIsModalOpened(false)} id="close-modal">Close ${conflict ? html` The shortcut "${conflict.newShortcut}" is already assigned. Do you want to override it? Yes No ` : null} ` })
The shortcut "${conflict.newShortcut}" is already assigned. Do you want to override it?