-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportmm.html
More file actions
581 lines (489 loc) · 16 KB
/
exportmm.html
File metadata and controls
581 lines (489 loc) · 16 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Text to Mind Map</title>
<style>
/* PWA & Font setup */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
</style>
<link rel="manifest" href="manifest.json">
<meta name="theme-color" content="#4a90e2">
<link rel="apple-touch-icon" href="icon.svg">
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./service-worker.js');
});
}
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--bg-gradient: #ffffff;
--glass-bg: rgba(255, 255, 255, 0.9);
--glass-border: rgba(0, 0, 0, 0.1);
--glass-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
--text-primary: #333333;
--text-secondary: #666666;
--accent-color: #4a90e2;
}
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: var(--bg-gradient);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
color: var(--text-primary);
}
.container {
background: var(--glass-bg);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border-radius: 20px;
border: 1px solid var(--glass-border);
box-shadow: var(--glass-shadow);
padding: 40px;
max-width: 600px;
width: 100%;
}
h1 {
font-size: 32px;
font-weight: 700;
margin-bottom: 10px;
text-align: center;
}
.subtitle {
text-align: center;
color: var(--text-secondary);
margin-bottom: 30px;
font-size: 14px;
}
.form-group {
margin-bottom: 24px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 500;
font-size: 14px;
color: var(--text-primary);
}
input[type="text"],
textarea {
width: 100%;
padding: 12px 16px;
background: #f9fafb;
border: 1px solid #ddd;
border-radius: 12px;
color: var(--text-primary);
font-size: 14px;
font-family: inherit;
transition: all 0.3s ease;
}
input[type="text"]:focus,
textarea:focus {
outline: none;
background: #ffffff;
border-color: var(--accent-color);
box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
}
input[type="text"]::placeholder,
textarea::placeholder {
color: #9ca3af;
}
textarea {
min-height: 200px;
resize: vertical;
font-family: 'Monaco', 'Courier New', monospace;
line-height: 1.6;
}
button {
width: 100%;
padding: 14px 24px;
background: var(--accent-color);
border: none;
border-radius: 12px;
color: white;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 6px rgba(74, 144, 226, 0.2);
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 12px rgba(74, 144, 226, 0.3);
background: #357abd;
}
button:active {
transform: translateY(0);
}
.instructions {
background: #f3f4f6;
border-radius: 12px;
padding: 16px;
margin-bottom: 24px;
font-size: 13px;
line-height: 1.6;
color: var(--text-secondary);
}
.instructions strong {
color: var(--text-primary);
display: block;
margin-bottom: 8px;
}
.instructions code {
background: #e5e7eb;
padding: 2px 6px;
border-radius: 4px;
font-family: 'Monaco', 'Courier New', monospace;
font-size: 12px;
}
.example {
margin-top: 8px;
padding: 8px;
background: #e5e7eb;
border-radius: 6px;
font-family: 'Monaco', 'Courier New', monospace;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<h1>📝 Text to Mind Map</h1>
<p class="subtitle">Convert indented text into beautiful mind maps</p>
<form id="export-form">
<div class="instructions">
<strong>How to use:</strong>
Use <code>Tab</code> or consistent spaces for indentation. Start with emojis for visual appeal!
<div class="example">
My Project<br>
💡 Ideas<br>
Research topic<br>
Brainstorm solutions<br>
🎯 Goals<br>
Complete by Friday
</div>
</div>
<div class="form-group">
<label for="central-subject">Central Topic</label>
<input type="text" id="central-subject" name="central-subject" placeholder="Enter your main topic" required>
</div>
<div class="form-group">
<label for="nodes">Nodes (use Tab or spaces for indentation)</label>
<textarea id="nodes" name="nodes" placeholder="💡 First idea 	Sub-idea 🎯 Second idea"></textarea>
</div>
<button type="submit">🚀 Create Mind Map</button>
</form>
</div>
<script>
const form = document.getElementById('export-form');
const nodesTextarea = document.getElementById('nodes');
// Enable Tab key indentation in textarea
nodesTextarea.addEventListener('keydown', function (e) {
if (e.key === 'Tab') {
e.preventDefault();
const start = this.selectionStart;
const end = this.selectionEnd;
// Insert tab character
this.value = this.value.substring(0, start) + '\t' + this.value.substring(end);
// Move cursor to right of inserted tab
this.selectionStart = this.selectionEnd = start + 1;
}
});
// Check for URL parameters to pre-fill the form
window.addEventListener('DOMContentLoaded', () => {
const urlParams = new URLSearchParams(window.location.search);
const titleParam = urlParams.get('title');
const nodesParam = urlParams.get('nodes');
const nodesB64Param = urlParams.get('nodes_b64');
if (titleParam) {
document.getElementById('central-subject').value = titleParam;
}
if (nodesB64Param) {
try {
// decodeURIComponent(escape(atob(...))) handles UTF-8 characters correctly
const decodedNodes = decodeURIComponent(escape(atob(nodesB64Param)));
document.getElementById('nodes').value = decodedNodes;
} catch (e) {
console.error('Failed to decode nodes_b64 parameter:', e);
}
} else if (nodesParam) {
document.getElementById('nodes').value = nodesParam;
}
});
form.addEventListener('submit', function (event) {
event.preventDefault();
const centralSubject = document.getElementById('central-subject').value.trim();
const nodesText = document.getElementById('nodes').value.trim();
if (!centralSubject) {
alert('Please enter a central topic.');
return;
}
// Prepare the central node - will be centered later
const centralNode = {
id: 'central-node',
parentId: null,
x: 0,
y: 0,
color: '#FFFFFF',
text: centralSubject
};
// Parse nodes
const parsedNodes = parseNodes(nodesText);
if (parsedNodes === null) {
alert('Failed to parse nodes. Please check your indentation is consistent.');
return;
}
// Combine central node with other parsed nodes
const allNodes = [centralNode, ...parsedNodes];
// Position the nodes using improved auto-layout
positionNodes(allNodes);
// Serialize and encode data
const serializedData = JSON.stringify(allNodes);
const encodedData = btoa(encodeURIComponent(serializedData));
// Construct the URL to mindmapplain.html with encoded data in hash
const mindmapURL = 'mindmapplain.html#' + encodedData;
// Open the mindmap in a new browser window
window.open(mindmapURL, '_blank');
});
function parseNodes(text) {
if (!text || text.trim() === '') return [];
const lines = text.split('\n');
const nodes = [];
const stack = [];
const emojis = ['💡', '🧠', '📚', '🎯', '✅', '❌', '🛠️', '🚦', '🎬', '🔬', '🎨', '📊'];
// Detect indentation type and size from first indented line
let indentSize = null;
let indentChar = null;
for (const line of lines) {
if (line.trim() !== '' && line.match(/^[\t ]+/)) {
const leadingWhitespace = line.match(/^[\t ]+/)[0];
if (leadingWhitespace[0] === '\t') {
indentChar = '\t';
indentSize = 1;
} else {
indentChar = ' ';
indentSize = leadingWhitespace.length;
}
break;
}
}
// If no indentation detected, default to tab
if (indentSize === null) {
indentChar = '\t';
indentSize = 1;
}
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.trim() === '') continue;
// Calculate depth
let depth = 0;
const leadingWhitespace = line.match(/^[\t ]*/)[0];
if (leadingWhitespace.length > 0) {
if (indentChar === '\t') {
depth = leadingWhitespace.split('\t').length - 1;
} else {
depth = Math.floor(leadingWhitespace.length / indentSize);
}
}
const content = line.trim();
if (content === '') continue;
let emoji = '';
let nodeText = content;
const firstChar = content.charAt(0);
if (emojis.includes(firstChar)) {
emoji = firstChar;
nodeText = content.substring(1).trim();
}
if (nodeText === '') continue;
const nodeId = generateId();
const newNode = {
id: nodeId,
parentId: null,
color: '#FFFFFF',
text: nodeText,
x: null,
y: null
};
if (depth === 0) {
newNode.parentId = 'central-node';
nodes.push(newNode);
stack.length = 0;
stack.push(newNode);
} else {
if (depth > stack.length) {
console.error(`Invalid indentation at line ${i + 1}: depth ${depth} but stack length ${stack.length}`);
return null;
}
while (depth < stack.length) {
stack.pop();
}
const parentNode = stack[stack.length - 1];
if (!parentNode) {
console.error(`No parent found for line ${i + 1}`);
return null;
}
newNode.parentId = parentNode.id;
nodes.push(newNode);
stack.push(newNode);
}
}
console.log('Parsed nodes:', nodes.length, 'nodes');
return nodes;
}
function generateId() {
return 'node-' + Math.random().toString(36).substr(2, 9);
}
function positionNodes(nodes) {
console.log('=== positionNodes called with', nodes.length, 'nodes ===');
const centralNode = nodes.find(n => n.id === 'central-node');
if (!centralNode) {
console.error('Central node not found.');
return;
}
// Constants for layout
const NODE_WIDTH = 140;
const NODE_HEIGHT = 60;
const H_GAP = 60;
const V_GAP = 20;
// 1. Build Tree Structure
function buildTree(nodeId) {
const node = nodes.find(n => n.id === nodeId);
if (!node) return null;
const layoutNode = {
id: node.id,
text: node.text,
width: NODE_WIDTH,
height: NODE_HEIGHT,
x: 0,
y: 0,
children: [],
side: null, // 'left' or 'right'
treeHeight: 0
};
// Find children
const childNodes = nodes.filter(n => n.parentId === nodeId);
layoutNode.children = childNodes.map(c => buildTree(c.id)).filter(n => n !== null);
return layoutNode;
}
const root = buildTree('central-node');
if (!root) return;
// Position root at 0,0 initially
root.x = 0;
root.y = 0;
// 2. Assign Sides (Balance Left/Right for root children)
const rightChildren = [];
const leftChildren = [];
root.children.forEach((child, index) => {
if (index % 2 === 0) {
child.side = "right";
rightChildren.push(child);
} else {
child.side = "left";
leftChildren.push(child);
}
});
// 3. Calculate Tree Heights (Pass 1: Bottom-Up)
function calculateTreeHeight(node) {
if (!node.children || node.children.length === 0) {
node.treeHeight = node.height;
return node.height;
}
let h = 0;
node.children.forEach(child => {
h += calculateTreeHeight(child);
});
// Add gaps between children
h += (node.children.length - 1) * V_GAP;
node.treeHeight = Math.max(node.height, h);
return node.treeHeight;
}
root.children.forEach(calculateTreeHeight);
// 4. Layout Children (Pass 2: Top-Down)
function layoutChildren(parent) {
let pRightChildren, pLeftChildren;
if (parent.id === 'central-node') {
pRightChildren = parent.children.filter(c => c.side === 'right');
pLeftChildren = parent.children.filter(c => c.side === 'left');
} else {
if (parent.side === 'right') {
pRightChildren = parent.children;
pLeftChildren = [];
} else {
pRightChildren = [];
pLeftChildren = parent.children;
}
}
const placeStack = (children, direction) => {
if (children.length === 0) return;
const totalHeight = children.reduce((sum, c) => sum + c.treeHeight, 0) + (children.length - 1) * V_GAP;
let currentY = parent.y + (parent.height / 2) - (totalHeight / 2);
children.forEach(child => {
child.side = direction;
const childSlotHeight = child.treeHeight;
const childY = currentY + (childSlotHeight - child.height) / 2;
child.y = childY;
if (direction === 'right') {
child.x = parent.x + parent.width + H_GAP;
} else {
child.x = parent.x - child.width - H_GAP;
}
layoutChildren(child);
currentY += childSlotHeight + V_GAP;
});
};
placeStack(pRightChildren, 'right');
placeStack(pLeftChildren, 'left');
}
layoutChildren(root);
// 5. Update original nodes array with new positions
function updateNodesData(layoutNode) {
const dataNode = nodes.find(n => n.id === layoutNode.id);
if (dataNode) {
dataNode.x = layoutNode.x;
dataNode.y = layoutNode.y;
}
layoutNode.children.forEach(updateNodesData);
}
updateNodesData(root);
// 6. Center the map at (600, 400)
let minX = Infinity, maxX = -Infinity;
let minY = Infinity, maxY = -Infinity;
nodes.forEach(node => {
minX = Math.min(minX, node.x);
maxX = Math.max(maxX, node.x);
minY = Math.min(minY, node.y);
maxY = Math.max(maxY, node.y);
});
const targetCenterX = 600;
const targetCenterY = 400;
const currentCenterX = (minX + maxX) / 2;
const currentCenterY = (minY + maxY) / 2;
const offsetX = targetCenterX - currentCenterX;
const offsetY = targetCenterY - currentCenterY;
nodes.forEach(node => {
node.x += offsetX;
node.y += offsetY;
});
}
function getNodeDepth(node, nodes, depth = 0) {
if (node.parentId === null) {
return depth;
}
const parent = nodes.find(n => n.id === node.parentId);
if (!parent) return depth;
return getNodeDepth(parent, nodes, depth + 1);
}
</script>
</body>
</html>