|
| 1 | +import { useCommandRegistry, formatKeybinding, type Command, type KeybindingLayer } from "@dotdirfm/commands"; |
| 2 | +import { useDialog } from "@/dialogs/dialogContext"; |
| 3 | +import { cx } from "@dotdirfm/ui-utils"; |
| 4 | +import { useCallback, useMemo, useRef, useState } from "react"; |
| 5 | +import { OverlayDialog } from "./OverlayDialog"; |
| 6 | +import styles from "./KeybindingsDialog.module.css"; |
| 7 | + |
| 8 | +interface KeybindingsDialogProps { |
| 9 | + onClose: () => void; |
| 10 | +} |
| 11 | + |
| 12 | +const LAYER_LABELS: Record<KeybindingLayer, string> = { |
| 13 | + user: "User", |
| 14 | + extension: "Extension", |
| 15 | + default: "Built-in", |
| 16 | +}; |
| 17 | + |
| 18 | +export function KeybindingsDialog({ onClose }: KeybindingsDialogProps) { |
| 19 | + const commandRegistry = useCommandRegistry(); |
| 20 | + const { showDialog } = useDialog(); |
| 21 | + const [search, setSearch] = useState(""); |
| 22 | + const [sortByPrecedence, setSortByPrecedence] = useState(true); |
| 23 | + const searchRef = useRef<HTMLInputElement>(null); |
| 24 | + |
| 25 | + const allCommands = useMemo(() => commandRegistry.getAllCommands(), [commandRegistry]); |
| 26 | + const commandMap = useMemo(() => { |
| 27 | + const map = new Map<string, Command>(); |
| 28 | + for (const c of allCommands) map.set(c.id, c); |
| 29 | + return map; |
| 30 | + }, [allCommands]); |
| 31 | + |
| 32 | + const bindingsByLayer = useMemo(() => { |
| 33 | + const layers: KeybindingLayer[] = ["user", "extension", "default"]; |
| 34 | + return layers.map((layer) => ({ |
| 35 | + layer, |
| 36 | + bindings: commandRegistry.getKeybindingsForLayer(layer), |
| 37 | + })); |
| 38 | + }, [commandRegistry]); |
| 39 | + |
| 40 | + const filteredRows = useMemo(() => { |
| 41 | + const q = search.toLowerCase(); |
| 42 | + const rows: Array<{ command: string; title: string; key: string; when: string; source: string; sourceKey?: string; layer: KeybindingLayer }> = []; |
| 43 | + |
| 44 | + for (const { layer, bindings } of bindingsByLayer) { |
| 45 | + for (const b of bindings) { |
| 46 | + if (!b.command) continue; |
| 47 | + const cmd = commandMap.get(b.command); |
| 48 | + const title = cmd?.title ?? b.command; |
| 49 | + if (q && !title.toLowerCase().includes(q) && !b.key.toLowerCase().includes(q) && !b.command.toLowerCase().includes(q)) continue; |
| 50 | + const sourceName = b.source ?? LAYER_LABELS[layer]; |
| 51 | + rows.push({ |
| 52 | + command: b.command, |
| 53 | + title, |
| 54 | + key: formatKeybinding(b), |
| 55 | + when: b.when ?? "", |
| 56 | + source: sourceName, |
| 57 | + sourceKey: b.source, |
| 58 | + layer, |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (sortByPrecedence) { |
| 64 | + const layerOrder: KeybindingLayer[] = ["user", "extension", "default"]; |
| 65 | + rows.sort((a, b) => layerOrder.indexOf(a.layer) - layerOrder.indexOf(b.layer)); |
| 66 | + } |
| 67 | + |
| 68 | + return rows; |
| 69 | + }, [bindingsByLayer, commandMap, search, sortByPrecedence]); |
| 70 | + |
| 71 | + const handleClearSearch = useCallback(() => { |
| 72 | + setSearch(""); |
| 73 | + searchRef.current?.focus(); |
| 74 | + }, []); |
| 75 | + |
| 76 | + const handleSourceClick = useCallback( |
| 77 | + (sourceKey: string) => { |
| 78 | + onClose(); |
| 79 | + showDialog({ type: "extensions", activeExtensionKey: sourceKey }); |
| 80 | + }, |
| 81 | + [onClose, showDialog], |
| 82 | + ); |
| 83 | + |
| 84 | + return ( |
| 85 | + <OverlayDialog className={cx(styles, "keybindings-dialog")} onClose={onClose} initialFocusRef={searchRef} focusLayer="modal"> |
| 86 | + <div className={styles["keybindings-header"]}> |
| 87 | + <div className={styles["keybindings-search"]}> |
| 88 | + <input |
| 89 | + ref={searchRef} |
| 90 | + type="text" |
| 91 | + placeholder="Type to search in keybindings" |
| 92 | + value={search} |
| 93 | + onChange={(e) => setSearch(e.target.value)} |
| 94 | + onKeyDown={(e) => { |
| 95 | + if (e.key === "Escape") { |
| 96 | + e.stopPropagation(); |
| 97 | + if (search) handleClearSearch(); |
| 98 | + else onClose(); |
| 99 | + } |
| 100 | + }} |
| 101 | + className={styles["keybindings-search-input"]} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + <div className={styles["keybindings-toolbar"]}> |
| 105 | + <button |
| 106 | + className={cx(styles, "keybindings-toolbar-button", sortByPrecedence && "active")} |
| 107 | + onClick={() => setSortByPrecedence((v) => !v)} |
| 108 | + title="Sort by Precedence (Highest first)" |
| 109 | + > |
| 110 | + Sort by Precedence |
| 111 | + </button> |
| 112 | + {search && ( |
| 113 | + <button className={cx(styles, "keybindings-toolbar-button")} onClick={handleClearSearch} title="Clear Keybindings Search Input (Escape)"> |
| 114 | + Clear |
| 115 | + </button> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + <div className={styles["keybindings-table-container"]}> |
| 120 | + <table className={styles["keybindings-table"]}> |
| 121 | + <thead> |
| 122 | + <tr> |
| 123 | + <th className={styles["col-command"]}>Command</th> |
| 124 | + <th className={styles["col-keybinding"]}>Keybinding</th> |
| 125 | + <th className={styles["col-when"]}>When</th> |
| 126 | + <th className={styles["col-source"]}>Source</th> |
| 127 | + </tr> |
| 128 | + </thead> |
| 129 | + <tbody> |
| 130 | + {filteredRows.map((row, i) => ( |
| 131 | + <tr key={`${row.command}-${i}`}> |
| 132 | + <td className={styles["col-command"]} title={row.command}> |
| 133 | + {row.title} |
| 134 | + </td> |
| 135 | + <td className={styles["col-keybinding"]}> |
| 136 | + <code>{row.key}</code> |
| 137 | + </td> |
| 138 | + <td className={styles["col-when"]}> |
| 139 | + <code>{row.when || "—"}</code> |
| 140 | + </td> |
| 141 | + <td className={styles["col-source"]}> |
| 142 | + {row.sourceKey ? ( |
| 143 | + <button className={styles["source-link"]} onClick={() => handleSourceClick(row.sourceKey!)} title={`Show ${row.source} extension`}> |
| 144 | + {row.source} |
| 145 | + </button> |
| 146 | + ) : ( |
| 147 | + row.source |
| 148 | + )} |
| 149 | + </td> |
| 150 | + </tr> |
| 151 | + ))} |
| 152 | + </tbody> |
| 153 | + </table> |
| 154 | + </div> |
| 155 | + </OverlayDialog> |
| 156 | + ); |
| 157 | +} |
0 commit comments