Skip to content

Commit

Permalink
Add Mac shortcut support (#41)
Browse files Browse the repository at this point in the history
Closes #40
  • Loading branch information
daavidrgz authored Oct 8, 2024
1 parent e92db6d commit 6f18803
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 34 deletions.
32 changes: 16 additions & 16 deletions crates/web/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions crates/web/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"@radix-ui/react-toast": "^1.2.2",
"@radix-ui/react-tooltip": "^1.1.3",
"@types/js-beautify": "^1.14.3",
"@uiw/codemirror-themes": "^4.23.4",
"@uiw/react-codemirror": "^4.23.4",
"@uiw/codemirror-themes": "^4.23.5",
"@uiw/react-codemirror": "^4.23.5",
"@wasm-tool/wasm-pack-plugin": "^1.7.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
Expand All @@ -49,7 +49,7 @@
"react-dom": "^18.3.1",
"react-resizable-panels": "^2.1.4",
"sonner": "^1.5.0",
"tailwind-merge": "^2.5.2",
"tailwind-merge": "^2.5.3",
"tailwindcss-animate": "^1.0.7",
"vaul": "^1.0.0",
"vscode-languageserver-protocol": "^3.17.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ActionButton from "@/components/action-button/action-button";
import { isMac } from "@/lib/utils";
import { CirclePlay, Play } from "lucide-react";
import { useCallback, useEffect } from "react";

Expand All @@ -10,7 +11,7 @@ interface Props {
const ApplyButton = ({ autoApply, onClick }: Props) => {
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.ctrlKey && e.key === "Enter") {
if ((isMac ? e.metaKey : e.ctrlKey) && e.key === "Enter") {
e.preventDefault();
onClick();
}
Expand Down
8 changes: 5 additions & 3 deletions crates/web/frontend/src/components/editor/editor-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isMac } from "@/lib/utils";
import type { Data } from "@/model/data";
import FileType from "@/model/file-type";
import {
Expand Down Expand Up @@ -96,6 +97,7 @@ const gqLanguageParser = LRLanguage.define({
const jsonLanguage = json();
const gqLanguage = new LanguageSupport(gqLanguageParser);
const yamlLanguage = yaml();
const modKey = isMac ? "Cmd" : "Ctrl";

const getCodemirrorLanguageByFileType = (fileType: FileType): LanguageSupport => {
switch (fileType) {
Expand Down Expand Up @@ -129,7 +131,7 @@ export const getCodemirrorExtensionsByFileType = (
return [
language,
urlPlugin,
Prec.highest(keymap.of([{ key: "Ctrl-Enter", run: () => true }])),
Prec.highest(keymap.of([{ key: `${modKey}-Enter`, run: () => true }])),
getDragAndDropExtension([FileType.JSON, FileType.YAML]),
];
case FileType.GQ:
Expand All @@ -144,8 +146,8 @@ export const getCodemirrorExtensionsByFileType = (
Prec.highest(
keymap.of([
{ key: "Tab", run: acceptCompletion },
{ key: "Ctrl-.", run: startCompletion },
{ key: "Ctrl-Enter", run: () => true },
{ key: `${modKey}-.`, run: startCompletion },
{ key: `${modKey}-Enter`, run: () => true },
]),
),
getDragAndDropExtension([FileType.GQ]),
Expand Down
4 changes: 2 additions & 2 deletions crates/web/frontend/src/components/editor/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useLazyState from "@/hooks/useLazyState";
import { gqTheme } from "@/lib/theme";
import { cn } from "@/lib/utils";
import { cn, isMac } from "@/lib/utils";
import { Data } from "@/model/data";
import FileType from "@/model/file-type";
import { type LoadingState, loading, notLoading } from "@/model/loading-state";
Expand Down Expand Up @@ -111,7 +111,7 @@ const Editor = ({
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (!focused) return;
if (event.ctrlKey && (event.key === "s" || event.key === "S")) {
if ((isMac ? event.metaKey : event.ctrlKey) && (event.key === "s" || event.key === "S")) {
event.preventDefault();
handleFormatCode(content, type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isMac } from "@/lib/utils";
import { Keyboard, X } from "lucide-react";
import { useState } from "react";
import ActionButton from "../action-button/action-button";
Expand Down Expand Up @@ -34,7 +35,7 @@ const ShortcutPopup = () => {
Check all available keyboard shortcuts to improve your efficiency
</DialogDescription>
</DialogHeader>
{shortcutSections.map((shortcutSection) => (
{shortcutSections(isMac).map((shortcutSection) => (
<div className="mt-8" key={shortcutSection.title}>
<h3 className="font-semibold mb-1">{shortcutSection.title}</h3>
<Table>
Expand Down
16 changes: 8 additions & 8 deletions crates/web/frontend/src/components/shortcut-popup/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ export type Shortcut = {
shortcut: string;
};

const globalShortcuts: Shortcut[] = [
const globalShortcuts = (isMac: boolean) => [
{
description: "Apply the current query",
shortcut: "Ctrl + Enter",
shortcut: `${isMac ? "⌘" : "Ctrl"} + Enter`,
},
];

const editorShortcuts: Shortcut[] = [
const editorShortcuts = (isMac: boolean) => [
{
description: "Show autocompletions",
shortcut: "Ctrl + .",
shortcut: `${isMac ? "⌘" : "Ctrl"} + .`,
},
{
description: "Format the content of the focused editor",
shortcut: "Ctrl + S",
shortcut: `${isMac ? "⌘" : "Ctrl"} + S`,
},
];

export const shortcutSections: ShortcutSection[] = [
export const shortcutSections = (isMac: boolean): ShortcutSection[] => [
{
title: "Global Scope",
shortcuts: globalShortcuts,
shortcuts: globalShortcuts(isMac),
},
{
title: "Editor Scope",
shortcuts: editorShortcuts,
shortcuts: editorShortcuts(isMac),
},
];
2 changes: 2 additions & 0 deletions crates/web/frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export function formatNumber(number: number, decimals = 1) {
return `${Number.parseFloat((number / 1000 ** i).toFixed(dm))}${sizes[i]}`;
}

export const isMac = navigator.platform.includes("Mac"); // Deprecated navigator.platform

export const statusTextMap = new Map([
[200, "OK"],
[201, "Created"],
Expand Down

0 comments on commit 6f18803

Please sign in to comment.