Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions e2e/command-menu-shortcuts.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
}
6 changes: 2 additions & 4 deletions e2e/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/features/menu/command-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/utils/hooks/use-command-k.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useEffect, useCallback } from "react";
import { isMac } from "../platform";

export const useCommandK = (isModalOpen?: boolean) => {
const [isCommandMenuOpen, setIsCommandMenuOpen] = useState(false);
Expand All @@ -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();
}
Expand Down
Loading