Debian Codegraph @

frontend/src/reports/editor/EditorMenu.svelte

raw
1 <script lang="ts">
2 import { toggleComment } from "@codemirror/commands";
3 import { foldAll, unfoldAll } from "@codemirror/language";
4 import type { EditorView } from "@codemirror/view";
5 import type { Snippet } from "svelte";
6
7 import { beancountFormat } from "../../codemirror/beancount-format";
8 import { scrollToLine } from "../../codemirror/editor-transactions";
9 import { urlFor } from "../../helpers";
10 import { _ } from "../../i18n";
11 import { modKey } from "../../keyboard-shortcuts";
12 import router from "../../router";
13 import { fava_options, options } from "../../stores";
14 import AppMenu from "./AppMenu.svelte";
15 import AppMenuItem from "./AppMenuItem.svelte";
16 import AppMenuSubItem from "./AppMenuSubItem.svelte";
17 import Key from "./Key.svelte";
18
19 interface Props {
20 file_path: string;
21 editor: EditorView;
22 children: Snippet;
23 }
24
25 let { file_path, editor, children }: Props = $props();
26
27 let sources = $derived([
28 $options.filename,
29 ...$options.include.filter((f) => f !== $options.filename),
30 ]);
31 let insertEntryOptions = $derived($fava_options.insert_entry);
32
33 function goToFileAndLine(filename: string, line?: number) {
34 const url = $urlFor("editor/", { file_path: filename, line });
35 // only load if the file changed.
36 const load = filename !== file_path;
37 router.navigate(url, load);
38 if (!load && line != null) {
39 // Scroll to line if we didn't change to a different file.
40 editor.dispatch(scrollToLine(editor.state, line));
41 editor.focus();
42 }
43 }
44 </script>
45
46 <div>
47 <AppMenu>
48 <AppMenuItem name={_("File")}>
49 {#each sources as source}
50 <AppMenuSubItem
51 action={() => {
52 goToFileAndLine(source);
53 }}
54 selected={source === file_path}
55 >
56 {source}
57 </AppMenuSubItem>
58 {/each}
59 </AppMenuItem>
60 <AppMenuItem name={_("Edit")}>
61 <AppMenuSubItem action={() => beancountFormat(editor)}>
62 {_("Align Amounts")}
63 {#snippet right()}
64 <Key key={[modKey, "d"]} />
65 {/snippet}
66 </AppMenuSubItem>
67 <AppMenuSubItem action={() => toggleComment(editor)}>
68 {_("Toggle Comment (selection)")}
69 {#snippet right()}
70 <Key key={[modKey, "/"]} />
71 {/snippet}
72 </AppMenuSubItem>
73 <AppMenuSubItem action={() => unfoldAll(editor)}>
74 {_("Open all folds")}
75 {#snippet right()}
76 <Key key={["Ctrl", "Alt", "]"]} />
77 {/snippet}
78 </AppMenuSubItem>
79 <AppMenuSubItem action={() => foldAll(editor)}>
80 {_("Close all folds")}
81 {#snippet right()}
82 <Key key={["Ctrl", "Alt", "["]} />
83 {/snippet}
84 </AppMenuSubItem>
85 </AppMenuItem>
86 {#if insertEntryOptions.length}
87 <AppMenuItem name={`'insert-entry' ${_("Options")}`}>
88 {#each insertEntryOptions as opt}
89 <AppMenuSubItem
90 title={`${opt.filename}:${opt.lineno.toString()}`}
91 action={() => {
92 goToFileAndLine(opt.filename, opt.lineno - 1);
93 }}
94 >
95 {opt.re}
96 {#snippet right()}
97 <span>{opt.date}</span>
98 {/snippet}
99 </AppMenuSubItem>
100 {/each}
101 </AppMenuItem>
102 {/if}
103 </AppMenu>
104 {@render children()}
105 </div>
106
107 <style>
108 div {
109 display: flex;
110 flex-wrap: wrap;
111 align-items: center;
112 background: var(--sidebar-background);
113 border-bottom: 1px solid var(--sidebar-border);
114 }
115 </style>