@staix/vault-mem-ui 0.0.0-stage → 0.0.0

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/LICENSE +21 -0
  3. package/README.md +96 -2
  4. package/THIRD_PARTY_NOTICES.md +164 -0
  5. package/bin/vmem-ui.ts +26 -0
  6. package/dist/panel.css +666 -0
  7. package/dist/panel.js +355 -0
  8. package/package.json +53 -4
  9. package/src/client/App.tsx +167 -0
  10. package/src/client/a11y.ts +54 -0
  11. package/src/client/acon.ts +46 -0
  12. package/src/client/components.tsx +243 -0
  13. package/src/client/data.ts +66 -0
  14. package/src/client/fake.ts +48 -0
  15. package/src/client/graph-export.tsx +21 -0
  16. package/src/client/host.ts +80 -0
  17. package/src/client/markdown/parse.ts +620 -0
  18. package/src/client/markdown/render.tsx +270 -0
  19. package/src/client/panel.css +666 -0
  20. package/src/client/panel.tsx +66 -0
  21. package/src/client/prefs.ts +76 -0
  22. package/src/client/standalone.ts +44 -0
  23. package/src/client/store.ts +201 -0
  24. package/src/client/views/assertions.tsx +109 -0
  25. package/src/client/views/common.ts +15 -0
  26. package/src/client/views/history.tsx +205 -0
  27. package/src/client/views/lint.tsx +27 -0
  28. package/src/client/views/log.tsx +64 -0
  29. package/src/client/views/note.tsx +128 -0
  30. package/src/client/views/notes.tsx +34 -0
  31. package/src/client/views/overview.tsx +74 -0
  32. package/src/client/views/promote.tsx +112 -0
  33. package/src/client/views/proposals.tsx +55 -0
  34. package/src/client/views/settings.tsx +57 -0
  35. package/src/client/write.ts +144 -0
  36. package/src/graph/decisionChains.ts +122 -0
  37. package/src/graph/derive.ts +338 -0
  38. package/src/graph/export.ts +44 -0
  39. package/src/graph/insights.ts +254 -0
  40. package/src/graph/labels.ts +67 -0
  41. package/src/graph/layout/client.ts +139 -0
  42. package/src/graph/layout/engine.ts +107 -0
  43. package/src/graph/layout/fa2.d.ts +8 -0
  44. package/src/graph/layout/protocol.ts +70 -0
  45. package/src/graph/layout/worker.ts +132 -0
  46. package/src/graph/model.ts +230 -0
  47. package/src/graph/positions.ts +41 -0
  48. package/src/graph/search.ts +54 -0
  49. package/src/graph/style.ts +301 -0
  50. package/src/graph/target.ts +38 -0
  51. package/src/graph/types.ts +64 -0
  52. package/src/graph/view/GraphView.tsx +279 -0
  53. package/src/graph/view/SidePanels.tsx +141 -0
  54. package/src/graph/view/cache.ts +42 -0
  55. package/src/graph/view/canvas.ts +309 -0
  56. package/src/graph/view/graph-snapshot.ts +75 -0
  57. package/src/graph/view/lod.ts +30 -0
  58. package/src/graph/view/motion.ts +4 -0
  59. package/src/graph/view/state.ts +240 -0
  60. package/src/plugin/assets.ts +33 -0
  61. package/src/plugin/graph.ts +71 -0
  62. package/src/plugin/index.ts +23 -0
  63. package/src/server/cli.ts +99 -0
  64. package/src/server/host.ts +287 -0
