Debian Codegraph @

frontend/src/reports/editor/Editor.svelte

raw
1 <script lang="ts">
2 import type { EditorView } from "@codemirror/view";
3 import { onMount, untrack } from "svelte";
4
5 import { get, put } from "../../api";
6 import {
7 replaceContents,
8 scrollToLine,
9 setErrors,
10 } from "../../codemirror/editor-transactions";
11 import { initBeancountEditor } from "../../codemirror/setup";
12 import SaveButton from "../../editor/SaveButton.svelte";
13 import { log_error } from "../../log";
14 import { notify_err } from "../../notifications";
15 import router from "../../router";
16 import { errors, fava_options } from "../../stores";
17 import { searchParams } from "../../stores/url";
18 import type { EditorReportProps } from ".";
19 import EditorMenu from "./EditorMenu.svelte";
20
21 let { source, beancount_language_support }: EditorReportProps = $props();
22
23 let file_path = $derived(source.file_path);
24
25 let changed = $state(false);
26 const onDocChanges = () => {
27 changed = true;
28 };
29
30 let sha256sum = $state("");
31 let saving = $state(false);
32
33 /**
34 * Save the contents of the editor.
35 */
36 async function save(cm: EditorView) {
37 saving = true;
38 try {
39 sha256sum = await put("source", {
40 file_path,
41 source: cm.state.sliceDoc(),
42 sha256sum,
43 });
44 changed = false;
45 cm.focus();
46 get("errors").then((v) => {
47 errors.set(v);
48 }, log_error);
49 } catch (error) {
50 notify_err(error, (e) => e.message);
51 } finally {
52 saving = false;
53 }
54 }
55
56 const { editor, renderEditor } = initBeancountEditor(
57 "",
58 onDocChanges,
59 [
60 {
61 key: "Control-s",
62 mac: "Meta-s",
63 run: () => {
64 save(editor).catch(() => {
65 // save should catch all errors itself, see above
66 });
67 return true;
68 },
69 },
70 ],
71 beancount_language_support,
72 );
73
74 $effect(() => {
75 // update editor contents if source changes
76 // eslint-disable-next-line @typescript-eslint/no-unused-expressions
77 source;
78 untrack(() => {
79 editor.dispatch(replaceContents(editor.state, source.source));
80 sha256sum = source.sha256sum;
81 editor.focus();
82 changed = false;
83 });
84 });
85
86 $effect(() => {
87 // Go to line if the edited file changes.
88 // eslint-disable-next-line @typescript-eslint/no-unused-expressions
89 file_path;
90 untrack(() => {
91 const opts = $fava_options.insert_entry.filter(
92 (f) => f.filename === file_path,
93 );
94 const last_insert_opt = opts[opts.length - 1];
95 const line = parseInt($searchParams.get("line") ?? "0", 10);
96 let line_to_scroll_to = null;
97 if (line > 0) {
98 line_to_scroll_to = line;
99 } else if (last_insert_opt) {
100 line_to_scroll_to = last_insert_opt.lineno - 1;
101 }
102 editor.dispatch(
103 scrollToLine(editor.state, line_to_scroll_to ?? editor.state.doc.lines),
104 );
105 });
106 });
107
108 $effect(() => {
109 // Update diagnostics, showing errors in the editor
110 // Only show errors for this file, or general errors (AKA no source)
111 const errorsForFile = $errors.filter(
112 (err) => err.source === null || err.source.filename === file_path,
113 );
114 editor.dispatch(setErrors(editor.state, errorsForFile));
115 });
116
117 const checkEditorChanges = () =>
118 changed
119 ? "There are unsaved changes. Are you sure you want to leave?"
120 : null;
121
122 onMount(() => router.addInteruptHandler(checkEditorChanges));
123 </script>
124
125 <form
126 class="fixed-fullsize-container"
127 onsubmit={async (event) => {
128 event.preventDefault();
129 return save(editor);
130 }}
131 >
132 <EditorMenu {file_path} {editor}>
133 <SaveButton {changed} {saving} />
134 </EditorMenu>
135 <div use:renderEditor></div>
136 </form>
137
138 <style>
139 form {
140 display: flex;
141 flex-direction: column;
142 }
143
144 form div {
145 flex: 1;
146 width: 100%;
147 height: 100px; /* As a flex-base so that it uses exactly the available space. */
148 }
149
150 form :global(.cm-editor) {
151 width: 100%;
152 height: 100%;
153 overflow: hidden;
154 }
155 </style>