Repository navigation
Expand file tree
/
Copy pathmindmap.html
More file actions
721 lines (670 loc) · 38.7 KB
/
Copy pathmindmap.html
File metadata and controls
721 lines (670 loc) · 38.7 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A fast, private mind map editor that runs entirely in your browser.">
<title>Branchly — Mind Map</title>
<style>
:root {
--ink: #17231f;
--muted: #6d7a75;
--panel: rgba(255, 255, 255, .94);
--line: #dfe6e2;
--green: #1f9d6a;
--green-dark: #137a51;
--shadow: 0 14px 35px rgba(25, 48, 40, .12);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
color: var(--ink);
font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
background: #f6f8f6;
}
button, input { font: inherit; }
button { color: inherit; }
.app { height: 100%; display: grid; grid-template-rows: 64px 1fr; }
.topbar {
z-index: 10;
display: flex;
align-items: center;
gap: 18px;
padding: 0 22px;
background: var(--panel);
border-bottom: 1px solid var(--line);
backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; letter-spacing: -.03em; }
.brand-mark { width: 34px; height: 34px; display: grid; place-items: center; background: var(--green); color: white; border-radius: 11px; box-shadow: 0 7px 16px rgba(31, 157, 106, .25); }
.brand-mark svg { width: 20px; }
.divider { width: 1px; height: 28px; background: var(--line); }
#mapTitle { border: 0; outline: 0; background: transparent; font-weight: 700; font-size: 16px; min-width: 150px; max-width: 280px; color: var(--ink); }
#mapTitle:focus { border-bottom: 2px solid #8bd5b5; }
.save-state { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.save-dot { width: 7px; height: 7px; border-radius: 50%; background: #51b98a; }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.drive-wrap { position: relative; }
.drive-menu {
position: absolute;
top: 46px;
right: 0;
width: 210px;
padding: 7px;
background: white;
border: 1px solid var(--line);
border-radius: 12px;
box-shadow: var(--shadow);
}
.drive-menu[hidden] { display: none; }
.drive-menu button { width: 100%; justify-content: flex-start; border: 0; box-shadow: none; }
.drive-menu button:hover { background: #eef5f1; transform: none; box-shadow: none; }
.drive-menu .danger { color: #c34c4c; }
.icon-btn, .action-btn {
border: 1px solid var(--line);
background: white;
border-radius: 10px;
height: 38px;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
cursor: pointer;
transition: .18s ease;
}
.icon-btn { width: 38px; padding: 0; }
.action-btn { padding: 0 14px; font-size: 13px; font-weight: 700; }
.icon-btn:hover, .action-btn:hover { border-color: #b9c9c1; transform: translateY(-1px); box-shadow: 0 5px 15px rgba(25, 48, 40, .08); }
.action-btn.primary { background: var(--green); color: white; border-color: var(--green); }
.action-btn.primary:hover { background: var(--green-dark); }
button svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.workspace { position: relative; min-height: 0; }
#canvas {
position: absolute;
inset: 0;
overflow: hidden;
cursor: grab;
background-color: #f7f9f7;
background-image: radial-gradient(#cfd8d3 1px, transparent 1px);
background-size: 24px 24px;
}
#canvas.panning { cursor: grabbing; }
#stage { position: absolute; left: 0; top: 0; width: 2400px; height: 1500px; transform-origin: 0 0; }
#connections { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.connection { fill: none; stroke-width: 3; stroke-linecap: round; opacity: .72; }
.node {
position: absolute;
display: flex;
align-items: center;
min-width: 126px;
max-width: 240px;
min-height: 46px;
padding: 11px 16px;
border-radius: 13px;
background: white;
border: 2px solid var(--node-color, #55ae8a);
box-shadow: 0 8px 22px rgba(25, 48, 40, .11);
cursor: move;
user-select: none;
transition: box-shadow .15s, transform .15s;
}
.node:hover { box-shadow: 0 11px 28px rgba(25, 48, 40, .16); }
.node.selected { box-shadow: 0 0 0 4px color-mix(in srgb, var(--node-color) 20%, transparent), 0 12px 28px rgba(25, 48, 40, .16); }
.node.root { min-width: 180px; min-height: 62px; padding: 15px 21px; color: white; border: 0; background: var(--node-color); border-radius: 17px; font-size: 18px; font-weight: 800; }
.node-text { outline: 0; line-height: 1.35; overflow-wrap: anywhere; width: 100%; }
.node-text[contenteditable="true"] { cursor: text; user-select: text; }
.toolbar {
position: absolute;
z-index: 5;
top: 18px;
left: 18px;
width: 56px;
padding: 7px;
display: flex;
flex-direction: column;
gap: 5px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel);
box-shadow: var(--shadow);
}
.toolbar .icon-btn { border: 0; background: transparent; }
.toolbar .icon-btn:hover { background: #eef5f1; box-shadow: none; }
.toolbar hr { width: 28px; border: 0; border-top: 1px solid var(--line); margin: 3px auto; }
.help-card {
position: absolute;
z-index: 4;
left: 88px;
top: 18px;
padding: 9px 13px;
color: #53615b;
background: rgba(255,255,255,.88);
border: 1px solid var(--line);
border-radius: 11px;
font-size: 12px;
pointer-events: none;
}
.help-card b { color: var(--ink); }
.zoom-controls {
position: absolute;
z-index: 5;
left: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 3px;
padding: 5px;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 12px;
box-shadow: var(--shadow);
}
.zoom-controls .icon-btn { border: 0; background: transparent; }
#zoomLabel { width: 48px; text-align: center; font-size: 12px; font-weight: 700; color: var(--muted); }
.inspector {
position: absolute;
z-index: 5;
right: 18px;
top: 18px;
width: 238px;
padding: 17px;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 16px;
box-shadow: var(--shadow);
transition: transform .2s, opacity .2s;
}
.inspector.hidden { transform: translateX(25px); opacity: 0; pointer-events: none; }
.inspector-title { display: flex; align-items: center; justify-content: space-between; margin-bottom: 15px; font-size: 13px; font-weight: 800; }
.label { display: block; color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; margin: 14px 0 8px; }
.colors { display: flex; gap: 8px; }
.swatch { width: 26px; height: 26px; border: 3px solid white; border-radius: 50%; background: var(--swatch); box-shadow: 0 0 0 1px #ccd5d0; cursor: pointer; }
.swatch.active { box-shadow: 0 0 0 2px var(--swatch); }
.inspector-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 17px; }
.inspector-actions button { padding: 9px 7px; border: 1px solid var(--line); border-radius: 9px; background: white; font-size: 12px; font-weight: 700; cursor: pointer; }
.inspector-actions .danger { color: #c34c4c; }
.toast {
position: absolute;
z-index: 20;
left: 50%;
bottom: 26px;
transform: translate(-50%, 15px);
padding: 10px 15px;
color: white;
background: #1e2d27;
border-radius: 10px;
font-size: 13px;
opacity: 0;
pointer-events: none;
transition: .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.dialog { width: min(500px, calc(100% - 32px)); padding: 0; border: 0; border-radius: 16px; box-shadow: 0 24px 70px rgba(25, 48, 40, .25); }
.dialog::backdrop { background: rgba(23, 35, 31, .42); backdrop-filter: blur(2px); }
.dialog-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px; border-bottom: 1px solid var(--line); }
.dialog-header h2 { margin: 0; font-size: 17px; }
.drive-files { max-height: 360px; min-height: 100px; padding: 8px; overflow: auto; }
.drive-file { width: 100%; padding: 12px; text-align: left; border: 0; border-radius: 10px; background: white; cursor: pointer; }
.drive-file:hover { background: #eef5f1; }
.drive-file strong, .drive-file span { display: block; }
.drive-file span, .empty-state { margin-top: 3px; color: var(--muted); font-size: 12px; }
.empty-state { padding: 28px; text-align: center; }
.mobile-label { display: none; }
@media (max-width: 700px) {
.topbar { padding: 0 12px; gap: 10px; }
.brand span, .save-state, .secondary-action { display: none; }
#mapTitle { min-width: 90px; max-width: 130px; }
.action-btn { padding: 0 11px; }
.action-btn .desktop-label { display: none; }
.mobile-label { display: inline; }
.help-card, .inspector { display: none; }
}
</style>
</head>
<body>
<main class="app">
<header class="topbar">
<a class="brand" href="index.html" aria-label="Back to tools" style="color:inherit;text-decoration:none">
<span class="brand-mark"><svg viewBox="0 0 24 24"><path d="M5 5v14M5 9h6a4 4 0 0 0 4-4M5 15h7a5 5 0 0 1 5 5"/></svg></span>
<span>Branchly</span>
</a>
<span class="divider"></span>
<input id="mapTitle" value="Product Launch Plan" aria-label="Mind map title">
<span class="save-state"><span class="save-dot"></span><span id="saveText">Saved locally</span></span>
<div class="top-actions">
<button class="icon-btn secondary-action" id="undoBtn" title="Undo (Ctrl+Z)" aria-label="Undo"><svg viewBox="0 0 24 24"><path d="M9 14 4 9l5-5"/><path d="M4 9h10a6 6 0 0 1 6 6v1"/></svg></button>
<button class="icon-btn secondary-action" id="redoBtn" title="Redo (Ctrl+Shift+Z)" aria-label="Redo"><svg viewBox="0 0 24 24"><path d="m15 14 5-5-5-5"/><path d="M20 9H10a6 6 0 0 0-6 6v1"/></svg></button>
<button class="action-btn" id="resetBtn"><svg viewBox="0 0 24 24"><path d="M3 12a9 9 0 1 0 3-6.7L3 8"/><path d="M3 3v5h5"/></svg><span class="desktop-label">New map</span></button>
<button class="action-btn" id="importBtn"><svg viewBox="0 0 24 24"><path d="M12 16V4m0 0 4 4m-4-4L8 8"/><path d="M5 20h14"/></svg><span class="desktop-label">Import</span><span class="mobile-label">Open</span></button>
<input id="importFile" type="file" accept="application/json,.json" hidden>
<div class="drive-wrap">
<button class="action-btn" id="driveBtn" disabled aria-haspopup="menu" aria-expanded="false"><svg viewBox="0 0 24 24"><path d="m8 3-5 9 4 7h10l4-7-5-9Z"/><path d="m8 3 4 7 4-7M3 12h9l5 7"/></svg><span class="desktop-label">Drive</span></button>
<div class="drive-menu" id="driveMenu" role="menu" hidden>
<button class="action-btn" id="connectDrive" role="menuitem">Connect Google Drive</button>
<button class="action-btn" id="openDrive" role="menuitem" hidden>Open from Drive…</button>
<button class="action-btn" id="saveDrive" role="menuitem" hidden>Save to Drive</button>
<button class="action-btn danger" id="disconnectDrive" role="menuitem" hidden>Disconnect</button>
</div>
</div>
<button class="action-btn primary" id="exportBtn"><svg viewBox="0 0 24 24"><path d="M12 3v12m0 0 4-4m-4 4-4-4"/><path d="M5 19h14"/></svg><span class="desktop-label">Export</span><span class="mobile-label">Save</span></button>
</div>
</header>
<section class="workspace">
<div id="canvas" aria-label="Mind map canvas">
<div id="stage"><svg id="connections"></svg><div id="nodes"></div></div>
</div>
<nav class="toolbar" aria-label="Mind map tools">
<button class="icon-btn" id="addChildBtn" title="Add child (Tab)" aria-label="Add child"><svg viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></svg></button>
<button class="icon-btn" id="addSiblingBtn" title="Add sibling (Enter)" aria-label="Add sibling"><svg viewBox="0 0 24 24"><path d="M4 6h7M4 18h7M7 6v12M15 12h6M18 9v6"/></svg></button>
<hr>
<button class="icon-btn" id="editBtn" title="Edit selected (E)" aria-label="Edit selected"><svg viewBox="0 0 24 24"><path d="m4 16-1 5 5-1L19 9l-4-4Z"/><path d="m13.5 6.5 4 4"/></svg></button>
<button class="icon-btn" id="deleteBtn" title="Delete selected" aria-label="Delete selected"><svg viewBox="0 0 24 24"><path d="M4 7h16M9 7V4h6v3M7 7l1 13h8l1-13"/></svg></button>
<hr>
<button class="icon-btn" id="centerBtn" title="Fit map" aria-label="Fit map"><svg viewBox="0 0 24 24"><path d="M8 3H3v5M16 3h5v5M8 21H3v-5M16 21h5v-5"/></svg></button>
</nav>
<div class="help-card"><b>Tip:</b> Select a thought, then press <b>Tab</b> to branch out</div>
<aside class="inspector hidden" id="inspector">
<div class="inspector-title"><span>Style thought</span><button class="icon-btn" id="closeInspector" aria-label="Close inspector" style="width:28px;height:28px;border:0"><svg viewBox="0 0 24 24"><path d="m6 6 12 12M18 6 6 18"/></svg></button></div>
<span class="label">Branch color</span>
<div class="colors" id="colors"></div>
<span class="label">Quick actions</span>
<div class="inspector-actions">
<button id="panelChild">+ Child</button>
<button id="panelEdit">Edit text</button>
<button class="danger" id="panelDelete">Delete</button>
<button id="panelCenter">Center</button>
</div>
</aside>
<div class="zoom-controls">
<button class="icon-btn" id="zoomOut" aria-label="Zoom out"><svg viewBox="0 0 24 24"><path d="M5 12h14"/></svg></button>
<span id="zoomLabel">100%</span>
<button class="icon-btn" id="zoomIn" aria-label="Zoom in"><svg viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></svg></button>
</div>
<div class="toast" id="toast"></div>
</section>
</main>
<dialog class="dialog" id="driveDialog">
<div class="dialog-header">
<h2>Open from Google Drive</h2>
<button class="icon-btn" id="closeDriveDialog" aria-label="Close"><svg viewBox="0 0 24 24"><path d="m6 6 12 12M18 6 6 18"/></svg></button>
</div>
<div class="drive-files" id="driveFiles"><p class="empty-state">Loading mind maps…</p></div>
</dialog>
<script>
(() => {
const palette = ['#1f9d6a', '#ee7d52', '#5c83e6', '#ae6ed0', '#dfb143', '#e45f75'];
const defaults = [
{ id: 1, parent: null, text: 'Product Launch', x: 1050, y: 690, color: palette[0] },
{ id: 2, parent: 1, text: 'Research', x: 760, y: 455, color: palette[2] },
{ id: 3, parent: 2, text: 'Customer interviews', x: 485, y: 365, color: palette[2] },
{ id: 4, parent: 2, text: 'Competitor scan', x: 485, y: 495, color: palette[2] },
{ id: 5, parent: 1, text: 'Build', x: 1370, y: 430, color: palette[1] },
{ id: 6, parent: 5, text: 'Prototype', x: 1635, y: 350, color: palette[1] },
{ id: 7, parent: 5, text: 'User testing', x: 1635, y: 485, color: palette[1] },
{ id: 8, parent: 1, text: 'Launch', x: 1390, y: 890, color: palette[3] },
{ id: 9, parent: 8, text: 'Press kit', x: 1660, y: 810, color: palette[3] },
{ id: 10, parent: 8, text: 'Launch event', x: 1660, y: 945, color: palette[3] },
{ id: 11, parent: 1, text: 'Measure', x: 750, y: 925, color: palette[4] },
{ id: 12, parent: 11, text: 'Activation rate', x: 475, y: 855, color: palette[4] },
{ id: 13, parent: 11, text: 'Feedback loop', x: 475, y: 985, color: palette[4] }
];
const $ = id => document.getElementById(id);
const canvas = $('canvas'), stage = $('stage'), nodesLayer = $('nodes'), svg = $('connections');
let nodes = [], selected = 1, nextId = 14, scale = 1, pan = { x: 0, y: 0 };
let dragging = null, panning = null, history = [], future = [], saveTimer;
const GOOGLE_CLIENT_ID = '1001017008332-peajlj58k24flbk6q9b8s618mf934uv3.apps.googleusercontent.com';
const DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive.file';
const DRIVE_FOLDER = 'Branchly Workspace';
let tokenClient, accessToken = null, gapiReady = false, identityReady = false;
let driveFileId = null, driveFolderId = null;
function load() {
try {
const saved = JSON.parse(localStorage.getItem('branchly-map'));
nodes = saved?.nodes?.length ? saved.nodes : structuredClone(defaults);
$('mapTitle').value = saved?.title || 'Product Launch Plan';
nextId = Math.max(...nodes.map(n => n.id)) + 1;
} catch { nodes = structuredClone(defaults); }
}
function snapshot() { return JSON.stringify({ nodes, title: $('mapTitle').value }); }
function checkpoint() { history.push(snapshot()); if (history.length > 60) history.shift(); future = []; }
function restore(data) {
const state = JSON.parse(data); nodes = state.nodes; $('mapTitle').value = state.title; selected = nodes.some(n => n.id === selected) ? selected : nodes[0]?.id; render(); save();
}
function undo() { if (!history.length) return; future.push(snapshot()); restore(history.pop()); }
function redo() { if (!future.length) return; history.push(snapshot()); restore(future.pop()); }
function save() {
$('saveText').textContent = 'Saving…';
clearTimeout(saveTimer);
saveTimer = setTimeout(() => {
localStorage.setItem('branchly-map', snapshot());
$('saveText').textContent = 'Saved locally';
}, 220);
}
function applyTransform() {
stage.style.transform = `translate(${pan.x}px, ${pan.y}px) scale(${scale})`;
$('zoomLabel').textContent = `${Math.round(scale * 100)}%`;
}
function render() {
nodesLayer.innerHTML = '';
nodes.forEach(node => {
const el = document.createElement('div');
el.className = `node${node.parent === null ? ' root' : ''}${node.id === selected ? ' selected' : ''}`;
el.dataset.id = node.id; el.style.left = `${node.x}px`; el.style.top = `${node.y}px`; el.style.setProperty('--node-color', node.color);
const text = document.createElement('div'); text.className = 'node-text'; text.textContent = node.text; el.appendChild(text);
el.addEventListener('pointerdown', startDrag);
el.addEventListener('dblclick', () => editNode(node.id));
nodesLayer.appendChild(el);
});
requestAnimationFrame(drawConnections);
$('inspector').classList.toggle('hidden', !nodes.some(n => n.id === selected));
renderColors();
}
function drawConnections() {
svg.innerHTML = '';
nodes.forEach(node => {
if (node.parent === null) return;
const parentEl = document.querySelector(`.node[data-id="${node.parent}"]`);
const childEl = document.querySelector(`.node[data-id="${node.id}"]`);
if (!parentEl || !childEl) return;
const p = nodes.find(n => n.id === node.parent);
const leftward = node.x < p.x;
const x1 = p.x + (leftward ? 0 : parentEl.offsetWidth), y1 = p.y + parentEl.offsetHeight / 2;
const x2 = node.x + (leftward ? childEl.offsetWidth : 0), y2 = node.y + childEl.offsetHeight / 2;
const bend = Math.max(55, Math.abs(x2 - x1) * .48);
const d = leftward ? `M${x1} ${y1} C${x1-bend} ${y1},${x2+bend} ${y2},${x2} ${y2}` : `M${x1} ${y1} C${x1+bend} ${y1},${x2-bend} ${y2},${x2} ${y2}`;
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute('d', d); path.setAttribute('class', 'connection'); path.setAttribute('stroke', node.color); svg.appendChild(path);
});
}
function select(id) { selected = id; render(); }
function startDrag(e) {
if (e.target.isContentEditable) return;
e.stopPropagation();
const id = Number(e.currentTarget.dataset.id); select(id);
const node = nodes.find(n => n.id === id);
checkpoint();
dragging = { id, startX: e.clientX, startY: e.clientY, x: node.x, y: node.y };
e.currentTarget.setPointerCapture(e.pointerId);
}
function addChild(parentId = selected) {
const parent = nodes.find(n => n.id === parentId); if (!parent) return;
checkpoint();
const siblings = nodes.filter(n => n.parent === parentId);
const direction = parent.parent === null ? (siblings.length % 2 ? -1 : 1) : Math.sign(parent.x - (nodes.find(n => n.id === parent.parent)?.x || 0)) || 1;
const color = parent.parent === null ? palette[(siblings.length + 1) % palette.length] : parent.color;
const node = { id: nextId++, parent: parentId, text: 'New thought', x: parent.x + direction * 275, y: parent.y + (siblings.length - 1) * 82, color };
nodes.push(node); selected = node.id; render(); save(); setTimeout(() => editNode(node.id), 20);
}
function addSibling() {
const node = nodes.find(n => n.id === selected); if (!node) return;
if (node.parent === null) return addChild(node.id);
addChild(node.parent);
}
function editNode(id = selected) {
const node = nodes.find(n => n.id === id), el = document.querySelector(`.node[data-id="${id}"] .node-text`); if (!node || !el) return;
checkpoint(); el.contentEditable = 'true'; el.focus(); document.execCommand('selectAll', false, null);
const finish = () => { el.contentEditable = 'false'; node.text = el.textContent.trim() || 'Untitled'; el.textContent = node.text; drawConnections(); save(); };
el.addEventListener('blur', finish, { once: true });
el.addEventListener('keydown', e => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); el.blur(); } if (e.key === 'Escape') el.blur(); });
}
function removeNode() {
const node = nodes.find(n => n.id === selected); if (!node || node.parent === null) return showToast('The central thought can’t be deleted');
checkpoint();
const remove = new Set([selected]); let changed = true;
while (changed) { changed = false; nodes.forEach(n => { if (remove.has(n.parent) && !remove.has(n.id)) { remove.add(n.id); changed = true; } }); }
selected = node.parent; nodes = nodes.filter(n => !remove.has(n.id)); render(); save(); showToast(`Deleted ${remove.size} thought${remove.size > 1 ? 's' : ''}`);
}
function renderColors() {
const current = nodes.find(n => n.id === selected);
$('colors').innerHTML = '';
palette.forEach(color => { const b = document.createElement('button'); b.className = `swatch${current?.color === color ? ' active' : ''}`; b.style.setProperty('--swatch', color); b.title = color; b.onclick = () => changeColor(color); $('colors').appendChild(b); });
}
function changeColor(color) {
const node = nodes.find(n => n.id === selected); if (!node) return; checkpoint();
const recolor = id => { nodes.filter(n => n.id === id || n.parent === id).forEach((n, i) => { n.color = color; if (i) recolor(n.id); }); };
recolor(node.id); render(); save();
}
function setZoom(value, cx = canvas.clientWidth / 2, cy = canvas.clientHeight / 2) {
const next = Math.min(1.6, Math.max(.4, value));
const worldX = (cx - pan.x) / scale, worldY = (cy - pan.y) / scale;
pan.x = cx - worldX * next; pan.y = cy - worldY * next; scale = next; applyTransform();
}
function fitMap() {
const pad = 150, minX = Math.min(...nodes.map(n => n.x)), maxX = Math.max(...nodes.map(n => n.x + 220));
const minY = Math.min(...nodes.map(n => n.y)), maxY = Math.max(...nodes.map(n => n.y + 70));
scale = Math.min(1, Math.max(.4, Math.min((canvas.clientWidth - pad * 2) / (maxX - minX), (canvas.clientHeight - pad) / (maxY - minY))));
pan.x = (canvas.clientWidth - (minX + maxX) * scale) / 2; pan.y = (canvas.clientHeight - (minY + maxY) * scale) / 2; applyTransform();
}
function centerSelected() {
const node = nodes.find(n => n.id === selected); if (!node) return;
pan.x = canvas.clientWidth / 2 - (node.x + 90) * scale; pan.y = canvas.clientHeight / 2 - (node.y + 28) * scale; applyTransform();
}
function showToast(message) { const t = $('toast'); t.textContent = message; t.classList.add('show'); setTimeout(() => t.classList.remove('show'), 1800); }
function setDriveConnected(connected) {
$('connectDrive').hidden = connected;
$('openDrive').hidden = !connected;
$('saveDrive').hidden = !connected;
$('disconnectDrive').hidden = !connected;
$('driveBtn').classList.toggle('primary', connected);
$('driveBtn').title = connected ? 'Google Drive connected' : 'Connect Google Drive';
}
function closeDriveMenu() {
$('driveMenu').hidden = true;
$('driveBtn').setAttribute('aria-expanded', 'false');
}
async function driveRequest(url, options = {}) {
const headers = new Headers(options.headers || {});
headers.set('Authorization', `Bearer ${accessToken}`);
const response = await fetch(url, { ...options, headers });
const data = response.status === 204 ? null : await response.json();
if (!response.ok) {
if (response.status === 401) {
accessToken = null;
setDriveConnected(false);
}
throw new Error(data?.error?.message || `Google Drive request failed (${response.status})`);
}
return data;
}
async function findOrCreateFolder(name, parentId) {
const escapedName = name.replace(/'/g, "\\'");
const parentQuery = parentId ? ` and '${parentId}' in parents` : '';
const query = `name='${escapedName}' and mimeType='application/vnd.google-apps.folder' and trashed=false${parentQuery}`;
const fields = encodeURIComponent('files(id,name)');
const result = await driveRequest(`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=${fields}`);
if (result.files?.length) return result.files[0].id;
const metadata = { name, mimeType: 'application/vnd.google-apps.folder' };
if (parentId) metadata.parents = [parentId];
const created = await driveRequest('https://www.googleapis.com/drive/v3/files?fields=id', {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(metadata)
});
return created.id;
}
async function getWorkspaceFolder() {
if (driveFolderId) return driveFolderId;
const toolsFolder = await findOrCreateFolder('itabu-tools');
driveFolderId = await findOrCreateFolder(DRIVE_FOLDER, toolsFolder);
return driveFolderId;
}
function driveFileName() {
const cleanTitle = $('mapTitle').value.trim().replace(/[\\/:*?"<>|]+/g, '-');
return `${cleanTitle || 'Untitled Mind Map'}.branchly.json`;
}
async function saveToDrive() {
if (!accessToken) return connectDrive(saveToDrive);
closeDriveMenu();
$('saveText').textContent = 'Saving to Drive…';
try {
const metadata = { name: driveFileName(), mimeType: 'application/json' };
if (!driveFileId) metadata.parents = [await getWorkspaceFolder()];
const form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
form.append('file', new Blob([snapshot()], { type: 'application/json' }));
const endpoint = driveFileId
? `https://www.googleapis.com/upload/drive/v3/files/${driveFileId}?uploadType=multipart&fields=id,name`
: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,name';
const saved = await driveRequest(endpoint, { method: driveFileId ? 'PATCH' : 'POST', body: form });
driveFileId = saved.id;
$('saveText').textContent = 'Saved to Google Drive';
showToast(`Saved ${saved.name || driveFileName()} to Drive`);
} catch (error) {
$('saveText').textContent = 'Saved locally';
showToast(`Drive save failed: ${error.message}`);
}
}
async function listDriveMaps() {
if (!accessToken) return connectDrive(listDriveMaps);
closeDriveMenu();
$('driveDialog').showModal();
$('driveFiles').innerHTML = '<p class="empty-state">Loading mind maps…</p>';
try {
const folderId = await getWorkspaceFolder();
const query = `'${folderId}' in parents and mimeType='application/json' and trashed=false`;
const fields = encodeURIComponent('files(id,name,modifiedTime,size)');
const order = encodeURIComponent('modifiedTime desc');
const result = await driveRequest(`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=${fields}&orderBy=${order}`);
const files = result.files || [];
$('driveFiles').innerHTML = files.length ? '' : '<p class="empty-state">No Branchly maps yet. Save this map to create one.</p>';
files.forEach(file => {
const button = document.createElement('button');
button.className = 'drive-file';
const title = document.createElement('strong'); title.textContent = file.name.replace(/\.branchly\.json$/i, '');
const detail = document.createElement('span'); detail.textContent = `Updated ${new Date(file.modifiedTime).toLocaleString()}`;
button.append(title, detail); button.onclick = () => openDriveMap(file);
$('driveFiles').appendChild(button);
});
} catch (error) {
$('driveFiles').innerHTML = '';
const message = document.createElement('p'); message.className = 'empty-state'; message.textContent = `Couldn’t load Drive files: ${error.message}`;
$('driveFiles').appendChild(message);
}
}
async function openDriveMap(file) {
try {
const data = await driveRequest(`https://www.googleapis.com/drive/v3/files/${file.id}?alt=media`);
importMapData(data, file.name.replace(/\.branchly\.json$/i, ''));
driveFileId = file.id;
$('driveDialog').close();
$('saveText').textContent = 'Opened from Google Drive';
showToast(`Opened ${file.name}`);
} catch (error) { showToast(`Couldn’t open map: ${error.message}`); }
}
function connectDrive(afterConnect) {
closeDriveMenu();
if (!tokenClient) return showToast('Google sign-in is still loading');
tokenClient.callback = response => {
if (response.error) return showToast(`Google sign-in failed: ${response.error}`);
accessToken = response.access_token;
setDriveConnected(true);
showToast('Google Drive connected');
if (afterConnect) afterConnect();
};
tokenClient.requestAccessToken({ prompt: accessToken ? '' : 'consent' });
}
function disconnectDrive() {
closeDriveMenu();
if (accessToken) google.accounts.oauth2.revoke(accessToken);
accessToken = null; driveFileId = null; driveFolderId = null;
setDriveConnected(false); showToast('Google Drive disconnected');
}
window.branchlyGapiLoaded = () => {
gapi.load('client', async () => { await gapi.client.init({}); gapiReady = true; if (identityReady) $('driveBtn').disabled = false; });
};
window.branchlyIdentityLoaded = () => {
tokenClient = google.accounts.oauth2.initTokenClient({ client_id: GOOGLE_CLIENT_ID, scope: DRIVE_SCOPE, callback: '' });
identityReady = true; if (gapiReady) $('driveBtn').disabled = false;
};
function exportMap() {
const content = JSON.stringify({ title: $('mapTitle').value, nodes }, null, 2);
const blob = new Blob([content], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a');
a.href = url; a.download = `${$('mapTitle').value.trim().replace(/[^a-z0-9]+/gi, '-').toLowerCase() || 'mind-map'}.json`; a.click(); URL.revokeObjectURL(url); showToast('Mind map exported');
}
function importMapData(data, fallbackTitle) {
if (!data || !Array.isArray(data.nodes) || !data.nodes.length) throw new Error('No thoughts found');
const ids = new Set();
const imported = data.nodes.map(raw => {
const id = Number(raw.id), parent = raw.parent === null ? null : Number(raw.parent);
if (!Number.isInteger(id) || ids.has(id) || typeof raw.text !== 'string' || !Number.isFinite(Number(raw.x)) || !Number.isFinite(Number(raw.y))) {
throw new Error('Invalid thought data');
}
ids.add(id);
return { id, parent, text: raw.text.trim() || 'Untitled', x: Number(raw.x), y: Number(raw.y), color: palette.includes(raw.color) ? raw.color : palette[0] };
});
const roots = imported.filter(node => node.parent === null);
if (roots.length !== 1 || imported.some(node => node.parent !== null && !ids.has(node.parent))) {
throw new Error('Invalid branch structure');
}
const reachable = new Set([roots[0].id]);
let foundBranch = true;
while (foundBranch) {
foundBranch = false;
imported.forEach(node => {
if (node.parent !== null && reachable.has(node.parent) && !reachable.has(node.id)) {
reachable.add(node.id); foundBranch = true;
}
});
}
if (reachable.size !== imported.length) throw new Error('Branches must connect to the central thought');
checkpoint();
nodes = imported; nextId = Math.max(...nodes.map(node => node.id)) + 1; selected = roots[0].id;
$('mapTitle').value = typeof data.title === 'string' && data.title.trim() ? data.title.trim().slice(0, 120) : fallbackTitle || 'Imported Mind Map';
render(); fitMap(); save();
}
async function importMap(file) {
if (!file) return;
try {
importMapData(JSON.parse(await file.text()), file.name.replace(/\.json$/i, ''));
driveFileId = null;
showToast('Mind map imported');
} catch (error) {
showToast(`Couldn’t import: ${error.message}`);
} finally {
$('importFile').value = '';
}
}
document.addEventListener('pointermove', e => {
if (dragging) { const node = nodes.find(n => n.id === dragging.id); node.x = dragging.x + (e.clientX - dragging.startX) / scale; node.y = dragging.y + (e.clientY - dragging.startY) / scale; const el = document.querySelector(`.node[data-id="${node.id}"]`); el.style.left = `${node.x}px`; el.style.top = `${node.y}px`; drawConnections(); }
if (panning) { pan.x = panning.x + e.clientX - panning.startX; pan.y = panning.y + e.clientY - panning.startY; applyTransform(); }
});
document.addEventListener('pointerup', () => { if (dragging) save(); dragging = null; panning = null; canvas.classList.remove('panning'); });
canvas.addEventListener('pointerdown', e => { if (e.target !== canvas && e.target !== stage && e.target !== nodesLayer && e.target !== svg) return; panning = { startX: e.clientX, startY: e.clientY, x: pan.x, y: pan.y }; canvas.classList.add('panning'); });
canvas.addEventListener('wheel', e => { e.preventDefault(); setZoom(scale * (e.deltaY > 0 ? .9 : 1.1), e.clientX, e.clientY - 64); }, { passive: false });
$('addChildBtn').onclick = () => addChild(); $('panelChild').onclick = () => addChild();
$('addSiblingBtn').onclick = addSibling; $('editBtn').onclick = () => editNode(); $('panelEdit').onclick = () => editNode();
$('deleteBtn').onclick = removeNode; $('panelDelete').onclick = removeNode; $('centerBtn').onclick = fitMap; $('panelCenter').onclick = centerSelected;
$('zoomIn').onclick = () => setZoom(scale + .1); $('zoomOut').onclick = () => setZoom(scale - .1);
$('closeInspector').onclick = () => $('inspector').classList.add('hidden'); $('undoBtn').onclick = undo; $('redoBtn').onclick = redo; $('exportBtn').onclick = exportMap;
$('importBtn').onclick = () => $('importFile').click();
$('importFile').addEventListener('change', event => importMap(event.target.files[0]));
$('driveBtn').onclick = event => {
event.stopPropagation();
const willOpen = $('driveMenu').hidden;
$('driveMenu').hidden = !willOpen;
$('driveBtn').setAttribute('aria-expanded', String(willOpen));
};
$('connectDrive').onclick = () => connectDrive();
$('openDrive').onclick = listDriveMaps;
$('saveDrive').onclick = saveToDrive;
$('disconnectDrive').onclick = disconnectDrive;
$('closeDriveDialog').onclick = () => $('driveDialog').close();
$('driveDialog').addEventListener('click', event => { if (event.target === $('driveDialog')) $('driveDialog').close(); });
document.addEventListener('click', event => { if (!event.target.closest('.drive-wrap')) closeDriveMenu(); });
$('mapTitle').addEventListener('input', save);
$('resetBtn').onclick = () => { if (!confirm('Start a new map? Your current map will be replaced.')) return; checkpoint(); nodes = [{ id: 1, parent: null, text: 'My Big Idea', x: 1100, y: 700, color: palette[0] }]; nextId = 2; selected = 1; driveFileId = null; $('mapTitle').value = 'Untitled Mind Map'; render(); fitMap(); save(); setTimeout(() => editNode(1), 20); };
document.addEventListener('keydown', e => {
if (e.target.isContentEditable || e.target.tagName === 'INPUT') return;
if (e.key === 'Tab') { e.preventDefault(); addChild(); }
else if (e.key === 'Enter') { e.preventDefault(); addSibling(); }
else if (e.key === 'Delete' || e.key === 'Backspace') removeNode();
else if (e.key.toLowerCase() === 'e') editNode();
else if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'z') { e.preventDefault(); e.shiftKey ? redo() : undo(); }
else if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'y') { e.preventDefault(); redo(); }
});
window.addEventListener('resize', fitMap);
load(); render(); setTimeout(fitMap, 40);
})();
</script>
<script src="https://apis.google.com/js/api.js" onload="branchlyGapiLoaded()" async defer></script>
<script src="https://accounts.google.com/gsi/client" onload="branchlyIdentityLoaded()" async defer></script>
</body>
</html>