toon-memory 3.1.0 → 3.2.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.
@@ -0,0 +1,880 @@
1
+ import { ViewerData, COLORS } from "./types"
2
+
3
+ export function generateHtml(viewerData: ViewerData): string {
4
+ const jsonData = JSON.stringify(viewerData)
5
+
6
+ return `<!DOCTYPE html>
7
+ <html lang="en">
8
+ <head>
9
+ <meta charset="UTF-8">
10
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
11
+ <title>toon-memory viewer</title>
12
+ <script src="https://d3js.org/d3.v7.min.js"><\/script>
13
+ <style>
14
+ *{box-sizing:border-box;margin:0;padding:0}
15
+ :root{
16
+ --bg:#0a0a0f;--bg2:#12121a;--bg3:#1a1a26;--border:#2a2a3a;
17
+ --text:#e2e2ee;--muted:#8888a0;--brand:#a78bfa;--brand2:#c084fc;
18
+ --green:#22c55e;--red:#ef4444;--blue:#3b82f6;--amber:#f59e0b;
19
+ --pink:#ec4899;--cyan:#06b6d4;
20
+ }
21
+ html,body{height:100%;font-family:system-ui,-apple-system,sans-serif;background:var(--bg);color:var(--text);overflow:hidden}
22
+ .app{display:grid;grid-template-rows:auto 1fr;height:100vh}
23
+ header{display:flex;align-items:center;justify-content:space-between;padding:0.75rem 1.5rem;border-bottom:1px solid var(--border);background:var(--bg2)}
24
+ header h1{font-size:1.1rem;font-weight:700;background:linear-gradient(135deg,var(--brand),var(--pink));-webkit-background-clip:text;-webkit-text-fill-color:transparent}
25
+ header .meta{display:flex;gap:1rem;font-size:0.8rem;color:var(--muted)}
26
+ header .meta span{display:flex;align-items:center;gap:0.3rem}
27
+ .main{display:grid;grid-template-columns:var(--sidebar-w,320px) 1fr;overflow:hidden;position:relative}
28
+ .sidebar{display:flex;flex-direction:column;border-right:1px solid var(--border);overflow:hidden}
29
+ .sidebar-resizer{width:4px;cursor:col-resize;background:transparent;position:absolute;left:var(--sidebar-w,320px);top:0;bottom:0;z-index:5;transition:background 0.15s}
30
+ .sidebar-resizer:hover,.sidebar-resizer:active{background:var(--brand)}
31
+ .sidebar-header{padding:0.75rem 1rem;border-bottom:1px solid var(--border);background:var(--bg2)}
32
+ .sidebar-header h2{font-size:0.85rem;font-weight:600;text-transform:uppercase;letter-spacing:0.05em;color:var(--muted);margin-bottom:0.5rem}
33
+ .search-box{width:100%;padding:0.5rem 0.75rem;border:1px solid var(--border);border-radius:8px;background:var(--bg3);color:var(--text);font-size:0.85rem;outline:none}
34
+ .search-box:focus{border-color:var(--brand)}
35
+ .filter-row{display:flex;gap:0.4rem;margin-top:0.5rem;flex-wrap:wrap}
36
+ .filter-btn{padding:0.2rem 0.5rem;border:1px solid var(--border);border-radius:6px;background:transparent;color:var(--muted);font-size:0.7rem;cursor:pointer;transition:all 0.15s}
37
+ .filter-btn:hover,.filter-btn.active{border-color:var(--brand);color:var(--brand);background:rgba(167,139,250,0.1)}
38
+ .entries-list{flex:1;overflow-y:auto;padding:0.5rem}
39
+ .entry-item{padding:0.6rem 0.75rem;border:1px solid var(--border);border-radius:8px;margin-bottom:0.4rem;cursor:pointer;transition:all 0.15s}
40
+ .entry-item:hover{border-color:var(--brand);background:rgba(167,139,250,0.05)}
41
+ .entry-item.selected{border-color:var(--brand);background:rgba(167,139,250,0.1)}
42
+ .entry-cat{font-size:0.65rem;font-weight:600;text-transform:uppercase;letter-spacing:0.05em;margin-bottom:0.2rem}
43
+ .entry-cat.decision{color:var(--brand)}
44
+ .entry-cat.bug{color:var(--red)}
45
+ .entry-cat.pattern{color:var(--green)}
46
+ .entry-cat.knowledge{color:var(--cyan)}
47
+ .entry-cat.architecture{color:var(--amber)}
48
+ .entry-key{font-size:0.85rem;font-weight:600;margin-bottom:0.15rem}
49
+ .entry-preview{font-size:0.75rem;color:var(--muted);line-height:1.4;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}
50
+ .entry-tags{display:flex;gap:0.25rem;margin-top:0.3rem;flex-wrap:wrap}
51
+ .entry-tag{font-size:0.6rem;padding:0.1em 0.35rem;border-radius:3px;background:var(--bg3);color:var(--muted);font-family:monospace}
52
+ .viewer-area{display:flex;flex-direction:column;overflow:hidden}
53
+ .tabs{display:flex;border-bottom:1px solid var(--border);background:var(--bg2);padding:0 1rem}
54
+ .tab{padding:0.6rem 1rem;font-size:0.8rem;font-weight:500;color:var(--muted);cursor:pointer;border-bottom:2px solid transparent;transition:all 0.15s}
55
+ .tab:hover{color:var(--text)}
56
+ .tab.active{color:var(--brand);border-bottom-color:var(--brand)}
57
+ .tab-content{flex:1;overflow:hidden;position:relative}
58
+ .tab-panel{display:none;width:100%;height:100%;overflow:auto}
59
+ .tab-panel.active{display:block}
60
+ .graph-container{width:100%;height:100%;background:var(--bg);position:relative}
61
+ .graph-container::before{content:'';position:absolute;inset:0;background-image:radial-gradient(var(--border) 1px,transparent 1px);background-size:24px 24px;opacity:0.3;pointer-events:none;z-index:0}
62
+ .graph-container svg{width:100%;height:100%;position:relative;z-index:1}
63
+ .node-circle{cursor:pointer;transition:r 0.2s ease,opacity 0.2s ease,filter 0.2s ease,stroke 0.2s ease}
64
+ .node-circle:hover{filter:brightness(1.3) drop-shadow(0 0 6px color-mix(in srgb,currentColor 60%,transparent))}
65
+ .node-circle.glow{filter:drop-shadow(0 0 12px var(--brand)) brightness(1.5)}
66
+ .node-circle.selected{stroke:var(--brand);stroke-width:3;filter:drop-shadow(0 0 14px var(--brand)) brightness(1.3)}
67
+ .node-label{font-size:9px;font-family:monospace;fill:var(--text);pointer-events:none;text-anchor:middle;transition:opacity 0.2s ease;font-weight:500;letter-spacing:0.02em}
68
+ .link{stroke:var(--border);stroke-opacity:0.4;transition:all 0.2s ease;cursor:pointer}
69
+ .link:hover{stroke-opacity:0.9;stroke-width:3}
70
+ .link.highlighted{stroke:var(--brand);stroke-opacity:0.9;stroke-width:2.5}
71
+ .link.faded{stroke-opacity:0.04}
72
+ .node-ring{fill:none;stroke-width:2;opacity:0;transition:opacity 0.3s ease,r 0.3s ease}
73
+ .node-ring.visible{opacity:0.6}
74
+ .stats-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:1rem;padding:1.5rem}
75
+ .stat-card{background:var(--bg2);border:1px solid var(--border);border-radius:12px;padding:1.25rem}
76
+ .stat-card h3{font-size:0.8rem;font-weight:600;text-transform:uppercase;letter-spacing:0.05em;color:var(--muted);margin-bottom:0.75rem}
77
+ .stat-big{font-size:2rem;font-weight:800;background:linear-gradient(135deg,var(--brand),var(--pink));-webkit-background-clip:text;-webkit-text-fill-color:transparent}
78
+ .bar-chart{display:flex;flex-direction:column;gap:0.4rem}
79
+ .bar-row{display:flex;align-items:center;gap:0.5rem}
80
+ .bar-label{font-size:0.75rem;color:var(--muted);width:80px;text-align:right;flex-shrink:0}
81
+ .bar-track{flex:1;height:20px;background:var(--bg3);border-radius:4px;overflow:hidden}
82
+ .bar-fill{height:100%;border-radius:4px;transition:width 0.6s cubic-bezier(0.16,1,0.3,1)}
83
+ .bar-value{font-size:0.7rem;color:var(--muted);width:30px}
84
+ .tag-cloud{display:flex;flex-wrap:wrap;gap:0.4rem}
85
+ .tag-pill{padding:0.25rem 0.6rem;border-radius:6px;font-size:0.75rem;font-family:monospace;border:1px solid var(--border);color:var(--muted);transition:all 0.15s}
86
+ .tag-pill:hover{border-color:var(--brand);color:var(--brand)}
87
+ .detail-panel{padding:1.5rem;max-width:700px}
88
+ .detail-panel h2{font-size:1.2rem;font-weight:700;margin-bottom:0.5rem}
89
+ .detail-panel .detail-cat{font-size:0.75rem;font-weight:600;text-transform:uppercase;letter-spacing:0.05em;margin-bottom:1rem}
90
+ .detail-field{margin-bottom:1rem}
91
+ .detail-field label{display:block;font-size:0.75rem;font-weight:600;color:var(--muted);text-transform:uppercase;letter-spacing:0.05em;margin-bottom:0.3rem}
92
+ .detail-field .value{font-size:0.9rem;line-height:1.6}
93
+ .detail-field .value.mono{font-family:monospace;background:var(--bg3);padding:0.5rem;border-radius:6px;display:block;overflow-x:auto;white-space:pre-wrap}
94
+ .detail-links{display:flex;gap:0.4rem;flex-wrap:wrap}
95
+ .detail-link{padding:0.2rem 0.5rem;border:1px solid var(--border);border-radius:6px;font-size:0.75rem;font-family:monospace;color:var(--brand);cursor:pointer;transition:all 0.15s}
96
+ .detail-link:hover{border-color:var(--brand);background:rgba(167,139,250,0.1)}
97
+ .empty-detail{display:flex;align-items:center;justify-content:center;height:100%;color:var(--muted);font-size:0.9rem}
98
+ .graph-legend{position:absolute;bottom:3.5rem;left:0.5rem;background:var(--bg2);border:1px solid var(--border);border-radius:8px;padding:0.75rem;font-size:0.7rem;z-index:10}
99
+ .legend-item{display:flex;align-items:center;gap:0.4rem;margin-bottom:0.25rem}
100
+ .legend-dot{width:10px;height:10px;border-radius:50%}
101
+ .graph-controls{position:absolute;top:1rem;right:1rem;display:flex;gap:0.4rem;z-index:10}
102
+ .graph-btn{width:32px;height:32px;border:1px solid var(--border);border-radius:6px;background:var(--bg2);color:var(--text);cursor:pointer;display:flex;align-items:center;justify-content:center;font-size:1rem;transition:all 0.15s}
103
+ .graph-btn:hover{border-color:var(--brand);color:var(--brand)}
104
+ .tooltip{position:fixed;pointer-events:none;background:rgba(18,18,26,0.92);backdrop-filter:blur(12px);-webkit-backdrop-filter:blur(12px);border:1px solid var(--border);border-radius:12px;padding:0.75rem 1rem;font-size:0.75rem;max-width:320px;z-index:1000;box-shadow:0 8px 32px rgba(0,0,0,0.5)}
105
+ .tooltip .tt-cat{font-size:0.6rem;font-weight:600;text-transform:uppercase;margin-bottom:0.2rem}
106
+ .tooltip .tt-key{font-weight:600;margin-bottom:0.2rem}
107
+ .tooltip .tt-content{color:var(--muted);line-height:1.4;display:-webkit-box;-webkit-line-clamp:3;-webkit-box-orient:vertical;overflow:hidden}
108
+ .tooltip .tt-score{margin-top:0.3rem;font-size:0.65rem;color:var(--brand)}
109
+ .tooltip .tt-edge{font-size:0.65rem;color:var(--muted);margin-top:0.3rem;padding-top:0.3rem;border-top:1px solid var(--border)}
110
+ .timeline-container{padding:1.5rem;overflow-y:auto;height:100%}
111
+ .timeline-header{font-size:1rem;font-weight:700;margin-bottom:1rem;color:var(--text)}
112
+ .timeline-track{position:relative;padding-left:2rem}
113
+ .timeline-line{position:absolute;left:0.5rem;top:0;bottom:0;width:2px;background:var(--border)}
114
+ .timeline-entry{position:relative;margin-bottom:1rem;padding:0.6rem 0.8rem;border:1px solid var(--border);border-radius:8px;background:var(--bg2);transition:all 0.15s;cursor:pointer}
115
+ .timeline-entry:hover{border-color:var(--brand)}
116
+ .timeline-entry::before{content:'';position:absolute;left:-1.75rem;top:0.8rem;width:10px;height:10px;border-radius:50%;border:2px solid var(--border);background:var(--bg)}
117
+ .timeline-entry .tl-date{font-size:0.65rem;color:var(--muted);margin-bottom:0.2rem}
118
+ .timeline-entry .tl-cat{font-size:0.6rem;font-weight:600;text-transform:uppercase}
119
+ .timeline-entry .tl-key{font-size:0.85rem;font-weight:600}
120
+ .timeline-entry .tl-content{font-size:0.75rem;color:var(--muted);margin-top:0.2rem}
121
+ .export-btn{padding:0.3rem 0.6rem;border:1px solid var(--border);border-radius:6px;background:var(--bg2);color:var(--text);font-size:0.7rem;cursor:pointer;transition:all 0.15s}
122
+ .export-btn:hover{border-color:var(--brand);color:var(--brand)}
123
+ .export-bar{display:flex;gap:0.4rem}
124
+ .theme-toggle{background:none;border:1px solid var(--border);border-radius:6px;padding:0.3rem 0.6rem;color:var(--text);cursor:pointer;font-size:0.8rem;transition:all 0.15s}
125
+ .theme-toggle:hover{border-color:var(--brand);color:var(--brand)}
126
+ .reload-btn{background:none;border:1px solid var(--border);border-radius:6px;padding:0.3rem 0.6rem;color:var(--muted);cursor:pointer;font-size:0.9rem;transition:all 0.15s;line-height:1}
127
+ .reload-btn:hover{border-color:var(--brand);color:var(--brand)}
128
+ .minimap{width:150px;height:100px;background:var(--bg2);border:1px solid var(--border);border-radius:8px;overflow:hidden;box-shadow:0 4px 16px rgba(0,0,0,0.3);cursor:pointer}
129
+ .minimap svg{width:100%;height:100%;display:block}
130
+ .minimap-viewport{fill:rgba(167,139,250,0.15);stroke:var(--brand);stroke-width:1.5}
131
+ .graph-right{position:absolute;bottom:1rem;right:1rem;z-index:10;display:flex;flex-direction:column;align-items:flex-end;gap:0.4rem}
132
+ .physics-panel{position:absolute;top:2.8rem;left:0.5rem;background:var(--bg2);border:1px solid var(--border);border-radius:8px;padding:0.75rem;font-size:0.7rem;z-index:10;min-width:180px}
133
+ .physics-panel h4{font-size:0.7rem;font-weight:600;text-transform:uppercase;letter-spacing:0.05em;color:var(--muted);margin-bottom:0.5rem}
134
+ .graph-toolbar{position:absolute;top:1rem;left:0.5rem;display:flex;gap:0.4rem;z-index:11}
135
+ .physics-row{display:flex;align-items:center;gap:0.5rem;margin-bottom:0.4rem}
136
+ .physics-row label{font-size:0.65rem;color:var(--muted);width:60px;flex-shrink:0}
137
+ .physics-row input[type="range"]{flex:1;height:4px;-webkit-appearance:none;background:var(--bg3);border-radius:2px;outline:none}
138
+ .physics-row input[type="range"]::-webkit-slider-thumb{-webkit-appearance:none;width:12px;height:12px;border-radius:50%;background:var(--brand);cursor:pointer}
139
+ .physics-row .physics-val{font-size:0.6rem;color:var(--muted);width:30px;text-align:right}
140
+ .node-circle.search-match{filter:drop-shadow(0 0 10px var(--brand)) brightness(1.4);animation:searchPulse 1.5s ease-in-out infinite}
141
+ @keyframes searchPulse{0%,100%{filter:drop-shadow(0 0 8px var(--brand)) brightness(1.2)}50%{filter:drop-shadow(0 0 18px var(--brand)) brightness(1.7)}}
142
+ .path-btn.active{background:var(--brand);color:#fff;border-color:var(--brand)}
143
+ .path-info{position:absolute;bottom:0.5rem;left:0.5rem;background:var(--bg2);border:1px solid var(--border);border-radius:8px;padding:0.4rem 0.7rem;font-size:0.7rem;z-index:10;display:none;max-width:500px;word-break:break-all}
144
+ .node-circle.path-start{stroke:var(--green);stroke-width:3;filter:drop-shadow(0 0 12px var(--green))}
145
+ .node-circle.path-end{stroke:var(--red);stroke-width:3;filter:drop-shadow(0 0 12px var(--red))}
146
+ .node-circle.path-node{stroke:var(--brand);stroke-width:2.5;animation:pathNodePulse 1.2s ease-in-out infinite}
147
+ @keyframes pathNodePulse{0%,100%{filter:drop-shadow(0 0 6px var(--brand))}50%{filter:drop-shadow(0 0 14px var(--brand))}}
148
+ .link.path-edge{stroke:var(--brand);stroke-opacity:0.9;stroke-width:3}
149
+ .light{--bg:#f4f4f9;--bg2:#ffffff;--bg3:#e8e8f0;--border:#c8c8d8;--text:#1a1a2e;--muted:#666680;--brand:#7c3aed;--brand2:#9333ea}
150
+ .light .tooltip{background:rgba(255,255,255,0.95);backdrop-filter:blur(12px);-webkit-backdrop-filter:blur(12px);border-color:#c8c8d8;box-shadow:0 8px 32px rgba(0,0,0,0.1)}
151
+ .light .timeline-entry{background:#fff}
152
+ .light .graph-btn,.light .export-btn,.light .theme-toggle{background:#fff}
153
+ .light .minimap{box-shadow:0 4px 16px rgba(0,0,0,0.08)}
154
+ @media(max-width:768px){.main{grid-template-columns:1fr}.sidebar{display:none}}
155
+ </style>
156
+ </head>
157
+ <body>
158
+ <div class="app">
159
+ <header>
160
+ <h1>toon-memory viewer</h1>
161
+ <div class="meta">
162
+ <span id="totalCount"></span>
163
+ <span id="edgeCount"></span>
164
+ <span id="categoryCount"></span>
165
+ <button class="theme-toggle" id="themeToggle" title="Toggle theme">☀</button>
166
+ <button class="reload-btn" id="reloadBtn" title="Reload (r)">↻</button>
167
+ </div>
168
+ </header>
169
+ <div class="main">
170
+ <div class="sidebar">
171
+ <div class="sidebar-header">
172
+ <h2>Entries</h2>
173
+ <input type="text" class="search-box" id="search" placeholder="Search entries... (/)">
174
+ <div class="filter-row" id="filters"></div>
175
+ </div>
176
+ <div class="entries-list" id="entriesList"></div>
177
+ </div>
178
+ <div class="sidebar-resizer" id="sidebarResizer"></div>
179
+ <div class="viewer-area">
180
+ <div class="tabs">
181
+ <div class="tab active" data-tab="graph">Graph</div>
182
+ <div class="tab" data-tab="stats">Stats</div>
183
+ <div class="tab" data-tab="timeline">Timeline</div>
184
+ <div class="tab" data-tab="detail">Detail</div>
185
+ </div>
186
+ <div class="tab-content">
187
+ <div class="tab-panel active" id="panel-graph">
188
+ <div class="graph-container" id="graphContainer">
189
+ <div class="graph-legend" id="legend"></div>
190
+ <div class="graph-right">
191
+ <div class="export-bar">
192
+ <button class="export-btn" id="exportPng">PNG</button>
193
+ <button class="export-btn" id="exportSvg">SVG</button>
194
+ </div>
195
+ <div class="minimap" id="minimap"><svg id="minimapSvg"></svg></div>
196
+ </div>
197
+ <div class="graph-controls">
198
+ <button class="graph-btn" id="zoomIn" title="Zoom in (+)">+</button>
199
+ <button class="graph-btn" id="zoomOut" title="Zoom out (-)">−</button>
200
+ <button class="graph-btn" id="zoomReset" title="Reset (0)">⟲</button>
201
+ </div>
202
+ <div class="graph-toolbar">
203
+ <button class="graph-btn" id="physicsToggle" title="Physics controls">⚙</button>
204
+ <button class="graph-btn path-btn" id="pathToggle" title="Find path between two nodes">⇿</button>
205
+ </div>
206
+ <div class="physics-panel" id="physicsPanel" style="display:none">
207
+ <h4>Graph Physics</h4>
208
+ <div class="physics-row"><label>Charge</label><input type="range" id="chargeSlider" min="-300" max="0" value="-120" step="10"><span class="physics-val" id="chargeVal">-120</span></div>
209
+ <div class="physics-row"><label>Distance</label><input type="range" id="distSlider" min="20" max="200" value="70" step="5"><span class="physics-val" id="distVal">70</span></div>
210
+ <div class="physics-row"><label>Center</label><input type="range" id="centerSlider" min="0" max="1" value="0.05" step="0.01"><span class="physics-val" id="centerVal">0.05</span></div>
211
+ </div>
212
+ <div class="path-info" id="pathInfo"></div>
213
+ </div>
214
+ </div>
215
+ <div class="tab-panel" id="panel-stats">
216
+ <div class="stats-grid" id="statsGrid"></div>
217
+ </div>
218
+ <div class="tab-panel" id="panel-timeline">
219
+ <div class="timeline-container" id="timelineContainer"></div>
220
+ </div>
221
+ <div class="tab-panel" id="panel-detail">
222
+ <div class="detail-panel" id="detailPanel">
223
+ <div class="empty-detail">Select an entry from the sidebar or click a node in the graph</div>
224
+ </div>
225
+ </div>
226
+ </div>
227
+ </div>
228
+ </div>
229
+ </div>
230
+ <div class="tooltip" id="tooltip" style="display:none"></div>
231
+ <script>
232
+ const DATA = ${jsonData};
233
+
234
+ const COLORS = {
235
+ decision: '#a78bfa', bug: '#ef4444', pattern: '#22c55e',
236
+ knowledge: '#06b6d4', architecture: '#f59e0b'
237
+ };
238
+
239
+ let selectedEntry = null;
240
+ let activeFilter = null;
241
+ let searchQuery = '';
242
+
243
+ // --- Theme Toggle ---
244
+ const themeToggle = document.getElementById('themeToggle');
245
+ const savedTheme = localStorage.getItem('toon-viewer-theme') || 'dark';
246
+ if (savedTheme === 'light') { document.documentElement.classList.add('light'); themeToggle.textContent = '☾'; }
247
+ themeToggle.addEventListener('click', () => {
248
+ document.documentElement.classList.toggle('light');
249
+ const isLight = document.documentElement.classList.contains('light');
250
+ themeToggle.textContent = isLight ? '☾' : '☀';
251
+ localStorage.setItem('toon-viewer-theme', isLight ? 'light' : 'dark');
252
+ });
253
+
254
+ // --- Tooltip ---
255
+ const tooltip = document.getElementById('tooltip');
256
+ function showTooltip(e, d) {
257
+ const score = d.quality || 0;
258
+ tooltip.innerHTML =
259
+ '<div class="tt-cat" style="color:' + (COLORS[d.category] || 'var(--muted)') + '">' + d.category + '</div>' +
260
+ '<div class="tt-key">' + escHtml(d.id) + '</div>' +
261
+ '<div class="tt-content">' + escHtml(d.content) + '</div>' +
262
+ '<div class="tt-score">Quality: ' + score.toFixed(2) + ' | Access: ' + (d.accessCount || 0) + '</div>';
263
+ tooltip.style.display = 'block';
264
+ positionTooltip(e);
265
+ }
266
+ function positionTooltip(e) {
267
+ const x = e.clientX + 12;
268
+ const y = e.clientY + 12;
269
+ const rect = tooltip.getBoundingClientRect();
270
+ tooltip.style.left = (x + rect.width > window.innerWidth ? e.clientX - rect.width - 8 : x) + 'px';
271
+ tooltip.style.top = (y + rect.height > window.innerHeight ? e.clientY - rect.height - 8 : y) + 'px';
272
+ }
273
+ function hideTooltip() { tooltip.style.display = 'none'; }
274
+ document.addEventListener('mousemove', (e) => { if (tooltip.style.display === 'block') positionTooltip(e); });
275
+
276
+ // --- Header ---
277
+ document.getElementById('totalCount').textContent = DATA.totalEntries + ' entries';
278
+ document.getElementById('edgeCount').textContent = DATA.edges.length + ' edges';
279
+ document.getElementById('categoryCount').textContent = Object.keys(DATA.categories).length + ' categories';
280
+
281
+ // --- Tabs ---
282
+ document.querySelectorAll('.tab').forEach(tab => {
283
+ tab.addEventListener('click', () => {
284
+ document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
285
+ document.querySelectorAll('.tab-panel').forEach(p => p.classList.remove('active'));
286
+ tab.classList.add('active');
287
+ document.getElementById('panel-' + tab.dataset.tab).classList.add('active');
288
+ if (tab.dataset.tab === 'graph') setTimeout(() => graph.resize(), 50);
289
+ });
290
+ });
291
+
292
+ function switchTab(name) {
293
+ document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
294
+ document.querySelectorAll('.tab-panel').forEach(p => p.classList.remove('active'));
295
+ document.querySelector('[data-tab="' + name + '"]').classList.add('active');
296
+ document.getElementById('panel-' + name).classList.add('active');
297
+ }
298
+
299
+ // --- Filters ---
300
+ const filterContainer = document.getElementById('filters');
301
+ const allBtn = document.createElement('button');
302
+ allBtn.className = 'filter-btn active';
303
+ allBtn.textContent = 'All';
304
+ allBtn.onclick = () => setFilter(null);
305
+ filterContainer.appendChild(allBtn);
306
+ Object.keys(DATA.categories).sort().forEach(cat => {
307
+ const btn = document.createElement('button');
308
+ btn.className = 'filter-btn';
309
+ btn.textContent = cat;
310
+ btn.dataset.cat = cat;
311
+ btn.onclick = () => setFilter(cat);
312
+ filterContainer.appendChild(btn);
313
+ });
314
+
315
+ function setFilter(cat) {
316
+ activeFilter = cat;
317
+ document.querySelectorAll('.filter-btn').forEach(b => {
318
+ b.classList.toggle('active', cat === null ? !b.dataset.cat : b.dataset.cat === cat);
319
+ });
320
+ renderEntries();
321
+ if (graph.highlightSearch) graph.highlightSearch();
322
+ }
323
+
324
+ // --- Search ---
325
+ document.getElementById('search').addEventListener('input', (e) => {
326
+ searchQuery = e.target.value.toLowerCase();
327
+ renderEntries();
328
+ if (graph.highlightSearch) graph.highlightSearch();
329
+ if (searchQuery && graph.centerOn) {
330
+ const first = DATA.nodes.find(n => n.id.toLowerCase().includes(searchQuery) || n.content.toLowerCase().includes(searchQuery) || n.tags.some(t => t.toLowerCase().includes(searchQuery)));
331
+ if (first) graph.centerOn(first.id);
332
+ }
333
+ });
334
+
335
+ // --- Entries List ---
336
+ function renderEntries() {
337
+ const q = searchQuery;
338
+ const list = document.getElementById('entriesList');
339
+ list.innerHTML = '';
340
+ DATA.nodes
341
+ .filter(e => !activeFilter || e.category === activeFilter)
342
+ .filter(e => !q || e.id.toLowerCase().includes(q) || e.content.toLowerCase().includes(q) || e.tags.some(t => t.toLowerCase().includes(q)))
343
+ .forEach(e => {
344
+ const div = document.createElement('div');
345
+ div.className = 'entry-item' + (selectedEntry && selectedEntry.id === e.id ? ' selected' : '');
346
+ div.innerHTML =
347
+ '<div class="entry-cat ' + e.category + '">' + e.category + '</div>' +
348
+ '<div class="entry-key">' + escHtml(e.id) + '</div>' +
349
+ '<div class="entry-preview">' + escHtml(e.content) + '</div>' +
350
+ (e.tags.length ? '<div class="entry-tags">' + e.tags.map(t => '<span class="entry-tag">' + escHtml(t) + '</span>').join('') + '</div>' : '');
351
+ div.onclick = () => selectEntry(e);
352
+ list.appendChild(div);
353
+ });
354
+ }
355
+
356
+ function escHtml(s) { return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'); }
357
+
358
+ // --- Entry Selection ---
359
+ function selectEntry(entry) {
360
+ selectedEntry = entry;
361
+ renderEntries();
362
+ showDetail(entry);
363
+ graph.highlightNode(entry.id);
364
+ graph.centerOn(entry.id);
365
+ switchTab('detail');
366
+ }
367
+
368
+ function showDetail(e) {
369
+ const panel = document.getElementById('detailPanel');
370
+ const linkedNodes = DATA.edges
371
+ .filter(ed => (typeof ed.source === 'object' ? ed.source.id : ed.source) === e.id || (typeof ed.target === 'object' ? ed.target.id : ed.target) === e.id)
372
+ .map(ed => (typeof ed.source === 'object' ? ed.source.id : ed.source) === e.id ? (typeof ed.target === 'object' ? ed.target.id : ed.target) : (typeof ed.source === 'object' ? ed.source.id : ed.source));
373
+
374
+ panel.innerHTML =
375
+ '<div class="detail-cat" style="color:' + (COLORS[e.category] || 'var(--muted)') + '">' + e.category + '</div>' +
376
+ '<h2>' + escHtml(e.id) + '</h2>' +
377
+ '<div class="detail-field"><label>Content</label><span class="value mono">' + escHtml(e.content) + '</span></div>' +
378
+ (e.file ? '<div class="detail-field"><label>File</label><span class="value mono"><a href="#" onclick="event.preventDefault()" style="color:var(--brand);text-decoration:none;cursor:pointer">' + escHtml(e.file) + '</a></span></div>' : '') +
379
+ (e.date ? '<div class="detail-field"><label>Date</label><span class="value">' + e.date + '</span></div>' : '') +
380
+ '<div class="detail-field"><label>Quality Score</label><span class="value" style="color:var(--brand);font-weight:700;font-size:1.1rem">' + (e.quality || 0).toFixed(3) + '</span></div>' +
381
+ '<div class="detail-field"><label>Access Count</label><span class="value">' + (e.accessCount || 0) + '</span></div>' +
382
+ (e.ttl ? '<div class="detail-field"><label>TTL</label><span class="value">' + e.ttl + '</span></div>' : '') +
383
+ (e.lastAccessed ? '<div class="detail-field"><label>Last Accessed</label><span class="value">' + e.lastAccessed + '</span></div>' : '') +
384
+ (e.tags.length ? '<div class="detail-field"><label>Tags</label><div class="entry-tags">' + e.tags.map(t => '<span class="entry-tag">' + escHtml(t) + '</span>').join('') + '</div></div>' : '') +
385
+ (linkedNodes.length ? '<div class="detail-field"><label>Linked Entries (' + linkedNodes.length + ')</label><div class="detail-links">' + linkedNodes.map(k => '<span class="detail-link" onclick="selectEntryById(\\'' + k + '\\')">' + escHtml(k) + '</span>').join('') + '</div></div>' : '');
386
+ }
387
+
388
+ window.selectEntryById = function(id) {
389
+ const e = DATA.nodes.find(n => n.id === id);
390
+ if (e) selectEntry(e);
391
+ };
392
+
393
+ // --- Stats ---
394
+ function renderStats() {
395
+ const grid = document.getElementById('statsGrid');
396
+ const catEntries = Object.entries(DATA.categories).sort((a,b) => b[1] - a[1]);
397
+ const maxCat = Math.max(...catEntries.map(c => c[1]));
398
+ const topTags = Object.entries(DATA.tagCounts).sort((a,b) => b[1] - a[1]).slice(0, 20);
399
+ const maxTag = Math.max(...topTags.map(t => t[1]));
400
+ const avgQuality = DATA.nodes.reduce((s, n) => s + (n.quality || 0), 0) / DATA.nodes.length;
401
+ const maxAccess = Math.max(...DATA.nodes.map(n => n.accessCount || 0));
402
+
403
+ grid.innerHTML =
404
+ '<div class="stat-card"><h3>Total Entries</h3><div class="stat-big">' + DATA.totalEntries + '</div></div>' +
405
+ '<div class="stat-card"><h3>Graph Edges</h3><div class="stat-big">' + DATA.edges.length + '</div></div>' +
406
+ '<div class="stat-card"><h3>Avg Quality</h3><div class="stat-big">' + avgQuality.toFixed(2) + '</div></div>' +
407
+ '<div class="stat-card"><h3>Max Access</h3><div class="stat-big">' + maxAccess + '</div></div>' +
408
+ '<div class="stat-card"><h3>Categories</h3><div class="bar-chart">' +
409
+ catEntries.map(([cat, count]) =>
410
+ '<div class="bar-row"><span class="bar-label">' + cat + '</span><div class="bar-track"><div class="bar-fill" style="width:' + (count/maxCat*100) + '%;background:' + (COLORS[cat] || 'var(--brand)') + '"></div></div><span class="bar-value">' + count + '</span></div>'
411
+ ).join('') +
412
+ '</div></div>' +
413
+ '<div class="stat-card"><h3>Top Tags</h3><div class="tag-cloud">' +
414
+ topTags.map(([tag, count]) =>
415
+ '<span class="tag-pill">' + escHtml(tag) + ' <small>(' + count + ')</small></span>'
416
+ ).join('') +
417
+ '</div></div>';
418
+ }
419
+
420
+ function renderTimeline() {
421
+ const container = document.getElementById('timelineContainer');
422
+ const sorted = [...DATA.nodes].sort((a, b) => (b.date || '').localeCompare(a.date || ''));
423
+ const grouped = {};
424
+ sorted.forEach(e => {
425
+ const d = e.date || 'unknown';
426
+ if (!grouped[d]) grouped[d] = [];
427
+ grouped[d].push(e);
428
+ });
429
+
430
+ let html = '<div class="timeline-header">Timeline (' + DATA.totalEntries + ' entries)</div><div class="timeline-track"><div class="timeline-line"></div>';
431
+ for (const [date, entries] of Object.entries(grouped)) {
432
+ for (const e of entries) {
433
+ html += '<div class="timeline-entry" onclick="selectEntryById(\\'' + e.id + '\\')" style="border-left:3px solid ' + (COLORS[e.category] || 'var(--border)') + '">' +
434
+ '<div class="tl-date">' + escHtml(date) + '</div>' +
435
+ '<div class="tl-cat" style="color:' + (COLORS[e.category] || 'var(--muted)') + '">' + e.category + '</div>' +
436
+ '<div class="tl-key">' + escHtml(e.id) + '</div>' +
437
+ '<div class="tl-content">' + escHtml(e.content.substring(0, 100)) + '</div>' +
438
+ '</div>';
439
+ }
440
+ }
441
+ html += '</div>';
442
+ container.innerHTML = html;
443
+ }
444
+
445
+ // --- Graph ---
446
+ const graph = (function() {
447
+ const container = document.getElementById('graphContainer');
448
+ const width = container.clientWidth;
449
+ const height = container.clientHeight;
450
+ const svg = d3.select(container).append('svg').attr('width', '100%').attr('height', '100%');
451
+
452
+ const defs = svg.append('defs');
453
+ const filter = defs.append('filter').attr('id', 'glow').attr('x', '-50%').attr('y', '-50%').attr('width', '200%').attr('height', '200%');
454
+ filter.append('feGaussianBlur').attr('stdDeviation', '4').attr('result', 'blur');
455
+ filter.append('feMerge').selectAll('feMergeNode').data(['blur', 'SourceGraphic']).join('feMergeNode').attr('in', d => d);
456
+
457
+ const g = svg.append('g');
458
+ let currentTransform = d3.zoomIdentity;
459
+ const zoom = d3.zoom().scaleExtent([0.1, 4]).on('zoom', e => { g.attr('transform', e.transform); currentTransform = e.transform; updateMinimap(); });
460
+ svg.call(zoom);
461
+
462
+ function nodeRadius(d) { return 4 + Math.min((d.accessCount || 0) * 0.8, 8); }
463
+
464
+ const connectionCount = new Map();
465
+ DATA.edges.forEach(e => {
466
+ const s = typeof e.source === 'object' ? e.source.id : e.source;
467
+ const t = typeof e.target === 'object' ? e.target.id : e.target;
468
+ connectionCount.set(s, (connectionCount.get(s) || 0) + 1);
469
+ connectionCount.set(t, (connectionCount.get(t) || 0) + 1);
470
+ });
471
+ function edgeWidth(d) {
472
+ const s = typeof d.source === 'object' ? d.source.id : d.source;
473
+ const t = typeof d.target === 'object' ? d.target.id : d.target;
474
+ const shared = Math.min(connectionCount.get(s) || 1, connectionCount.get(t) || 1);
475
+ return 0.5 + Math.min(shared * 0.3, 3);
476
+ }
477
+
478
+ function getSharedTags(a, b) {
479
+ const na = nodeMap.get(a), nb = nodeMap.get(b);
480
+ if (!na || !nb || !na.tags || !nb.tags) return [];
481
+ return na.tags.filter(t => nb.tags.includes(t));
482
+ }
483
+
484
+ let chargeStrength = -120;
485
+ let linkDistance = 70;
486
+ let centerStrength = 0.05;
487
+
488
+ const nodeMap = new Map(DATA.nodes.map(n => [n.id, n]));
489
+ const simulation = d3.forceSimulation(DATA.nodes)
490
+ .force('link', d3.forceLink(DATA.edges).id(d => d.id).distance(linkDistance).strength(0.5))
491
+ .force('charge', d3.forceManyBody().strength(chargeStrength))
492
+ .force('center', d3.forceCenter(width / 2, height / 2).strength(centerStrength))
493
+ .force('collision', d3.forceCollide().radius(d => nodeRadius(d) + 4));
494
+
495
+ const link = g.append('g').selectAll('line').data(DATA.edges).join('line')
496
+ .attr('class', 'link')
497
+ .attr('stroke-width', d => edgeWidth(d))
498
+ .attr('stroke-linecap', 'round');
499
+
500
+ const node = g.append('g').selectAll('circle').data(DATA.nodes).join('circle')
501
+ .attr('class', 'node-circle')
502
+ .attr('r', d => nodeRadius(d))
503
+ .attr('fill', d => COLORS[d.category] || '#888')
504
+ .attr('stroke', d => { const c = COLORS[d.category] || '#888'; return c; })
505
+ .attr('stroke-width', 1.5)
506
+ .attr('stroke-opacity', 0.6)
507
+ .call(d3.drag()
508
+ .on('start', (e, d) => { if (!e.active) simulation.alphaTarget(0.3).restart(); d.fx = d.x; d.fy = d.y; })
509
+ .on('drag', (e, d) => { d.fx = e.x; d.fy = e.y; })
510
+ .on('end', (e, d) => { if (!e.active) simulation.alphaTarget(0); })
511
+ )
512
+ .on('contextmenu', (e, d) => { e.preventDefault(); d.fx = null; d.fy = null; simulation.alpha(0.3).restart(); });
513
+ const label = g.append('g').selectAll('text').data(DATA.nodes).join('text')
514
+ .attr('class', 'node-label').attr('dy', d => -(nodeRadius(d) + 4))
515
+ .text(d => d.id.length > 18 ? d.id.substring(0, 16) + '..' : d.id);
516
+
517
+ // Edge tooltips
518
+ link.on('mouseover', (e, d) => {
519
+ const s = typeof d.source === 'object' ? d.source.id : d.source;
520
+ const t = typeof d.target === 'object' ? d.target.id : d.target;
521
+ const sharedTags = getSharedTags(s, t);
522
+ const html = '<div class="tt-edge">' +
523
+ '<span style="color:' + (COLORS[nodeMap.get(s)?.category] || 'var(--muted)') + '">' + escHtml(s) + '</span>' +
524
+ ' → ' +
525
+ '<span style="color:' + (COLORS[nodeMap.get(t)?.category] || 'var(--muted)') + '">' + escHtml(t) + '</span>' +
526
+ (sharedTags.length ? '<br>Tags: ' + sharedTags.map(tag => '<span class="entry-tag">' + escHtml(tag) + '</span>').join(' ') : '') +
527
+ '</div>';
528
+ tooltip.innerHTML = html;
529
+ tooltip.style.display = 'block';
530
+ positionTooltip(e);
531
+ })
532
+ .on('mouseout', () => { hideTooltip(); });
533
+
534
+ node.on('mouseover', (e, d) => {
535
+ showTooltip(e, d);
536
+ highlightNode(d.id);
537
+ })
538
+ .on('mouseout', () => {
539
+ hideTooltip();
540
+ if (!selectedEntry) highlightNode(null);
541
+ else highlightNode(selectedEntry.id);
542
+ });
543
+
544
+ svg.on('click', () => { selectedEntry = null; highlightNode(null); renderEntries(); });
545
+
546
+ simulation.on('tick', () => {
547
+ link.attr('x1', d => d.source.x).attr('y1', d => d.source.y)
548
+ .attr('x2', d => d.target.x).attr('y2', d => d.target.y);
549
+ node.attr('cx', d => d.x).attr('cy', d => d.y);
550
+ label.attr('x', d => d.x).attr('y', d => d.y);
551
+ updateMinimap();
552
+ });
553
+
554
+ function highlightNode(id) {
555
+ const neighbors = new Set();
556
+ if (id) {
557
+ DATA.edges.forEach(e => {
558
+ const s = typeof e.source === 'object' ? e.source.id : e.source;
559
+ const t = typeof e.target === 'object' ? e.target.id : e.target;
560
+ if (s === id) neighbors.add(t);
561
+ if (t === id) neighbors.add(s);
562
+ });
563
+ neighbors.add(id);
564
+ }
565
+ node.attr('r', d => id ? (d.id === id ? nodeRadius(d) + 4 : neighbors.has(d.id) ? nodeRadius(d) + 1 : 2) : nodeRadius(d))
566
+ .attr('opacity', d => id ? (neighbors.has(d.id) ? 1 : 0.12) : 1)
567
+ .classed('glow', d => id && d.id === id);
568
+ link.classed('highlighted', d => id && ((typeof d.source === 'object' ? d.source.id : d.source) === id || (typeof d.target === 'object' ? d.target.id : d.target) === id));
569
+ link.attr('opacity', d => id ? (((typeof d.source === 'object' ? d.source.id : d.source) === id || (typeof d.target === 'object' ? d.target.id : d.target) === id) ? 1 : 0.04) : 0.5);
570
+ label.attr('opacity', d => id ? (neighbors.has(d.id) ? 1 : 0.08) : 1);
571
+ }
572
+
573
+ function highlightSearch() {
574
+ if (!searchQuery && !activeFilter) { highlightNode(null); return; }
575
+ const q = searchQuery;
576
+ const matches = new Set();
577
+ DATA.nodes.forEach(n => {
578
+ const match = (!q || n.id.toLowerCase().includes(q) || n.content.toLowerCase().includes(q) || n.tags.some(t => t.toLowerCase().includes(q)));
579
+ const catMatch = !activeFilter || n.category === activeFilter;
580
+ if (match && catMatch) matches.add(n.id);
581
+ });
582
+ node.attr('r', d => matches.has(d.id) ? nodeRadius(d) + 3 : 2)
583
+ .attr('opacity', d => matches.has(d.id) ? 1 : 0.06)
584
+ .classed('glow search-match', d => matches.has(d.id));
585
+ label.attr('opacity', d => matches.has(d.id) ? 1 : 0.03);
586
+ link.attr('opacity', 0.03);
587
+ }
588
+
589
+ // Zoom controls
590
+ document.getElementById('zoomIn').onclick = () => svg.transition().call(zoom.scaleBy, 1.5);
591
+ document.getElementById('zoomOut').onclick = () => svg.transition().call(zoom.scaleBy, 0.67);
592
+ document.getElementById('zoomReset').onclick = () => svg.transition().call(zoom.transform, d3.zoomIdentity);
593
+
594
+ // Legend
595
+ const legend = document.getElementById('legend');
596
+ legend.innerHTML = Object.entries(COLORS).map(([cat, color]) =>
597
+ '<div class="legend-item"><span class="legend-dot" style="background:' + color + '"></span>' + cat + '</div>'
598
+ ).join('');
599
+
600
+ // Physics controls
601
+ const physicsToggle = document.getElementById('physicsToggle');
602
+ const physicsPanel = document.getElementById('physicsPanel');
603
+ physicsToggle.onclick = () => {
604
+ physicsPanel.style.display = physicsPanel.style.display === 'none' ? 'block' : 'none';
605
+ };
606
+
607
+ function updatePhysics() {
608
+ simulation.force('charge').strength(chargeStrength);
609
+ simulation.force('link').distance(linkDistance);
610
+ simulation.force('center').strength(centerStrength);
611
+ simulation.alpha(0.5).restart();
612
+ }
613
+
614
+ document.getElementById('chargeSlider').oninput = (e) => {
615
+ chargeStrength = parseInt(e.target.value);
616
+ document.getElementById('chargeVal').textContent = chargeStrength;
617
+ updatePhysics();
618
+ };
619
+ document.getElementById('distSlider').oninput = (e) => {
620
+ linkDistance = parseInt(e.target.value);
621
+ document.getElementById('distVal').textContent = linkDistance;
622
+ updatePhysics();
623
+ };
624
+ document.getElementById('centerSlider').oninput = (e) => {
625
+ centerStrength = parseFloat(e.target.value);
626
+ document.getElementById('centerVal').textContent = centerStrength;
627
+ updatePhysics();
628
+ };
629
+
630
+ // Center on a node
631
+ function centerOn(id) {
632
+ const n = DATA.nodes.find(d => d.id === id);
633
+ if (!n || n.x == null) return;
634
+ const w = container.clientWidth, h = container.clientHeight;
635
+ svg.transition().duration(400).call(zoom.transform,
636
+ d3.zoomIdentity.translate(w / 2 - n.x * currentTransform.k, h / 2 - n.y * currentTransform.k).scale(currentTransform.k));
637
+ }
638
+
639
+ // Path finder
640
+ let pathStart = null, pathActive = false;
641
+ function findPath(a, b) {
642
+ const adj = new Map();
643
+ DATA.edges.forEach(e => {
644
+ const s = typeof e.source === 'object' ? e.source.id : e.source;
645
+ const t = typeof e.target === 'object' ? e.target.id : e.target;
646
+ if (!adj.has(s)) adj.set(s, []);
647
+ if (!adj.has(t)) adj.set(t, []);
648
+ adj.get(s).push(t);
649
+ adj.get(t).push(s);
650
+ });
651
+ const visited = new Set([a]), parent = new Map([[a, null]]);
652
+ const queue = [a];
653
+ while (queue.length) {
654
+ const cur = queue.shift();
655
+ if (cur === b) break;
656
+ for (const nb of (adj.get(cur) || [])) {
657
+ if (!visited.has(nb)) { visited.add(nb); parent.set(nb, cur); queue.push(nb); }
658
+ }
659
+ }
660
+ if (!parent.has(b)) return null;
661
+ const path = [];
662
+ let cur = b;
663
+ while (cur !== null) { path.unshift(cur); cur = parent.get(cur); }
664
+ return path;
665
+ }
666
+ function highlightPath(path) {
667
+ link.classed('path-edge', false).attr('opacity', 0.5);
668
+ node.classed('path-start path-end path-node', false).attr('r', d => nodeRadius(d));
669
+ if (!path) return;
670
+ const set = new Set(path);
671
+ path.forEach((id, i) => {
672
+ node.filter(d => d.id === id)
673
+ .classed(i === 0 ? 'path-start' : i === path.length - 1 ? 'path-end' : 'path-node', true)
674
+ .attr('r', d => nodeRadius(d) + 3);
675
+ });
676
+ for (let i = 0; i < path.length - 1; i++) {
677
+ link.filter(d => {
678
+ const s = typeof d.source === 'object' ? d.source.id : d.source;
679
+ const t = typeof d.target === 'object' ? d.target.id : d.target;
680
+ return (s === path[i] && t === path[i+1]) || (s === path[i+1] && t === path[i]);
681
+ }).classed('path-edge', true).attr('opacity', 1);
682
+ }
683
+ }
684
+
685
+ // Minimap
686
+ const minimapSvg = d3.select('#minimapSvg');
687
+ const minimapScale = 0.1;
688
+ const minimapW = 150, minimapH = 100;
689
+ const minimapG = minimapSvg.append('g');
690
+ minimapG.append('g').attr('class', 'minimap-links');
691
+ minimapG.append('g').attr('class', 'minimap-nodes');
692
+ const minimapViewport = minimapSvg.append('rect').attr('class', 'minimap-viewport').attr('fill', 'rgba(167,139,250,0.12)').attr('stroke', 'var(--brand)').attr('stroke-width', 1.5).attr('rx', 3);
693
+
694
+ function getGraphBounds() {
695
+ const allX = DATA.nodes.map(n => n.x || 0);
696
+ const allY = DATA.nodes.map(n => n.y || 0);
697
+ return { minX: Math.min(...allX) - 20, minY: Math.min(...allY) - 20, maxX: Math.max(...allX) + 20, maxY: Math.max(...allY) + 20 };
698
+ }
699
+
700
+ function updateMinimap() {
701
+ const { minX, minY } = getGraphBounds();
702
+ minimapG.attr('transform', 'translate(' + (-minX * minimapScale) + ',' + (-minY * minimapScale) + ')');
703
+
704
+ minimapG.select('.minimap-links').selectAll('line').data(DATA.edges).join('line')
705
+ .attr('x1', d => (d.source.x || 0) * minimapScale)
706
+ .attr('y1', d => (d.source.y || 0) * minimapScale)
707
+ .attr('x2', d => (d.target.x || 0) * minimapScale)
708
+ .attr('y2', d => (d.target.y || 0) * minimapScale)
709
+ .attr('stroke', 'var(--border)').attr('stroke-width', 0.5).attr('stroke-opacity', 0.3);
710
+
711
+ minimapG.select('.minimap-nodes').selectAll('circle').data(DATA.nodes).join('circle')
712
+ .attr('cx', d => (d.x || 0) * minimapScale).attr('cy', d => (d.y || 0) * minimapScale)
713
+ .attr('r', 1.8).attr('fill', d => COLORS[d.category] || '#888').attr('stroke', 'none');
714
+
715
+ const t = currentTransform;
716
+ const containerW = container.clientWidth;
717
+ const containerH = container.clientHeight;
718
+ const vpX = (-t.x / t.k - minX) * minimapScale;
719
+ const vpY = (-t.y / t.k - minY) * minimapScale;
720
+ const vpW = (containerW / t.k) * minimapScale;
721
+ const vpH = (containerH / t.k) * minimapScale;
722
+ minimapViewport.attr('x', Math.max(0, vpX)).attr('y', Math.max(0, vpY))
723
+ .attr('width', Math.min(vpW, minimapW)).attr('height', Math.min(vpH, minimapH));
724
+ }
725
+
726
+ minimapSvg.on('click', (e) => {
727
+ const { minX, minY } = getGraphBounds();
728
+ const rect = minimapSvg.node().getBoundingClientRect();
729
+ const mmx = (e.clientX - rect.left) / minimapScale + minX;
730
+ const mmy = (e.clientY - rect.top) / minimapScale + minY;
731
+ const w2 = container.clientWidth / 2, h2 = container.clientHeight / 2;
732
+ svg.transition().duration(300).call(zoom.transform, d3.zoomIdentity.translate(w2 - mmx, h2 - mmy));
733
+ });
734
+
735
+ // Export PNG
736
+ document.getElementById('exportPng').onclick = () => {
737
+ const svgEl = container.querySelector('svg');
738
+ const svgData = new XMLSerializer().serializeToString(svgEl);
739
+ const canvas = document.createElement('canvas');
740
+ const ctx = canvas.getContext('2d');
741
+ const img = new Image();
742
+ canvas.width = svgEl.clientWidth * 2;
743
+ canvas.height = svgEl.clientHeight * 2;
744
+ img.onload = () => {
745
+ ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--bg').trim();
746
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
747
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
748
+ const link = document.createElement('a');
749
+ link.download = 'toon-memory-graph.png';
750
+ link.href = canvas.toDataURL('image/png');
751
+ link.click();
752
+ };
753
+ img.src = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svgData)));
754
+ };
755
+
756
+ // Export SVG
757
+ document.getElementById('exportSvg').onclick = () => {
758
+ const svgEl = container.querySelector('svg');
759
+ const svgData = new XMLSerializer().serializeToString(svgEl);
760
+ const blob = new Blob([svgData], { type: 'image/svg+xml' });
761
+ const link = document.createElement('a');
762
+ link.download = 'toon-memory-graph.svg';
763
+ link.href = URL.createObjectURL(blob);
764
+ link.click();
765
+ };
766
+
767
+ // Path toggle
768
+ document.getElementById('pathToggle').onclick = () => {
769
+ pathActive = !pathActive;
770
+ document.getElementById('pathToggle').classList.toggle('active', pathActive);
771
+ if (!pathActive) { pathStart = null; highlightPath(null); }
772
+ document.getElementById('pathInfo').style.display = 'none';
773
+ };
774
+ function selectEntryGraph(entry) {
775
+ selectedEntry = entry;
776
+ renderEntries();
777
+ showDetail(entry);
778
+ graph.highlightNode(entry.id);
779
+ graph.centerOn(entry.id);
780
+ }
781
+
782
+ let clickTimer = null;
783
+ node.on('click', (e, d) => {
784
+ e.stopPropagation();
785
+ if (pathActive) {
786
+ if (!pathStart) {
787
+ pathStart = d.id;
788
+ document.getElementById('pathInfo').textContent = 'Click target node';
789
+ document.getElementById('pathInfo').style.display = 'block';
790
+ highlightPath(null);
791
+ node.classed('path-start', false);
792
+ node.filter(n => n.id === d.id).classed('path-start', true);
793
+ } else {
794
+ const path = findPath(pathStart, d.id);
795
+ if (path) {
796
+ const info = document.getElementById('pathInfo');
797
+ info.innerHTML = 'Path: ' + path.join(' → ');
798
+ info.style.display = 'block';
799
+ highlightPath(path);
800
+ centerOn(path[Math.floor(path.length / 2)]);
801
+ } else {
802
+ document.getElementById('pathInfo').textContent = 'No path found';
803
+ document.getElementById('pathInfo').style.display = 'block';
804
+ }
805
+ pathStart = null;
806
+ pathActive = false;
807
+ document.getElementById('pathToggle').classList.remove('active');
808
+ }
809
+ return;
810
+ }
811
+ if (clickTimer) { clearTimeout(clickTimer); clickTimer = null; return; }
812
+ clickTimer = setTimeout(() => {
813
+ clickTimer = null;
814
+ selectEntryGraph(d);
815
+ }, 220);
816
+ });
817
+ node.on('dblclick', (e, d) => {
818
+ e.stopPropagation();
819
+ if (clickTimer) { clearTimeout(clickTimer); clickTimer = null; }
820
+ selectEntry(d); hideTooltip();
821
+ });
822
+
823
+ return { highlightNode, highlightSearch, centerOn, resize: () => {
824
+ const w = container.clientWidth;
825
+ const h = container.clientHeight;
826
+ simulation.force('center', d3.forceCenter(w / 2, h / 2).strength(centerStrength));
827
+ simulation.alpha(0.3).restart();
828
+ }};
829
+ })();
830
+
831
+ // --- Keyboard Shortcuts ---
832
+ document.addEventListener('keydown', (e) => {
833
+ if (e.target.tagName === 'INPUT') {
834
+ if (e.key === 'Escape') { e.target.value = ''; searchQuery = ''; renderEntries(); if (graph.highlightSearch) graph.highlightSearch(); e.target.blur(); }
835
+ return;
836
+ }
837
+ if (e.key === '/') { e.preventDefault(); document.getElementById('search').focus(); }
838
+ if (e.key === 'Escape') { selectedEntry = null; renderEntries(); graph.highlightNode(null); switchTab('graph'); }
839
+ if (e.key === '1') switchTab('graph');
840
+ if (e.key === '2') switchTab('stats');
841
+ if (e.key === '3') switchTab('timeline');
842
+ if (e.key === '4') switchTab('detail');
843
+ });
844
+
845
+ // --- Sidebar Resize ---
846
+ (function() {
847
+ const resizer = document.getElementById('sidebarResizer');
848
+ let startX, startW;
849
+ resizer.addEventListener('mousedown', (e) => {
850
+ startX = e.clientX;
851
+ startW = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--sidebar-w')) || 320;
852
+ document.body.style.cursor = 'col-resize';
853
+ document.body.style.userSelect = 'none';
854
+ const onMove = (ev) => {
855
+ const w = Math.max(160, Math.min(600, startW + ev.clientX - startX));
856
+ document.documentElement.style.setProperty('--sidebar-w', w + 'px');
857
+ };
858
+ const onUp = () => {
859
+ document.body.style.cursor = '';
860
+ document.body.style.userSelect = '';
861
+ document.removeEventListener('mousemove', onMove);
862
+ document.removeEventListener('mouseup', onUp);
863
+ };
864
+ document.addEventListener('mousemove', onMove);
865
+ document.addEventListener('mouseup', onUp);
866
+ });
867
+ })();
868
+
869
+ renderStats();
870
+ renderTimeline();
871
+ renderEntries();
872
+
873
+ document.getElementById('reloadBtn').addEventListener('click', () => location.reload());
874
+ document.addEventListener('keydown', (e) => {
875
+ if (e.key === 'r' && !e.metaKey && !e.ctrlKey && !e.target.closest('input,textarea')) location.reload();
876
+ });
877
+ <\/script>
878
+ </body>
879
+ </html>`
880
+ }