@diagrammo/dgmo 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/.claude/skills/dgmo-chart/SKILL.md +28 -0
  2. package/.claude/skills/dgmo-generate/SKILL.md +1 -0
  3. package/.claude/skills/dgmo-sequence/SKILL.md +24 -1
  4. package/.cursorrules +27 -2
  5. package/.github/copilot-instructions.md +36 -3
  6. package/.windsurfrules +27 -2
  7. package/README.md +12 -3
  8. package/dist/cli.cjs +197 -154
  9. package/dist/index.cjs +8647 -3447
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +503 -58
  12. package/dist/index.d.ts +503 -58
  13. package/dist/index.js +8379 -3200
  14. package/dist/index.js.map +1 -1
  15. package/docs/ai-integration.md +1 -1
  16. package/docs/language-reference.md +336 -17
  17. package/docs/migration-sequence-color-to-tags.md +98 -0
  18. package/package.json +1 -1
  19. package/src/c4/renderer.ts +1 -20
  20. package/src/class/renderer.ts +1 -11
  21. package/src/cli.ts +40 -0
  22. package/src/d3.ts +92 -2
  23. package/src/dgmo-router.ts +11 -0
  24. package/src/echarts.ts +74 -8
  25. package/src/er/parser.ts +29 -3
  26. package/src/er/renderer.ts +1 -15
  27. package/src/graph/flowchart-parser.ts +7 -30
  28. package/src/graph/flowchart-renderer.ts +62 -69
  29. package/src/graph/layout.ts +5 -0
  30. package/src/graph/state-parser.ts +388 -0
  31. package/src/graph/state-renderer.ts +496 -0
  32. package/src/graph/types.ts +4 -2
  33. package/src/index.ts +42 -1
  34. package/src/infra/compute.ts +1113 -0
  35. package/src/infra/layout.ts +578 -0
  36. package/src/infra/parser.ts +559 -0
  37. package/src/infra/renderer.ts +1553 -0
  38. package/src/infra/roles.ts +60 -0
  39. package/src/infra/serialize.ts +67 -0
  40. package/src/infra/types.ts +221 -0
  41. package/src/infra/validation.ts +192 -0
  42. package/src/initiative-status/layout.ts +56 -61
  43. package/src/initiative-status/renderer.ts +13 -13
  44. package/src/kanban/renderer.ts +1 -24
  45. package/src/org/layout.ts +28 -37
  46. package/src/org/parser.ts +16 -1
  47. package/src/org/renderer.ts +159 -121
  48. package/src/org/resolver.ts +90 -23
  49. package/src/palettes/color-utils.ts +30 -0
  50. package/src/render.ts +2 -0
  51. package/src/sequence/parser.ts +202 -42
  52. package/src/sequence/renderer.ts +576 -113
  53. package/src/sequence/tag-resolution.ts +163 -0
  54. package/src/sharing.ts +8 -0
  55. package/src/sitemap/collapse.ts +187 -0
  56. package/src/sitemap/layout.ts +738 -0
  57. package/src/sitemap/parser.ts +489 -0
  58. package/src/sitemap/renderer.ts +774 -0
  59. package/src/sitemap/types.ts +42 -0
  60. package/src/utils/tag-groups.ts +119 -0
