@lapis-notes/lapis-plugin-beancount 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +341 -0
- package/README.md +38 -2
- package/dist/LedgerEditor.svelte +113 -0
- package/dist/LedgerEditor.svelte.d.ts +10 -0
- package/dist/LedgerSidebar.svelte +31 -0
- package/dist/LedgerSidebar.svelte.d.ts +9 -0
- package/dist/StudioLeaf.svelte +18 -0
- package/dist/StudioLeaf.svelte.d.ts +10 -0
- package/dist/beancount-editor.css +11 -0
- package/dist/beancount-plugin.d.ts +11 -0
- package/dist/beancount-plugin.js +79 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/runtime.d.ts +3 -0
- package/dist/runtime.js +3 -0
- package/dist/studio-views.d.ts +37 -0
- package/dist/studio-views.js +151 -0
- package/dist/vault-ledger.d.ts +8 -0
- package/dist/vault-ledger.js +181 -0
- package/dist/vault-projects.d.ts +9 -0
- package/dist/vault-projects.js +72 -0
- package/dist/view-types.d.ts +6 -0
- package/dist/view-types.js +6 -0
- package/manifest.json +67 -0
- package/package.json +151 -3
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Plugin, SourceTextFileView, } from "@lapis-notes/api";
|
|
2
|
+
import { markupEditor } from "@lapis-notes/api/editor";
|
|
3
|
+
import { getWorkspaceHostBinding } from "@lapis-notes/api/workspace-host";
|
|
4
|
+
import { createLedgerEditorExtensions } from "@beancount-js/ui/embedded";
|
|
5
|
+
import { beanCount } from "@beancount-js/codemirror-lang-beancount";
|
|
6
|
+
import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
|
|
7
|
+
import { EditorView } from "@codemirror/view";
|
|
8
|
+
import { tags } from "@lezer/highlight";
|
|
9
|
+
import "./beancount-editor.css";
|
|
10
|
+
import { writable } from "svelte/store";
|
|
11
|
+
import { createVaultProjects } from "./vault-projects";
|
|
12
|
+
import { LedgerSidebarView, StudioView, } from "./studio-views";
|
|
13
|
+
import { BEANCOUNT_SIDEBAR_VIEW_TYPE, BEANCOUNT_SOURCE_VIEW_TYPE, BEANCOUNT_STUDIO_VIEW_TYPE, } from "./view-types";
|
|
14
|
+
const MANIFEST = {
|
|
15
|
+
id: "beancount",
|
|
16
|
+
name: "Beancount",
|
|
17
|
+
version: "0.1.0",
|
|
18
|
+
minAppVersion: "0.0.1",
|
|
19
|
+
description: "Beancount Studio for Lapis Notes ledger workspaces",
|
|
20
|
+
author: "Lapis Notes",
|
|
21
|
+
requiredCapabilities: ["plugin:secrets"],
|
|
22
|
+
};
|
|
23
|
+
export class BeancountPlugin extends Plugin {
|
|
24
|
+
studioOptions;
|
|
25
|
+
studio = writable();
|
|
26
|
+
constructor(app, manifest = MANIFEST, studioOptions) {
|
|
27
|
+
super(app, manifest);
|
|
28
|
+
this.studioOptions = studioOptions;
|
|
29
|
+
}
|
|
30
|
+
async onload() {
|
|
31
|
+
const options = this.studioOptions ?? {
|
|
32
|
+
source: createVaultProjects(this.app.vault, await this.loadData(), (data) => this.saveData(data)),
|
|
33
|
+
};
|
|
34
|
+
this.registerView(BEANCOUNT_STUDIO_VIEW_TYPE, (leaf) => new StudioView(leaf, this.studio, options));
|
|
35
|
+
this.registerSidebarView(BEANCOUNT_SIDEBAR_VIEW_TYPE, (leaf) => new LedgerSidebarView(leaf, this.studio, () => this.openStudio()), { side: "left", title: "Beancount Ledger", icon: "book-open" });
|
|
36
|
+
this.registerView(BEANCOUNT_SOURCE_VIEW_TYPE, (leaf) => new SourceTextFileView(leaf, BEANCOUNT_SOURCE_VIEW_TYPE, "beancount"), { kind: "file" });
|
|
37
|
+
this.registerEditorView({
|
|
38
|
+
id: BEANCOUNT_SOURCE_VIEW_TYPE,
|
|
39
|
+
viewType: BEANCOUNT_SOURCE_VIEW_TYPE,
|
|
40
|
+
label: "Beancount source",
|
|
41
|
+
filenamePatterns: ["*.beancount"],
|
|
42
|
+
priority: "exclusive",
|
|
43
|
+
});
|
|
44
|
+
this.registerExtensions(["beancount"], BEANCOUNT_SOURCE_VIEW_TYPE);
|
|
45
|
+
this.registerEditorExtension(() => {
|
|
46
|
+
const language = beanCount();
|
|
47
|
+
const identifiers = HighlightStyle.define([{ tag: tags.variableName, class: "beancount-identifier" }], { scope: language.language });
|
|
48
|
+
return markupEditor({ app: this.app, language: "beancount" }, EditorView.contentAttributes.of({ "aria-label": "Beancount source" }), language, syntaxHighlighting(identifiers), createLedgerEditorExtensions());
|
|
49
|
+
}, BEANCOUNT_SOURCE_VIEW_TYPE);
|
|
50
|
+
this.addCommand({
|
|
51
|
+
id: "open",
|
|
52
|
+
name: "Open Beancount Studio",
|
|
53
|
+
callback: () => void this.openStudio(),
|
|
54
|
+
});
|
|
55
|
+
this.addRibbonIcon("book-open", "Open Beancount Studio", () => void this.openStudio());
|
|
56
|
+
}
|
|
57
|
+
async openStudio() {
|
|
58
|
+
const { workspace } = this.app;
|
|
59
|
+
if (!workspace.getLeavesOfType(BEANCOUNT_SIDEBAR_VIEW_TYPE).length) {
|
|
60
|
+
const sidebar = workspace.ensureSideLeaf(BEANCOUNT_SIDEBAR_VIEW_TYPE);
|
|
61
|
+
await sidebar.setViewState({ type: BEANCOUNT_SIDEBAR_VIEW_TYPE });
|
|
62
|
+
await workspace.revealLeaf(sidebar);
|
|
63
|
+
}
|
|
64
|
+
const existing = workspace.getLeavesOfType(BEANCOUNT_STUDIO_VIEW_TYPE)[0];
|
|
65
|
+
const active = workspace.activeRootLeaf;
|
|
66
|
+
const leaf = existing ??
|
|
67
|
+
(active?.view.getViewType() === "empty" ? active : workspace.getLeaf("tab"));
|
|
68
|
+
if (!existing) {
|
|
69
|
+
await leaf.setViewState({ type: BEANCOUNT_STUDIO_VIEW_TYPE, active: true });
|
|
70
|
+
}
|
|
71
|
+
workspace.activateLeaf(leaf);
|
|
72
|
+
await workspace.revealLeaf(leaf);
|
|
73
|
+
const { renderer } = getWorkspaceHostBinding(workspace).controller;
|
|
74
|
+
if (renderer.displayMode === "mobile")
|
|
75
|
+
renderer.mobileNavigation.openEditor();
|
|
76
|
+
return leaf;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export default BeancountPlugin;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { View, type WorkspaceLeaf } from "@lapis-notes/api";
|
|
2
|
+
import { type BeancountController, type BeancountDataSource } from "@beancount-js/ui/controller";
|
|
3
|
+
import type { DateTime } from "luxon";
|
|
4
|
+
import type { Writable } from "svelte/store";
|
|
5
|
+
export interface StudioOptions {
|
|
6
|
+
source: BeancountDataSource;
|
|
7
|
+
clock?: () => DateTime;
|
|
8
|
+
initialRoute?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare class StudioView extends View {
|
|
11
|
+
private readonly studio;
|
|
12
|
+
private readonly options;
|
|
13
|
+
private component?;
|
|
14
|
+
private controller?;
|
|
15
|
+
constructor(leaf: WorkspaceLeaf, studio: Writable<BeancountController | undefined>, options: StudioOptions);
|
|
16
|
+
getViewType(): string;
|
|
17
|
+
getDisplayText(): string;
|
|
18
|
+
getIcon(): string;
|
|
19
|
+
protected onOpen(): Promise<void>;
|
|
20
|
+
protected onClose(): Promise<void>;
|
|
21
|
+
onload(): void;
|
|
22
|
+
private observeVault;
|
|
23
|
+
onunload(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export declare class LedgerSidebarView extends View {
|
|
26
|
+
private readonly studio;
|
|
27
|
+
private readonly openStudio;
|
|
28
|
+
private component?;
|
|
29
|
+
constructor(leaf: WorkspaceLeaf, studio: Writable<BeancountController | undefined>, openStudio: () => Promise<unknown>);
|
|
30
|
+
getViewType(): string;
|
|
31
|
+
getDisplayText(): string;
|
|
32
|
+
getIcon(): string;
|
|
33
|
+
protected onOpen(): Promise<void>;
|
|
34
|
+
protected onClose(): Promise<void>;
|
|
35
|
+
onload(): void;
|
|
36
|
+
onunload(): Promise<void>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { View } from "@lapis-notes/api";
|
|
2
|
+
import { createBeancountController, } from "@beancount-js/ui/controller";
|
|
3
|
+
import { mount, unmount } from "svelte";
|
|
4
|
+
import { BEANCOUNT_SIDEBAR_VIEW_TYPE, BEANCOUNT_STUDIO_VIEW_TYPE, } from "./view-types";
|
|
5
|
+
import { resolveVaultPath } from "./vault-ledger";
|
|
6
|
+
import LedgerSidebar from "./LedgerSidebar.svelte";
|
|
7
|
+
import StudioLeaf from "./StudioLeaf.svelte";
|
|
8
|
+
export class StudioView extends View {
|
|
9
|
+
studio;
|
|
10
|
+
options;
|
|
11
|
+
component;
|
|
12
|
+
controller;
|
|
13
|
+
constructor(leaf, studio, options) {
|
|
14
|
+
super(leaf);
|
|
15
|
+
this.studio = studio;
|
|
16
|
+
this.options = options;
|
|
17
|
+
}
|
|
18
|
+
getViewType() {
|
|
19
|
+
return BEANCOUNT_STUDIO_VIEW_TYPE;
|
|
20
|
+
}
|
|
21
|
+
getDisplayText() {
|
|
22
|
+
return "Beancount";
|
|
23
|
+
}
|
|
24
|
+
getIcon() {
|
|
25
|
+
return "book-open";
|
|
26
|
+
}
|
|
27
|
+
async onOpen() { }
|
|
28
|
+
async onClose() { }
|
|
29
|
+
onload() {
|
|
30
|
+
this.containerEl.style.cssText +=
|
|
31
|
+
";display:flex;width:100%;height:100%;min-height:0;overflow:hidden";
|
|
32
|
+
const controller = createBeancountController({
|
|
33
|
+
source: this.options.source,
|
|
34
|
+
clock: this.options.clock,
|
|
35
|
+
initialRoute: this.options.initialRoute,
|
|
36
|
+
});
|
|
37
|
+
this.controller = controller;
|
|
38
|
+
this.component = mount(StudioLeaf, {
|
|
39
|
+
target: this.containerEl,
|
|
40
|
+
props: { leaf: this.leaf, controller },
|
|
41
|
+
});
|
|
42
|
+
this.studio.set(controller);
|
|
43
|
+
this.observeVault(controller);
|
|
44
|
+
void controller
|
|
45
|
+
.initialize()
|
|
46
|
+
.then(() => {
|
|
47
|
+
if (controller.state.canAddProject && !controller.state.projects.length) {
|
|
48
|
+
controller.actions.sidebar.setMode("projects");
|
|
49
|
+
}
|
|
50
|
+
if (this.options.source.request) {
|
|
51
|
+
return controller.workspace.refreshReviewLedger();
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
.catch(() => undefined);
|
|
55
|
+
}
|
|
56
|
+
observeVault(controller) {
|
|
57
|
+
const { vault } = this.app;
|
|
58
|
+
let queued = false;
|
|
59
|
+
let running = false;
|
|
60
|
+
const refresh = async () => {
|
|
61
|
+
running = true;
|
|
62
|
+
try {
|
|
63
|
+
while (queued && this.controller === controller) {
|
|
64
|
+
queued = false;
|
|
65
|
+
await controller.actions.refresh().catch(() => undefined);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
running = false;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const changed = ({ path }, oldPath) => {
|
|
73
|
+
const { snapshot } = controller.state;
|
|
74
|
+
if (!snapshot || this.controller !== controller)
|
|
75
|
+
return;
|
|
76
|
+
const affected = (candidate) => candidate === path ||
|
|
77
|
+
candidate.startsWith(path + "/") ||
|
|
78
|
+
(oldPath !== undefined &&
|
|
79
|
+
(candidate === oldPath || candidate.startsWith(oldPath + "/")));
|
|
80
|
+
const known = snapshot.ledgerFiles.some((file) => affected(file.path)) ||
|
|
81
|
+
snapshot.resources.some((file) => affected(file.path));
|
|
82
|
+
const missing = snapshot.issues.some((issue) => {
|
|
83
|
+
if ((issue.kind !== "missing-include" && issue.kind !== "missing-resource") ||
|
|
84
|
+
!issue.sourcePath || !issue.referencedPath)
|
|
85
|
+
return false;
|
|
86
|
+
const target = resolveVaultPath(issue.sourcePath, issue.referencedPath);
|
|
87
|
+
return target !== undefined && affected(target);
|
|
88
|
+
});
|
|
89
|
+
if (!known && !missing)
|
|
90
|
+
return;
|
|
91
|
+
queued = true;
|
|
92
|
+
if (!running)
|
|
93
|
+
void refresh();
|
|
94
|
+
};
|
|
95
|
+
this.registerEvent(vault.on("modify", changed));
|
|
96
|
+
this.registerEvent(vault.on("create", changed));
|
|
97
|
+
this.registerEvent(vault.on("delete", changed));
|
|
98
|
+
this.registerEvent(vault.on("rename", changed));
|
|
99
|
+
}
|
|
100
|
+
async onunload() {
|
|
101
|
+
const { component, controller } = this;
|
|
102
|
+
this.component = undefined;
|
|
103
|
+
this.controller = undefined;
|
|
104
|
+
this.studio.update((current) => current === controller ? undefined : current);
|
|
105
|
+
try {
|
|
106
|
+
if (component)
|
|
107
|
+
await unmount(component);
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
controller?.dispose();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export class LedgerSidebarView extends View {
|
|
115
|
+
studio;
|
|
116
|
+
openStudio;
|
|
117
|
+
component;
|
|
118
|
+
constructor(leaf, studio, openStudio) {
|
|
119
|
+
super(leaf);
|
|
120
|
+
this.studio = studio;
|
|
121
|
+
this.openStudio = openStudio;
|
|
122
|
+
}
|
|
123
|
+
getViewType() {
|
|
124
|
+
return BEANCOUNT_SIDEBAR_VIEW_TYPE;
|
|
125
|
+
}
|
|
126
|
+
getDisplayText() {
|
|
127
|
+
return "Beancount Ledger";
|
|
128
|
+
}
|
|
129
|
+
getIcon() {
|
|
130
|
+
return "book-open";
|
|
131
|
+
}
|
|
132
|
+
async onOpen() { }
|
|
133
|
+
async onClose() { }
|
|
134
|
+
onload() {
|
|
135
|
+
this.containerEl.style.cssText +=
|
|
136
|
+
";height:100%;min-height:0;overflow:hidden";
|
|
137
|
+
this.component = mount(LedgerSidebar, {
|
|
138
|
+
target: this.containerEl,
|
|
139
|
+
props: {
|
|
140
|
+
studio: this.studio,
|
|
141
|
+
openStudio: () => void this.openStudio(),
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async onunload() {
|
|
146
|
+
const component = this.component;
|
|
147
|
+
this.component = undefined;
|
|
148
|
+
if (component)
|
|
149
|
+
await unmount(component);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { App } from "@lapis-notes/api";
|
|
2
|
+
import type { BeancountDataSource } from "@beancount-js/ui/controller";
|
|
3
|
+
type LedgerVault = Pick<App["vault"], "getFileByPath" | "read" | "readBinary" | "process">;
|
|
4
|
+
type VaultLedgerSource = BeancountDataSource & Required<Pick<BeancountDataSource, "resourceUrl">>;
|
|
5
|
+
type Snapshot = Awaited<ReturnType<BeancountDataSource["loadProject"]>>;
|
|
6
|
+
export declare function resolveVaultPath(sourcePath: string, reference: string): string | undefined;
|
|
7
|
+
export declare function createVaultLedgerSource(vault: LedgerVault, project: Snapshot["project"]): VaultLedgerSource;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { DocumentDirective, IncludeDirective, parseDocument } from "@beancount-js/parser";
|
|
2
|
+
const documentTypes = {
|
|
3
|
+
csv: "text/csv",
|
|
4
|
+
gif: "image/gif",
|
|
5
|
+
jpeg: "image/jpeg",
|
|
6
|
+
jpg: "image/jpeg",
|
|
7
|
+
json: "application/json",
|
|
8
|
+
md: "text/markdown",
|
|
9
|
+
pdf: "application/pdf",
|
|
10
|
+
png: "image/png",
|
|
11
|
+
svg: "image/svg+xml",
|
|
12
|
+
txt: "text/plain",
|
|
13
|
+
webp: "image/webp",
|
|
14
|
+
yaml: "application/yaml",
|
|
15
|
+
yml: "application/yaml",
|
|
16
|
+
};
|
|
17
|
+
const ledgerExtension = /\.(?:bean|beancount|ledger)$/i;
|
|
18
|
+
export function resolveVaultPath(sourcePath, reference) {
|
|
19
|
+
if (!reference || reference.startsWith("/") || /[\\:\x00-\x1f\x7f]/.test(reference)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const components = sourcePath.split("/").slice(0, -1);
|
|
23
|
+
for (const part of reference.split("/")) {
|
|
24
|
+
if (!part || part === ".")
|
|
25
|
+
continue;
|
|
26
|
+
if (part === "..") {
|
|
27
|
+
if (!components.length)
|
|
28
|
+
return;
|
|
29
|
+
components.pop();
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (/^\.(?:lapis|git|jj)$/i.test(part) || /[. ]$/.test(part))
|
|
33
|
+
return;
|
|
34
|
+
components.push(part);
|
|
35
|
+
}
|
|
36
|
+
return components.join("/");
|
|
37
|
+
}
|
|
38
|
+
export function createVaultLedgerSource(vault, project) {
|
|
39
|
+
const { entryFile, id: projectId, updatedAt } = project;
|
|
40
|
+
if (resolveVaultPath("", entryFile) !== entryFile ||
|
|
41
|
+
!ledgerExtension.test(entryFile)) {
|
|
42
|
+
throw new Error("Select a vault-relative Beancount ledger entry.");
|
|
43
|
+
}
|
|
44
|
+
async function loadProject(id, signal) {
|
|
45
|
+
signal?.throwIfAborted();
|
|
46
|
+
if (id !== projectId)
|
|
47
|
+
throw new Error("The ledger project is unavailable.");
|
|
48
|
+
const ledgerFiles = [];
|
|
49
|
+
const issues = [];
|
|
50
|
+
const resources = new Map();
|
|
51
|
+
const pending = [{ path: entryFile, sourcePath: undefined, referencedPath: undefined }];
|
|
52
|
+
const visited = new Set([entryFile]);
|
|
53
|
+
for (const { path, sourcePath, referencedPath } of pending) {
|
|
54
|
+
signal?.throwIfAborted();
|
|
55
|
+
let contents;
|
|
56
|
+
try {
|
|
57
|
+
const file = vault.getFileByPath(path);
|
|
58
|
+
if (!file)
|
|
59
|
+
throw new Error("File unavailable.");
|
|
60
|
+
contents = await vault.read(file);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
signal?.throwIfAborted();
|
|
64
|
+
if (!sourcePath)
|
|
65
|
+
throw error;
|
|
66
|
+
issues.push({
|
|
67
|
+
kind: "missing-include",
|
|
68
|
+
message: `Unable to read ledger include: ${referencedPath}`,
|
|
69
|
+
sourcePath,
|
|
70
|
+
referencedPath,
|
|
71
|
+
});
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
signal?.throwIfAborted();
|
|
75
|
+
ledgerFiles.push({ path, contents });
|
|
76
|
+
const { directives } = parseDocument(contents, {
|
|
77
|
+
strict: false,
|
|
78
|
+
filename: path,
|
|
79
|
+
bookingMethod: "DEFAULT",
|
|
80
|
+
attributes: { filename: path },
|
|
81
|
+
});
|
|
82
|
+
for (const directive of directives) {
|
|
83
|
+
if (directive instanceof DocumentDirective) {
|
|
84
|
+
const reference = directive.path;
|
|
85
|
+
const target = resolveVaultPath(path, reference);
|
|
86
|
+
const visiblePath = target && !target.split("/").some((part) => part.startsWith("."));
|
|
87
|
+
const file = visiblePath && vault.getFileByPath(target);
|
|
88
|
+
if (!target || !file) {
|
|
89
|
+
issues.push({
|
|
90
|
+
kind: visiblePath ? "missing-resource" : "outside-root",
|
|
91
|
+
message: `Unable to load ledger document: ${reference}`,
|
|
92
|
+
sourcePath: path,
|
|
93
|
+
referencedPath: reference,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
resources.set(target, {
|
|
98
|
+
path: target,
|
|
99
|
+
mimeType: documentTypes[file.extension.toLowerCase()] ?? "application/octet-stream",
|
|
100
|
+
size: file.stat.size,
|
|
101
|
+
updatedAt: new Date(file.stat.mtime).toISOString(),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (!(directive instanceof IncludeDirective))
|
|
107
|
+
continue;
|
|
108
|
+
const reference = directive.value;
|
|
109
|
+
const target = resolveVaultPath(path, reference);
|
|
110
|
+
if (!target || !ledgerExtension.test(target) || /[*?[\]]/.test(reference)) {
|
|
111
|
+
issues.push({
|
|
112
|
+
kind: target ? "invalid-reference" : "outside-root",
|
|
113
|
+
message: `Unable to resolve ledger include: ${reference}`,
|
|
114
|
+
sourcePath: path,
|
|
115
|
+
referencedPath: reference,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
else if (!visited.has(target)) {
|
|
119
|
+
visited.add(target);
|
|
120
|
+
pending.push({ path: target, sourcePath: path, referencedPath: reference });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
project,
|
|
126
|
+
ledgerFiles,
|
|
127
|
+
revisions: ledgerFiles.map(({ path, contents }) => ({
|
|
128
|
+
path,
|
|
129
|
+
revision: contents,
|
|
130
|
+
updatedAt,
|
|
131
|
+
})),
|
|
132
|
+
resources: [...resources.values()],
|
|
133
|
+
issues,
|
|
134
|
+
envKeys: [],
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
async listProjects(signal) {
|
|
139
|
+
signal?.throwIfAborted();
|
|
140
|
+
return [project];
|
|
141
|
+
},
|
|
142
|
+
loadProject,
|
|
143
|
+
async resourceUrl(id, path) {
|
|
144
|
+
const snapshot = await loadProject(id);
|
|
145
|
+
const resource = snapshot.resources.find((item) => item.path === path);
|
|
146
|
+
const file = resource && vault.getFileByPath(resource.path);
|
|
147
|
+
if (!resource || !file)
|
|
148
|
+
throw new Error("The ledger document is unavailable.");
|
|
149
|
+
const mimeType = resource.mimeType;
|
|
150
|
+
const bytes = await vault.readBinary(file);
|
|
151
|
+
const url = URL.createObjectURL(new Blob([bytes], { type: mimeType }));
|
|
152
|
+
const text = mimeType.startsWith("text/") ||
|
|
153
|
+
mimeType === "application/json" ||
|
|
154
|
+
mimeType === "application/yaml"
|
|
155
|
+
? new TextDecoder().decode(bytes)
|
|
156
|
+
: undefined;
|
|
157
|
+
return {
|
|
158
|
+
url,
|
|
159
|
+
mimeType,
|
|
160
|
+
text,
|
|
161
|
+
revoke: () => URL.revokeObjectURL(url),
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
async saveFile(id, path, contents, expectedRevision, signal) {
|
|
165
|
+
const snapshot = await loadProject(id, signal);
|
|
166
|
+
const file = snapshot.ledgerFiles.some((source) => source.path === path)
|
|
167
|
+
? vault.getFileByPath(path)
|
|
168
|
+
: null;
|
|
169
|
+
if (!file)
|
|
170
|
+
throw new Error("The ledger file is unavailable.");
|
|
171
|
+
await vault.process(file, (current) => {
|
|
172
|
+
signal?.throwIfAborted();
|
|
173
|
+
if (current !== expectedRevision) {
|
|
174
|
+
throw new Error("The ledger changed since you opened it. Your draft is still here.");
|
|
175
|
+
}
|
|
176
|
+
return contents;
|
|
177
|
+
});
|
|
178
|
+
return loadProject(id);
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { BeancountDataSource } from "@beancount-js/ui/controller";
|
|
2
|
+
import { createVaultLedgerSource } from "./vault-ledger";
|
|
3
|
+
type Vault = Parameters<typeof createVaultLedgerSource>[0];
|
|
4
|
+
type Project = Parameters<typeof createVaultLedgerSource>[1];
|
|
5
|
+
export declare function createVaultProjects(vault: Vault, stored: unknown, save: (data: {
|
|
6
|
+
version: 1;
|
|
7
|
+
projects: Project[];
|
|
8
|
+
}) => Promise<void>): BeancountDataSource;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createVaultLedgerSource } from "./vault-ledger";
|
|
2
|
+
function isProject(value) {
|
|
3
|
+
if (!value || typeof value !== "object")
|
|
4
|
+
return false;
|
|
5
|
+
const project = value;
|
|
6
|
+
return (project.version === 1 &&
|
|
7
|
+
[project.id, project.name, project.entryFile, project.createdAt, project.updatedAt]
|
|
8
|
+
.every((part) => typeof part === "string" && part.length > 0));
|
|
9
|
+
}
|
|
10
|
+
export function createVaultProjects(vault, stored, save) {
|
|
11
|
+
let projects = [];
|
|
12
|
+
if (stored &&
|
|
13
|
+
typeof stored === "object" &&
|
|
14
|
+
"version" in stored &&
|
|
15
|
+
stored.version === 1 &&
|
|
16
|
+
"projects" in stored &&
|
|
17
|
+
Array.isArray(stored.projects)) {
|
|
18
|
+
projects = stored.projects
|
|
19
|
+
.filter(isProject)
|
|
20
|
+
.map(({ id, name, entryFile, createdAt, updatedAt }) => ({
|
|
21
|
+
version: 1,
|
|
22
|
+
id,
|
|
23
|
+
name,
|
|
24
|
+
entryFile,
|
|
25
|
+
createdAt,
|
|
26
|
+
updatedAt,
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
let writeTail = Promise.resolve();
|
|
30
|
+
function source(id) {
|
|
31
|
+
const project = projects.find((entry) => entry.id === id);
|
|
32
|
+
if (!project)
|
|
33
|
+
throw new Error("The ledger project is unavailable.");
|
|
34
|
+
return createVaultLedgerSource(vault, project);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
async listProjects(signal) {
|
|
38
|
+
signal?.throwIfAborted();
|
|
39
|
+
return [...projects];
|
|
40
|
+
},
|
|
41
|
+
loadProject: async (id, signal) => source(id).loadProject(id, signal),
|
|
42
|
+
resourceUrl: (id, path) => source(id).resourceUrl(id, path),
|
|
43
|
+
saveFile: async (id, path, contents, revision, signal) => source(id).saveFile(id, path, contents, revision, signal),
|
|
44
|
+
addProject(path, signal) {
|
|
45
|
+
const select = writeTail.then(async () => {
|
|
46
|
+
signal?.throwIfAborted();
|
|
47
|
+
if (!path)
|
|
48
|
+
throw new Error("Enter a vault-relative Beancount ledger entry.");
|
|
49
|
+
const existing = projects.find(({ entryFile }) => entryFile === path);
|
|
50
|
+
if (existing)
|
|
51
|
+
return source(existing.id).loadProject(existing.id, signal);
|
|
52
|
+
const timestamp = new Date().toISOString();
|
|
53
|
+
const project = {
|
|
54
|
+
version: 1,
|
|
55
|
+
id: crypto.randomUUID(),
|
|
56
|
+
name: path.split("/").at(-1).replace(/\.[^.]+$/, ""),
|
|
57
|
+
entryFile: path,
|
|
58
|
+
createdAt: timestamp,
|
|
59
|
+
updatedAt: timestamp,
|
|
60
|
+
};
|
|
61
|
+
const ledgerSource = createVaultLedgerSource(vault, project);
|
|
62
|
+
const snapshot = await ledgerSource.loadProject(project.id, signal);
|
|
63
|
+
const updated = [...projects, project];
|
|
64
|
+
await save({ version: 1, projects: updated });
|
|
65
|
+
projects = updated;
|
|
66
|
+
return snapshot;
|
|
67
|
+
});
|
|
68
|
+
writeTail = select.then(() => undefined, () => undefined);
|
|
69
|
+
return select;
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Workspace view identifier for the Studio application. */
|
|
2
|
+
export declare const BEANCOUNT_STUDIO_VIEW_TYPE = "beancount-studio";
|
|
3
|
+
/** Sidebar view identifier for the Studio ledger controls. */
|
|
4
|
+
export declare const BEANCOUNT_SIDEBAR_VIEW_TYPE = "beancount-ledger";
|
|
5
|
+
/** View identifier for individual Beancount files. */
|
|
6
|
+
export declare const BEANCOUNT_SOURCE_VIEW_TYPE = "beancount-source";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Workspace view identifier for the Studio application. */
|
|
2
|
+
export const BEANCOUNT_STUDIO_VIEW_TYPE = "beancount-studio";
|
|
3
|
+
/** Sidebar view identifier for the Studio ledger controls. */
|
|
4
|
+
export const BEANCOUNT_SIDEBAR_VIEW_TYPE = "beancount-ledger";
|
|
5
|
+
/** View identifier for individual Beancount files. */
|
|
6
|
+
export const BEANCOUNT_SOURCE_VIEW_TYPE = "beancount-source";
|
package/manifest.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "beancount",
|
|
3
|
+
"name": "Beancount",
|
|
4
|
+
"version": "0.1.1",
|
|
5
|
+
"minAppVersion": "0.0.1",
|
|
6
|
+
"description": "Beancount Studio for Lapis Notes ledger workspaces",
|
|
7
|
+
"author": "Lapis Notes",
|
|
8
|
+
"requiredCapabilities": [
|
|
9
|
+
"plugin:secrets"
|
|
10
|
+
],
|
|
11
|
+
"distribution": "first-party-external",
|
|
12
|
+
"provenance": "official",
|
|
13
|
+
"enabledByDefault": false,
|
|
14
|
+
"lapis": {
|
|
15
|
+
"manifestVersion": 1,
|
|
16
|
+
"runtime": {
|
|
17
|
+
"entries": {
|
|
18
|
+
"workspace": {
|
|
19
|
+
"path": "main.mjs",
|
|
20
|
+
"format": "esm",
|
|
21
|
+
"sharedDependencies": [
|
|
22
|
+
"@codemirror/autocomplete",
|
|
23
|
+
"@codemirror/commands",
|
|
24
|
+
"@codemirror/language",
|
|
25
|
+
"@codemirror/lint",
|
|
26
|
+
"@codemirror/search",
|
|
27
|
+
"@codemirror/state",
|
|
28
|
+
"@codemirror/view",
|
|
29
|
+
"@lapis-notes/api",
|
|
30
|
+
"@lapis-notes/api/editor",
|
|
31
|
+
"@lapis-notes/api/workspace-host",
|
|
32
|
+
"@lapismd/design-core/shadcn/alert",
|
|
33
|
+
"@lapismd/design-core/shadcn/badge",
|
|
34
|
+
"@lapismd/design-core/shadcn/button",
|
|
35
|
+
"@lapismd/design-core/shadcn/button-group",
|
|
36
|
+
"@lapismd/design-core/shadcn/dialog",
|
|
37
|
+
"@lapismd/design-core/shadcn/dropdown-menu",
|
|
38
|
+
"@lapismd/design-core/shadcn/input",
|
|
39
|
+
"@lapismd/design-core/shadcn/input-group",
|
|
40
|
+
"@lapismd/design-core/shadcn/popover",
|
|
41
|
+
"@lapismd/design-core/shadcn/progress",
|
|
42
|
+
"@lapismd/design-core/shadcn/scroll-area",
|
|
43
|
+
"@lapismd/design-core/shadcn/select",
|
|
44
|
+
"@lapismd/design-core/shadcn/separator",
|
|
45
|
+
"@lapismd/design-core/shadcn/sheet",
|
|
46
|
+
"@lapismd/design-core/shadcn/switch",
|
|
47
|
+
"@lapismd/design-core/shadcn/toggle-group",
|
|
48
|
+
"@lapismd/design-core/shadcn/tooltip",
|
|
49
|
+
"@lezer/common",
|
|
50
|
+
"@lezer/highlight",
|
|
51
|
+
"@lezer/lr",
|
|
52
|
+
"bits-ui",
|
|
53
|
+
"clsx",
|
|
54
|
+
"luxon",
|
|
55
|
+
"svelte",
|
|
56
|
+
"svelte/renderer/5.56.10/internal/client",
|
|
57
|
+
"svelte/renderer/5.56.10/internal/disclose-version",
|
|
58
|
+
"svelte/renderer/5.56.10/internal/flags/legacy",
|
|
59
|
+
"svelte/renderer/5.56.10/reactivity",
|
|
60
|
+
"svelte/store",
|
|
61
|
+
"zod"
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|