Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenInPickerShortcut.ts

84 lines2.9 KB

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 );
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 useEffect(() => {
56 if (!enabled || remote.mode !== "local-exec") return;
57 const handler = (event: globalThis.KeyboardEvent) => {
58 if (event.defaultPrevented || !isOpenFavoriteEditorShortcut(event, keybindings)) return;
59 if (!openInCwd || !preferredEditor) return;
60​
61 event.preventDefault();
62 const label = editorLabelForPlatform(preferredEditor, navigator.platform);
63 void openInEditorMutation({
64 environmentId,
65 input: {
66 cwd: openInCwd,
67 editor: preferredEditor,
68 },
69 }).then((result) => {
70 if (result._tag === "Success") announce(`Opened in ${label}`);
71 });
72 };
73 window.addEventListener("keydown", handler);
74 return () => window.removeEventListener("keydown", handler);
75 }, [
76 enabled,
77 environmentId,
78 keybindings,
79 openInCwd,
80 openInEditorMutation,
81 preferredEditor,
82 remote.mode,
83 ]);