Open History to see the last commit
1import type { EditorId, EnvironmentId, ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { useEffect } from "react";
3
4import { usePreferredEditor } from "../../editorPreferences";
5import { editorLabelForPlatform } from "../../editorLabels";
6import { isOpenFavoriteEditorShortcut } from "../../keybindings";
7import { announce } from "../../lib/announce";
8import { useRemoteOpenState } from "../../remoteOpen";
9import { registerCommands } from "../../shortcuts/dispatcher";
10import { shellEnvironment } from "../../state/shell";
11import { useAtomCommand } from "../../state/use-atom-command";
12
13/** Marks an OpenInPicker's primary button as the target of `editor.openFavorite`. */
14export const OPEN_FAVORITE_EDITOR_ATTRIBUTE = "data-open-favorite-editor";
15
16/**
17 * `editor.openFavorite` presses the marked OpenInPicker primary button, so the shortcut opens
18 * exactly what the button on screen would. A missing or disabled button leaves the chord to the
19 * bubble-phase fallback below. Capture phase, so a focused terminal does not swallow the chord.
20 */
21export function registerOpenFavoriteEditorCommand(): () => void {
22 return registerCommands(
23 {
24 "editor.openFavorite": (event) => {
25 if (event.repeat) return false;
26 const button = [
27 ...document.querySelectorAll<HTMLButtonElement>(`[${OPEN_FAVORITE_EDITOR_ATTRIBUTE}]`),
28 ].at(-1);
29 if (button === undefined || button.disabled) return false;
30 event.preventDefault();
31 button.click();
32 },
33 },
34 { capture: true },
35 );
36}
37
38export function useOpenFavoriteEditorShortcut({
39 enabled,
40 environmentId,
41 keybindings,
42 availableEditors,
43 openInCwd,
44}: {
45 enabled: boolean;
46 environmentId: EnvironmentId;
47 keybindings: ResolvedKeybindingsConfig;
48 availableEditors: ReadonlyArray<EditorId>;
49 openInCwd: string | null;
50}) {
51 const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor");
52 const [preferredEditor] = usePreferredEditor(availableEditors);
53 const remote = useRemoteOpenState(environmentId);
54
55 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes a window keydown listener for the open-in-editor shortcut while enabled locally
56 useEffect(() => {
57 if (!enabled || remote.mode !== "local-exec") return;
58 const handler = (event: globalThis.KeyboardEvent) => {
59 if (event.defaultPrevented || !isOpenFavoriteEditorShortcut(event, keybindings)) return;
60 if (!openInCwd || !preferredEditor) return;
61
62 event.preventDefault();
63 const label = editorLabelForPlatform(preferredEditor, navigator.platform);
64 void openInEditorMutation({
65 environmentId,
66 input: {
67 cwd: openInCwd,
68 editor: preferredEditor,
69 },
70 }).then((result) => {
71 if (result._tag === "Success") announce(`Opened in ${label}`);
72 });
73 };
74 window.addEventListener("keydown", handler);
75 return () => window.removeEventListener("keydown", handler);
76 }, [
77 enabled,
78 environmentId,
79 keybindings,
80 openInCwd,
81 openInEditorMutation,
82 preferredEditor,
83 remote.mode,
84 ]);
85}