diff --git a/e2e/command-menu-shortcuts.spec.ts b/e2e/command-menu-shortcuts.spec.ts new file mode 100644 index 0000000..781c371 --- /dev/null +++ b/e2e/command-menu-shortcuts.spec.ts @@ -0,0 +1,38 @@ +import { expect, test } from "@playwright/test"; + +const platforms = [ + { name: "macOS", userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)", modifier: "Meta" }, + { name: "Windows", userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", modifier: "Control" }, + { name: "Linux", userAgent: "Mozilla/5.0 (X11; Linux x86_64)", modifier: "Control" }, +]; + +for (const { name, userAgent, modifier } of platforms) { + test.describe(`${name} command menu shortcut`, () => { + test.use({ userAgent }); + + test("uses the platform modifier without intercepting macOS Control+K", async ({ page }) => { + await page.goto("/"); + await expect(page.locator(".cm-editor")).toBeVisible(); + await page.locator(".cm-content").focus(); + + const dialog = page.getByRole("dialog"); + await expect(dialog).not.toBeVisible(); + + if (name === "macOS") { + await page.keyboard.press("Control+k"); + await expect(dialog).not.toBeVisible(); + } + + await page.keyboard.press(`${modifier}+k`); + await expect(dialog).toBeVisible(); + + if (name === "macOS") { + await page.keyboard.press("Control+k"); + await expect(dialog).toBeVisible(); + } + + await page.keyboard.press(`${modifier}+k`); + await expect(dialog).not.toBeVisible(); + }); + }); +} diff --git a/e2e/editor.spec.ts b/e2e/editor.spec.ts index 9b31a79..9d3a7dd 100644 --- a/e2e/editor.spec.ts +++ b/e2e/editor.spec.ts @@ -187,8 +187,7 @@ test.describe("Editor Page", () => { await expect(dialog).not.toBeVisible(); // open command menu by Ctrl+K(or Cmd+K) - const isMac = process.platform === "darwin"; - const modifier = isMac ? "Meta" : "Control"; + const modifier = await page.evaluate(() => (/mac/i.test(navigator.userAgent) ? "Meta" : "Control")); await page.keyboard.press(`${modifier}+k`); await expect(dialog).toBeVisible(); await expect(dialog).toHaveClass(/opacity-100/); @@ -206,8 +205,7 @@ test.describe("Editor Page", () => { await expect(dialog).not.toBeVisible(); // open command menu - const isMac = process.platform === "darwin"; - const modifier = isMac ? "Meta" : "Control"; + const modifier = await page.evaluate(() => (/mac/i.test(navigator.userAgent) ? "Meta" : "Control")); await page.keyboard.press(`${modifier}+k`); // Dialog should now be visible diff --git a/src/features/menu/command-menu.tsx b/src/features/menu/command-menu.tsx index 9547db5..0e5eff2 100644 --- a/src/features/menu/command-menu.tsx +++ b/src/features/menu/command-menu.tsx @@ -31,6 +31,7 @@ import { import { saveSnapshotWithToast } from "../snapshots/snapshot-storage"; import { useFocusMode } from "../../utils/hooks/use-focus-mode"; import { useAtom } from "jotai"; +import { isMac } from "../../utils/platform"; type CommandMenuProps = { open: boolean; @@ -86,7 +87,7 @@ export const CommandMenu = ({ // which trips jsx-a11y/no-autofocus. const raf = requestAnimationFrame(() => inputRef.current?.focus()); const handleKeyDown = (e: KeyboardEvent) => { - if (e.key === "Escape" || (e.key === "k" && (e.ctrlKey || e.metaKey))) { + if (e.key === "Escape" || (e.key === "k" && (isMac() ? e.metaKey : e.ctrlKey))) { e.preventDefault(); e.stopPropagation(); onClose(); diff --git a/src/utils/hooks/use-command-k.ts b/src/utils/hooks/use-command-k.ts index de2a386..407b401 100644 --- a/src/utils/hooks/use-command-k.ts +++ b/src/utils/hooks/use-command-k.ts @@ -1,4 +1,5 @@ import { useState, useEffect, useCallback } from "react"; +import { isMac } from "../platform"; export const useCommandK = (isModalOpen?: boolean) => { const [isCommandMenuOpen, setIsCommandMenuOpen] = useState(false); @@ -15,7 +16,7 @@ export const useCommandK = (isModalOpen?: boolean) => { useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { - if (event.key === "k" && (event.ctrlKey || event.metaKey)) { + if (event.key === "k" && (isMac() ? event.metaKey : event.ctrlKey)) { event.preventDefault(); toggleCommandMenu(); }