@@ -0,0 +1,738 @@
1
+ // ============================================================
2
+ // Sitemap Diagram Layout Engine (Dagre flat graph)
3
+ // ============================================================
4
+
5
+ import dagre from '@dagrejs/dagre';
6
+ import type { ParsedSitemap, SitemapNode, SitemapEdge } from './types';
7
+ import type { TagGroup } from '../utils/tag-groups';
8
+ import { resolveTagColor, injectDefaultTagMetadata } from '../utils/tag-groups';
9
+
10
+ // ============================================================
11
+ // Types
12
+ // ============================================================
13
+
14
+ export interface SitemapLayoutNode {
15
+ id: string;
16
+ label: string;
17
+ metadata: Record<string, string>;
18
+ /** Original (unfiltered) metadata for tag-based coloring and hover dimming */
19
+ tagMetadata: Record<string, string>;
20
+ isContainer: boolean;
21
+ lineNumber: number;
22
+ color?: string;
23
+ x: number;
24
+ y: number;
25
+ width: number;
26
+ height: number;
27
+ /** Count of hidden descendants when collapsed */
28
+ hiddenCount?: number;
29
+ /** True if node has children (expanded or collapsed) — drives toggle UI */
30
+ hasChildren?: boolean;
31
+ }
32
+
33
+ export interface SitemapLayoutEdge {
34
+ sourceId: string;
35
+ targetId: string;
36
+ points: { x: number; y: number }[];
37
+ label?: string;
38
+ color?: string;
39
+ lineNumber: number;
40
+ }
41
+
42
+ export interface SitemapContainerBounds {
43
+ nodeId: string;
44
+ label: string;
45
+ lineNumber: number;
46
+ color?: string;
47
+ metadata: Record<string, string>;
48
+ /** Original (unfiltered) metadata for tag-based coloring and hover dimming */
49
+ tagMetadata: Record<string, string>;
50
+ x: number;
51
+ y: number;
52
+ width: number;
53
+ height: number;
54
+ labelHeight: number;
55
+ /** Count of hidden descendants when collapsed */
56
+ hiddenCount?: number;
57
+ /** True if container has children (expanded or collapsed) */
58
+ hasChildren?: boolean;
59
+ }
60
+
61
+ export interface SitemapLegendEntry {
62
+ value: string;
63
+ color: string;
64
+ }
65
+
66
+ export interface SitemapLegendGroup {
67
+ name: string;
68
+ alias?: string;
69
+ entries: SitemapLegendEntry[];
70
+ x: number;
71
+ y: number;
72
+ width: number;
73
+ height: number;
74
+ minifiedWidth: number;
75
+ minifiedHeight: number;
76
+ }
77
+
78
+ export interface SitemapLayoutResult {
79
+ nodes: SitemapLayoutNode[];
80
+ edges: SitemapLayoutEdge[];
81
+ containers: SitemapContainerBounds[];
82
+ legend: SitemapLegendGroup[];
83
+ width: number;
84
+ height: number;
85
+ }
86
+
87
+ // ============================================================
88
+ // Constants
89
+ // ============================================================
90
+
91
+ const CHAR_WIDTH = 7.5;
92
+ const LABEL_FONT_SIZE = 13;
93
+ const META_FONT_SIZE = 11;
94
+ const META_LINE_HEIGHT = 16;
95
+ const HEADER_HEIGHT = 28;
96
+ const SEPARATOR_GAP = 6;
97
+ const CARD_H_PAD = 20;
98
+ const CARD_V_PAD = 10;
99
+ const MIN_CARD_WIDTH = 140;
100
+ const MARGIN = 40;
101
+ const CONTAINER_PAD_X = 24;
102
+ const CONTAINER_PAD_TOP = 40;
103
+ const CONTAINER_PAD_BOTTOM = 24;
104
+ const CONTAINER_LABEL_HEIGHT = 28;
105
+ const CONTAINER_META_LINE_HEIGHT = 16;
106
+
107
+ // Legend (kanban-style pills)
108
+ const LEGEND_GAP = 30;
109
+ const LEGEND_HEIGHT = 28;
110
+ const LEGEND_PILL_PAD = 16;
111
+ const LEGEND_PILL_FONT_W = 11 * 0.6;
112
+ const LEGEND_CAPSULE_PAD = 4;
113
+ const LEGEND_DOT_R = 4;
114
+ const LEGEND_ENTRY_FONT_W = 10 * 0.6;
115
+ const LEGEND_ENTRY_DOT_GAP = 4;
116
+ const LEGEND_ENTRY_TRAIL = 8;
117
+ const LEGEND_GROUP_GAP = 12;
118
+ const LEGEND_EYE_SIZE = 14;
119
+ const LEGEND_EYE_GAP = 6;
120
+
121
+ // ============================================================
122
+ // Helpers
123
+ // ============================================================
124
+
125
+ function filterMetadata(
126
+ metadata: Record<string, string>,
127
+ hiddenAttributes?: Set<string>,
128
+ ): Record<string, string> {
129
+ if (!hiddenAttributes || hiddenAttributes.size === 0) return metadata;
130
+ const filtered: Record<string, string> = {};
131
+ for (const [key, value] of Object.entries(metadata)) {
132
+ if (!hiddenAttributes.has(key)) {
133
+ filtered[key] = value;
134
+ }
135
+ }
136
+ return filtered;
137
+ }
138
+
139
+ function computeCardWidth(label: string, meta: Record<string, string>): number {
140
+ let maxChars = label.length;
141
+ for (const [key, value] of Object.entries(meta)) {
142
+ const lineChars = key.length + 2 + value.length;
143
+ if (lineChars > maxChars) maxChars = lineChars;
144
+ }
145
+ return Math.max(MIN_CARD_WIDTH, Math.ceil(maxChars * CHAR_WIDTH) + CARD_H_PAD * 2);
146
+ }
147
+
148
+ function computeCardHeight(meta: Record<string, string>): number {
149
+ const metaCount = Object.keys(meta).length;
150
+ if (metaCount === 0) return HEADER_HEIGHT + CARD_V_PAD;
151
+ return HEADER_HEIGHT + SEPARATOR_GAP + metaCount * META_LINE_HEIGHT + CARD_V_PAD;
152
+ }
153
+
154
+ function resolveNodeColor(
155
+ node: SitemapNode,
156
+ tagGroups: TagGroup[],
157
+ activeGroupName: string | null,
158
+ ): string | undefined {
159
+ if (node.color) return node.color;
160
+ return resolveTagColor(node.metadata, tagGroups, activeGroupName, node.isContainer);
161
+ }
162
+
163
+ const OVERLAP_GAP = 20;
164
+
165
+ function countDescendantNodes(node: SitemapNode, hiddenCounts?: Map<string, number>): number {
166
+ let count = 0;
167
+ for (const child of node.children) {
168
+ count += (child.isContainer ? 0 : 1) + countDescendantNodes(child, hiddenCounts);
169
+ const hc = hiddenCounts?.get(child.id);
170
+ if (hc) count += hc;
171
+ }
172
+ return count;
173
+ }
174
+
175
+ // ============================================================
176
+ // Legend
177
+ // ============================================================
178
+
179
+ function computeLegendGroups(
180
+ tagGroups: TagGroup[],
181
+ usedValuesByGroup?: Map<string, Set<string>>,
182
+ ): SitemapLegendGroup[] {
183
+ const groups: SitemapLegendGroup[] = [];
184
+
185
+ for (const group of tagGroups) {
186
+ if (group.entries.length === 0) continue;
187
+
188
+ const usedValues = usedValuesByGroup?.get(group.name.toLowerCase());
189
+ const visibleEntries = usedValues
190
+ ? group.entries.filter((e) => usedValues.has(e.value.toLowerCase()))
191
+ : group.entries;
192
+ if (visibleEntries.length === 0) continue;
193
+
194
+ const pillWidth = group.name.length * LEGEND_PILL_FONT_W + LEGEND_PILL_PAD;
195
+ const minPillWidth = pillWidth;
196
+
197
+ let entriesWidth = 0;
198
+ for (const entry of visibleEntries) {
199
+ entriesWidth +=
200
+ LEGEND_DOT_R * 2 +
201
+ LEGEND_ENTRY_DOT_GAP +
202
+ entry.value.length * LEGEND_ENTRY_FONT_W +
203
+ LEGEND_ENTRY_TRAIL;
204
+ }
205
+ const eyeSpace = LEGEND_EYE_SIZE + LEGEND_EYE_GAP;
206
+ const capsuleWidth = LEGEND_CAPSULE_PAD * 2 + pillWidth + 4 + eyeSpace + entriesWidth;
207
+
208
+ groups.push({
209
+ name: group.name,
210
+ alias: group.alias,
211
+ entries: visibleEntries.map((e) => ({ value: e.value, color: e.color })),
212
+ x: 0,
213
+ y: 0,
214
+ width: capsuleWidth,
215
+ height: LEGEND_HEIGHT,
216
+ minifiedWidth: minPillWidth,
217
+ minifiedHeight: LEGEND_HEIGHT,
218
+ });
219
+ }
220
+
221
+ return groups;
222
+ }
223
+
224
+ // ============================================================
225
+ // Flatten tree into page-node and container lists
226
+ // ============================================================
227
+
228
+ interface FlatNode {
229
+ sitemapNode: SitemapNode;
230
+ parentContainerId: string | null;
231
+ /** Nearest ancestor that is a page (not container) — used for invisible hierarchy edges */
232
+ parentPageId: string | null;
233
+ meta: Record<string, string>;
234
+ /** Original (unfiltered) metadata — used for tag coloring/hover even when hidden */
235
+ fullMeta: Record<string, string>;
236
+ width: number;
237
+ height: number;
238
+ }
239
+
240
+ function flattenNodes(
241
+ nodes: SitemapNode[],
242
+ parentContainerId: string | null,
243
+ parentPageId: string | null,
244
+ hiddenCounts: Map<string, number> | undefined,
245
+ hiddenAttributes: Set<string> | undefined,
246
+ result: FlatNode[],
247
+ ): void {
248
+ for (const node of nodes) {
249
+ const meta = filterMetadata(node.metadata, hiddenAttributes);
250
+ if (node.isContainer) {
251
+ // Container gets added as a flat entry (not added to dagre — bounds computed post-hoc)
252
+ const metaCount = Object.keys(meta).length;
253
+ const labelHeight = CONTAINER_LABEL_HEIGHT + metaCount * CONTAINER_META_LINE_HEIGHT;
254
+ result.push({
255
+ sitemapNode: node,
256
+ parentContainerId,
257
+ parentPageId,
258
+ meta,
259
+ fullMeta: { ...node.metadata },
260
+ width: Math.max(MIN_CARD_WIDTH, node.label.length * CHAR_WIDTH + CARD_H_PAD * 2),
261
+ height: labelHeight + CONTAINER_PAD_BOTTOM,
262
+ });
263
+ // Recurse into children — container becomes parent container, parentPageId stays the same
264
+ flattenNodes(node.children, node.id, parentPageId, hiddenCounts, hiddenAttributes, result);
265
+ } else {
266
+ result.push({
267
+ sitemapNode: node,
268
+ parentContainerId,
269
+ parentPageId,
270
+ meta,
271
+ fullMeta: { ...node.metadata },
272
+ width: computeCardWidth(node.label, meta),
273
+ height: computeCardHeight(meta),
274
+ });
275
+ // Pages can have children too (nested pages) — this page becomes the parentPageId
276
+ if (node.children.length > 0) {
277
+ flattenNodes(node.children, parentContainerId, node.id, hiddenCounts, hiddenAttributes, result);
278
+ }
279
+ }
280
+ }
281
+ }
282
+
283
+ // ============================================================
284
+ // Layout
285
+ // ============================================================
286
+
287
+ export function layoutSitemap(
288
+ parsed: ParsedSitemap,
289
+ hiddenCounts?: Map<string, number>,
290
+ activeTagGroup?: string | null,
291
+ hiddenAttributes?: Set<string>,
292
+ expandAllLegend?: boolean,
293
+ ): SitemapLayoutResult {
294
+ if (parsed.roots.length === 0) {
295
+ return { nodes: [], edges: [], containers: [], legend: [], width: 0, height: 0 };
296
+ }
297
+
298
+ // Inject default tag metadata
299
+ const allNodes: SitemapNode[] = [];
300
+ const collect = (node: SitemapNode) => {
301
+ allNodes.push(node);
302
+ for (const child of node.children) collect(child);
303
+ };
304
+ for (const root of parsed.roots) collect(root);
305
+ injectDefaultTagMetadata(allNodes, parsed.tagGroups, (e) => (e as SitemapNode).isContainer);
306
+
307
+ // Flatten hierarchy
308
+ const flatNodes: FlatNode[] = [];
309
+ flattenNodes(parsed.roots, null, null, hiddenCounts, hiddenAttributes, flatNodes);
310
+
311
+ // Build nodeMap for lookups
312
+ const nodeMap = new Map<string, FlatNode>();
313
+ for (const flat of flatNodes) {
314
+ nodeMap.set(flat.sitemapNode.id, flat);
315
+ }
316
+
317
+ // Build compound dagre graph — containers use setParent() for clean grouping.
318
+ // Collapsed containers (no children) are added as regular nodes.
319
+ // Multigraph: collapsed edges can produce multiple edges between the same pair
320
+ // (e.g. Dashboard→Account for both "settings" and "billing").
321
+ const g = new dagre.graphlib.Graph({ compound: true, multigraph: true });
322
+ g.setGraph({
323
+ rankdir: parsed.direction,
324
+ nodesep: 50,
325
+ ranksep: 60,
326
+ edgesep: 30,
327
+ marginx: MARGIN,
328
+ marginy: MARGIN,
329
+ });
330
+ g.setDefaultEdgeLabel(() => ({}));
331
+
332
+ const containerIds = new Set<string>();
333
+ const pageNodeIds = new Set<string>();
334
+ const collapsedContainerIds = new Set<string>();
335
+
336
+ // Identify containers vs pages, and detect collapsed (empty) containers
337
+ for (const flat of flatNodes) {
338
+ if (flat.sitemapNode.isContainer) {
339
+ containerIds.add(flat.sitemapNode.id);
340
+ // A container is "collapsed" if it has no children at all in the flat list
341
+ const hasAnyChild = flatNodes.some(
342
+ (f) => f.parentContainerId === flat.sitemapNode.id,
343
+ );
344
+ if (!hasAnyChild) {
345
+ collapsedContainerIds.add(flat.sitemapNode.id);
346
+ }
347
+ } else {
348
+ pageNodeIds.add(flat.sitemapNode.id);
349
+ }
350
+ }
351
+
352
+ // Add nodes to dagre
353
+ for (const flat of flatNodes) {
354
+ const node = flat.sitemapNode;
355
+ if (node.isContainer) {
356
+ if (collapsedContainerIds.has(node.id)) {
357
+ // Collapsed container — regular node with explicit dimensions
358
+ g.setNode(node.id, {
359
+ label: node.label,
360
+ width: flat.width,
361
+ height: flat.height,
362
+ });
363
+ } else {
364
+ // Regular container — compound node with padding for child layout
365
+ g.setNode(node.id, {
366
+ label: node.label,
367
+ paddingLeft: CONTAINER_PAD_X,
368
+ paddingRight: CONTAINER_PAD_X,
369
+ paddingTop: CONTAINER_PAD_TOP,
370
+ paddingBottom: CONTAINER_PAD_BOTTOM,
371
+ });
372
+ }
373
+ } else {
374
+ g.setNode(node.id, {
375
+ label: node.label,
376
+ width: flat.width,
377
+ height: flat.height,
378
+ });
379
+ }
380
+ }
381
+
382
+ // Set parent relationships — dagre compound nesting keeps nodes grouped
383
+ for (const flat of flatNodes) {
384
+ if (flat.parentContainerId && !collapsedContainerIds.has(flat.parentContainerId)) {
385
+ g.setParent(flat.sitemapNode.id, flat.parentContainerId);
386
+ }
387
+ }
388
+
389
+ // Add user edges (named for multigraph — each edge gets unique routing)
390
+ for (let i = 0; i < parsed.edges.length; i++) {
391
+ const edge = parsed.edges[i];
392
+ if (g.hasNode(edge.sourceId) && g.hasNode(edge.targetId)) {
393
+ g.setEdge(edge.sourceId, edge.targetId, {
394
+ label: edge.label ?? '',
395
+ minlen: 1,
396
+ }, `e${i}`);
397
+ }
398
+ }
399
+
400
+ // Run dagre layout
401
+ dagre.layout(g);
402
+
403
+ // Extract layout results — all positions from dagre
404
+ const layoutNodes: SitemapLayoutNode[] = [];
405
+ const layoutContainers: SitemapContainerBounds[] = [];
406
+
407
+ // Page nodes
408
+ for (const flat of flatNodes) {
409
+ const node = flat.sitemapNode;
410
+ if (node.isContainer) continue;
411
+ const pos = g.node(node.id);
412
+ if (!pos) continue;
413
+
414
+ const hc = hiddenCounts?.get(node.id);
415
+ layoutNodes.push({
416
+ id: node.id,
417
+ label: node.label,
418
+ metadata: flat.meta,
419
+ tagMetadata: flat.fullMeta,
420
+ isContainer: false,
421
+ lineNumber: node.lineNumber,
422
+ color: resolveNodeColor(node, parsed.tagGroups, activeTagGroup ?? null),
423
+ x: pos.x,
424
+ y: pos.y - pos.height / 2,
425
+ width: pos.width,
426
+ height: pos.height,
427
+ hiddenCount: hc,
428
+ hasChildren:
429
+ (node.children.length > 0 || (hc != null && hc > 0)) || undefined,
430
+ });
431
+ }
432
+
433
+ // Containers — bounds from dagre compound layout
434
+ for (const flat of flatNodes) {
435
+ const node = flat.sitemapNode;
436
+ if (!node.isContainer) continue;
437
+
438
+ const pos = g.node(node.id);
439
+ const hc = hiddenCounts?.get(node.id);
440
+ const metaCount = Object.keys(flat.meta).length;
441
+ const labelHeight = CONTAINER_LABEL_HEIGHT + metaCount * CONTAINER_META_LINE_HEIGHT;
442
+
443
+ if (pos) {
444
+ layoutContainers.push({
445
+ nodeId: node.id,
446
+ label: node.label,
447
+ lineNumber: node.lineNumber,
448
+ color: resolveNodeColor(node, parsed.tagGroups, activeTagGroup ?? null),
449
+ metadata: flat.meta,
450
+ tagMetadata: flat.fullMeta,
451
+ x: pos.x - pos.width / 2,
452
+ y: pos.y - pos.height / 2,
453
+ width: pos.width,
454
+ height: pos.height,
455
+ labelHeight,
456
+ hiddenCount: hc,
457
+ hasChildren:
458
+ (node.children.length > 0 || (hc != null && hc > 0)) || undefined,
459
+ });
460
+ } else {
461
+ // Fallback
462
+ layoutContainers.push({
463
+ nodeId: node.id,
464
+ label: node.label,
465
+ lineNumber: node.lineNumber,
466
+ color: resolveNodeColor(node, parsed.tagGroups, activeTagGroup ?? null),
467
+ metadata: flat.meta,
468
+ tagMetadata: flat.fullMeta,
469
+ x: MARGIN,
470
+ y: MARGIN,
471
+ width: flat.width,
472
+ height: labelHeight + CONTAINER_PAD_BOTTOM,
473
+ labelHeight,
474
+ hiddenCount: hc,
475
+ hasChildren:
476
+ (node.children.length > 0 || (hc != null && hc > 0)) || undefined,
477
+ });
478
+ }
479
+ }
480
+
481
+ // Edge waypoints from dagre (named edges for multigraph)
482
+ const layoutEdges: SitemapLayoutEdge[] = [];
483
+ for (let i = 0; i < parsed.edges.length; i++) {
484
+ const edge = parsed.edges[i];
485
+ if (!g.hasNode(edge.sourceId) || !g.hasNode(edge.targetId)) continue;
486
+ const edgeData = g.edge({ v: edge.sourceId, w: edge.targetId, name: `e${i}` });
487
+ if (!edgeData) continue;
488
+
489
+ layoutEdges.push({
490
+ sourceId: edge.sourceId,
491
+ targetId: edge.targetId,
492
+ points: edgeData.points ?? [],
493
+ label: edge.label,
494
+ color: edge.color,
495
+ lineNumber: edge.lineNumber,
496
+ });
497
+ }
498
+
499
+ // === Isolated subgraph separation ===
500
+ // Disconnected subgraphs (like Admin with no edges to main content) get pushed
501
+ // below the main content so they don't compete for top-level positioning.
502
+ {
503
+ // Union-find on page nodes + collapsed containers using user edges
504
+ const allNodeIds = new Set([...pageNodeIds, ...collapsedContainerIds]);
505
+ const ufParent = new Map<string, string>();
506
+ for (const id of allNodeIds) ufParent.set(id, id);
507
+ const ufFind = (x: string): string => {
508
+ while (ufParent.get(x) !== x) {
509
+ ufParent.set(x, ufParent.get(ufParent.get(x)!)!);
510
+ x = ufParent.get(x)!;
511
+ }
512
+ return x;
513
+ };
514
+ const ufUnion = (a: string, b: string): void => {
515
+ const ra = ufFind(a);
516
+ const rb = ufFind(b);
517
+ if (ra !== rb) ufParent.set(ra, rb);
518
+ };
519
+ for (const edge of parsed.edges) {
520
+ if (allNodeIds.has(edge.sourceId) && allNodeIds.has(edge.targetId)) {
521
+ ufUnion(edge.sourceId, edge.targetId);
522
+ }
523
+ }
524
+
525
+ // Main component = component containing the first root page
526
+ const firstRootPage = flatNodes.find((f) => !f.sitemapNode.isContainer)?.sitemapNode.id;
527
+ const mainRoot = firstRootPage ? ufFind(firstRootPage) : null;
528
+
529
+ // Collect isolated node IDs (not in main component)
530
+ const isolatedNodeIds = new Set<string>();
531
+ for (const id of allNodeIds) {
532
+ if (mainRoot && ufFind(id) !== mainRoot) {
533
+ isolatedNodeIds.add(id);
534
+ }
535
+ }
536
+
537
+ // Identify isolated containers (all page descendants are isolated)
538
+ const isolatedContainerIds = new Set<string>();
539
+ for (const cid of containerIds) {
540
+ if (collapsedContainerIds.has(cid)) {
541
+ if (isolatedNodeIds.has(cid)) isolatedContainerIds.add(cid);
542
+ continue;
543
+ }
544
+ const members = flatNodes.filter(
545
+ (f) => !f.sitemapNode.isContainer && f.parentContainerId === cid,
546
+ );
547
+ if (
548
+ members.length > 0 &&
549
+ members.every((m) => isolatedNodeIds.has(m.sitemapNode.id))
550
+ ) {
551
+ isolatedContainerIds.add(cid);
552
+ }
553
+ }
554
+
555
+ if (isolatedNodeIds.size > 0) {
556
+ const isVertical = parsed.direction === 'TB';
557
+
558
+ // Place isolated subgraphs BESIDE the main content (right for TB, below for LR)
559
+ // instead of extending the diagram in the primary axis. This keeps the diagram
560
+ // compact and allows better zoom.
561
+
562
+ // Main content bounding box
563
+ let mainRight = 0;
564
+ let mainBottom = 0;
565
+ let mainTop = Infinity;
566
+ let mainLeft = Infinity;
567
+ for (const n of layoutNodes) {
568
+ if (!isolatedNodeIds.has(n.id)) {
569
+ mainRight = Math.max(mainRight, n.x + n.width / 2);
570
+ mainBottom = Math.max(mainBottom, n.y + n.height);
571
+ mainTop = Math.min(mainTop, n.y);
572
+ mainLeft = Math.min(mainLeft, n.x - n.width / 2);
573
+ }
574
+ }
575
+ for (const c of layoutContainers) {
576
+ if (!isolatedContainerIds.has(c.nodeId)) {
577
+ mainRight = Math.max(mainRight, c.x + c.width);
578
+ mainBottom = Math.max(mainBottom, c.y + c.height);
579
+ mainTop = Math.min(mainTop, c.y);
580
+ mainLeft = Math.min(mainLeft, c.x);
581
+ }
582
+ }
583
+
584
+ // Isolated content bounding box
585
+ let isoLeft = Infinity;
586
+ let isoTop = Infinity;
587
+ let isoRight = 0;
588
+ let isoBottom = 0;
589
+ for (const n of layoutNodes) {
590
+ if (isolatedNodeIds.has(n.id)) {
591
+ isoLeft = Math.min(isoLeft, n.x - n.width / 2);
592
+ isoTop = Math.min(isoTop, n.y);
593
+ isoRight = Math.max(isoRight, n.x + n.width / 2);
594
+ isoBottom = Math.max(isoBottom, n.y + n.height);
595
+ }
596
+ }
597
+ for (const c of layoutContainers) {
598
+ if (isolatedContainerIds.has(c.nodeId)) {
599
+ isoLeft = Math.min(isoLeft, c.x);
600
+ isoTop = Math.min(isoTop, c.y);
601
+ isoRight = Math.max(isoRight, c.x + c.width);
602
+ isoBottom = Math.max(isoBottom, c.y + c.height);
603
+ }
604
+ }
605
+
606
+ if (isoLeft !== Infinity) {
607
+ // TB: place isolated to the RIGHT, aligned to top of main content
608
+ // LR: place isolated BELOW, aligned to left of main content
609
+ const gap = OVERLAP_GAP * 2;
610
+ let shiftX: number;
611
+ let shiftY: number;
612
+
613
+ if (isVertical) {
614
+ shiftX = mainRight + gap - isoLeft;
615
+ shiftY = (mainTop === Infinity ? 0 : mainTop) - isoTop;
616
+ } else {
617
+ shiftX = (mainLeft === Infinity ? 0 : mainLeft) - isoLeft;
618
+ shiftY = mainBottom + gap - isoTop;
619
+ }
620
+
621
+ if (shiftX !== 0 || shiftY !== 0) {
622
+ for (const n of layoutNodes) {
623
+ if (isolatedNodeIds.has(n.id)) {
624
+ n.x += shiftX;
625
+ n.y += shiftY;
626
+ }
627
+ }
628
+ for (const c of layoutContainers) {
629
+ if (isolatedContainerIds.has(c.nodeId)) {
630
+ c.x += shiftX;
631
+ c.y += shiftY;
632
+ }
633
+ }
634
+ for (const e of layoutEdges) {
635
+ const srcIsolated = isolatedNodeIds.has(e.sourceId);
636
+ const tgtIsolated = isolatedNodeIds.has(e.targetId);
637
+ if (srcIsolated || tgtIsolated) {
638
+ for (const p of e.points) {
639
+ p.x += shiftX;
640
+ p.y += shiftY;
641
+ }
642
+ }
643
+ }
644
+ }
645
+ }
646
+ }
647
+ }
648
+
649
+ // Compute bounding box
650
+ let totalWidth = 0;
651
+ let totalHeight = 0;
652
+
653
+ for (const node of layoutNodes) {
654
+ const right = node.x + node.width / 2;
655
+ const bottom = node.y + node.height;
656
+ if (right > totalWidth) totalWidth = right;
657
+ if (bottom > totalHeight) totalHeight = bottom;
658
+ }
659
+ for (const c of layoutContainers) {
660
+ const right = c.x + c.width;
661
+ const bottom = c.y + c.height;
662
+ if (right > totalWidth) totalWidth = right;
663
+ if (bottom > totalHeight) totalHeight = bottom;
664
+ }
665
+ // Include edge points in bounding box
666
+ for (const edge of layoutEdges) {
667
+ for (const p of edge.points) {
668
+ if (p.x > totalWidth) totalWidth = p.x;
669
+ if (p.y > totalHeight) totalHeight = p.y;
670
+ }
671
+ }
672
+
673
+ totalWidth += MARGIN;
674
+ totalHeight += MARGIN;
675
+
676
+ // Collect used tag values
677
+ const usedValuesByGroup = new Map<string, Set<string>>();
678
+ for (const group of parsed.tagGroups) {
679
+ const key = group.name.toLowerCase();
680
+ const used = new Set<string>();
681
+ const walk = (node: SitemapNode) => {
682
+ if (!node.isContainer && node.metadata[key]) {
683
+ used.add(node.metadata[key].toLowerCase());
684
+ }
685
+ for (const child of node.children) walk(child);
686
+ };
687
+ for (const root of parsed.roots) walk(root);
688
+ usedValuesByGroup.set(key, used);
689
+ }
690
+
691
+ // Legend
692
+ const legendGroups = computeLegendGroups(parsed.tagGroups, usedValuesByGroup);
693
+
694
+ const visibleGroups = activeTagGroup != null
695
+ ? legendGroups.filter((g) => g.name.toLowerCase() === activeTagGroup.toLowerCase())
696
+ : legendGroups;
697
+ const allExpanded = expandAllLegend && activeTagGroup == null;
698
+ const effectiveW = (g: SitemapLegendGroup) =>
699
+ activeTagGroup != null || allExpanded ? g.width : g.minifiedWidth;
700
+
701
+ if (visibleGroups.length > 0) {
702
+ // Top position: horizontal row above chart
703
+ const legendShift = LEGEND_HEIGHT + LEGEND_GROUP_GAP;
704
+
705
+ // Push chart content down
706
+ for (const n of layoutNodes) n.y += legendShift;
707
+ for (const c of layoutContainers) c.y += legendShift;
708
+ for (const e of layoutEdges) {
709
+ for (const p of e.points) p.y += legendShift;
710
+ }
711
+
712
+ const totalGroupsWidth =
713
+ visibleGroups.reduce((s, g) => s + effectiveW(g), 0) +
714
+ (visibleGroups.length - 1) * LEGEND_GROUP_GAP;
715
+
716
+ let cx = MARGIN;
717
+ for (const g of visibleGroups) {
718
+ g.x = cx;
719
+ g.y = MARGIN;
720
+ cx += effectiveW(g) + LEGEND_GROUP_GAP;
721
+ }
722
+
723
+ totalHeight += legendShift;
724
+ const neededWidth = totalGroupsWidth + MARGIN * 2;
725
+ if (neededWidth > totalWidth) {
726
+ totalWidth = neededWidth;
727
+ }
728
+ }
729
+
730
+ return {
731
+ nodes: layoutNodes,
732
+ edges: layoutEdges,
733
+ containers: layoutContainers,
734
+ legend: legendGroups,
735
+ width: totalWidth,
736
+ height: totalHeight,
737
+ };
738
+ }