Skip to content

Commit a6519cd

Browse files
committed
Add keybinds
1 parent 012c5a8 commit a6519cd

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

web/src/lib/components/menu-bar/MenuBar.svelte

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<script lang="ts">
22
import { MultiFileDiffViewerState } from "$lib/diff-viewer.svelte";
3+
import { Keybinds } from "$lib/keybinds.svelte";
34
import { Menubar, Button } from "bits-ui";
45
56
const viewer = MultiFileDiffViewerState.get();
67
</script>
78

9+
{#snippet keybind(key: string)}
10+
<span class="text-em-med">{Keybinds.getModifierKey()}+{key}</span>
11+
{/snippet}
12+
813
<Menubar.Root class="flex border-b leading-none">
914
<Menubar.Menu>
1015
<Menubar.Trigger class="btn-ghost px-2 py-1 text-sm font-medium data-[state=open]:btn-ghost-hover">diffs.dev</Menubar.Trigger>
@@ -45,12 +50,13 @@
4550
</Menubar.Item>
4651
<Menubar.Separator class="h-px w-full bg-edge" />
4752
<Menubar.Item
48-
class="btn-ghost px-2 py-1"
53+
class="flex justify-between gap-2 btn-ghost px-2 py-1"
4954
onSelect={() => {
50-
viewer.settingsDialogOpen = true;
55+
viewer.openSettingsDialog();
5156
}}
5257
>
5358
Open Settings
59+
{@render keybind(",")}
5460
</Menubar.Item>
5561
</Menubar.Content>
5662
</Menubar.Portal>
@@ -60,12 +66,13 @@
6066
<Menubar.Portal>
6167
<Menubar.Content class="border bg-neutral text-sm" align="start">
6268
<Menubar.Item
63-
class="btn-ghost px-2 py-1"
69+
class="flex justify-between gap-2 btn-ghost px-2 py-1"
6470
onSelect={() => {
65-
viewer.openDiffDialogOpen = true;
71+
viewer.openOpenDiffDialog();
6672
}}
6773
>
6874
Open
75+
{@render keybind("O")}
6976
</Menubar.Item>
7077
</Menubar.Content>
7178
</Menubar.Portal>

web/src/lib/diff-viewer.svelte.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { VList } from "virtua/svelte";
1717
import { Context, Debounced, watch } from "runed";
1818
import { MediaQuery } from "svelte/reactivity";
1919
import { ProgressBarState } from "$lib/components/progress-bar/index.svelte";
20+
import { Keybinds } from "./keybinds.svelte";
2021

2122
export const GITHUB_URL_PARAM = "github_url";
2223
export const PATCH_URL_PARAM = "patch_url";
@@ -241,6 +242,20 @@ export class MultiFileDiffViewerState {
241242
private constructor() {
242243
// Make sure to revoke object URLs when the component is destroyed
243244
onDestroy(() => this.clearImages());
245+
246+
const keybinds = new Keybinds();
247+
keybinds.registerModifierBind("o", () => this.openOpenDiffDialog());
248+
keybinds.registerModifierBind(",", () => this.openSettingsDialog());
249+
}
250+
251+
openOpenDiffDialog() {
252+
this.openDiffDialogOpen = true;
253+
this.settingsDialogOpen = false;
254+
}
255+
256+
openSettingsDialog() {
257+
this.settingsDialogOpen = true;
258+
this.openDiffDialogOpen = false;
244259
}
245260

246261
filterFile(file: FileDetails): boolean {

web/src/lib/keybinds.svelte.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { onMount } from "svelte";
2+
import { on } from "svelte/events";
3+
4+
export class Keybinds {
5+
private static readonly IS_MAC = typeof navigator !== "undefined" && navigator.userAgent.includes("Mac");
6+
7+
static getModifierKey() {
8+
return Keybinds.IS_MAC ? "⌘" : "Ctrl";
9+
}
10+
11+
private readonly binds = new Map<string, () => void>();
12+
13+
constructor() {
14+
onMount(() => {
15+
return on(document, "keydown", (event) => {
16+
const modifierPressed = Keybinds.IS_MAC ? event.metaKey : event.ctrlKey;
17+
if (modifierPressed) {
18+
const bindAction = this.binds.get(event.key.toLowerCase());
19+
if (bindAction) {
20+
bindAction();
21+
event.preventDefault();
22+
}
23+
}
24+
});
25+
});
26+
}
27+
28+
registerModifierBind(key: string, action: () => void) {
29+
this.binds.set(key.toLowerCase(), action);
30+
}
31+
}

0 commit comments

Comments
 (0)