@visualli/core 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +263 -0
- package/dist/index.cjs +2325 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1181 -0
- package/dist/index.d.ts +1181 -0
- package/dist/index.js +2119 -0
- package/dist/index.js.map +1 -0
- package/package.json +80 -0
- package/src/animations/constants.ts +72 -0
- package/src/animations/easing.ts +70 -0
- package/src/animations/index.ts +2 -0
- package/src/config/blobShapes.ts +146 -0
- package/src/config/textScaling.ts +54 -0
- package/src/constants/design.ts +281 -0
- package/src/constants/index.ts +3 -0
- package/src/constants/performanceConstants.ts +157 -0
- package/src/constants/renderConfig.ts +134 -0
- package/src/index.ts +18 -0
- package/src/layout/circularLayout.ts +95 -0
- package/src/layout/index.ts +3 -0
- package/src/layout/layoutUtils.ts +132 -0
- package/src/layout/linearLayout.ts +84 -0
- package/src/parser/configUtils.ts +129 -0
- package/src/parser/index.ts +4 -0
- package/src/parser/middleware.ts +24 -0
- package/src/parser/mindmapUtils.ts +76 -0
- package/src/parser/visualliConverter.ts +328 -0
- package/src/parser/visualliParser.ts +191 -0
- package/src/performance/index.ts +1 -0
- package/src/performance/performanceMonitor.ts +145 -0
- package/src/rendering/culling.ts +71 -0
- package/src/spatial/index.ts +1 -0
- package/src/spatial/spatialIndex.ts +83 -0
- package/src/stores/index.ts +61 -0
- package/src/types/document.ts +15 -0
- package/src/types/index.ts +17 -0
- package/src/types/layer.ts +67 -0
- package/src/types/meta.ts +9 -0
- package/src/types/mindmap.ts +136 -0
- package/src/types/schema.ts +36 -0
- package/src/utils/navigation.ts +161 -0
- package/src/viewport/index.ts +1 -0
- package/src/viewport/viewportUtils.ts +195 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// ─── Mindmap Tree Utilities ───────────────────────────────────────────────────
|
|
2
|
+
|
|
3
|
+
import type { MindMapNode, TopLevelConnection } from '../types/mindmap.js';
|
|
4
|
+
|
|
5
|
+
/** Flatten a nested MindMapNode tree into a depth-first array. */
|
|
6
|
+
export function flattenNodes(nodes: MindMapNode[]): MindMapNode[] {
|
|
7
|
+
const result: MindMapNode[] = [];
|
|
8
|
+
const traverse = (list: MindMapNode[]) => {
|
|
9
|
+
for (const n of list) {
|
|
10
|
+
result.push(n);
|
|
11
|
+
if (n.children) traverse(n.children);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
traverse(nodes);
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Return nodes visible at the given zoom level.
|
|
20
|
+
*
|
|
21
|
+
* At lower zoom values only top-level nodes are shown; each 5% zoom increment
|
|
22
|
+
* reveals one additional hierarchy level.
|
|
23
|
+
*/
|
|
24
|
+
export function getVisibleNodes(
|
|
25
|
+
nodes: MindMapNode[],
|
|
26
|
+
zoom: number,
|
|
27
|
+
focusedNode?: string,
|
|
28
|
+
): MindMapNode[] {
|
|
29
|
+
const flat = flattenNodes(nodes);
|
|
30
|
+
|
|
31
|
+
if (focusedNode) {
|
|
32
|
+
const focused = flat.find(n => n.id === focusedNode);
|
|
33
|
+
if (focused) {
|
|
34
|
+
return focused.children ? [focused, ...focused.children] : [focused];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Map zoom to max visible level (level 0 at ≤70%, +1 level per 5% increment)
|
|
39
|
+
if (zoom <= 0.70) return flat.filter(n => n.level === 0);
|
|
40
|
+
const maxLevel = Math.floor((zoom - 0.70) / 0.05);
|
|
41
|
+
return flat.filter(n => n.level <= maxLevel);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Build parent→child connection pairs from the node tree. */
|
|
45
|
+
export function getNodeConnections(
|
|
46
|
+
nodes: MindMapNode[],
|
|
47
|
+
): Array<{ from: MindMapNode; to: MindMapNode; label?: string }> {
|
|
48
|
+
const flat = flattenNodes(nodes);
|
|
49
|
+
const result: Array<{ from: MindMapNode; to: MindMapNode; label?: string }> = [];
|
|
50
|
+
for (const n of flat) {
|
|
51
|
+
if (!n.parent) continue;
|
|
52
|
+
const parent = flat.find(p => p.id === n.parent);
|
|
53
|
+
if (parent) result.push({ from: parent, to: n, label: n.relationshipLabel });
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Resolve top-level cross-connections to node pairs. */
|
|
59
|
+
export function getTopLevelConnections(
|
|
60
|
+
nodes: MindMapNode[],
|
|
61
|
+
topLevelConnections: TopLevelConnection[],
|
|
62
|
+
): Array<{ from: MindMapNode; to: MindMapNode; label: string }> {
|
|
63
|
+
const flat = flattenNodes(nodes);
|
|
64
|
+
const result: Array<{ from: MindMapNode; to: MindMapNode; label: string }> = [];
|
|
65
|
+
for (const conn of topLevelConnections) {
|
|
66
|
+
const from = flat.find(n => n.id === conn.from);
|
|
67
|
+
const to = flat.find(n => n.id === conn.to);
|
|
68
|
+
if (from && to) result.push({ from, to, label: conn.label ?? '' });
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Euclidean distance between two world-space points. */
|
|
74
|
+
export function calculateDistance(x1: number, y1: number, x2: number, y2: number): number {
|
|
75
|
+
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
|
|
76
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// ─── .visualli → FlatNode Converter ──────────────────────────────────────────
|
|
2
|
+
//
|
|
3
|
+
// Transforms the layer-based .visualli format into the flat Map<id, FlatNode>
|
|
4
|
+
// structure consumed by the canvas renderer.
|
|
5
|
+
|
|
6
|
+
import type { VisualliDocument } from '../types/document.js';
|
|
7
|
+
import type { VisualliLayer } from '../types/layer.js';
|
|
8
|
+
import type { FlatNode, NodeMap } from '../types/mindmap.js';
|
|
9
|
+
import { countLayersBeneath } from './visualliParser.js';
|
|
10
|
+
import { applyCircularLayout, calculateOptimalRadiusPercentage } from '../layout/circularLayout.js';
|
|
11
|
+
import { applyLinearHorizontalLayout, applyLinearVerticalLayout } from '../layout/linearLayout.js';
|
|
12
|
+
|
|
13
|
+
// ── Overlap Resolution ────────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
function doNodesOverlap(n1: FlatNode, n2: FlatNode, padding = 20): boolean {
|
|
16
|
+
return !(
|
|
17
|
+
n1.x + n1.width / 2 + padding < n2.x - n2.width / 2 - padding ||
|
|
18
|
+
n1.x - n1.width / 2 - padding > n2.x + n2.width / 2 + padding ||
|
|
19
|
+
n1.y + n1.height / 2 + padding < n2.y - n2.height / 2 - padding ||
|
|
20
|
+
n1.y - n1.height / 2 - padding > n2.y + n2.height / 2 + padding
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Iteratively push overlapping nodes apart using symmetric force separation. */
|
|
25
|
+
export function resolveNodeOverlaps(nodes: FlatNode[], maxIterations = 10): void {
|
|
26
|
+
const padding = 150;
|
|
27
|
+
|
|
28
|
+
for (let iter = 0; iter < maxIterations; iter++) {
|
|
29
|
+
let any = false;
|
|
30
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
31
|
+
for (let j = i + 1; j < nodes.length; j++) {
|
|
32
|
+
const n1 = nodes[i];
|
|
33
|
+
const n2 = nodes[j];
|
|
34
|
+
if (!doNodesOverlap(n1, n2, padding)) continue;
|
|
35
|
+
|
|
36
|
+
any = true;
|
|
37
|
+
const dx = n2.x - n1.x;
|
|
38
|
+
const dy = n2.y - n1.y;
|
|
39
|
+
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
40
|
+
|
|
41
|
+
if (dist < 1) {
|
|
42
|
+
n2.x += n2.width / 2 + padding;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const minDist = (n1.width + n2.width) / 2 + padding;
|
|
47
|
+
if (dist < minDist) {
|
|
48
|
+
const push = (minDist - dist) / 2;
|
|
49
|
+
n1.x -= (dx / dist) * push;
|
|
50
|
+
n1.y -= (dy / dist) * push;
|
|
51
|
+
n2.x += (dx / dist) * push;
|
|
52
|
+
n2.y += (dy / dist) * push;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (!any) break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ── Color Helpers ─────────────────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
const COLOR_PALETTE = [
|
|
63
|
+
'#12C7D3', '#325E8C', '#8A70A6', '#F54A57', '#FF6C4D',
|
|
64
|
+
'#F28C16', '#FFD347', '#12C7D3', '#7F7F7F', '#8D8D8D', '#12C7D3',
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get a deterministic random color from palette based on node ID
|
|
69
|
+
*/
|
|
70
|
+
function getRandomColorForNode(nodeId: string): string {
|
|
71
|
+
// Simple hash function to convert string to number
|
|
72
|
+
let hash = 0;
|
|
73
|
+
for (let i = 0; i < nodeId.length; i++) {
|
|
74
|
+
hash = nodeId.charCodeAt(i) + ((hash << 5) - hash);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Use absolute value and modulo to get palette index
|
|
78
|
+
const index = Math.abs(hash) % COLOR_PALETTE.length;
|
|
79
|
+
return COLOR_PALETTE[index];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ── Container-formation Layout ────────────────────────────────────────────────
|
|
83
|
+
|
|
84
|
+
interface Proxy { id: string; x: number; y: number; width: number; height: number }
|
|
85
|
+
|
|
86
|
+
function applyFormation(nodes: FlatNode[], formation: string): void {
|
|
87
|
+
if (nodes.length === 0) return;
|
|
88
|
+
if (nodes.length === 1) { nodes[0].x = 0; nodes[0].y = 0; return; }
|
|
89
|
+
if (formation === 'linear-horizontal' || formation === 'linear') {
|
|
90
|
+
applyLinearHorizontalLayout(nodes, { centerX: 0, centerY: 0 });
|
|
91
|
+
} else if (formation === 'linear-vertical') {
|
|
92
|
+
applyLinearVerticalLayout(nodes, { centerX: 0, centerY: 0 });
|
|
93
|
+
} else {
|
|
94
|
+
const r = Math.max(250, nodes.length * 70);
|
|
95
|
+
const step = (2 * Math.PI) / nodes.length;
|
|
96
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
97
|
+
nodes[i].x = r * Math.cos(i * step);
|
|
98
|
+
nodes[i].y = r * Math.sin(i * step);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function containerBBox(nodes: FlatNode[], padding: number): { width: number; height: number } {
|
|
104
|
+
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
|
|
105
|
+
for (const n of nodes) {
|
|
106
|
+
minX = Math.min(minX, n.x - n.width / 2);
|
|
107
|
+
maxX = Math.max(maxX, n.x + n.width / 2);
|
|
108
|
+
minY = Math.min(minY, n.y - n.height / 2);
|
|
109
|
+
maxY = Math.max(maxY, n.y + n.height / 2);
|
|
110
|
+
}
|
|
111
|
+
return { width: maxX - minX + padding * 2, height: maxY - minY + padding * 2 };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function proxyLinearH(proxies: Proxy[], gap = 180): void {
|
|
115
|
+
const total = proxies.reduce((s, p) => s + p.width, 0) + gap * (proxies.length - 1);
|
|
116
|
+
let x = -total / 2;
|
|
117
|
+
for (const p of proxies) { p.x = x + p.width / 2; p.y = 0; x += p.width + gap; }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function proxyLinearV(proxies: Proxy[], gap = 180): void {
|
|
121
|
+
const total = proxies.reduce((s, p) => s + p.height, 0) + gap * (proxies.length - 1);
|
|
122
|
+
let y = -total / 2;
|
|
123
|
+
for (const p of proxies) { p.x = 0; p.y = y + p.height / 2; y += p.height + gap; }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function proxyRadial(proxies: Proxy[], radius = 500): void {
|
|
127
|
+
if (proxies.length === 1) { proxies[0].x = 0; proxies[0].y = 0; return; }
|
|
128
|
+
// Start at top (12 o'clock) for n>=3, horizontal for n=2 (matches circular layout)
|
|
129
|
+
const startAngle = proxies.length === 2 ? 0 : -Math.PI / 2;
|
|
130
|
+
const step = (2 * Math.PI) / proxies.length;
|
|
131
|
+
for (let i = 0; i < proxies.length; i++) {
|
|
132
|
+
const angle = startAngle + i * step;
|
|
133
|
+
proxies[i].x = radius * Math.cos(angle);
|
|
134
|
+
proxies[i].y = radius * Math.sin(angle);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ── Layer Conversion ──────────────────────────────────────────────────────────
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Calculate dynamic node width based on title length.
|
|
142
|
+
* Formula matches visualli.ai: max(200, title.length * 8 + 40)
|
|
143
|
+
*/
|
|
144
|
+
function calculateNodeWidth(title: string): number {
|
|
145
|
+
return Math.max(200, title.length * 8 + 40);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function makeFlatNode(
|
|
149
|
+
nodeId: string,
|
|
150
|
+
label: string,
|
|
151
|
+
summary: string,
|
|
152
|
+
layer: VisualliLayer,
|
|
153
|
+
color: string,
|
|
154
|
+
branchCount: number,
|
|
155
|
+
x = 0,
|
|
156
|
+
y = 0,
|
|
157
|
+
): FlatNode {
|
|
158
|
+
return {
|
|
159
|
+
id: nodeId,
|
|
160
|
+
parentId: layer.parentNodeId ?? null,
|
|
161
|
+
x,
|
|
162
|
+
y,
|
|
163
|
+
level: layer.level,
|
|
164
|
+
title: label,
|
|
165
|
+
description: summary,
|
|
166
|
+
color,
|
|
167
|
+
width: calculateNodeWidth(label),
|
|
168
|
+
height: 80,
|
|
169
|
+
isExpanded: branchCount > 0,
|
|
170
|
+
branchCount,
|
|
171
|
+
createdAt: new Date(),
|
|
172
|
+
updatedAt: new Date(),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function convertLayerWithContainers(
|
|
177
|
+
layer: VisualliLayer,
|
|
178
|
+
doc: VisualliDocument,
|
|
179
|
+
): FlatNode[] {
|
|
180
|
+
const PADDING = 80;
|
|
181
|
+
const flatNodes: FlatNode[] = [];
|
|
182
|
+
|
|
183
|
+
for (const node of layer.nodes) {
|
|
184
|
+
const label = Array.isArray(node.data.label)
|
|
185
|
+
? (node.data.label as string[]).join(' ')
|
|
186
|
+
: (node.data.label || 'Untitled');
|
|
187
|
+
const branchCount = countLayersBeneath(doc, node.id);
|
|
188
|
+
flatNodes.push(
|
|
189
|
+
makeFlatNode(
|
|
190
|
+
node.id, label, node.data.summary || '', layer,
|
|
191
|
+
node.data.color || getRandomColorForNode(node.id), branchCount,
|
|
192
|
+
),
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const byId = new Map(flatNodes.map(n => [n.id, n]));
|
|
197
|
+
const containerNodeIds = new Set<string>();
|
|
198
|
+
const containerGroups = new Map<string, FlatNode[]>();
|
|
199
|
+
|
|
200
|
+
for (const container of layer.containers) {
|
|
201
|
+
const group = container.nodes.map(id => byId.get(id)).filter((n): n is FlatNode => !!n);
|
|
202
|
+
if (!group.length) continue;
|
|
203
|
+
container.nodes.forEach(id => containerNodeIds.add(id));
|
|
204
|
+
const formation = ((container.data as unknown as Record<string, unknown> | undefined)?.['formation'] as string | undefined) ?? 'radial';
|
|
205
|
+
applyFormation(group, formation);
|
|
206
|
+
containerGroups.set(container.id, group);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const proxies: Proxy[] = [];
|
|
210
|
+
for (const node of layer.nodes) {
|
|
211
|
+
if (!containerNodeIds.has(node.id)) {
|
|
212
|
+
const nodeWidth = calculateNodeWidth(node.data.label);
|
|
213
|
+
proxies.push({ id: node.id, x: 0, y: 0, width: nodeWidth, height: 80 });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
for (const container of layer.containers) {
|
|
217
|
+
const group = containerGroups.get(container.id);
|
|
218
|
+
if (!group?.length) continue;
|
|
219
|
+
const bbox = containerBBox(group, PADDING);
|
|
220
|
+
proxies.push({ id: `__c__${container.id}`, x: 0, y: 0, width: bbox.width, height: bbox.height });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const layout = layer.layout || 'radial';
|
|
224
|
+
if (layout === 'linear-horizontal') proxyLinearH(proxies, 180);
|
|
225
|
+
else if (layout === 'linear-vertical') proxyLinearV(proxies, 180);
|
|
226
|
+
else proxyRadial(proxies, 500);
|
|
227
|
+
|
|
228
|
+
for (const proxy of proxies) {
|
|
229
|
+
if (proxy.id.startsWith('__c__')) {
|
|
230
|
+
const group = containerGroups.get(proxy.id.slice(5));
|
|
231
|
+
group?.forEach(n => { n.x += proxy.x; n.y += proxy.y; });
|
|
232
|
+
} else {
|
|
233
|
+
const fn = byId.get(proxy.id);
|
|
234
|
+
if (fn) { fn.x = proxy.x; fn.y = proxy.y; }
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
resolveNodeOverlaps(flatNodes);
|
|
239
|
+
|
|
240
|
+
return flatNodes;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Convert a single layer to an array of FlatNode objects.
|
|
245
|
+
* Layout algorithm is determined by `layer.layout` (default: radial).
|
|
246
|
+
*/
|
|
247
|
+
export function convertLayerToFlatNodes(
|
|
248
|
+
layer: VisualliLayer,
|
|
249
|
+
doc: VisualliDocument,
|
|
250
|
+
): FlatNode[] {
|
|
251
|
+
if ((layer.containers ?? []).length > 0) {
|
|
252
|
+
return convertLayerWithContainers(layer, doc);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const flatNodes: FlatNode[] = [];
|
|
256
|
+
|
|
257
|
+
for (const node of layer.nodes) {
|
|
258
|
+
const label = Array.isArray(node.data.label)
|
|
259
|
+
? (node.data.label as string[]).join(' ')
|
|
260
|
+
: (node.data.label || 'Untitled');
|
|
261
|
+
const branchCount = countLayersBeneath(doc, node.id);
|
|
262
|
+
flatNodes.push(
|
|
263
|
+
makeFlatNode(
|
|
264
|
+
node.id, label, node.data.summary || '', layer,
|
|
265
|
+
node.data.color || getRandomColorForNode(node.id), branchCount,
|
|
266
|
+
0, 0,
|
|
267
|
+
),
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (flatNodes.length > 0) {
|
|
272
|
+
const layout = layer.layout || 'radial';
|
|
273
|
+
if (layout === 'linear-horizontal') {
|
|
274
|
+
applyLinearHorizontalLayout(flatNodes, { centerX: 0, centerY: 0 });
|
|
275
|
+
} else if (layout === 'linear-vertical') {
|
|
276
|
+
applyLinearVerticalLayout(flatNodes, { centerX: 0, centerY: 0 });
|
|
277
|
+
} else {
|
|
278
|
+
const pct = calculateOptimalRadiusPercentage(flatNodes.length, 2000, 2000, 200, 30);
|
|
279
|
+
applyCircularLayout(flatNodes, {
|
|
280
|
+
radiusPercentage: pct,
|
|
281
|
+
containerWidth: 2000,
|
|
282
|
+
containerHeight: 2000,
|
|
283
|
+
centerX: 0,
|
|
284
|
+
centerY: 0,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
resolveNodeOverlaps(flatNodes);
|
|
290
|
+
|
|
291
|
+
return flatNodes;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Convert an entire .visualli document to a flat NodeMap.
|
|
296
|
+
* All layers are converted and a children-index is built for O(1) tree traversal.
|
|
297
|
+
*/
|
|
298
|
+
export function convertVisualliToFlatNodes(doc: VisualliDocument): NodeMap {
|
|
299
|
+
const map: NodeMap = new Map();
|
|
300
|
+
|
|
301
|
+
for (const layer of doc.layers.values()) {
|
|
302
|
+
for (const node of convertLayerToFlatNodes(layer, doc)) {
|
|
303
|
+
map.set(node.id, node);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Build children index
|
|
308
|
+
for (const node of map.values()) {
|
|
309
|
+
node.childrenIds = [];
|
|
310
|
+
for (const candidate of map.values()) {
|
|
311
|
+
if (candidate.parentId === node.id) node.childrenIds.push(candidate.id);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return map;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Get flat nodes for a specific layer only.
|
|
320
|
+
*
|
|
321
|
+
* @param doc - Parsed .visualli document
|
|
322
|
+
* @param layerId - Layer ID to extract
|
|
323
|
+
* @returns FlatNode array, or empty array if layer not found
|
|
324
|
+
*/
|
|
325
|
+
export function getNodesForLayer(doc: VisualliDocument, layerId: string): FlatNode[] {
|
|
326
|
+
const layer = doc.layers.get(layerId);
|
|
327
|
+
return layer ? convertLayerToFlatNodes(layer, doc) : [];
|
|
328
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// ─── .visualli File Parser ────────────────────────────────────────────────────
|
|
2
|
+
//
|
|
3
|
+
// Reads JSONL (JSON Lines) format and parses into a structured VisualliDocument.
|
|
4
|
+
|
|
5
|
+
import type { VisualliDocument } from '../types/document.js';
|
|
6
|
+
import type { VisualliMeta } from '../types/meta.js';
|
|
7
|
+
import type { VisualliLayer } from '../types/layer.js';
|
|
8
|
+
import type { Layer } from '../types/schema.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Convert schema Layer to VisualliLayer by ensuring required arrays exist.
|
|
12
|
+
*/
|
|
13
|
+
function toVisualliLayer(schemaLayer: Layer): VisualliLayer {
|
|
14
|
+
return {
|
|
15
|
+
type: 'layer',
|
|
16
|
+
id: schemaLayer.id,
|
|
17
|
+
level: schemaLayer.level,
|
|
18
|
+
parentLayerId: schemaLayer.parentLayerId,
|
|
19
|
+
parentNodeId: schemaLayer.parentNodeId,
|
|
20
|
+
layout: schemaLayer.layout,
|
|
21
|
+
nodes: schemaLayer.nodes ?? [],
|
|
22
|
+
connections: schemaLayer.connections ?? [],
|
|
23
|
+
containers: schemaLayer.containers ?? [],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class VisualliParseError extends Error {
|
|
28
|
+
constructor(message: string, public readonly lineNumber?: number) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = 'VisualliParseError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Parse a .visualli file content (JSONL format) into a VisualliDocument.
|
|
36
|
+
*
|
|
37
|
+
* @throws VisualliParseError on malformed input
|
|
38
|
+
*/
|
|
39
|
+
export function parseVisualliFile(content: string): VisualliDocument {
|
|
40
|
+
const lines = content.trim().split('\n');
|
|
41
|
+
|
|
42
|
+
const doc: VisualliDocument = {
|
|
43
|
+
meta: null as unknown as VisualliMeta,
|
|
44
|
+
layers: new Map(),
|
|
45
|
+
layersByLevel: new Map(),
|
|
46
|
+
rootLayer: null,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
let lineNumber = 0;
|
|
50
|
+
|
|
51
|
+
for (const line of lines) {
|
|
52
|
+
lineNumber++;
|
|
53
|
+
if (!line.trim()) continue;
|
|
54
|
+
|
|
55
|
+
let obj: Record<string, unknown>;
|
|
56
|
+
try {
|
|
57
|
+
obj = JSON.parse(line) as Record<string, unknown>;
|
|
58
|
+
} catch (err) {
|
|
59
|
+
throw new VisualliParseError(
|
|
60
|
+
`Failed to parse JSON at line ${lineNumber}: ${err}`,
|
|
61
|
+
lineNumber,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!obj['type']) {
|
|
66
|
+
throw new VisualliParseError(
|
|
67
|
+
`Missing 'type' field at line ${lineNumber}`,
|
|
68
|
+
lineNumber,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
switch (obj['type']) {
|
|
73
|
+
case 'meta':
|
|
74
|
+
doc.meta = obj as unknown as VisualliMeta;
|
|
75
|
+
break;
|
|
76
|
+
|
|
77
|
+
case 'layer': {
|
|
78
|
+
// Convert schema Layer to VisualliLayer
|
|
79
|
+
const schemaLayer = obj as unknown as Layer;
|
|
80
|
+
const layer = toVisualliLayer(schemaLayer);
|
|
81
|
+
|
|
82
|
+
doc.layers.set(layer.id, layer);
|
|
83
|
+
|
|
84
|
+
if (!doc.layersByLevel.has(layer.level)) {
|
|
85
|
+
doc.layersByLevel.set(layer.level, []);
|
|
86
|
+
}
|
|
87
|
+
doc.layersByLevel.get(layer.level)!.push(layer);
|
|
88
|
+
|
|
89
|
+
if (layer.level === 0) {
|
|
90
|
+
doc.rootLayer = layer;
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
default:
|
|
96
|
+
// Unknown type — silently skip
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!doc.meta) throw new VisualliParseError('Missing required meta section');
|
|
102
|
+
if (!doc.rootLayer) throw new VisualliParseError('Missing root layer (level 0)');
|
|
103
|
+
|
|
104
|
+
return doc;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Fetch and parse a .visualli file from a URL.
|
|
109
|
+
*
|
|
110
|
+
* @throws VisualliParseError | Error
|
|
111
|
+
*/
|
|
112
|
+
export async function loadVisualliFile(url: string): Promise<VisualliDocument> {
|
|
113
|
+
const response = await fetch(url);
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
116
|
+
}
|
|
117
|
+
return parseVisualliFile(await response.text());
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Parse .visualli JSONL data as returned from the MongoDB `visualli` field.
|
|
122
|
+
*
|
|
123
|
+
* @throws VisualliParseError
|
|
124
|
+
*/
|
|
125
|
+
export function parseVisualliFromMongoDB(visualliJsonl: string): VisualliDocument {
|
|
126
|
+
if (!visualliJsonl || typeof visualliJsonl !== 'string') {
|
|
127
|
+
throw new VisualliParseError('Invalid visualli data: expected a JSONL string');
|
|
128
|
+
}
|
|
129
|
+
const trimmed = visualliJsonl.trim();
|
|
130
|
+
if (!trimmed) throw new VisualliParseError('Empty visualli data from MongoDB');
|
|
131
|
+
return parseVisualliFile(trimmed);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Parse a .visualli file from a browser `File` object.
|
|
136
|
+
* (Browser-only – requires `FileReader`.)
|
|
137
|
+
*/
|
|
138
|
+
export function loadVisualliFileFromFile(file: File): Promise<VisualliDocument> {
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
const reader = new FileReader();
|
|
141
|
+
reader.onload = e => {
|
|
142
|
+
try {
|
|
143
|
+
resolve(parseVisualliFile(e.target?.result as string));
|
|
144
|
+
} catch (err) {
|
|
145
|
+
reject(err);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
reader.onerror = () => reject(new Error('Failed to read file'));
|
|
149
|
+
reader.readAsText(file);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ── Tree Helpers ──────────────────────────────────────────────────────────────
|
|
154
|
+
|
|
155
|
+
/** Return all layers whose `parentNodeId` matches the given node ID. */
|
|
156
|
+
export function getChildLayers(
|
|
157
|
+
doc: VisualliDocument,
|
|
158
|
+
nodeId: string,
|
|
159
|
+
): VisualliLayer[] {
|
|
160
|
+
const result: VisualliLayer[] = [];
|
|
161
|
+
for (const layer of doc.layers.values()) {
|
|
162
|
+
if (layer.parentNodeId === nodeId) result.push(layer);
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Count the maximum depth of layers beneath a node (its "magnitude").
|
|
169
|
+
* Returns 0 when the node has no child layers.
|
|
170
|
+
*/
|
|
171
|
+
export function countLayersBeneath(
|
|
172
|
+
doc: VisualliDocument,
|
|
173
|
+
nodeId: string,
|
|
174
|
+
visited = new Set<string>(),
|
|
175
|
+
): number {
|
|
176
|
+
if (visited.has(nodeId)) return 0;
|
|
177
|
+
visited.add(nodeId);
|
|
178
|
+
|
|
179
|
+
const children = getChildLayers(doc, nodeId);
|
|
180
|
+
if (children.length === 0) return 0;
|
|
181
|
+
|
|
182
|
+
let maxDepth = 0;
|
|
183
|
+
for (const layer of children) {
|
|
184
|
+
let depth = 1;
|
|
185
|
+
for (const node of layer.nodes) {
|
|
186
|
+
depth = Math.max(depth, 1 + countLayersBeneath(doc, node.id, visited));
|
|
187
|
+
}
|
|
188
|
+
maxDepth = Math.max(maxDepth, depth);
|
|
189
|
+
}
|
|
190
|
+
return maxDepth;
|
|
191
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './performanceMonitor.js';
|