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
|
class MindmapModule {
constructor() {
this.commands = {};
this.terminal = null;
this.root = null;
this.colors = ['red', 'green', 'yellow', 'blue', 'cyan', 'magenta', 'white'];
this._colorIdx = 0;
}
getHelpInfo() {
return { 'mindmap': 'mindmap <new|add|show|export|save|load|list|delete|rename|color|tag> [...] — Create and manage interactive mindmaps' };
}
_nextColor() {
const c = this.colors[this._colorIdx % this.colors.length];
this._colorIdx++;
return c;
}
_genId() {
return Math.random().toString(36).slice(2, 6);
}
_findNode(id, node) {
if (node.id === id) return node;
for (const child of (node.children || [])) {
const found = this._findNode(id, child);
if (found) return found;
}
return null;
}
_treeLines(node, prefix, isLast) {
const lines = [];
const connector = isLast ? '\u2514\u2500\u2500 ' : '\u251c\u2500\u2500 ';
const tagStr = (node.tags && node.tags.length) ? ' [' + node.tags.join(', ') + ']' : '';
const colorTag = node.color ? '[' + node.color + ']' : '';
lines.push(prefix + connector + colorTag + node.text + ' (' + node.id + ')' + tagStr);
if (node.children) {
const sorted = node.children;
sorted.forEach((child, i) => {
const subPrefix = prefix + (isLast ? ' ' : '\u2502 ');
this._treeLines(child, subPrefix, i === sorted.length - 1).forEach(l => lines.push(l));
});
}
return lines;
}
registerCommands() {
const self = this;
this.commands['mindmap'] = (args) => {
if (!args[0] || args[0] === '--help' || args[0] === '-h') {
this.terminal.print('Usage: mindmap <new|add|show|export|save|load|list|delete|rename|color|tag|svg> [...]', 'info');
this.terminal.print(' new <topic> [-t tags] — Create a new mindmap', 'info');
this.terminal.print(' add <parent-id> <text> [-t tags] — Add a child node', 'info');
this.terminal.print(' show — Display the mindmap tree', 'info');
this.terminal.print(' export — Print as indented text', 'info');
this.terminal.print(' svg [name] — Render as inline SVG, optionally save to VFS', 'info');
this.terminal.print(' save [name] — Save to VFS', 'info');
this.terminal.print(' load <name> — Load from VFS', 'info');
this.terminal.print(' list — List saved mindmaps', 'info');
this.terminal.print(' delete <id> — Remove a node', 'info');
this.terminal.print(' rename <id> <new-text>', 'info');
this.terminal.print(' color <id> <color>', 'info');
this.terminal.print(' tag <id> [tag1,tag2...]', 'info');
return;
}
const sub = args[0];
const rest = args.slice(1);
switch (sub) {
case '--help':
case '-h':
case 'help':
this.terminal.print('Usage: mindmap <new|add|show|export|save|load|list|delete|rename|color|tag> [...]', 'info');
return;
case 'new': return this._new(rest);
case 'add': return this._add(rest);
case 'show': return this._show(rest);
case 'export': return this._export(rest);
case 'save': return this._save(rest);
case 'load': return this._load(rest);
case 'list': return this._list();
case 'delete': return this._delete(rest);
case 'rename': return this._rename(rest);
case 'color': return this._color(rest);
case 'tag': return this._tag(rest);
case 'svg': return this._svg(rest);
default: this.terminal.print('Unknown subcommand: ' + sub + '. Try: new, add, show, export, save, load, list, delete, rename, color, tag, svg', 'warning');
}
};
}
_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 };
}
_new(args) {
const { args: clean, tags } = this._parseTags([...args]);
if (!clean.length) return this.terminal.print('Usage: mindmap new <topic> [-t tag1,tag2]', 'warning');
if (this.root) return this.terminal.print('A mindmap already exists. Use mindmap add to extend it.', 'warning');
this.root = {
id: this._genId(),
text: clean.join(' '),
color: this._nextColor(),
tags: tags,
children: []
};
this.terminal.print('Created root: [' + this.root.color + '] ' + this.root.text + ' (' + this.root.id + ')', 'success');
}
_add(args) {
const { args: clean, tags } = this._parseTags([...args]);
if (clean.length < 2) return this.terminal.print('Usage: mindmap add <parent-id> <text> [-t tag1,tag2]', 'warning');
if (!this.root) return this.terminal.print('No mindmap. Create one with mindmap new first.', 'warning');
const parentId = clean[0];
const text = clean.slice(1).join(' ');
const parent = this._findNode(parentId, this.root);
if (!parent) return this.terminal.print('Node not found: ' + parentId, 'error');
const node = {
id: this._genId(),
text: text,
color: this._nextColor(),
tags: tags,
children: []
};
parent.children.push(node);
this.terminal.print('Added [' + node.color + '] ' + text + ' (' + node.id + ')', 'success');
}
_show(args) {
if (!this.root) return this.terminal.print('No mindmap. Create one with mindmap new first.', 'warning');
let node = this.root;
if (args.length && args[0]) {
const found = this._findNode(args[0], this.root);
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');
}
_countNodes(node) {
let c = 1;
for (const child of (node.children || [])) c += this._countNodes(child);
return c;
}
_export(args) {
if (!this.root) return this.terminal.print('No mindmap. Create one with mindmap new first.', 'warning');
let node = this.root;
if (args.length && args[0]) {
const found = this._findNode(args[0], this.root);
if (!found) return this.terminal.print('Node not found: ' + args[0], 'error');
node = found;
}
const md = this._toMarkdown(node, 0);
this.terminal.print(md);
this.terminal.print('(Markdown copied to clipboard)', 'info');
if (navigator.clipboard) {
navigator.clipboard.writeText(md);
}
}
_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;
}
_save(args) {
if (!args[0]) return this.terminal.print('Usage: mindmap save <name>', 'warning');
if (!this.root) return this.terminal.print('No mindmap to save.', 'warning');
try {
const fs = this.terminal.fs;
fs.mkdirRecursive('/mindmaps');
fs.writeFile('/mindmaps/' + args[0] + '.json', JSON.stringify(this.root, null, 2));
this.terminal.print('Mindmap saved as: ' + args[0], '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>', 'warning');
try {
const fs = this.terminal.fs;
const data = fs.readFile('/mindmaps/' + args[0] + '.json');
if (!data) return this.terminal.print('No saved mindmap: ' + args[0], 'error');
this.root = JSON.parse(data);
this.terminal.print('Loaded mindmap: ' + args[0] + ' (' + this._countNodes(this.root) + ' nodes)', 'success');
} catch (e) {
this.terminal.print('Failed to load: ' + e.message, 'error');
}
}
_list() {
const fs = this.terminal.fs;
const files = fs.readDir('/mindmaps') || [];
const keys = files.filter(f => 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));
}
_delete(args) {
if (!args[0]) return this.terminal.print('Usage: mindmap delete <name>', 'warning');
const fs = this.terminal.fs;
fs.deleteFile('/mindmaps/' + args[0] + '.json');
this.terminal.print('Deleted mindmap: ' + args[0], 'success');
}
_rename(args) {
if (args.length < 2) return this.terminal.print('Usage: mindmap rename <id> <text>', 'warning');
if (!this.root) return this.terminal.print('No mindmap.', 'warning');
const id = args[0];
const text = args.slice(1).join(' ');
const node = this._findNode(id, this.root);
if (!node) return this.terminal.print('Node not found: ' + id, 'error');
node.text = text;
this.terminal.print('Renamed ' + id + ' to: ' + text, 'success');
}
_color(args) {
if (args.length < 2) return this.terminal.print('Usage: mindmap color <id> <color>', 'warning');
if (!this.root) return this.terminal.print('No mindmap.', 'warning');
const id = 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._findNode(id, this.root);
if (!node) return this.terminal.print('Node not found: ' + id, 'error');
node.color = color;
this.terminal.print('Set color for ' + id + ' to ' + color, 'success');
}
_tag(args) {
if (args.length < 2) return this.terminal.print('Usage: mindmap tag <id> <tag>', 'warning');
if (!this.root) return this.terminal.print('No mindmap.', 'warning');
const id = args[0];
const tag = args.slice(1).join(' ');
const node = this._findNode(id, this.root);
if (!node) return this.terminal.print('Node not found: ' + id, 'error');
if (!node.tags) node.tags = [];
node.tags.push(tag);
this.terminal.print('Added tag "' + tag + '" to ' + id, 'success');
}
_svg(args) {
if (!this.root) return this.terminal.print('No mindmap. Create one with mindmap new first.', 'warning');
const saveName = args[0] || null;
// Layout settings
const nw = 160, nh = 44, hGap = 24, vGap = 60, pad = 30;
// Color mapping from name to hex
const colorMap = {
red: '#fb7185', green: '#34d399', yellow: '#fbbf24', blue: '#818cf8',
cyan: '#22d3ee', magenta: '#e879f9', white: '#e0e0e0'
};
const layout = { nodes: [], edges: [], width: 0, height: 0, _y: pad };
const layoutTree = (node, x) => {
const myY = layout._y;
const myX = x;
const color = colorMap[node.color] || '#818cf8';
layout.nodes.push({ x: myX, y: myY, w: nw, h: nh, text: node.text, color, id: node.id });
if (node.children && node.children.length > 0) {
const totalW = node.children.length * nw + (node.children.length - 1) * hGap;
let cx = x + nw / 2 - totalW / 2;
layout._y += nh + vGap;
for (const child of node.children) {
const childX = cx;
layout.edges.push({ x1: myX + nw / 2, y1: myY, x2: childX + nw / 2, y2: layout._y - vGap + nh });
layoutTree(child, childX);
cx += nw + hGap;
}
layout._y -= nh + vGap;
}
const rightEdge = x + nw;
if (rightEdge > layout.width) layout.width = rightEdge;
};
layout._y = pad;
layoutTree(this.root, pad);
layout.height = layout._y + nh + pad;
// Build SVG
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${layout.width + pad} ${layout.height}" style="max-width:100%;background:#0a0a14;font-family:monospace;border-radius:8px;">`;
// Edges first (behind nodes)
for (const e of layout.edges) {
const mx = (e.x1 + e.x2) / 2;
svg += `<path d="M${e.x1},${e.y1} C${mx},${e.y1 + 20} ${mx},${e.y2 - 20} ${e.x2},${e.y2}" fill="none" stroke="#2a2a4a" stroke-width="1.5"/>`;
}
// Nodes on top
for (const n of layout.nodes) {
svg += `<rect x="${n.x}" y="${n.y}" width="${n.w}" height="${n.h}" rx="6" fill="${n.color}18" stroke="${n.color}" stroke-width="1.5"/>`;
const text = n.text.length > 18 ? n.text.substring(0, 16) + '..' : n.text;
svg += `<text x="${n.x + n.w / 2}" y="${n.y + n.h / 2 + 4}" text-anchor="middle" fill="#e0e0e0" font-size="10" font-family="monospace">${text.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')}</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', 'success');
} catch (e) {
this.terminal.print('Failed to save SVG: ' + e.message, 'error');
}
}
}
}
if (typeof window.webx11 !== 'undefined' && window.webx11) {
webx11.moduleManager.registerModule(new MindmapModule(), 'https://webx11.duckdns.org/spaces/newos4/modules/ext/mindmap.js');
}
|