aboutsummaryrefslogtreecommitdiff
path: root/modules/ext/mindmap.js
blob: a5e7286f36e6d440000a95000ab243cb446da8bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
// v4.5.44 — Mindmap module: multi-mindmap, find, move, undo, alias, import, merge, diff, stats, mermaid, refresh-html, archive, clone, weight, dirty-flag
class MindmapModule {
    constructor() {
        // Multi-mindmap state
        this.mindmaps = {};     // {name: {root, dirty, lastSave, aliases}}
        this.current = null;
        this.undoLimit = 10;
        this.undoStack = [];    // [{mindmap, op, inverse, ts}]
        this._idCounter = 0;
        // Shared
        this.commands = {};
        this.terminal = null;
        this.colors = ['red', 'green', 'yellow', 'blue', 'cyan', 'magenta', 'white'];
        this._colorIdx = 0;
    }
    getHelpInfo() {
        return {
            'mindmap': 'mindmap <subcommand> [...] — Create, edit, visualize mindmaps. v4.5.44: multi-mindmap, find, move, undo, import, merge, diff, stats, mermaid, refresh-html. Run `mindmap help` for full list.'
        };
    }
    _printHelp() {
        this.terminal.print('Usage: mindmap <subcommand> [...]', 'info');
        this.terminal.print('', '');
        this.terminal.print('  ── Creating ──', 'info');
        this.terminal.print('  new <topic> [-t tags]           Create a new mindmap (in-memory)');
        this.terminal.print('  switch <name>                   Switch to a different in-memory mindmap');
        this.terminal.print('  load <name> [--force]           Load a saved mindmap from VFS');
        this.terminal.print('  save [name]                     Save current mindmap to VFS');
        this.terminal.print('  import <file> [--format=...]    Import JSON/Mermaid/Markdown from VFS file');
        this.terminal.print('  merge <name> [--into=<id>]      Merge a saved mindmap into current');
        this.terminal.print('  clone <id>                      Duplicate a node (and its children)');
        this.terminal.print('  archive [name]                  Save to /mindmaps/archive/<name>-<ts>.json');
        this.terminal.print('', '');
        this.terminal.print('  ── Editing ──', 'info');
        this.terminal.print('  add <parent-id> <text> [-t tags]   Add a child node');
        this.terminal.print('  edit <id> <new-text>                Rename a node (alias: rename)');
        this.terminal.print('  move <id> <new-parent-id>           Reparent a node (with all children)');
        this.terminal.print('  remove <id>                         Remove a node and its children');
        this.terminal.print('  color <id> <color>                  Set node color');
        this.terminal.print('  weight <id> <1-5>                   Set node weight (affects SVG size)');
        this.terminal.print('  tag <id> <tag>                      Add a tag to a node');
        this.terminal.print('  alias <id> <name>                   Set a human-readable alias');
        this.terminal.print('  undo                                Undo last destructive op (stack of 10)');
        this.terminal.print('', '');
        this.terminal.print('  ── Viewing ──', 'info');
        this.terminal.print('  show [id]                       Display the mindmap tree');
        this.terminal.print('  find <text>                     Search nodes by text (case-insensitive)');
        this.terminal.print('  stats                           Node count, depth, tags, colors');
        this.terminal.print('  tags-list [tag]                 List nodes with a given tag (or all tags)');
        this.terminal.print('  diff <name1> <name2>            Compare two saved mindmaps');
        this.terminal.print('', '');
        this.terminal.print('  ── Export ──', 'info');
        this.terminal.print('  export <md|json|mermaid>        Export in format (default md)');
        this.terminal.print('  json                            Print internal JSON');
        this.terminal.print('  mermaid                         Print Mermaid mindmap syntax');
        this.terminal.print('  svg [name]                      Render inline SVG, optionally save');
        this.terminal.print('  refresh-html [name]             Regenerate standalone architecture HTML');
        this.terminal.print('', '');
        this.terminal.print('  ── Files ──', 'info');
        this.terminal.print('  list                            List saved mindmaps in VFS');
        this.terminal.print('  list-loaded                     List in-memory mindmaps');
        this.terminal.print('  delete-file <name>              Delete a saved mindmap file (alias: delete)');
    }
    // ── Helpers ──
    _genId() {
        let id, attempts = 0;
        do {
            id = Math.random().toString(36).slice(2, 7);
            attempts++;
        } while (this._idExists(id) && attempts < 100);
        if (this._idExists(id)) {
            id = 'n' + (++this._idCounter).toString(36);
        }
        return id;
    }
    _idExists(id) {
        if (!this.current || !this.mindmaps[this.current]) return false;
        const cur = this.mindmaps[this.current];
        return this._findNode(id, cur.root) !== null;
    }
    _nextColor() {
        const c = this.colors[this._colorIdx % this.colors.length];
        this._colorIdx++;
        return c;
    }
    _ensureMindmap() {
        if (!this.current || !this.mindmaps[this.current]) return null;
        return this.mindmaps[this.current];
    }
    _findNode(id, node) {
        if (!node) return null;
        if (node.id === id) return node;
        for (const child of (node.children || [])) {
            const found = this._findNode(id, child);
            if (found) return found;
        }
        return null;
    }
    _findParent(id, node, parent) {
        if (!node) return null;
        if (node.id === id) return parent || null;
        for (const child of (node.children || [])) {
            const found = this._findParent(id, child, node);
            if (found) return found;
        }
        return null;
    }
    _isDescendant(possible, ancestor) {
        if (!ancestor.children) return false;
        for (const c of ancestor.children) {
            if (c.id === possible.id) return true;
            if (this._isDescendant(possible, c)) return true;
        }
        return false;
    }
    _resolveRef(ref) {
        const cur = this._ensureMindmap();
        if (!cur) return null;
        const byId = this._findNode(ref, cur.root);
        if (byId) return byId;
        if (cur.aliases && cur.aliases[ref]) {
            return this._findNode(cur.aliases[ref], cur.root);
        }
        return null;
    }
    _countNodes(node) {
        let c = 1;
        for (const child of (node.children || [])) c += this._countNodes(child);
        return c;
    }
    _maxDepth(node, d) {
        if (d === undefined) d = 0;
        if (!node.children || node.children.length === 0) return d;
        let max = d;
        for (const c of node.children) {
            const cd = this._maxDepth(c, d + 1);
            if (cd > max) max = cd;
        }
        return max;
    }
    _treeLines(node, prefix, isLast) {
        const lines = [];
        const connector = isLast ? '└── ' : '├── ';
        const tagStr = (node.tags && node.tags.length) ? ' [' + node.tags.join(', ') + ']' : '';
        const colorTag = node.color ? '[' + node.color + ']' : '';
        const weightTag = node.weight && node.weight !== 3 ? ' (w:' + node.weight + ')' : '';
        lines.push(prefix + connector + colorTag + node.text + ' (' + node.id + ')' + tagStr + weightTag);
        if (node.children) {
            node.children.forEach((child, i) => {
                const subPrefix = prefix + (isLast ? '    ' : '│   ');
                this._treeLines(child, subPrefix, i === node.children.length - 1).forEach(l => lines.push(l));
            });
        }
        return lines;
    }
    _toMarkdown(node, depth) {
        const prefix = '  '.repeat(depth) + '- ';
        const tagStr = (node.tags && node.tags.length) ? ' *[' + node.tags.join(', ') + ']*' : '';
        let out = prefix + '**' + node.text + '**' + tagStr + '\n';
        if (node.children) {
            node.children.forEach(c => { out += this._toMarkdown(c, depth + 1); });
        }
        return out;
    }
    _parseTags(args) {
        const tagIdx = args.indexOf('-t');
        let tags = [];
        if (tagIdx >= 0) {
            tags = args[tagIdx + 1] ? args[tagIdx + 1].split(',').map(t => t.trim()).filter(Boolean) : [];
            args.splice(tagIdx, 2);
        }
        return { args, tags };
    }
    _escapeHtml(s) {
        return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    }
    _escapeMermaid(s) {
        return String(s)
            .replace(/&/g, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/\(/g, '&#40;')
            .replace(/\)/g, '&#41;')
            .replace(/#/g, '&#35;')
            .replace(/\[/g, '&#91;')
            .replace(/\]/g, '&#93;');
    }
    _pushUndo(op, inverse, mindmapName) {
        if (!mindmapName) mindmapName = this.current;
        this.undoStack.push({ op: op, inverse: inverse, mindmap: mindmapName, ts: Date.now() });
        if (this.undoStack.length > this.undoLimit) {
            this.undoStack.shift();
        }
    }
    _cloneNodeWithNewIds(node) {
        const newNode = {
            id: this._genId(),
            text: node.text,
            color: node.color,
            tags: node.tags ? [...node.tags] : [],
            children: []
        };
        if (node.weight) newNode.weight = node.weight;
        if (node.children) {
            for (const c of node.children) {
                newNode.children.push(this._cloneNodeWithNewIds(c));
            }
        }
        return newNode;
    }
    // ── Layout (improved for scale) ──
    _layoutTree(root) {
        const nw = 160, nh = 44, hGap = 24, vGap = 60, pad = 30;
        // Pass 1: subtree widths
        const computeWidth = (node) => {
            if (!node.children || node.children.length === 0) {
                node._w = nw;
                return nw;
            }
            let totalW = 0;
            for (const c of node.children) {
                totalW += computeWidth(c);
            }
            totalW += (node.children.length - 1) * hGap;
            node._w = Math.max(nw, totalW);
            return node._w;
        };
        computeWidth(root);
        // Pass 2: position
        const nodes = [], edges = [];
        let maxY = 0;
        const position = (node, x, y) => {
            const w = nw * (node.weight ? node.weight / 3 : 1);
            const h = nh * (node.weight ? node.weight / 3 : 1);
            nodes.push({ x: x, y: y, w: w, h: h, text: node.text, color: node.color, id: node.id, weight: node.weight || 3 });
            if (y + h > maxY) maxY = y + h;
            if (node.children && node.children.length > 0) {
                let childOffset = 0;
                const startX = x + nw / 2 - node._w / 2;
                for (let i = 0; i < node.children.length; i++) {
                    const c = node.children[i];
                    const cx = startX + childOffset;
                    edges.push({ x1: x + nw / 2, y1: y + h, x2: cx + nw / 2, y2: y + h + vGap });
                    position(c, cx, y + h + vGap);
                    childOffset += c._w + hGap;
                }
            }
        };
        position(root, pad, pad);
        // Bounds
        let minX = Infinity, maxX = 0;
        for (const n of nodes) {
            if (n.x < minX) minX = n.x;
            if (n.x + n.w > maxX) maxX = n.x + n.w;
        }
        if (minX === Infinity) { minX = 0; maxX = nw; }
        return { nodes: nodes, edges: edges, width: (maxX - minX) + pad * 2, height: maxY + pad, minX: minX };
    }
    // ── Subcommands ──
    _new(args) {
        const parsed = this._parseTags([...args]);
        const clean = parsed.args, tags = parsed.tags;
        if (!clean.length) return this.terminal.print('Usage: mindmap new <topic> [-t tag1,tag2]', 'warning');
        const name = clean.join(' ');
        if (this.mindmaps[name]) {
            return this.terminal.print(`Mindmap '${name}' already exists in memory. Use mindmap switch ${name}.`, 'warning');
        }
        this.mindmaps[name] = {
            root: {
                id: this._genId(),
                text: name,
                color: this._nextColor(),
                tags: tags,
                children: []
            },
            dirty: false,
            lastSave: null,
            aliases: {}
        };
        this.current = name;
        const mm = this.mindmaps[name];
        this.terminal.print('Created mindmap: [' + mm.root.color + '] ' + name + ' (' + mm.root.id + ')', 'success');
    }
    _switch(args) {
        if (!args[0]) return this.terminal.print('Usage: mindmap switch <name>', 'warning');
        const name = args[0];
        if (!this.mindmaps[name]) {
            return this.terminal.print(`Mindmap '${name}' not in memory. Use mindmap load to load a saved one.`, 'error');
        }
        this.current = name;
        const cur = this.mindmaps[name];
        this.terminal.print(`Switched to: ${name} (${this._countNodes(cur.root)} nodes${cur.dirty ? ', DIRTY' : ''})`, 'info');
    }
    _add(args) {
        const parsed = this._parseTags([...args]);
        const clean = parsed.args, tags = parsed.tags;
        if (clean.length < 2) return this.terminal.print('Usage: mindmap add <parent-id> <text> [-t tag1,tag2]', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap. Create one with mindmap new first.', 'warning');
        const parentRef = clean[0];
        const text = clean.slice(1).join(' ');
        const parent = this._resolveRef(parentRef);
        if (!parent) return this.terminal.print(`Node not found: ${parentRef}`, 'error');
        const node = {
            id: this._genId(),
            text: text,
            color: this._nextColor(),
            tags: tags,
            children: []
        };
        if (!parent.children) parent.children = [];
        parent.children.push(node);
        cur.dirty = true;
        this._pushUndo('add', { parentId: parent.id, node: JSON.parse(JSON.stringify(node)) });
        this.terminal.print('Added [' + node.color + '] ' + text + ' (' + node.id + ')', 'success');
    }
    _edit(args) {
        return this._rename(args);
    }
    _rename(args) {
        if (args.length < 2) return this.terminal.print('Usage: mindmap rename <id> <new-text>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const ref = args[0];
        const text = args.slice(1).join(' ');
        const node = this._resolveRef(ref);
        if (!node) return this.terminal.print(`Node not found: ${ref}`, 'error');
        const oldText = node.text;
        node.text = text;
        cur.dirty = true;
        this._pushUndo('rename', { id: node.id, oldText: oldText, newText: text });
        this.terminal.print('Renamed ' + ref + ' to: ' + text, 'success');
    }
    _move(args) {
        if (args.length < 2) return this.terminal.print('Usage: mindmap move <id> <new-parent-id>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const ref = args[0];
        const newParentRef = args[1];
        const node = this._resolveRef(ref);
        if (!node) return this.terminal.print(`Node not found: ${ref}`, 'error');
        const newParent = this._resolveRef(newParentRef);
        if (!newParent) return this.terminal.print(`Parent not found: ${newParentRef}`, 'error');
        if (node.id === newParent.id) return this.terminal.print('Cannot move a node under itself', 'error');
        if (this._isDescendant(newParent, node)) {
            return this.terminal.print('Cannot move a node under one of its descendants (would create a cycle)', 'error');
        }
        const oldParent = this._findParent(node.id, cur.root);
        if (!oldParent) return this.terminal.print('Old parent not found (orphan node)', 'error');
        oldParent.children = oldParent.children.filter(c => c.id !== node.id);
        if (!newParent.children) newParent.children = [];
        newParent.children.push(node);
        cur.dirty = true;
        this._pushUndo('move', { id: node.id, oldParentId: oldParent.id, newParentId: newParent.id });
        this.terminal.print(`Moved ${ref} under ${newParentRef}`, 'success');
    }
    _removeNode(args) {
        if (!args[0]) return this.terminal.print('Usage: mindmap remove <id>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const ref = args[0];
        const node = this._resolveRef(ref);
        if (!node) return this.terminal.print(`Node not found: ${ref}`, 'error');
        if (node.id === cur.root.id) {
            delete this.mindmaps[this.current];
            this.current = null;
            return this.terminal.print('Removed root mindmap. Create a new one with mindmap new.', 'info');
        }
        const parent = this._findParent(node.id, cur.root);
        if (!parent) return this.terminal.print('Parent not found', 'error');
        const idx = parent.children.findIndex(c => c.id === node.id);
        const removedNode = parent.children.splice(idx, 1)[0];
        cur.dirty = true;
        this._pushUndo('remove', { parentId: parent.id, idx: idx, node: JSON.parse(JSON.stringify(removedNode)) });
        this.terminal.print(`Removed ${ref} (${this._countNodes(removedNode)} nodes)`, 'success');
    }
    _show(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap. Create one with mindmap new first.', 'warning');
        let node = cur.root;
        if (args.length && args[0]) {
            const found = this._resolveRef(args[0]);
            if (!found) return this.terminal.print(`Node not found: ${args[0]}`, 'error');
            node = found;
        }
        const lines = this._treeLines(node, '', true);
        lines.forEach(l => this.terminal.print(l));
        this.terminal.print(`(${node.text} — ${this._countNodes(node)} nodes total)`, 'info');
    }
    _find(args) {
        if (!args[0]) return this.terminal.print('Usage: mindmap find <text>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const q = args.join(' ').toLowerCase();
        const matches = [];
        const walk = (node, depth) => {
            if (node.text.toLowerCase().includes(q) || (node.tags && node.tags.some(t => t.toLowerCase().includes(q)))) {
                matches.push({ id: node.id, text: node.text, depth: depth, tags: node.tags || [] });
            }
            for (const c of (node.children || [])) walk(c, depth + 1);
        };
        walk(cur.root, 0);
        if (matches.length === 0) {
            return this.terminal.print(`No matches for "${q}"`, 'info');
        }
        this.terminal.print(`Found ${matches.length} match(es) for "${q}":`, 'info');
        for (const m of matches) {
            const indent = '  '.repeat(m.depth);
            this.terminal.print(`  ${indent}${m.id}  ${m.text}${m.tags.length ? ' [' + m.tags.join(', ') + ']' : ''}`);
        }
    }
    _alias(args) {
        if (args.length < 2) return this.terminal.print('Usage: mindmap alias <id> <name>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const ref = args[0];
        const alias = args.slice(1).join(' ');
        const node = this._resolveRef(ref);
        if (!node) return this.terminal.print(`Node not found: ${ref}`, 'error');
        if (cur.aliases[alias] && cur.aliases[alias] !== node.id) {
            return this.terminal.print(`Alias '${alias}' is already used by node ${cur.aliases[alias]}`, 'warning');
        }
        for (const a of Object.keys(cur.aliases)) {
            if (cur.aliases[a] === node.id) delete cur.aliases[a];
        }
        cur.aliases[alias] = node.id;
        cur.dirty = true;
        this.terminal.print(`Aliased ${ref} as '${alias}'`, 'success');
    }
    _export(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const format = (args[0] || 'md').toLowerCase();
        if (format === 'md' || format === 'markdown') {
            const md = this._toMarkdown(cur.root, 0);
            this.terminal.print(md);
            this.terminal.print('(Markdown copied to clipboard)', 'info');
            if (navigator.clipboard) navigator.clipboard.writeText(md).catch(() => {});
            return;
        } else if (format === 'json') {
            return this._json(args.slice(1));
        } else if (format === 'mermaid') {
            return this._mermaid(args.slice(1));
        } else {
            return this.terminal.print(`Unknown format: ${format}. Use md, json, or mermaid.`, 'warning');
        }
    }
    _json(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const data = { root: cur.root, aliases: cur.aliases };
        const json = JSON.stringify(data, null, 2);
        this.terminal.print(json);
        if (navigator.clipboard) navigator.clipboard.writeText(json).catch(() => {});
        this.terminal.print(`(${json.length} chars, copied to clipboard)`, 'info');
    }
    _mermaid(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const mm = 'mindmap\n' + this._mermaidWalk(cur.root, 1);
        this.terminal.print(mm);
        if (navigator.clipboard) navigator.clipboard.writeText(mm).catch(() => {});
        this.terminal.print(`(Mermaid syntax, ${mm.length} chars, copied to clipboard)`, 'info');
    }
    _mermaidWalk(node, depth) {
        const indent = '  '.repeat(depth);
        let out = indent + this._escapeMermaid(node.text) + '\n';
        if (node.children) {
            for (const c of node.children) {
                out += this._mermaidWalk(c, depth + 1);
            }
        }
        return out;
    }
    _save(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap to save.', 'warning');
        const name = args[0] || this.current;
        if (!name) return this.terminal.print('Usage: mindmap save <name>', 'warning');
        try {
            const fs = this.terminal.fs;
            fs.mkdirRecursive('/mindmaps');
            const data = { root: cur.root, aliases: cur.aliases };
            fs.writeFile('/mindmaps/' + name + '.json', JSON.stringify(data, null, 2));
            cur.dirty = false;
            cur.lastSave = Date.now();
            this.terminal.print('Mindmap saved as: ' + name + ' (' + this._countNodes(cur.root) + ' nodes)', 'success');
        } catch (e) {
            this.terminal.print('Failed to save: ' + e.message, 'error');
        }
    }
    _load(args) {
        if (!args[0]) return this.terminal.print('Usage: mindmap load <name> [--force]', 'warning');
        const cur = this._ensureMindmap();
        const force = args.includes('--force');
        const name = args.filter(a => a !== '--force')[0];
        if (cur && cur.dirty && !force) {
            return this.terminal.print(`⚠️ Current mindmap '${this.current}' has unsaved changes. Use --force to load anyway, or save first.`, 'warning');
        }
        try {
            const fs = this.terminal.fs;
            const data = fs.readFile('/mindmaps/' + name + '.json');
            if (!data) return this.terminal.print('No saved mindmap: ' + name, 'error');
            const parsed = JSON.parse(data);
            if (parsed.root) {
                this.mindmaps[name] = {
                    root: parsed.root,
                    dirty: false,
                    lastSave: null,
                    aliases: parsed.aliases || {}
                };
            } else {
                this.mindmaps[name] = {
                    root: parsed,
                    dirty: false,
                    lastSave: null,
                    aliases: {}
                };
            }
            this.current = name;
            this.terminal.print(`Loaded mindmap: ${name} (${this._countNodes(this.mindmaps[name].root)} nodes)`, 'success');
        } catch (e) {
            this.terminal.print('Failed to load: ' + e.message, 'error');
        }
    }
    _list(args) {
        try {
            const fs = this.terminal.fs;
            const files = fs.readDir('/mindmaps') || [];
            const keys = files.filter(f => f.name && f.name.endsWith('.json')).map(f => f.name.replace('.json', ''));
            if (!keys.length) return this.terminal.print('No saved mindmaps.', 'info');
            this.terminal.print('=== Saved Mindmaps ===', 'info');
            keys.forEach(k => this.terminal.print('  ' + k));
        } catch (e) {
            this.terminal.print('No saved mindmaps (or /mindmaps does not exist).', 'info');
        }
    }
    _listLoaded(args) {
        const names = Object.keys(this.mindmaps);
        if (!names.length) return this.terminal.print('No mindmaps in memory. Use mindmap new to create one.', 'info');
        this.terminal.print('=== In-Memory Mindmaps ===', 'info');
        for (const name of names) {
            const mm = this.mindmaps[name];
            const isCurrent = name === this.current ? ' *' : '';
            const dirty = mm.dirty ? ' [DIRTY]' : '';
            this.terminal.print(`  ${name}${isCurrent}${dirty} (${this._countNodes(mm.root)} nodes)`);
        }
    }
    _deleteFile(args) {
        if (!args[0]) return this.terminal.print('Usage: mindmap delete-file <name>', 'warning');
        try {
            const fs = this.terminal.fs;
            fs.deleteFile('/mindmaps/' + args[0] + '.json');
            this.terminal.print('Deleted mindmap file: ' + args[0], 'success');
        } catch (e) {
            this.terminal.print('Failed to delete: ' + e.message, 'error');
        }
    }
    _color(args) {
        if (args.length < 2) return this.terminal.print('Usage: mindmap color <id> <color>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const ref = args[0];
        const color = args[1].toLowerCase();
        if (!this.colors.includes(color)) return this.terminal.print('Available colors: ' + this.colors.join(', '), 'warning');
        const node = this._resolveRef(ref);
        if (!node) return this.terminal.print(`Node not found: ${ref}`, 'error');
        const oldColor = node.color;
        node.color = color;
        cur.dirty = true;
        this._pushUndo('color', { id: node.id, oldColor: oldColor, newColor: color });
        this.terminal.print('Set color for ' + ref + ' to ' + color, 'success');
    }
    _weight(args) {
        if (args.length < 2) return this.terminal.print('Usage: mindmap weight <id> <1-5>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const ref = args[0];
        const w = parseInt(args[1]);
        if (isNaN(w) || w < 1 || w > 5) return this.terminal.print('Weight must be 1-5', 'warning');
        const node = this._resolveRef(ref);
        if (!node) return this.terminal.print(`Node not found: ${ref}`, 'error');
        const oldWeight = node.weight || 3;
        node.weight = w;
        cur.dirty = true;
        this._pushUndo('weight', { id: node.id, oldWeight: oldWeight, newWeight: w });
        this.terminal.print('Set weight for ' + ref + ' to ' + w, 'success');
    }
    _tag(args) {
        if (args.length < 2) return this.terminal.print('Usage: mindmap tag <id> <tag>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const ref = args[0];
        const tag = args.slice(1).join(' ');
        const node = this._resolveRef(ref);
        if (!node) return this.terminal.print(`Node not found: ${ref}`, 'error');
        if (!node.tags) node.tags = [];
        if (node.tags.includes(tag)) return this.terminal.print(`Tag '${tag}' already on node`, 'info');
        node.tags.push(tag);
        cur.dirty = true;
        this._pushUndo('tag', { id: node.id, tag: tag, action: 'add' });
        this.terminal.print('Added tag "' + tag + '" to ' + ref, 'success');
    }
    _tagsList(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const tagCounts = {};
        const tagNodes = {};
        const walk = (node) => {
            for (const t of (node.tags || [])) {
                tagCounts[t] = (tagCounts[t] || 0) + 1;
                if (!tagNodes[t]) tagNodes[t] = [];
                tagNodes[t].push({ id: node.id, text: node.text });
            }
            for (const c of (node.children || [])) walk(c);
        };
        walk(cur.root);
        if (args[0]) {
            const tag = args[0];
            const nodes = tagNodes[tag];
            if (!nodes) return this.terminal.print(`No nodes with tag '${tag}'`, 'info');
            this.terminal.print(`Nodes with tag '${tag}':`, 'info');
            for (const n of nodes) {
                this.terminal.print(`  ${n.id}  ${n.text}`);
            }
        } else {
            const tags = Object.keys(tagCounts).sort();
            if (!tags.length) return this.terminal.print('No tags in current mindmap', 'info');
            this.terminal.print('Tags in current mindmap:', 'info');
            for (const t of tags) {
                this.terminal.print(`  ${t}  (${tagCounts[t]} node${tagCounts[t] > 1 ? 's' : ''})`);
            }
        }
    }
    _import(args) {
        if (!args[0]) return this.terminal.print('Usage: mindmap import <file> [--format=md|json|mermaid]', 'warning');
        const fs = this.terminal.fs;
        const file = args[0];
        const formatFlag = args.find(a => a.startsWith('--format='));
        let format = formatFlag ? formatFlag.split('=')[1] : null;
        const content = fs.readFile(file);
        if (!content) return this.terminal.print(`Cannot read file: ${file}`, 'error');
        if (!format) {
            if (file.endsWith('.json')) format = 'json';
            else if (file.endsWith('.mmd') || content.trimStart().startsWith('mindmap')) format = 'mermaid';
            else if (file.endsWith('.md')) format = 'md';
            else format = 'md';
        }
        let root;
        try {
            if (format === 'json') {
                const parsed = JSON.parse(content);
                root = parsed.root || parsed;
            } else if (format === 'mermaid') {
                root = this._parseMermaid(content);
            } else if (format === 'md' || format === 'markdown') {
                root = this._parseMarkdown(content);
            } else {
                return this.terminal.print(`Unknown format: ${format}`, 'error');
            }
        } catch (e) {
            return this.terminal.print(`Parse failed: ${e.message}`, 'error');
        }
        if (!root) return this.terminal.print('No content parsed', 'error');
        this._regenerateIds(root);
        const name = root.text || 'imported';
        if (this.mindmaps[name]) {
            return this.terminal.print(`Mindmap '${name}' already exists in memory`, 'warning');
        }
        this.mindmaps[name] = { root: root, dirty: true, lastSave: null, aliases: {} };
        this.current = name;
        this.terminal.print(`Imported ${this._countNodes(root)} nodes as '${name}'`, 'success');
    }
    _parseMermaid(content) {
        const lines = content.split('\n').filter(l => l.trim());
        if (lines[0] && lines[0].trim() === 'mindmap') lines.shift();
        const stack = [];
        let root = null;
        for (const line of lines) {
            const m = line.match(/^(\s*)/);
            const depth = m[1].length;
            const text = line.trim();
            if (!text) continue;
            const node = { id: this._genId(), text: text, color: this._nextColor(), tags: [], children: [] };
            while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
                stack.pop();
            }
            if (stack.length === 0) {
                root = node;
            } else {
                if (!stack[stack.length - 1].node.children) stack[stack.length - 1].node.children = [];
                stack[stack.length - 1].node.children.push(node);
            }
            stack.push({ node: node, depth: depth });
        }
        return root;
    }
    _parseMarkdown(content) {
        const lines = content.split('\n');
        const stack = [];
        let root = null;
        for (const line of lines) {
            const m = line.match(/^(\s*)- (.*)/);
            if (!m) continue;
            const depth = Math.floor(m[1].length / 2);
            let text = m[2];
            const tags = [];
            const boldMatch = text.match(/^\*\*(.+?)\*\*/);
            if (boldMatch) text = boldMatch[1];
            const tagMatch = text.match(/\*\[(.+?)\]\*/);
            if (tagMatch) {
                tags.push(...tagMatch[1].split(',').map(t => t.trim()));
                text = text.replace(/\*\[.+?\]\*/, '').trim();
            }
            const node = { id: this._genId(), text: text, color: this._nextColor(), tags: tags, children: [] };
            while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
                stack.pop();
            }
            if (stack.length === 0) {
                root = node;
            } else {
                if (!stack[stack.length - 1].node.children) stack[stack.length - 1].node.children = [];
                stack[stack.length - 1].node.children.push(node);
            }
            stack.push({ node: node, depth: depth });
        }
        return root;
    }
    _regenerateIds(node) {
        node.id = this._genId();
        if (node.children) {
            for (const c of node.children) this._regenerateIds(c);
        }
    }
    _merge(args) {
        if (!args[0]) return this.terminal.print('Usage: mindmap merge <name> [--into=<parent-id>]', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const name = args[0];
        const intoFlag = args.find(a => a.startsWith('--into='));
        const targetRef = intoFlag ? intoFlag.split('=')[1] : null;
        try {
            const fs = this.terminal.fs;
            const data = fs.readFile('/mindmaps/' + name + '.json');
            if (!data) return this.terminal.print(`No saved mindmap: ${name}`, 'error');
            const parsed = JSON.parse(data);
            const sourceRoot = parsed.root || parsed;
            let target = cur.root;
            if (targetRef) {
                target = this._resolveRef(targetRef);
                if (!target) return this.terminal.print(`Target not found: ${targetRef}`, 'error');
            }
            const cloned = this._cloneNodeWithNewIds(sourceRoot);
            if (!target.children) target.children = [];
            target.children.push(cloned);
            cur.dirty = true;
            this._pushUndo('merge', { parentId: target.id, nodeId: cloned.id });
            this.terminal.print(`Merged ${this._countNodes(sourceRoot)} nodes from '${name}' into '${target.text}' (new root id: ${cloned.id})`, 'success');
        } catch (e) {
            this.terminal.print('Merge failed: ' + e.message, 'error');
        }
    }
    _diff(args) {
        if (args.length < 2) return this.terminal.print('Usage: mindmap diff <name1> <name2>', 'warning');
        try {
            const fs = this.terminal.fs;
            const a = JSON.parse(fs.readFile('/mindmaps/' + args[0] + '.json') || '{}');
            const b = JSON.parse(fs.readFile('/mindmaps/' + args[1] + '.json') || '{}');
            const aRoot = a.root || a;
            const bRoot = b.root || b;
            if (!aRoot.text || !bRoot.text) return this.terminal.print('Could not load both mindmaps', 'error');
            const result = this._diffTrees(aRoot, bRoot);
            this.terminal.print(`=== Diff: ${args[0]} → ${args[1]} ===`, 'header');
            if (result.added.length === 0 && result.removed.length === 0 && result.modified.length === 0) {
                return this.terminal.print('  (no differences)', 'info');
            }
            if (result.added.length) {
                this.terminal.print(`  + Added (${result.added.length}):`, 'success');
                for (const a of result.added) this.terminal.print(`    + ${a.path} (${a.text})`);
            }
            if (result.removed.length) {
                this.terminal.print(`  - Removed (${result.removed.length}):`, 'error');
                for (const r of result.removed) this.terminal.print(`    - ${r.path} (${r.text})`);
            }
            if (result.modified.length) {
                this.terminal.print(`  ~ Modified (${result.modified.length}):`, 'warning');
                for (const m of result.modified) this.terminal.print(`    ~ ${m.path}: tags ${JSON.stringify(m.aTags)} → ${JSON.stringify(m.bTags)}`);
            }
        } catch (e) {
            this.terminal.print('Diff failed: ' + e.message, 'error');
        }
    }
    _diffTrees(a, b, path) {
        if (path === undefined) path = '';
        const added = [], removed = [], modified = [];
        const aMap = new Map();
        const bMap = new Map();
        if (a.children) for (const c of a.children) aMap.set(c.text, c);
        if (b.children) for (const c of b.children) bMap.set(c.text, c);
        for (const text of aMap.keys()) {
            if (!bMap.has(text)) {
                removed.push({ path: path + '/' + text, text: text });
            } else {
                const aNode = aMap.get(text);
                const bNode = bMap.get(text);
                const aTags = JSON.stringify(aNode.tags || []);
                const bTags = JSON.stringify(bNode.tags || []);
                if (aTags !== bTags) {
                    modified.push({ path: path + '/' + text, aText: text, bText: text, aTags: aNode.tags, bTags: bNode.tags });
                }
                const sub = this._diffTrees(aNode, bNode, path + '/' + text);
                added.push(...sub.added);
                removed.push(...sub.removed);
                modified.push(...sub.modified);
            }
        }
        for (const text of bMap.keys()) {
            if (!aMap.has(text)) {
                added.push({ path: path + '/' + text, text: text });
            }
        }
        return { added: added, removed: removed, modified: modified };
    }
    _stats(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const total = this._countNodes(cur.root);
        const maxDepth = this._maxDepth(cur.root);
        const levelCounts = {};
        const colorCounts = {};
        const tagCounts = {};
        const walk = (node, d) => {
            levelCounts[d] = (levelCounts[d] || 0) + 1;
            if (node.color) colorCounts[node.color] = (colorCounts[node.color] || 0) + 1;
            for (const t of (node.tags || [])) tagCounts[t] = (tagCounts[t] || 0) + 1;
            for (const c of (node.children || [])) walk(c, d + 1);
        };
        walk(cur.root, 0);
        this.terminal.print(`📊 Mindmap Stats: ${this.current}`, 'header');
        this.terminal.print(`  Total nodes:    ${total}`);
        this.terminal.print(`  Max depth:      ${maxDepth + 1} levels`);
        this.terminal.print(`  Aliases:        ${Object.keys(cur.aliases).length}`);
        this.terminal.print(`  Dirty:          ${cur.dirty ? 'yes (unsaved changes)' : 'no'}`);
        if (cur.lastSave) this.terminal.print(`  Last save:      ${new Date(cur.lastSave).toISOString()}`);
        this.terminal.print(`  ── Nodes per level ──`, 'info');
        for (const d of Object.keys(levelCounts).sort()) {
            this.terminal.print(`    Level ${d}: ${levelCounts[d]}`);
        }
        if (Object.keys(colorCounts).length) {
            this.terminal.print(`  ── Colors ──`, 'info');
            for (const c of Object.keys(colorCounts)) {
                this.terminal.print(`    ${c}: ${colorCounts[c]}`);
            }
        }
        if (Object.keys(tagCounts).length) {
            this.terminal.print(`  ── Tags (top 10) ──`, 'info');
            const sorted = Object.entries(tagCounts).sort((a, b) => b[1] - a[1]).slice(0, 10);
            for (const [t, n] of sorted) {
                this.terminal.print(`    ${t}: ${n}`);
            }
        }
    }
    _undo(args) {
        if (this.undoStack.length === 0) {
            return this.terminal.print('Nothing to undo', 'info');
        }
        const entry = this.undoStack.pop();
        const cur = this.mindmaps[entry.mindmap];
        if (!cur) {
            return this.terminal.print(`Cannot undo: mindmap '${entry.mindmap}' no longer exists`, 'error');
        }
        this.current = entry.mindmap;
        switch (entry.op) {
            case 'add': {
                const parent = this._findNode(entry.inverse.parentId, cur.root);
                if (parent) {
                    parent.children = parent.children.filter(c => c.id !== entry.inverse.node.id);
                }
                this.terminal.print(`Undid add: removed ${entry.inverse.node.text}`, 'success');
                break;
            }
            case 'remove': {
                const parent = this._findNode(entry.inverse.parentId, cur.root);
                if (parent) {
                    parent.children.splice(entry.inverse.idx, 0, entry.inverse.node);
                }
                this.terminal.print(`Undid remove: restored ${entry.inverse.node.text}`, 'success');
                break;
            }
            case 'rename': {
                const node = this._findNode(entry.inverse.id, cur.root);
                if (node) node.text = entry.inverse.oldText;
                this.terminal.print(`Undid rename: ${entry.inverse.newText} → ${entry.inverse.oldText}`, 'success');
                break;
            }
            case 'move': {
                const oldParent = this._findNode(entry.inverse.oldParentId, cur.root);
                const newParent = this._findNode(entry.inverse.newParentId, cur.root);
                if (oldParent && newParent) {
                    newParent.children = newParent.children.filter(c => c.id !== entry.inverse.id);
                    const node = this._findNode(entry.inverse.id, cur.root);
                    if (node) oldParent.children.push(node);
                }
                this.terminal.print(`Undid move`, 'success');
                break;
            }
            case 'color': {
                const node = this._findNode(entry.inverse.id, cur.root);
                if (node) node.color = entry.inverse.oldColor;
                this.terminal.print(`Undid color change`, 'success');
                break;
            }
            case 'tag': {
                const node = this._findNode(entry.inverse.id, cur.root);
                if (node && entry.inverse.action === 'add') {
                    node.tags = node.tags.filter(t => t !== entry.inverse.tag);
                }
                this.terminal.print(`Undid tag add`, 'success');
                break;
            }
            case 'weight': {
                const node = this._findNode(entry.inverse.id, cur.root);
                if (node) node.weight = entry.inverse.oldWeight;
                this.terminal.print(`Undid weight change`, 'success');
                break;
            }
            case 'merge': {
                const parent = this._findNode(entry.inverse.parentId, cur.root);
                if (parent) {
                    parent.children = parent.children.filter(c => c.id !== entry.inverse.nodeId);
                }
                this.terminal.print(`Undid merge`, 'success');
                break;
            }
        }
    }
    _clone(args) {
        if (!args[0]) return this.terminal.print('Usage: mindmap clone <id>', 'warning');
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const node = this._resolveRef(args[0]);
        if (!node) return this.terminal.print(`Node not found: ${args[0]}`, 'error');
        const parent = this._findParent(node.id, cur.root);
        if (!parent) return this.terminal.print('Cannot clone root (use new instead)', 'error');
        const cloned = this._cloneNodeWithNewIds(node);
        cloned.text = node.text + ' (copy)';
        parent.children.push(cloned);
        cur.dirty = true;
        this._pushUndo('add', { parentId: parent.id, node: JSON.parse(JSON.stringify(cloned)) });
        this.terminal.print(`Cloned ${args[0]} as ${cloned.id} (${this._countNodes(cloned)} nodes)`, 'success');
    }
    _archive(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap.', 'warning');
        const name = args[0] || this.current;
        const ts = new Date().toISOString().replace(/[:.]/g, '-');
        const filename = `archive/${name}-${ts}.json`;
        try {
            const fs = this.terminal.fs;
            fs.mkdirRecursive('/mindmaps/archive');
            const data = { root: cur.root, aliases: cur.aliases };
            fs.writeFile('/mindmaps/' + filename, JSON.stringify(data, null, 2));
            this.terminal.print(`Archived to /mindmaps/${filename}`, 'success');
            this._cleanArchives(name);
        } catch (e) {
            this.terminal.print('Archive failed: ' + e.message, 'error');
        }
    }
    _cleanArchives(name) {
        try {
            const fs = this.terminal.fs;
            const dir = fs.readDir('/mindmaps/archive') || [];
            const matching = dir.filter(f => f.name && f.name.startsWith(name + '-') && f.name.endsWith('.json'))
                .map(f => f.name)
                .sort();
            while (matching.length > 20) {
                const oldest = matching.shift();
                fs.deleteFile('/mindmaps/archive/' + oldest);
            }
        } catch (e) {}
    }
    _refreshHtml(args) {
        this.terminal.print('🔄 Regenerating architecture HTML from modules-index.json...', 'info');
        const cacheVer = (typeof localStorage !== 'undefined' && localStorage.getItem('webx11_cache_ver')) || '';
        fetch('/spaces/newos4/modules-index.json?v=' + cacheVer)
            .then(r => r.json())
            .then(idx => {
                const byLayer = {};
                for (const m of Object.values(idx.modules)) {
                    const l = m.layer || m.type || 'other';
                    if (!byLayer[l]) byLayer[l] = [];
                    byLayer[l].push(m);
                }
                const ver = (typeof window !== 'undefined' && window.NEWOS4_VERSION) || 'v4.5.44';
                let html = '<!DOCTYPE html>\n<html lang="en">\n<head>\n';
                html += '<meta charset="UTF-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n';
                html += '<title>newOS4 ' + ver + ' — Architecture</title>\n';
                html += '<script src="mermaid.min.js?v=11"><\/script>\n';
                html += '<style>body{background:#0a0a14;color:#e0e0e0;font-family:Segoe UI,sans-serif;padding:20px;max-width:1400px;margin:0 auto}h1{color:#00ff88;text-align:center;border-bottom:2px solid #00ff88;padding-bottom:12px}.layer{background:#12121e;border-left:3px solid #7c5cfc;padding:12px;margin:12px 0;border-radius:6px}.layer h2{color:#a78bfa;margin-bottom:8px;text-transform:uppercase}.mod{background:#1a1a2e;padding:8px;margin:4px 0;border-radius:4px;border-left:2px solid #2a2a4a}.mod .name{color:#22d3ee;font-weight:600}.mod .cmds{color:#888;font-size:0.9em;margin-top:4px}.mod .desc{color:#aaa;font-size:0.85em;margin-top:2px;font-style:italic}.footer{text-align:center;margin-top:30px;color:#666;font-size:0.85em;border-top:1px solid #2a2a4a;padding-top:12px}</style>\n';
                html += '</head>\n<body>\n';
                html += '<h1>🗺️ newOS4 ' + ver + ' — Architecture</h1>\n';
                html += '<p style="text-align:center;color:#888">Auto-generated from modules-index.json · ' + new Date().toISOString() + '</p>\n';
                for (const layer of Object.keys(byLayer).sort()) {
                    const mods = byLayer[layer];
                    html += '<div class="layer"><h2>' + this._escapeHtml(layer) + ' (' + mods.length + ' modules)</h2>\n';
                    for (const m of mods) {
                        const cmds = m.commands ? m.commands.slice(0, 6).join(', ') + (m.commands.length > 6 ? '...' : '') : '';
                        html += '<div class="mod"><div class="name">' + this._escapeHtml(m.name) + '</div>';
                        if (cmds) html += '<div class="cmds">' + this._escapeHtml(cmds) + '</div>';
                        if (m.description) html += '<div class="desc">' + this._escapeHtml(m.description.substring(0, 80)) + '</div>';
                        html += '</div>\n';
                    }
                    html += '</div>\n';
                }
                html += '<div class="footer">Generated by <code>mindmap refresh-html</code> · ' + new Date().toISOString() + '</div>\n';
                html += '</body>\n</html>\n';
                try {
                    const fs = this.terminal.fs;
                    fs.mkdirRecursive('/mindmaps');
                    const outFile = '/mindmaps/_generated-architecture.html';
                    fs.writeFile(outFile, html);
                    this.terminal.print(`✅ Wrote ${(html.length / 1024).toFixed(1)}KB to ${outFile}`, 'success');
                    const preview = html.split('\n').slice(0, 25).join('\n');
                    this.terminal.print('--- Preview (first 25 lines) ---', 'info');
                    this.terminal.print(preview);
                    this.terminal.print('--- End preview ---', 'info');
                    this.terminal.print(`💡 To deploy: cat ${outFile} > /var/www/webx11.duckdns.org/spaces/newos4/mindmap.html`, 'info');
                } catch (e) {
                    this.terminal.print('Could not save: ' + e.message, 'error');
                }
            })
            .catch(e => {
                this.terminal.print('Failed to fetch modules-index.json: ' + e.message, 'error');
            });
    }
    _svg(args) {
        const cur = this._ensureMindmap();
        if (!cur) return this.terminal.print('No mindmap. Create one with mindmap new first.', 'warning');
        const saveName = args[0] || null;
        const layout = this._layoutTree(cur.root);
        const colorMap = {
            red: '#fb7185', green: '#34d399', yellow: '#fbbf24', blue: '#818cf8',
            cyan: '#22d3ee', magenta: '#e879f9', white: '#e0e0e0'
        };
        const xShift = layout.minX === Infinity ? 0 : -layout.minX;
        const svgWidth = layout.width;
        const svgHeight = layout.height;
        let svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' + svgWidth + ' ' + svgHeight + '" style="max-width:100%;background:#0a0a14;font-family:monospace;border-radius:8px;">';
        // Edges first
        for (const e of layout.edges) {
            const sx = e.x1 + xShift + 30, sy = e.y1;
            const ex = e.x2 + xShift + 30, ey = e.y2;
            const mx = (sx + ex) / 2;
            svg += '<path d="M' + sx + ',' + sy + ' C' + mx + ',' + (sy + 20) + ' ' + mx + ',' + (ey - 20) + ' ' + ex + ',' + ey + '" fill="none" stroke="#2a2a4a" stroke-width="1.5"/>';
        }
        // Nodes
        for (const n of layout.nodes) {
            const nx = n.x + xShift + 30;
            const color = colorMap[n.color] || '#818cf8';
            const fontSize = n.w < 120 ? '9' : '10';
            const text = n.text.length > 18 ? n.text.substring(0, 16) + '..' : n.text;
            svg += '<rect x="' + nx + '" y="' + n.y + '" width="' + n.w + '" height="' + n.h + '" rx="6" fill="' + color + '18" stroke="' + color + '" stroke-width="1.5"/>';
            svg += '<text x="' + (nx + n.w / 2) + '" y="' + (n.y + n.h / 2 + 4) + '" text-anchor="middle" fill="#e0e0e0" font-size="' + fontSize + '" font-family="monospace">' + this._escapeHtml(text) + '</text>';
        }
        svg += '</svg>';
        this.terminal.printHTML(svg);
        if (saveName) {
            try {
                const fs = this.terminal.fs;
                fs.mkdirRecursive('/mindmaps');
                fs.writeFile('/mindmaps/' + saveName + '.svg', svg);
                this.terminal.print(`💾 Saved to /mindmaps/${saveName}.svg (${layout.nodes.length} nodes, ${svgWidth}x${svgHeight})`, 'success');
            } catch (e) {
                this.terminal.print('Failed to save SVG: ' + e.message, 'error');
            }
        }
    }
    // ── Main dispatcher ──
    registerCommands() {
        this.commands['mindmap'] = (args) => {
            if (!args[0] || args[0] === '--help' || args[0] === '-h' || args[0] === 'help') {
                this._printHelp();
                return;
            }
            const sub = args[0];
            const rest = args.slice(1);
            switch (sub) {
                case 'new': return this._new(rest);
                case 'add': return this._add(rest);
                case 'edit': return this._edit(rest);
                case 'rename': return this._rename(rest);
                case 'show': return this._show(rest);
                case 'find': return this._find(rest);
                case 'export': return this._export(rest);
                case 'json': return this._json(rest);
                case 'mermaid': return this._mermaid(rest);
                case 'save': return this._save(rest);
                case 'load': return this._load(rest);
                case 'list': return this._list(rest);
                case 'list-loaded': return this._listLoaded(rest);
                case 'switch': return this._switch(rest);
                case 'delete': return this._deleteFile(rest);
                case 'delete-file': return this._deleteFile(rest);
                case 'remove': return this._removeNode(rest);
                case 'move': return this._move(rest);
                case 'color': return this._color(rest);
                case 'weight': return this._weight(rest);
                case 'tag': return this._tag(rest);
                case 'tags-list': return this._tagsList(rest);
                case 'alias': return this._alias(rest);
                case 'import': return this._import(rest);
                case 'merge': return this._merge(rest);
                case 'diff': return this._diff(rest);
                case 'stats': return this._stats(rest);
                case 'undo': return this._undo(rest);
                case 'clone': return this._clone(rest);
                case 'archive': return this._archive(rest);
                case 'refresh-html': return this._refreshHtml(rest);
                case 'svg': return this._svg(rest);
                default: this.terminal.print('Unknown subcommand: ' + sub + '. Try: mindmap help', 'warning');
            }
        };
    }
}
if (typeof window.webx11 !== 'undefined' && window.webx11) {
    webx11.moduleManager.registerModule(new MindmapModule(), 'https://webx11.duckdns.org/spaces/newos4/modules/ext/mindmap.js');
}