All files / client/src/hooks useSimulatorKeyboardShortcuts.ts

97.43% Statements 38/39
87.75% Branches 43/49
100% Functions 8/8
97.05% Lines 33/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                                                  48x 13x 13x 13x                                   21x 18x 1x 1x 1x 1x   1x 1x   1x 1x 1x   1x 1x   1x                       18x 18x       21x 19x     16x     16x     12x         1x     11x         6x     5x         1x     4x                 19x 19x                            
import { useEffect } from "react";
import type { ToastFn } from "@/hooks/use-toast";
import type { ServerCapabilities } from "@/lib/server-capabilities";
 
type UseSimulatorKeyboardShortcutsOptions = {
  isMac: boolean;
  simulationStatus: "idle" | "running" | "compiling" | "queued" | "paused";
  compilePending: boolean;
  startPending: boolean;
  capabilities: ServerCapabilities;
  handleCompile: () => void;
  handleCompileAndStart: () => void;
  handleStop: () => void;
  handleFormatCode: () => void;
  handleNewFile: () => void;
  setDebugMode: (value: boolean) => void;
  toast: ToastFn;
};
 
function runShortcut(
  event: KeyboardEvent,
  matches: boolean,
  shouldRun: boolean,
  action: () => void,
): boolean {
  if (!matches) return false;
  event.preventDefault();
  if (shouldRun) action();
  return true;
}
 
export function useSimulatorKeyboardShortcuts({
  isMac,
  simulationStatus,
  compilePending,
  startPending,
  capabilities,
  handleCompile,
  handleCompileAndStart,
  handleStop,
  handleFormatCode,
  handleNewFile,
  setDebugMode,
  toast,
}: UseSimulatorKeyboardShortcutsOptions) {
  // Debug mode toggle (⌘+D / Ctrl+D)
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      const isModifierPressed = e.metaKey || e.ctrlKey;
      Eif (isModifierPressed && !e.altKey && !e.shiftKey && (e.key === "d" || e.key === "D")) {
        e.preventDefault();
        e.stopImmediatePropagation();
 
        const currentValue = globalThis.localStorage.getItem("unoDebugMode") === "1";
        const newValue = !currentValue;
 
        try {
          globalThis.localStorage.setItem("unoDebugMode", newValue ? "1" : "0");
          setDebugMode(newValue);
 
          const ev = new CustomEvent("debugModeChange", { detail: { value: newValue } });
          document.dispatchEvent(ev);
 
          toast({
            title: newValue ? "Debug Mode Enabled" : "Debug Mode Disabled",
            description: newValue
              ? "Telemetry displays are now visible"
              : "Telemetry displays are now hidden",
          });
        } catch (err) {
          console.error("Failed to toggle debug mode:", err);
        }
      }
    };
 
    document.addEventListener("keydown", handleKeyDown, { capture: true });
    return () => document.removeEventListener("keydown", handleKeyDown, { capture: true });
  }, [isMac, setDebugMode, toast]);
 
  // Application-level hotkeys (F5, Escape, ⌘/Ctrl+U)
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      // Always allow global shortcuts, even when focus is inside an editor/input.
      // (Avoid blocking them for the main editor textarea etc.)
      const isModifierPressed = isMac ? e.metaKey : e.ctrlKey;
 
      // F5: Compile only
      if (runShortcut(e, e.key === "F5", capabilities.canCompile && !compilePending, handleCompile)) return;
 
      // Escape: Stop simulation
      if (runShortcut(
        e,
        e.key === "Escape" && simulationStatus === "running" && capabilities.canSimulate,
        true,
        handleStop,
      )) return;
 
      // Meta/Ctrl + U: Compile and start
      if (runShortcut(
        e,
        isModifierPressed && e.key.toLowerCase() === "u",
        capabilities.canCompile && capabilities.canSimulate && !compilePending && !startPending,
        handleCompileAndStart,
      )) return;
 
      // Meta/Ctrl + Shift + F: Format code
      if (runShortcut(
        e,
        isModifierPressed && e.shiftKey && e.key.toLowerCase() === "f",
        true,
        handleFormatCode,
      )) return;
 
      // Meta/Ctrl + Alt + Shift + N: New file (less likely to be caught by browser menu shortcuts)
      runShortcut(
        e,
        isModifierPressed && e.altKey && e.shiftKey &&
          (e.key === "n" || e.key === "N" || e.code === "KeyN"),
        true,
        handleNewFile,
      );
    };
 
    globalThis.addEventListener("keydown", handleKeyDown, { capture: true });
    return () => globalThis.removeEventListener("keydown", handleKeyDown, { capture: true });
  }, [
    compilePending,
    startPending,
    capabilities,
    simulationStatus,
    isMac,
    handleCompile,
    handleCompileAndStart,
    handleStop,
    handleFormatCode,
    handleNewFile,
  ]);
}