Skip to content

Commit 7ba9999

Browse files
committed
refactor: use shared keybinding capture logic from KeybindingsSettings.logic
Remove duplicated normalizeShortcutKeyToken and keybindingFromEvent from ProjectScriptsControl.tsx and import the shared keybindingFromKeyboardEvent from KeybindingsSettings.logic.ts instead. This consolidates the keybinding parsing logic into a single module, fixing subtle differences in function-key validation (regex vs startsWith+length check) and reducing maintenance burden.
1 parent 823caf3 commit 7ba9999

1 file changed

Lines changed: 2 additions & 53 deletions

File tree

apps/web/src/components/ProjectScriptsControl.tsx

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from "lucide-react";
1717
import React, { type FormEvent, type KeyboardEvent, useCallback, useMemo, useState } from "react";
1818

19+
import { keybindingFromKeyboardEvent } from "~/components/settings/KeybindingsSettings.logic";
1920
import {
2021
keybindingValueForCommand,
2122
decodeProjectScriptKeybindingRule,
@@ -26,7 +27,6 @@ import {
2627
primaryProjectScript,
2728
} from "~/projectScripts";
2829
import { shortcutLabelForCommand } from "~/keybindings";
29-
import { isMacPlatform } from "~/lib/utils";
3030
import {
3131
AlertDialog,
3232
AlertDialogClose,
@@ -96,57 +96,6 @@ interface ProjectScriptsControlProps {
9696
onDeleteScript: (scriptId: string) => Promise<void> | void;
9797
}
9898

99-
function normalizeShortcutKeyToken(key: string): string | null {
100-
const normalized = key.toLowerCase();
101-
if (
102-
normalized === "meta" ||
103-
normalized === "control" ||
104-
normalized === "ctrl" ||
105-
normalized === "shift" ||
106-
normalized === "alt" ||
107-
normalized === "option"
108-
) {
109-
return null;
110-
}
111-
if (normalized === " ") return "space";
112-
if (normalized === "escape") return "esc";
113-
if (normalized === "arrowup") return "arrowup";
114-
if (normalized === "arrowdown") return "arrowdown";
115-
if (normalized === "arrowleft") return "arrowleft";
116-
if (normalized === "arrowright") return "arrowright";
117-
if (normalized.length === 1) return normalized;
118-
if (normalized.startsWith("f") && normalized.length <= 3) return normalized;
119-
if (normalized === "enter" || normalized === "tab" || normalized === "backspace") {
120-
return normalized;
121-
}
122-
if (normalized === "delete" || normalized === "home" || normalized === "end") {
123-
return normalized;
124-
}
125-
if (normalized === "pageup" || normalized === "pagedown") return normalized;
126-
return null;
127-
}
128-
129-
function keybindingFromEvent(event: KeyboardEvent<HTMLInputElement>): string | null {
130-
const keyToken = normalizeShortcutKeyToken(event.key);
131-
if (!keyToken) return null;
132-
133-
const parts: string[] = [];
134-
if (isMacPlatform(navigator.platform)) {
135-
if (event.metaKey) parts.push("mod");
136-
if (event.ctrlKey) parts.push("ctrl");
137-
} else {
138-
if (event.ctrlKey) parts.push("mod");
139-
if (event.metaKey) parts.push("meta");
140-
}
141-
if (event.altKey) parts.push("alt");
142-
if (event.shiftKey) parts.push("shift");
143-
if (parts.length === 0) {
144-
return null;
145-
}
146-
parts.push(keyToken);
147-
return parts.join("+");
148-
}
149-
15099
export default function ProjectScriptsControl({
151100
scripts,
152101
keybindings,
@@ -186,7 +135,7 @@ export default function ProjectScriptsControl({
186135
setKeybinding("");
187136
return;
188137
}
189-
const next = keybindingFromEvent(event);
138+
const next = keybindingFromKeyboardEvent(event, navigator.platform);
190139
if (!next) return;
191140
setKeybinding(next);
192141
};

0 commit comments

Comments
 (0)