create-ai-memory 0.13.0 → 0.14.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.
@@ -0,0 +1,666 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>AI Memory</title>
7
+ <link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Ccircle cx='4' cy='5' r='2.4' fill='%232563eb'/%3E%3Ccircle cx='12' cy='4' r='1.8' fill='%230d9488'/%3E%3Ccircle cx='9' cy='12' r='2' fill='%23b45309'/%3E%3C/svg%3E">
8
+ <style>
9
+ /* One palette, named. Light is the default because the graph reads as ink on
10
+ paper; dark mirrors it rather than inverting the node colours, which would
11
+ make types unrecognisable between themes. */
12
+ :root {
13
+ --bg: #fbfaf8;
14
+ --surface: #ffffff;
15
+ --line: #e6e2db;
16
+ --line-strong: #d3cec5;
17
+ --ink: #1c1b19;
18
+ --ink-2: #55514b;
19
+ --ink-3: #8a857d;
20
+ --accent: #b45309;
21
+ --edge: #cfcac2;
22
+ --shadow: 0 1px 2px rgba(28,27,25,.05), 0 8px 24px rgba(28,27,25,.06);
23
+ --radius: 8px;
24
+ --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
25
+ --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
26
+ }
27
+ @media (prefers-color-scheme: dark) {
28
+ :root:not([data-theme="light"]) {
29
+ --bg: #16151a;
30
+ --surface: #1d1c22;
31
+ --line: #2c2a33;
32
+ --line-strong: #3a3743;
33
+ --ink: #ecebe8;
34
+ --ink-2: #a8a49d;
35
+ --ink-3: #736f68;
36
+ --accent: #f59e0b;
37
+ --edge: #3a3743;
38
+ --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
39
+ }
40
+ }
41
+ * { box-sizing: border-box; }
42
+ html, body { height: 100%; }
43
+ body {
44
+ margin: 0; background: var(--bg); color: var(--ink);
45
+ font: 14px/1.5 var(--sans);
46
+ -webkit-font-smoothing: antialiased;
47
+ }
48
+
49
+ /* ---- shell ---------------------------------------------------------- */
50
+ #app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
51
+
52
+ header {
53
+ display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
54
+ padding: 10px 16px; border-bottom: 1px solid var(--line);
55
+ background: var(--surface);
56
+ }
57
+ .brand { display: flex; align-items: baseline; gap: 8px; margin-right: auto; }
58
+ .brand h1 { font-size: 14px; font-weight: 620; margin: 0; letter-spacing: -.01em; }
59
+ .brand span { font-size: 11px; color: var(--ink-3); }
60
+
61
+ .control {
62
+ font: inherit; font-size: 12px; color: var(--ink);
63
+ background: var(--bg); border: 1px solid var(--line-strong);
64
+ border-radius: 6px; padding: 5px 8px; min-width: 0;
65
+ }
66
+ .control:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
67
+ #q { width: 200px; }
68
+ button.control { cursor: pointer; }
69
+ button.control:hover { background: var(--surface); border-color: var(--ink-3); }
70
+
71
+ /* ---- panes ---------------------------------------------------------- */
72
+ main { display: flex; flex: 1; min-height: 0; }
73
+ #graphwrap { position: relative; flex: 1; min-width: 0; }
74
+ canvas { display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; }
75
+ canvas.dragging { cursor: grabbing; }
76
+
77
+ #legend {
78
+ position: absolute; left: 12px; bottom: 12px;
79
+ display: flex; flex-wrap: wrap; gap: 4px 12px; max-width: calc(100% - 24px);
80
+ padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--radius);
81
+ background: color-mix(in srgb, var(--surface) 88%, transparent);
82
+ backdrop-filter: blur(6px); font-size: 11px; color: var(--ink-2);
83
+ }
84
+ #legend b { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: baseline; }
85
+ #legend i { font-style: normal; cursor: pointer; white-space: nowrap; }
86
+ #legend i[aria-pressed="false"] { opacity: .35; text-decoration: line-through; }
87
+
88
+ #hint {
89
+ position: absolute; inset: 0; display: grid; place-content: center;
90
+ color: var(--ink-3); font-size: 13px; pointer-events: none; text-align: center; padding: 24px;
91
+ }
92
+
93
+ /* ---- detail --------------------------------------------------------- */
94
+ #panel {
95
+ width: 40%; max-width: 560px; min-width: 320px;
96
+ border-left: 1px solid var(--line); background: var(--surface);
97
+ overflow-y: auto; overscroll-behavior: contain;
98
+ }
99
+ #panel[hidden] { display: none; }
100
+ .pad { padding: 18px 20px; }
101
+
102
+ .phead { display: flex; gap: 12px; align-items: flex-start; }
103
+ .badge {
104
+ flex: none; font-size: 10px; letter-spacing: .06em; text-transform: uppercase;
105
+ padding: 3px 7px; border-radius: 4px; color: var(--surface); font-weight: 600;
106
+ }
107
+ .ptitle { font-size: 17px; font-weight: 620; line-height: 1.3; margin: 0; letter-spacing: -.01em; flex: 1; }
108
+ .ppath { font: 11px/1.5 var(--mono); color: var(--ink-3); word-break: break-all; margin-top: 6px; }
109
+ #close {
110
+ flex: none; border: 0; background: none; color: var(--ink-3); cursor: pointer;
111
+ font-size: 18px; line-height: 1; padding: 2px 4px; border-radius: 4px;
112
+ }
113
+ #close:hover { color: var(--ink); background: var(--bg); }
114
+
115
+ .fields { margin: 14px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 12px; }
116
+ .fields dt { color: var(--ink-3); }
117
+ .fields dd { margin: 0; color: var(--ink-2); }
118
+ .tag {
119
+ display: inline-block; font-size: 11px; padding: 2px 7px; margin: 0 4px 4px 0;
120
+ border: 1px solid var(--line-strong); border-radius: 999px; color: var(--ink-2);
121
+ }
122
+
123
+ hr.rule { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
124
+
125
+ /* Rendered note body. Sized down slightly from the chrome so a long note
126
+ stays readable in a narrow column without the panel feeling like an app. */
127
+ .body { font-size: 13.5px; line-height: 1.62; color: var(--ink-2); }
128
+ .body h1, .body h2, .body h3 { color: var(--ink); line-height: 1.3; margin: 20px 0 8px; letter-spacing: -.01em; }
129
+ .body h1 { font-size: 17px; } .body h2 { font-size: 15px; } .body h3 { font-size: 13.5px; }
130
+ .body h1:first-child, .body h2:first-child { margin-top: 0; }
131
+ .body p, .body ul, .body ol { margin: 0 0 10px; }
132
+ .body ul, .body ol { padding-left: 20px; }
133
+ .body li { margin-bottom: 3px; }
134
+ .body code { font: 12px/1.5 var(--mono); background: var(--bg); border: 1px solid var(--line); padding: 1px 4px; border-radius: 4px; }
135
+ .body pre { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 10px 12px; overflow-x: auto; }
136
+ .body pre code { background: none; border: 0; padding: 0; }
137
+ .body blockquote { margin: 0 0 10px; padding-left: 12px; border-left: 2px solid var(--line-strong); color: var(--ink-3); }
138
+ .body a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
139
+ .body strong { color: var(--ink); font-weight: 620; }
140
+ .body hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
141
+ .tablewrap { overflow-x: auto; margin-bottom: 10px; }
142
+ .body table { border-collapse: collapse; font-size: 12.5px; }
143
+ .body th, .body td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
144
+ .body th { background: var(--bg); color: var(--ink); font-weight: 600; }
145
+ .wikilink { color: var(--accent); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
146
+ .wikilink.dead { color: var(--ink-3); cursor: default; text-decoration-style: dotted; }
147
+
148
+ .links { margin-top: 4px; }
149
+ .links h4 { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); margin: 0 0 8px; font-weight: 600; }
150
+ .links button {
151
+ display: block; width: 100%; text-align: left; font: inherit; font-size: 12.5px;
152
+ background: none; border: 0; border-radius: 5px; padding: 5px 7px; margin-left: -7px;
153
+ color: var(--ink-2); cursor: pointer;
154
+ }
155
+ .links button:hover { background: var(--bg); color: var(--ink); }
156
+ .links button b { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 7px; }
157
+
158
+ /* Below the split point the graph and the note stop competing for width:
159
+ the note becomes a sheet over the graph rather than a squeezed column. */
160
+ @media (max-width: 768px) {
161
+ main { position: relative; }
162
+ #panel {
163
+ position: absolute; inset: auto 0 0 0; width: 100%; max-width: none; min-width: 0;
164
+ height: 62%; border-left: 0; border-top: 1px solid var(--line-strong);
165
+ border-radius: 12px 12px 0 0; box-shadow: var(--shadow);
166
+ }
167
+ #q { width: 100%; order: 3; }
168
+ .brand { width: 100%; }
169
+ #legend { display: none; }
170
+ }
171
+ </style>
172
+ </head>
173
+ <body>
174
+ <div id="app">
175
+ <header>
176
+ <div class="brand"><h1>AI Memory</h1><span id="sub">loading…</span></div>
177
+ <input id="q" class="control" type="search" placeholder="Search title, path or tag" autocomplete="off">
178
+ <select id="scope" class="control" title="Which notes to draw">
179
+ <option value="durable">Durable knowledge</option>
180
+ <option value="all">Everything</option>
181
+ </select>
182
+ <button id="reset" class="control" type="button">Reset view</button>
183
+ </header>
184
+
185
+ <main>
186
+ <div id="graphwrap">
187
+ <canvas id="c"></canvas>
188
+ <div id="hint">Loading the vault…</div>
189
+ <div id="legend"></div>
190
+ </div>
191
+ <aside id="panel" hidden><div class="pad" id="panelbody"></div></aside>
192
+ </main>
193
+ </div>
194
+
195
+ <script>
196
+ 'use strict';
197
+
198
+ /* Colour by `type`, the one field OKF requires, so the graph is coloured by
199
+ the vault's own vocabulary rather than by anything invented here. Types the
200
+ palette does not name fall through to a neutral -- a vault with its own
201
+ types still renders, just without bespoke colours. */
202
+ const TYPE_COLOR = {
203
+ 'ai-lesson': '#0d9488',
204
+ 'ai-project-context': '#2563eb',
205
+ 'ai-session-log': '#8b8681',
206
+ 'ai-global-profile': '#b45309',
207
+ 'ai-standards': '#b45309',
208
+ 'proposal': '#7c3aed',
209
+ 'untyped': '#a8a29a',
210
+ };
211
+ const colorFor = (t) => TYPE_COLOR[t] || '#6b7280';
212
+
213
+ /* Session logs outnumber everything else roughly three to one and all point at
214
+ their project note, so drawing every one yields a hairball with the durable
215
+ knowledge buried inside it. "Durable" is the default view and the dropdown
216
+ says plainly what is being left out.
217
+ Judged by LOCATION, not by the `type` field. Session logs predate that
218
+ field, so a vault that has not been backfilled has hundreds of logs typed
219
+ `untyped` -- filtering on the type alone silently drew all of them. Where a
220
+ note lives is the fact that is always true. */
221
+ const isTransient = (n) => n.id.startsWith('_session_logs/') || n.type === 'ai-session-log';
222
+
223
+ const $ = (s) => document.querySelector(s);
224
+ const canvas = $('#c');
225
+ const ctx = canvas.getContext('2d');
226
+
227
+ let RAW = { nodes: [], edges: [] };
228
+ let nodes = [], edges = [], byId = new Map();
229
+ let view = { x: 0, y: 0, k: 1 };
230
+ let selected = null, hovered = null;
231
+ let alpha = 1, running = false;
232
+
233
+ /* ---- data ------------------------------------------------------------ */
234
+ async function load() {
235
+ const g = await (await fetch('/api/graph')).json();
236
+ RAW = g;
237
+ $('#sub').textContent = g.nodes.length + ' notes · ' + g.edges.length + ' links';
238
+ document.title = 'AI Memory · ' + g.nodes.length + ' notes';
239
+ build();
240
+ }
241
+
242
+ function build() {
243
+ const scope = $('#scope').value;
244
+ const hidden = new Set([...document.querySelectorAll('#legend i[aria-pressed="false"]')].map((e) => e.dataset.type));
245
+ const keep = RAW.nodes.filter((n) => {
246
+ if (hidden.has(n.type)) return false;
247
+ if (scope === 'durable' && isTransient(n)) return false;
248
+ return true;
249
+ });
250
+ const ids = new Set(keep.map((n) => n.id));
251
+
252
+ byId = new Map();
253
+ nodes = keep.map((n) => {
254
+ const prev = byId.get(n.id);
255
+ const o = Object.assign({}, n, {
256
+ x: prev ? prev.x : (Math.random() - 0.5) * 600,
257
+ y: prev ? prev.y : (Math.random() - 0.5) * 600,
258
+ vx: 0, vy: 0, deg: 0,
259
+ });
260
+ byId.set(n.id, o);
261
+ return o;
262
+ });
263
+ edges = RAW.edges.filter((e) => ids.has(e.source) && ids.has(e.target));
264
+ for (const e of edges) {
265
+ byId.get(e.source).deg++;
266
+ byId.get(e.target).deg++;
267
+ }
268
+ // Radius by connectedness: a hub should look like one. sqrt keeps a note
269
+ // with 147 links from swallowing the canvas.
270
+ for (const n of nodes) n.r = 5 + Math.sqrt(n.deg) * 2.6;
271
+
272
+ drawLegend();
273
+ $('#hint').textContent = nodes.length ? '' : 'Nothing to draw with these filters.';
274
+ alpha = 1;
275
+ fitPending = true;
276
+ if (!running) { running = true; requestAnimationFrame(tick); }
277
+ }
278
+
279
+ /* The simulation has no idea how big the screen is: left alone it settles to
280
+ whatever size the forces imply, which on a real vault was roughly 3600px
281
+ across inside an 850px canvas -- most of the graph off-screen, and the tool
282
+ looking broken on first load. Frame it once, when it stops moving. */
283
+ let fitPending = false;
284
+ function fitToView() {
285
+ if (!nodes.length) return;
286
+ const w = canvas.clientWidth, h = canvas.clientHeight;
287
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
288
+ for (const n of nodes) {
289
+ x0 = Math.min(x0, n.x - n.r); y0 = Math.min(y0, n.y - n.r);
290
+ x1 = Math.max(x1, n.x + n.r); y1 = Math.max(y1, n.y + n.r);
291
+ }
292
+ const pad = 60;
293
+ view.k = Math.min(4, Math.max(0.12, Math.min((w - pad) / (x1 - x0 || 1), (h - pad) / (y1 - y0 || 1))));
294
+ view.x = -((x0 + x1) / 2) * view.k;
295
+ view.y = -((y0 + y1) / 2) * view.k;
296
+ }
297
+
298
+ function drawLegend() {
299
+ const counts = {};
300
+ for (const n of RAW.nodes) counts[n.type] = (counts[n.type] || 0) + 1;
301
+ const prev = new Map([...document.querySelectorAll('#legend i')].map((e) => [e.dataset.type, e.getAttribute('aria-pressed')]));
302
+ $('#legend').innerHTML = Object.entries(counts)
303
+ .sort((a, b) => b[1] - a[1]).slice(0, 8)
304
+ .map(([t, c]) => '<i data-type="' + t + '" role="button" tabindex="0" aria-pressed="' + (prev.get(t) || 'true') +
305
+ '"><b style="background:' + colorFor(t) + '"></b>' + t + ' ' + c + '</i>').join('');
306
+ }
307
+
308
+ /* ---- force layout ----------------------------------------------------
309
+ Hand-rolled rather than pulled from a CDN: the package ships no
310
+ dependencies, and a local tool that breaks without a network is worse than
311
+ one with a simpler layout. Repulsion is naive O(n^2), which is fine at this
312
+ size, and the simulation cools to a stop instead of running forever -- a
313
+ settled graph you can read beats one that never stops moving. */
314
+ function tick() {
315
+ if (alpha <= 0.004 && fitPending) { fitPending = false; fitToView(); }
316
+ if (alpha > 0.004) {
317
+ alpha *= 0.985;
318
+ const n = nodes.length;
319
+ for (let i = 0; i < n; i++) {
320
+ const a = nodes[i];
321
+ for (let j = i + 1; j < n; j++) {
322
+ const b = nodes[j];
323
+ let dx = b.x - a.x, dy = b.y - a.y;
324
+ let d2 = dx * dx + dy * dy;
325
+ if (d2 < 0.01) { dx = Math.random() - 0.5; dy = Math.random() - 0.5; d2 = 0.01; }
326
+ if (d2 > 90000) continue; // ignore distant pairs
327
+ const f = 900 / d2;
328
+ const d = Math.sqrt(d2);
329
+ const fx = (dx / d) * f, fy = (dy / d) * f;
330
+ a.vx -= fx; a.vy -= fy; b.vx += fx; b.vy += fy;
331
+ }
332
+ a.vx -= a.x * 0.0016; // gentle pull to centre
333
+ a.vy -= a.y * 0.0016;
334
+ }
335
+ for (const e of edges) {
336
+ const a = byId.get(e.source), b = byId.get(e.target);
337
+ const dx = b.x - a.x, dy = b.y - a.y;
338
+ const d = Math.hypot(dx, dy) || 1;
339
+ const f = (d - (60 + a.r + b.r)) * 0.012;
340
+ const fx = (dx / d) * f, fy = (dy / d) * f;
341
+ a.vx += fx; a.vy += fy; b.vx -= fx; b.vy -= fy;
342
+ }
343
+ for (const p of nodes) {
344
+ if (p === dragNode) continue;
345
+ p.x += (p.vx *= 0.82) * alpha * 2.2;
346
+ p.y += (p.vy *= 0.82) * alpha * 2.2;
347
+ }
348
+ }
349
+ draw();
350
+ requestAnimationFrame(tick);
351
+ }
352
+
353
+ /* ---- rendering -------------------------------------------------------- */
354
+ function resize() {
355
+ const r = canvas.getBoundingClientRect();
356
+ const dpr = Math.min(window.devicePixelRatio || 1, 2);
357
+ canvas.width = r.width * dpr;
358
+ canvas.height = r.height * dpr;
359
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
360
+ }
361
+ new ResizeObserver(resize).observe(canvas);
362
+
363
+ const css = (v) => getComputedStyle(document.documentElement).getPropertyValue(v).trim();
364
+ const q = () => $('#q').value.trim().toLowerCase();
365
+
366
+ function matches(n) {
367
+ const s = q();
368
+ if (!s) return true;
369
+ return (n.title + ' ' + n.id + ' ' + n.tags.join(' ')).toLowerCase().includes(s);
370
+ }
371
+
372
+ function draw() {
373
+ const w = canvas.clientWidth, h = canvas.clientHeight;
374
+ ctx.clearRect(0, 0, w, h);
375
+ ctx.save();
376
+ ctx.translate(w / 2 + view.x, h / 2 + view.y);
377
+ ctx.scale(view.k, view.k);
378
+
379
+ const searching = q().length > 0;
380
+ const neighbours = new Set();
381
+ if (selected) {
382
+ neighbours.add(selected.id);
383
+ for (const e of edges) {
384
+ if (e.source === selected.id) neighbours.add(e.target);
385
+ if (e.target === selected.id) neighbours.add(e.source);
386
+ }
387
+ }
388
+
389
+ ctx.lineWidth = 1 / view.k;
390
+ for (const e of edges) {
391
+ const a = byId.get(e.source), b = byId.get(e.target);
392
+ const lit = selected && (e.source === selected.id || e.target === selected.id);
393
+ ctx.strokeStyle = lit ? css('--accent') : css('--edge');
394
+ ctx.globalAlpha = lit ? 0.85 : (selected ? 0.2 : 0.55);
395
+ ctx.beginPath();
396
+ ctx.moveTo(a.x, a.y);
397
+ ctx.lineTo(b.x, b.y);
398
+ ctx.stroke();
399
+ }
400
+ ctx.globalAlpha = 1;
401
+
402
+ const labelAll = view.k > 1.15;
403
+ for (const n of nodes) {
404
+ const hit = matches(n);
405
+ const dim = (searching && !hit) || (selected && !neighbours.has(n.id));
406
+ ctx.globalAlpha = dim ? 0.16 : 1;
407
+
408
+ ctx.beginPath();
409
+ ctx.arc(n.x, n.y, n.r, 0, Math.PI * 2);
410
+ ctx.fillStyle = colorFor(n.type);
411
+ ctx.fill();
412
+
413
+ if (n === selected || n === hovered) {
414
+ ctx.strokeStyle = css('--accent');
415
+ ctx.lineWidth = 2.5 / view.k;
416
+ ctx.stroke();
417
+ }
418
+
419
+ // Label only what the reader can actually take in: hubs always, the rest
420
+ // once zoomed, plus anything the search or selection has singled out.
421
+ if (!dim && (labelAll || n.deg >= 6 || n === selected || n === hovered || (searching && hit))) {
422
+ ctx.globalAlpha = dim ? 0.16 : 0.95;
423
+ ctx.font = (11 / view.k) + 'px ' + css('--sans');
424
+ ctx.textAlign = 'center';
425
+ const t = n.title.length > 34 ? n.title.slice(0, 33) + '…' : n.title;
426
+ // Stroke the background colour behind the glyphs first. A hub sits under
427
+ // its own spokes, and without this its name is unreadable at exactly the
428
+ // moment it matters most.
429
+ ctx.lineWidth = 3 / view.k;
430
+ ctx.strokeStyle = css('--bg');
431
+ ctx.lineJoin = 'round';
432
+ ctx.strokeText(t, n.x, n.y + n.r + 12 / view.k);
433
+ ctx.fillStyle = css('--ink-2');
434
+ ctx.fillText(t, n.x, n.y + n.r + 12 / view.k);
435
+ }
436
+ }
437
+ ctx.globalAlpha = 1;
438
+ ctx.restore();
439
+ }
440
+
441
+ /* ---- interaction ------------------------------------------------------ */
442
+ let dragNode = null, panning = false, last = null, moved = false;
443
+
444
+ const toWorld = (ev) => {
445
+ const r = canvas.getBoundingClientRect();
446
+ return {
447
+ x: (ev.clientX - r.left - r.width / 2 - view.x) / view.k,
448
+ y: (ev.clientY - r.top - r.height / 2 - view.y) / view.k,
449
+ };
450
+ };
451
+ const nodeAt = (p) => {
452
+ for (let i = nodes.length - 1; i >= 0; i--) {
453
+ const n = nodes[i];
454
+ if (Math.hypot(n.x - p.x, n.y - p.y) <= n.r + 4) return n;
455
+ }
456
+ return null;
457
+ };
458
+
459
+ canvas.addEventListener('pointerdown', (ev) => {
460
+ canvas.setPointerCapture(ev.pointerId);
461
+ const p = toWorld(ev);
462
+ moved = false;
463
+ dragNode = nodeAt(p);
464
+ if (dragNode) { alpha = Math.max(alpha, 0.35); }
465
+ else { panning = true; canvas.classList.add('dragging'); }
466
+ last = { x: ev.clientX, y: ev.clientY };
467
+ });
468
+ canvas.addEventListener('pointermove', (ev) => {
469
+ const p = toWorld(ev);
470
+ if (dragNode) { dragNode.x = p.x; dragNode.y = p.y; moved = true; return; }
471
+ if (panning) {
472
+ view.x += ev.clientX - last.x;
473
+ view.y += ev.clientY - last.y;
474
+ last = { x: ev.clientX, y: ev.clientY };
475
+ moved = true;
476
+ return;
477
+ }
478
+ const h = nodeAt(p);
479
+ if (h !== hovered) { hovered = h; canvas.style.cursor = h ? 'pointer' : 'grab'; }
480
+ });
481
+ canvas.addEventListener('pointerup', (ev) => {
482
+ // A click is a click only if it did not turn into a drag.
483
+ if (!moved) {
484
+ const n = nodeAt(toWorld(ev));
485
+ if (n) open(n); else closePanel();
486
+ }
487
+ dragNode = null; panning = false; canvas.classList.remove('dragging');
488
+ });
489
+ canvas.addEventListener('wheel', (ev) => {
490
+ ev.preventDefault();
491
+ const r = canvas.getBoundingClientRect();
492
+ const mx = ev.clientX - r.left - r.width / 2, my = ev.clientY - r.top - r.height / 2;
493
+ const k = Math.min(4, Math.max(0.2, view.k * Math.exp(-ev.deltaY * 0.0015)));
494
+ // Zoom toward the cursor, so the thing under it stays under it.
495
+ view.x = mx - ((mx - view.x) / view.k) * k;
496
+ view.y = my - ((my - view.y) / view.k) * k;
497
+ view.k = k;
498
+ }, { passive: false });
499
+
500
+ $('#q').addEventListener('input', () => { $('#hint').textContent = ''; });
501
+ $('#scope').addEventListener('change', build);
502
+ $('#legend').addEventListener('click', (ev) => {
503
+ const i = ev.target.closest('i');
504
+ if (!i) return;
505
+ i.setAttribute('aria-pressed', i.getAttribute('aria-pressed') === 'false' ? 'true' : 'false');
506
+ build();
507
+ });
508
+ $('#reset').addEventListener('click', () => {
509
+ $('#q').value = '';
510
+ closePanel();
511
+ fitToView();
512
+ });
513
+ document.addEventListener('keydown', (ev) => {
514
+ if (ev.key === 'Escape') closePanel();
515
+ if (ev.key === '/' && document.activeElement !== $('#q')) { ev.preventDefault(); $('#q').focus(); }
516
+ });
517
+
518
+ /* ---- detail panel ----------------------------------------------------- */
519
+ function closePanel() { selected = null; $('#panel').hidden = true; }
520
+
521
+ /* Pan a selected node back into the visible part of the canvas. Opening the
522
+ panel covers up to 40% of the width, so a node that was comfortably framed
523
+ can end up underneath it -- or, as here, clipped by the bottom edge. */
524
+ function bringIntoView(n) {
525
+ const w = canvas.clientWidth, h = canvas.clientHeight, m = 90;
526
+ const sx = w / 2 + view.x + n.x * view.k;
527
+ const sy = h / 2 + view.y + n.y * view.k;
528
+ if (sx < m) view.x += m - sx;
529
+ if (sx > w - m) view.x -= sx - (w - m);
530
+ if (sy < m) view.y += m - sy;
531
+ if (sy > h - m) view.y -= sy - (h - m);
532
+ }
533
+
534
+ async function open(n) {
535
+ selected = n;
536
+ const panel = $('#panel');
537
+ panel.hidden = false;
538
+ panel.scrollTop = 0;
539
+ const head = '<div class="phead"><span class="badge" style="background:' + colorFor(n.type) + '">' +
540
+ esc(n.type) + '</span><h2 class="ptitle">' + esc(n.title) + '</h2>' +
541
+ '<button id="close" type="button" aria-label="Close">&times;</button></div>' +
542
+ '<div class="ppath">' + esc(n.id) + '</div>';
543
+ $('#panelbody').innerHTML = head + '<p class="body">Loading…</p>';
544
+ $('#close').onclick = closePanel;
545
+ bringIntoView(n);
546
+
547
+ let text = '';
548
+ try {
549
+ const r = await fetch('/api/note?path=' + encodeURIComponent(n.id));
550
+ text = r.ok ? (await r.json()).text : 'Could not read this note.';
551
+ } catch { text = 'Could not read this note.'; }
552
+ if (selected !== n) return; // a faster click won
553
+
554
+ const linked = [];
555
+ for (const e of edges) {
556
+ if (e.source === n.id) linked.push(byId.get(e.target));
557
+ else if (e.target === n.id) linked.push(byId.get(e.source));
558
+ }
559
+
560
+ let html = '';
561
+ const fields = [];
562
+ if (n.description) fields.push(['Description', esc(n.description)]);
563
+ if (n.resource) fields.push(['Resource', '<a href="' + esc(n.resource) + '" target="_blank" rel="noopener">' + esc(n.resource) + '</a>']);
564
+ if (n.tags.length) fields.push(['Tags', n.tags.map((t) => '<span class="tag">' + esc(t) + '</span>').join('')]);
565
+ if (fields.length) {
566
+ html += '<dl class="fields">' + fields.map(([k, v]) => '<dt>' + k + '</dt><dd>' + v + '</dd>').join('') + '</dl>';
567
+ }
568
+ // The title was taken from the note's first H1, so rendering that H1 again
569
+ // right underneath it just repeats the heading.
570
+ let body = stripFrontmatter(text).replace(/^\s*#\s+(.+)\n/, (m, h) => (h.trim() === n.title ? '' : m));
571
+ html += '<hr class="rule"><div class="body">' + md(body) + '</div>';
572
+ if (linked.length) {
573
+ html += '<hr class="rule"><div class="links"><h4>' + linked.length + ' linked note' + (linked.length > 1 ? 's' : '') + '</h4>' +
574
+ linked.map((l) => '<button type="button" data-id="' + esc(l.id) + '"><b style="background:' + colorFor(l.type) + '"></b>' + esc(l.title) + '</button>').join('') +
575
+ '</div>';
576
+ }
577
+ // Replace the whole panel rather than appending: appending left the
578
+ // "Loading…" placeholder sitting above the note it was standing in for.
579
+ $('#panelbody').innerHTML = head + html;
580
+ $('#close').onclick = closePanel;
581
+ }
582
+
583
+ // One delegated listener, bound once. Binding inside open() added another on
584
+ // every click, so the tenth note opened fired ten handlers.
585
+ $('#panelbody').addEventListener('click', (ev) => {
586
+ const b = ev.target.closest('[data-id]');
587
+ if (b && byId.has(b.dataset.id)) return open(byId.get(b.dataset.id));
588
+ const wl = ev.target.closest('.wikilink:not(.dead)');
589
+ if (wl) {
590
+ const hit = nodes.find((x) => x.id.endsWith('/' + wl.dataset.link + '.md') || x.id === wl.dataset.link + '.md');
591
+ if (hit) open(hit);
592
+ }
593
+ });
594
+
595
+ const stripFrontmatter = (t) => (t.startsWith('---') && t.indexOf('\n---', 3) > -1 ? t.slice(t.indexOf('\n---', 3) + 4) : t);
596
+ const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
597
+
598
+ /* A small Markdown renderer. Everything is escaped before any markup is
599
+ added, so note content can never inject HTML into this page. Covers what
600
+ the vault actually contains -- headings, lists, tables, fences, wikilinks --
601
+ rather than the whole spec. */
602
+ function md(src) {
603
+ const fences = [];
604
+ src = src.replace(/```[\s\S]*?```/g, (m) => {
605
+ fences.push('<pre><code>' + esc(m.replace(/^```[^\n]*\n?/, '').replace(/```$/, '')) + '</code></pre>');
606
+ return 'FENCE' + (fences.length - 1) + '';
607
+ });
608
+
609
+ const inline = (s) => esc(s)
610
+ .replace(/`([^`]+)`/g, (_, c) => '<code>' + c + '</code>')
611
+ .replace(/\[\[([^\]|#]+)(?:\|([^\]]+))?\]\]/g, (_, t, label) => {
612
+ const target = t.trim();
613
+ const known = nodes.some((x) => x.id.endsWith('/' + target + '.md') || x.id === target + '.md');
614
+ return '<span class="wikilink' + (known ? '' : ' dead') + '" data-link="' + esc(target) + '">' + esc(label || target) + '</span>';
615
+ })
616
+ .replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, '<a href="$2" target="_blank" rel="noopener">$1</a>')
617
+ .replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
618
+ .replace(/(^|\W)\*([^*\n]+)\*/g, '$1<em>$2</em>');
619
+
620
+ const out = [];
621
+ const lines = src.split('\n');
622
+ let list = null, para = [], table = null;
623
+ const flushP = () => { if (para.length) { out.push('<p>' + inline(para.join(' ')) + '</p>'); para = []; } };
624
+ const flushL = () => { if (list) { out.push('</' + list + '>'); list = null; } };
625
+ const flushT = () => {
626
+ if (!table) return;
627
+ const cells = (r) => r.replace(/^\||\|$/g, '').split('|').map((c) => c.trim());
628
+ const head = cells(table[0]).map((c) => '<th>' + inline(c) + '</th>').join('');
629
+ const rows = table.slice(2).map((r) => '<tr>' + cells(r).map((c) => '<td>' + inline(c) + '</td>').join('') + '</tr>').join('');
630
+ out.push('<div class="tablewrap"><table><thead><tr>' + head + '</tr></thead><tbody>' + rows + '</tbody></table></div>');
631
+ table = null;
632
+ };
633
+ const flushAll = () => { flushP(); flushL(); flushT(); };
634
+
635
+ for (const line of lines) {
636
+ if (table !== null) {
637
+ if (/\|/.test(line)) { table.push(line); continue; }
638
+ flushT();
639
+ }
640
+ if (/^\s*\|.*\|\s*$/.test(line)) { flushP(); flushL(); table = [line]; continue; }
641
+
642
+ const h = line.match(/^(#{1,6})\s+(.*)$/);
643
+ if (h) { flushAll(); out.push('<h' + Math.min(h[1].length, 3) + '>' + inline(h[2]) + '</h' + Math.min(h[1].length, 3) + '>'); continue; }
644
+ if (/^\s*(---|\*\*\*|___)\s*$/.test(line)) { flushAll(); out.push('<hr>'); continue; }
645
+ if (/^\s*>\s?/.test(line)) { flushAll(); out.push('<blockquote>' + inline(line.replace(/^\s*>\s?/, '')) + '</blockquote>'); continue; }
646
+
647
+ const li = line.match(/^\s*([-*+]|\d+\.)\s+(.*)$/);
648
+ if (li) {
649
+ flushP();
650
+ const want = /\d/.test(li[1]) ? 'ol' : 'ul';
651
+ if (list !== want) { flushL(); out.push('<' + want + '>'); list = want; }
652
+ out.push('<li>' + inline(li[2]) + '</li>');
653
+ continue;
654
+ }
655
+ if (!line.trim()) { flushAll(); continue; }
656
+ flushL();
657
+ para.push(line.trim());
658
+ }
659
+ flushAll();
660
+ return out.join('\n').replace(/FENCE(\d+)/g, (_, i) => fences[i]);
661
+ }
662
+
663
+ load().catch(() => { $('#hint').textContent = 'Could not reach the vault server.'; });
664
+ </script>
665
+ </body>
666
+ </html>