@@ -0,0 +1,270 @@
1
+ /** The note body as React elements: one element per parsed node, so no string of markup is ever assembled. */
2
+
3
+ import { Fragment, useId, useMemo, type ReactNode } from "react";
4
+ import { list, rec, str } from "../data.ts";
5
+ import { parseMarkdown, safeHref, type Block, type Inline } from "./parse.ts";
6
+
7
+ type OpenNote = (path: string) => void;
8
+
9
+ /**
10
+ * What the provider answered for one written link: the note it resolved to, or why it could not and the note it
11
+ * suggests instead. `vmem_links` states both (`reason`, `suggested`), so the panel repeats them rather than writing a
12
+ * sentence of its own about a link it cannot see.
13
+ */
14
+ export type WikiLink =
15
+ | { kind: "resolved"; path: string }
16
+ | { kind: "dead"; reason: string; suggested: string };
17
+
18
+ export type WikiIndex = Map<string, WikiLink>;
19
+
20
+ /**
21
+ * The identity the provider resolves: the target and the anchor written with it. `[[Alpha]]` and `[[Alpha#gone]]` are
22
+ * two links to one note and vmem can resolve the first while it refuses the second, so the target alone is not the key.
23
+ * A target cannot contain a newline (vmem's own link scan excludes it), which makes it a safe separator. An empty
24
+ * target is its own identity, not a missing one: `[[#Heading]]` and `[[#^block]]` name the note the body belongs to,
25
+ * and vmem answers them with `target: ""`.
26
+ */
27
+ export function wikiKey(target: string, heading: string, block: string): string {
28
+ return `${linkText(target)}\n${linkText(heading)}\n${linkText(block)}`;
29
+ }
30
+
31
+ /** The provider's own bound on a link's text, `LINK_TEXT_BYTES` in vault-mem's index. */
32
+ const LINK_TEXT_BYTES = 1024;
33
+
34
+ /**
35
+ * The same cut `vmem_links` makes before it answers: a target, heading or block past 1024 UTF-8 bytes arrives shortened
36
+ * at a character boundary, so the key has to be shortened the same way or a long link never matches its own answer.
37
+ */
38
+ function linkText(value: string): string {
39
+ // A UTF-16 code unit never takes more than three UTF-8 bytes, so short enough in units is short enough in bytes.
40
+ if (value.length * 3 <= LINK_TEXT_BYTES) return value;
41
+ const bytes = new TextEncoder().encode(value);
42
+ if (bytes.length <= LINK_TEXT_BYTES) return value;
43
+ let end = LINK_TEXT_BYTES;
44
+ while (end > 0 && (bytes[end]! & 0xc0) === 0x80) end -= 1;
45
+ return new TextDecoder().decode(bytes.subarray(0, end));
46
+ }
47
+
48
+ /**
49
+ * Read the note panel's own `links` answer (`out` resolved, `dead` not) into a map keyed by link rather than by note,
50
+ * so each wiki-link in the body shows the resolution vmem computed for that link instead of a guess made here. A link
51
+ * this answer does not list is absent from the map, because `vmem_links` is capped (50 by default): absence means the
52
+ * answer never read that link, not that the provider could not resolve it.
53
+ */
54
+ export function wikiIndex(links: unknown): WikiIndex {
55
+ const box = rec(links);
56
+ const index: WikiIndex = new Map();
57
+ for (const row of [...list<unknown>(box.out), ...list<unknown>(box.dead)]) {
58
+ const link = rec(row);
59
+ // A row with no `target` at all is a same-note link (`target: ""`) as far as this map is concerned: vmem answers
60
+ // `[[#Heading]]` and `[[#^block]]` that way, and dropping them left both states rendering as unlisted.
61
+ const target = str(link.target);
62
+ const resolved = str(link.resolved);
63
+ index.set(wikiKey(target, str(link.heading), str(link.block)), resolved
64
+ ? { kind: "resolved", path: resolved }
65
+ : { kind: "dead", reason: str(link.reason), suggested: str(rec(link.suggested).path) });
66
+ }
67
+ return index;
68
+ }
69
+
70
+ export function Markdown(props: { text: string; links?: unknown; onOpenNote?: OpenNote }) {
71
+ const blocks = useMemo(() => parseMarkdown(props.text), [props.text]);
72
+ const wiki = useMemo(() => wikiIndex(props.links), [props.links]);
73
+ return (
74
+ <div className="vmem-md">
75
+ {blocks.map((block, at) => (
76
+ <BlockNode key={at} block={block} wiki={wiki} onOpenNote={props.onOpenNote} />
77
+ ))}
78
+ </div>
79
+ );
80
+ }
81
+
82
+ function BlockNode(props: { block: Block; wiki: WikiIndex; onOpenNote?: OpenNote }): ReactNode {
83
+ const { block, wiki, onOpenNote } = props;
84
+ switch (block.kind) {
85
+ case "heading": {
86
+ // The note's own title is the panel's h1, so a body heading starts at h2 and stops at h6.
87
+ const content = inline(block.children, wiki, onOpenNote);
88
+ const depth = Math.min(Math.max(block.depth, 1), 6);
89
+ if (depth === 1) return <h2 className="vmem-md-h">{content}</h2>;
90
+ if (depth === 2) return <h3 className="vmem-md-h">{content}</h3>;
91
+ if (depth === 3) return <h4 className="vmem-md-h">{content}</h4>;
92
+ if (depth === 4) return <h5 className="vmem-md-h">{content}</h5>;
93
+ return <h6 className="vmem-md-h">{content}</h6>;
94
+ }
95
+ case "paragraph":
96
+ return <p className="vmem-md-p">{inline(block.children, wiki, onOpenNote)}</p>;
97
+ case "code":
98
+ return (
99
+ <pre className="vmem-md-pre" data-lang={block.lang || undefined}>
100
+ <code>{block.text}</code>
101
+ </pre>
102
+ );
103
+ case "quote":
104
+ return (
105
+ <blockquote className="vmem-md-quote">
106
+ {block.children.map((child, at) => (
107
+ <BlockNode key={at} block={child} wiki={wiki} onOpenNote={onOpenNote} />
108
+ ))}
109
+ </blockquote>
110
+ );
111
+ case "list": {
112
+ const items = block.items.map((item, at) => (
113
+ <li key={at} className={item.checked === null ? undefined : "vmem-md-task"}>
114
+ {item.checked === null ? null : (
115
+ <input className="vmem-md-check" type="checkbox" checked={item.checked} disabled readOnly />
116
+ )}
117
+ {item.children.map((child, index) => (
118
+ <BlockNode key={index} block={child} wiki={wiki} onOpenNote={onOpenNote} />
119
+ ))}
120
+ </li>
121
+ ));
122
+ return block.ordered ? (
123
+ <ol className="vmem-md-list" start={block.start === 1 ? undefined : block.start}>
124
+ {items}
125
+ </ol>
126
+ ) : (
127
+ <ul className="vmem-md-list">{items}</ul>
128
+ );
129
+ }
130
+ case "table":
131
+ return (
132
+ <div className="vmem-md-scroll">
133
+ <table className="vmem-md-table">
134
+ <thead>
135
+ <tr>
136
+ {block.head.map((cell, at) => (
137
+ <th key={at} className="vmem-md-cell" scope="col" data-align={block.align[at] ?? undefined}>
138
+ {inline(cell, wiki, onOpenNote)}
139
+ </th>
140
+ ))}
141
+ </tr>
142
+ </thead>
143
+ <tbody>
144
+ {block.rows.map((row, at) => (
145
+ <tr key={at}>
146
+ {row.map((cell, index) => (
147
+ <td key={index} className="vmem-md-cell" data-align={block.align[index] ?? undefined}>
148
+ {inline(cell, wiki, onOpenNote)}
149
+ </td>
150
+ ))}
151
+ </tr>
152
+ ))}
153
+ </tbody>
154
+ </table>
155
+ </div>
156
+ );
157
+ case "rule":
158
+ return <hr className="vmem-md-rule" />;
159
+ }
160
+ }
161
+
162
+ /**
163
+ * `inAnchor` marks the label of a link: an anchor cannot hold an anchor, so inside a label an image's target and a
164
+ * wiki-link keep their text and lose the part that would be clickable, instead of nesting one anchor in another.
165
+ */
166
+ function inline(nodes: Inline[], wiki: WikiIndex, onOpenNote?: OpenNote, inAnchor = false): ReactNode {
167
+ return nodes.map((node, at) => <Fragment key={at}>{inlineNode(node, wiki, onOpenNote, inAnchor)}</Fragment>);
168
+ }
169
+
170
+ function inlineNode(node: Inline, wiki: WikiIndex, onOpenNote?: OpenNote, inAnchor = false): ReactNode {
171
+ switch (node.kind) {
172
+ case "text":
173
+ return node.text;
174
+ case "code":
175
+ return <code className="vmem-md-code">{node.text}</code>;
176
+ case "strong":
177
+ return <strong>{inline(node.children, wiki, onOpenNote, inAnchor)}</strong>;
178
+ case "em":
179
+ return <em>{inline(node.children, wiki, onOpenNote, inAnchor)}</em>;
180
+ case "link":
181
+ return (
182
+ <a className="vmem-md-link" href={node.href} target="_blank" rel="noopener noreferrer">
183
+ {inline(node.children, wiki, onOpenNote, true)}
184
+ </a>
185
+ );
186
+ case "image":
187
+ // The panel's CSP allows no other origin and this module fetches nothing, so an image is its alt text and a
188
+ // readable target rather than a broken frame.
189
+ return (
190
+ <span className="vmem-md-image">
191
+ {node.alt ? <span className="vmem-md-image-alt">{node.alt}</span> : null}
192
+ {imageTarget(node.src, inAnchor)}
193
+ </span>
194
+ );
195
+ case "wiki":
196
+ return <Wiki node={node} wiki={wiki} onOpenNote={inAnchor ? undefined : onOpenNote} />;
197
+ case "break":
198
+ return <br />;
199
+ }
200
+ }
201
+
202
+ function imageTarget(src: string, inAnchor: boolean): ReactNode {
203
+ const href = safeHref(src);
204
+ if (!href || inAnchor) return <span className="vmem-md-image-src">{src}</span>;
205
+ return (
206
+ <a className="vmem-md-link" href={href} target="_blank" rel="noopener noreferrer">
207
+ {src}
208
+ </a>
209
+ );
210
+ }
211
+
212
+ /** What vmem says it could not do, in the words the person reads beside the link. */
213
+ const DEAD_WHY: Record<string, string> = {
214
+ "missing-note": "no note has this name",
215
+ "ambiguous-note": "more than one note has this name",
216
+ "missing-heading": "the note it names has no such heading",
217
+ "missing-block": "the note it names has no such block",
218
+ };
219
+
220
+ /** A link that names no note at all points at the note the body belongs to, so it is called that rather than by "". */
221
+ function linkName(target: string): string {
222
+ return target || "this note";
223
+ }
224
+
225
+ function deadTitle(target: string, answer: { reason: string; suggested: string }): string {
226
+ const why = DEAD_WHY[answer.reason] ?? "vmem could not resolve this link";
227
+ return answer.suggested
228
+ ? `${linkName(target)}: ${why}; vmem suggests ${answer.suggested}`
229
+ : `${linkName(target)}: ${why}`;
230
+ }
231
+
232
+ function Wiki(props: { node: Extract<Inline, { kind: "wiki" }>; wiki: WikiIndex; onOpenNote?: OpenNote }) {
233
+ const node = props.node;
234
+ // A title is a mouse tooltip: the same words are rendered for a screen reader, tied to the span, because the span
235
+ // itself cannot take focus and a reason that only exists in a tooltip reaches nobody using the keyboard.
236
+ const reasonId = useId();
237
+ const answer = props.wiki.get(wikiKey(node.target, node.heading, node.block));
238
+ const kind = node.embed ? " vmem-md-wiki-embed" : "";
239
+ // Absent from the answer is its own state: the link list is capped, so the panel says it cannot place the link rather
240
+ // than calling a link broken that the provider never answered for.
241
+ if (!answer) {
242
+ const title = `${linkName(node.target)}: this note's link list does not include it, so the panel cannot place it`;
243
+ return (
244
+ <>
245
+ <span className={`vmem-md-wiki${kind} vmem-md-wiki-unknown`} title={title} aria-describedby={reasonId}>
246
+ {node.label}
247
+ </span>
248
+ <span className="vmem-md-note" id={reasonId}>{title}</span>
249
+ </>
250
+ );
251
+ }
252
+ if (answer.kind === "dead") {
253
+ const title = deadTitle(node.target, answer);
254
+ return (
255
+ <>
256
+ <span className={`vmem-md-wiki${kind} vmem-md-wiki-missing`} title={title} aria-describedby={reasonId}>
257
+ {node.label}
258
+ </span>
259
+ <span className="vmem-md-note" id={reasonId}>{title}</span>
260
+ </>
261
+ );
262
+ }
263
+ if (!props.onOpenNote) return <span className={`vmem-md-wiki${kind}`}>{node.label}</span>;
264
+ const open = props.onOpenNote;
265
+ return (
266
+ <button type="button" className={`vmem-md-wiki${kind}`} onClick={() => open(answer.path)}>
267
+ {node.label}
268
+ </button>
269
+ );
270
+ }