All files / client/src/hooks useSimulatorKeyboardShortcuts.ts

97.77% Statements 44/45
84.78% Branches 39/46
100% Functions 7/7
97.67% Lines 42/43

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                                                              16x 14x 1x 1x 1x 1x   1x 1x   1x 1x 1x   1x 1x   1x                       14x 14x       16x 14x     9x     9x 2x 2x 1x   2x       7x 1x 1x 1x       6x 2x 2x 1x   2x       4x 1x 1x 1x       3x           1x 1x       14x 14x                          
import { useEffect } from "react";
import type { ToastFn } from "@/hooks/use-toast";
 
type UseSimulatorKeyboardShortcutsOptions = {
  isMac: boolean;
  simulationStatus: "running" | "stopped" | "paused";
  compilePending: boolean;
  startPending: boolean;
  handleCompile: () => void;
  handleCompileAndStart: () => void;
  handleStop: () => void;
  handleFormatCode: () => void;
  handleNewFile: () => void;
  setDebugMode: (value: boolean) => void;
  toast: ToastFn;
};
 
export function useSimulatorKeyboardShortcuts({
  isMac,
  simulationStatus,
  compilePending,
  startPending,
  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 (e.key === "F5") {
        e.preventDefault();
        if (!compilePending) {
          handleCompile();
        }
        return;
      }
 
      // Escape: Stop simulation
      if (e.key === "Escape" && simulationStatus === "running") {
        e.preventDefault();
        handleStop();
        return;
      }
 
      // Meta/Ctrl + U: Compile and start
      if (isModifierPressed && e.key.toLowerCase() === "u") {
        e.preventDefault();
        if (!compilePending && !startPending) {
          handleCompileAndStart();
        }
        return;
      }
 
      // Meta/Ctrl + Shift + F: Format code
      if (isModifierPressed && e.shiftKey && e.key.toLowerCase() === "f") {
        e.preventDefault();
        handleFormatCode();
        return;
      }
 
      // Meta/Ctrl + Alt + Shift + N: New file (less likely to be caught by browser menu shortcuts)
      if (
        isModifierPressed &&
        e.altKey &&
        e.shiftKey &&
        (e.key === "n" || e.key === "N" || e.code === "KeyN")
      ) {
        e.preventDefault();
        handleNewFile();
      }
    };
 
    globalThis.addEventListener("keydown", handleKeyDown, { capture: true });
    return () => globalThis.removeEventListener("keydown", handleKeyDown, { capture: true });
  }, [
    compilePending,
    startPending,
    simulationStatus,
    isMac,
    handleCompile,
    handleCompileAndStart,
    handleStop,
    handleFormatCode,
    handleNewFile,
  ]);
}