-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherd.html
More file actions
1336 lines (1180 loc) · 66.4 KB
/
Copy patherd.html
File metadata and controls
1336 lines (1180 loc) · 66.4 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime SQL ERD Generator</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://unpkg.com/boxicons@2.1.2/css/boxicons.min.css" rel="stylesheet" />
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
body { font-family: 'Inter', sans-serif; background-color: #f8fafc; }
.dark body { background-color: #353935; }
/* Grid background */
.canvas-bg {
background-size: 20px 20px;
background-image: radial-gradient(circle, #cbd5e1 1px, rgba(0, 0, 0, 0) 1px);
}
html.dark .canvas-bg {
background-image: radial-gradient(circle, #475569 1px, rgba(0, 0, 0, 0) 1px);
}
/* ERD Table Node */
.erd-table {
position: absolute;
background: white;
border-radius: 12px;
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
border: 1px solid #e2e8f0;
cursor: move;
user-select: none;
min-width: 280px;
max-width: 720px;
width: max-content;
z-index: 10;
overflow: hidden;
transition: box-shadow 0.2s ease, border-color 0.2s ease, width 0.3s cubic-bezier(0.4, 0, 0.2, 1), height 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
html.dark .erd-table {
background: #1e293b;
border-color: #334155;
color: #f1f5f9;
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.3);
}
.erd-table.selected {
border-color: #3b82f6;
box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.4), 0 10px 15px -3px rgba(0, 0, 0, 0.1);
z-index: 20;
}
html.dark .erd-table.selected {
box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.5), 0 10px 15px -3px rgba(0, 0, 0, 0.5);
}
.canvas-container {
transform-origin: 0 0;
will-change: transform;
}
/* SVG lines */
#svg-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 5;
overflow: visible;
}
.nav-link.active::after {
content: '';
position: absolute;
bottom: -23px;
left: 0;
width: 100%;
height: 3px;
background-color: #2563eb;
border-radius: 9999px 9999px 0 0;
}
/* Custom scrollbars for records preview */
.custom-scrollbar::-webkit-scrollbar {
width: 6px;
height: 6px;
}
.custom-scrollbar::-webkit-scrollbar-track {
background: transparent;
}
.custom-scrollbar::-webkit-scrollbar-thumb {
background-color: rgba(156, 163, 175, 0.3);
border-radius: 3px;
}
html.dark .custom-scrollbar::-webkit-scrollbar-thumb {
background-color: rgba(71, 85, 105, 0.6);
}
</style>
</head>
<body class="flex flex-col h-screen w-screen overflow-hidden dark bg-transparent">
<!-- App Body -->
<div class="flex-1 flex overflow-hidden">
<!-- Sidebar Tools -->
<aside class="w-64 bg-white dark:bg-[#2b2d2b] border-r border-gray-200 dark:border-gray-700/50 flex flex-col z-40 shrink-0 relative transition-all duration-300" id="left-sidebar">
<div class="p-4 border-b border-gray-100 dark:border-gray-800 flex flex-col gap-3">
<h3 class="text-xs font-bold text-gray-400 uppercase tracking-wider mb-1 mt-1">Tools</h3>
<button onclick="addTable()" class="w-full py-2.5 px-3 bg-blue-600 hover:bg-blue-700 hover:shadow-[0_4px_12px_rgba(59,130,246,0.3)] text-white rounded-lg text-sm font-medium flex items-center justify-center gap-2 transition-all duration-300 ease-out transform hover:-translate-y-0.5 hover:scale-[1.02] active:translate-y-0 active:scale-[0.98] shadow-sm">
<i class="bx bx-plus"></i> Add Table
</button>
</div>
<div class="p-4 border-b border-gray-100 dark:border-gray-800">
<h3 class="text-xs font-bold text-gray-400 uppercase tracking-wider mb-3">Import / Export</h3>
<div class="space-y-2">
<button onclick="document.getElementById('import-sql').click()" class="w-full py-2 px-3 bg-gray-50 border border-transparent hover:border-blue-500/30 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-lg text-sm font-medium flex items-center justify-start gap-2 transition-all duration-300 ease-out transform hover:-translate-y-0.5 hover:scale-[1.02] active:translate-y-0 active:scale-[0.98] shadow-sm">
<i class="bx bx-import text-blue-500"></i> Import SQL
</button>
<input type="file" id="import-sql" accept=".sql" class="hidden" onchange="importSQL(event)">
<button onclick="exportSQL()" class="w-full py-2 px-3 bg-gray-50 border border-transparent hover:border-emerald-500/30 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-lg text-sm font-medium flex items-center justify-start gap-2 transition-all duration-300 ease-out transform hover:-translate-y-0.5 hover:scale-[1.02] active:translate-y-0 active:scale-[0.98] shadow-sm">
<i class="bx bx-export text-emerald-500"></i> Export SQL
</button>
<button onclick="exportJSON()" class="w-full py-2 px-3 bg-gray-50 border border-transparent hover:border-yellow-500/30 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-lg text-sm font-medium flex items-center justify-start gap-2 transition-all duration-300 ease-out transform hover:-translate-y-0.5 hover:scale-[1.02] active:translate-y-0 active:scale-[0.98] shadow-sm">
<i class="bx bxs-file-json text-yellow-500"></i> Save JSON
</button>
<button onclick="document.getElementById('load-json').click()" class="w-full py-2 px-3 bg-gray-50 border border-transparent hover:border-orange-500/30 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-lg text-sm font-medium flex items-center justify-start gap-2 transition-all duration-300 ease-out transform hover:-translate-y-0.5 hover:scale-[1.02] active:translate-y-0 active:scale-[0.98] shadow-sm">
<i class="bx bx-folder-open text-orange-500"></i> Load JSON
</button>
<input type="file" id="load-json" accept=".json" class="hidden" onchange="loadJSON(event)">
<button onclick="clearCanvas()" class="w-full mt-2 py-2 px-3 bg-red-50 border border-transparent hover:border-red-500/30 hover:bg-red-100 dark:bg-red-900/10 dark:hover:bg-red-900/30 text-red-600 dark:text-red-400 rounded-lg text-sm font-medium flex items-center justify-start gap-2 transition-all duration-300 ease-out transform hover:-translate-y-0.5 hover:scale-[1.02] active:translate-y-0 active:scale-[0.98] shadow-sm">
<i class="bx bx-trash"></i> Clear Canvas
</button>
</div>
</div>
<div class="flex-1 overflow-auto p-4">
<h3 class="text-xs font-bold text-gray-400 uppercase tracking-wider mb-3">Tables</h3>
<div id="table-list" class="space-y-1">
<!-- List of tables generated here -->
</div>
</div>
</aside>
<!-- Canvas Area -->
<main class="flex-1 relative overflow-hidden bg-gray-50 dark:bg-[#1a1c1a] canvas-bg outline-none" id="main-canvas" tabindex="0" onmousedown="startPan(event)">
<div id="canvas-container" class="canvas-container w-full h-full relative" style="transform: translate(0px, 0px) scale(1);">
<!-- SVG Layer for relationships -->
<svg id="svg-layer"></svg>
<!-- Tables appended here -->
</div>
<!-- Zoom Controls -->
<div class="absolute bottom-6 left-6 bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 flex items-center p-1 z-30">
<button onclick="zoomOut()" class="w-8 h-8 flex items-center justify-center text-gray-500 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-md"><i class="bx bx-minus"></i></button>
<div id="zoom-level" class="px-2 text-xs font-medium text-gray-600 dark:text-gray-300">100%</div>
<button onclick="zoomIn()" class="w-8 h-8 flex items-center justify-center text-gray-500 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-md"><i class="bx bx-plus"></i></button>
</div>
</main>
<!-- Right Side Properties Panel -->
<aside id="properties-panel" class="w-80 bg-white dark:bg-[#2b2d2b] border-l border-gray-200 dark:border-gray-700/50 flex flex-col z-40 shrink-0 hidden shadow-xl">
<div class="p-4 border-b border-gray-100 dark:border-gray-800 flex justify-between items-center bg-gray-50 dark:bg-gray-800/50">
<h2 class="text-sm font-semibold text-gray-800 dark:text-gray-200" id="prop-title">Table Properties</h2>
<button onclick="closeProperties()" class="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200">
<i class="bx bx-x text-2xl"></i>
</button>
</div>
<div class="flex-1 overflow-y-auto p-4" id="prop-body">
<!-- Dynamic Content (table edit or col edit) -->
</div>
<div class="p-4 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-[#2b2d2b]" id="prop-footer">
</div>
</aside>
</div>
<!-- Unified Modal -->
<div id="app-modal" class="fixed inset-0 bg-black/50 z-[100] hidden items-center justify-center backdrop-blur-sm">
<div class="bg-white dark:bg-[#2b2d2b] w-full max-w-sm p-6 rounded-2xl shadow-2xl border border-gray-200 dark:border-gray-700/50 transform scale-95 opacity-0 transition-all duration-200" id="app-modal-content">
<h3 class="text-lg font-bold text-gray-900 dark:text-white mb-2" id="modal-title">Title</h3>
<div class="text-sm text-gray-600 dark:text-gray-400 mb-4" id="modal-body">Body</div>
<div id="modal-input-container" class="hidden mb-4">
<!-- For prompts or custom selects -->
</div>
<div class="flex justify-end gap-2" id="modal-footer">
<button id="modal-btn-cancel" class="px-4 py-2 rounded-lg text-sm font-medium text-gray-600 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-800 transition-colors hidden">Cancel</button>
<button id="modal-btn-confirm" class="px-4 py-2 rounded-lg text-sm font-medium bg-blue-600 hover:bg-blue-700 text-white transition-colors">OK</button>
</div>
</div>
</div>
<script>
const AppModal = {
show(options) {
return new Promise((resolve) => {
const el = document.getElementById('app-modal');
const content = document.getElementById('app-modal-content');
const title = document.getElementById('modal-title');
const body = document.getElementById('modal-body');
const inputContainer = document.getElementById('modal-input-container');
const cancelBtn = document.getElementById('modal-btn-cancel');
const confirmBtn = document.getElementById('modal-btn-confirm');
title.innerText = options.title || 'Message';
body.innerHTML = options.body || '';
inputContainer.classList.add('hidden');
inputContainer.innerHTML = '';
cancelBtn.classList.add('hidden');
if(options.type === 'confirm' || options.type === 'prompt' || options.type === 'custom') {
cancelBtn.classList.remove('hidden');
}
if (options.type === 'prompt') {
inputContainer.classList.remove('hidden');
inputContainer.innerHTML = `<input type="text" id="modal-prompt-input" class="w-full bg-gray-50 dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded px-3 py-2 text-sm dark:text-white outline-none focus:border-blue-500" placeholder="${options.placeholder || ''}" value="${options.defaultValue || ''}">`;
} else if (options.type === 'custom') {
inputContainer.classList.remove('hidden');
inputContainer.innerHTML = options.customHtml;
}
const close = (result) => {
content.classList.remove('scale-100', 'opacity-100');
content.classList.add('scale-95', 'opacity-0');
setTimeout(() => {
el.classList.add('hidden');
el.classList.remove('flex');
resolve(result);
}, 200);
};
cancelBtn.onclick = () => close(null);
confirmBtn.onclick = () => {
if (options.type === 'prompt') {
close(document.getElementById('modal-prompt-input').value);
} else if (options.type === 'custom' && options.getCustomValue) {
close(options.getCustomValue());
} else {
close(true);
}
};
if (options.type === 'prompt' || options.type === 'custom') {
confirmBtn.innerText = 'Save';
confirmBtn.className = 'px-4 py-2 rounded-lg text-sm font-medium bg-blue-600 hover:bg-blue-700 text-white transition-colors';
} else if (options.type === 'confirm') {
confirmBtn.innerText = 'Confirm';
if (options.danger) {
confirmBtn.className = 'px-4 py-2 rounded-lg text-sm font-medium bg-red-600 hover:bg-red-700 text-white transition-colors';
} else {
confirmBtn.className = 'px-4 py-2 rounded-lg text-sm font-medium bg-blue-600 hover:bg-blue-700 text-white transition-colors';
}
} else {
confirmBtn.innerText = 'OK';
confirmBtn.className = 'px-4 py-2 rounded-lg text-sm font-medium bg-blue-600 hover:bg-blue-700 text-white transition-colors';
}
el.classList.remove('hidden');
el.classList.add('flex');
// Trigger reflow
void el.offsetWidth;
content.classList.remove('scale-95', 'opacity-0');
content.classList.add('scale-100', 'opacity-100');
if (options.type === 'prompt') {
setTimeout(() => document.getElementById('modal-prompt-input').focus(), 250);
}
});
}
};
// State
let isSyncingFromParent = false;
let tables = [];
let relationships = [];
let pan = { x: 0, y: 0 };
let scale = 1;
let isDragging = false;
let isPanning = false;
let startX, startY;
let selectedTableId = null;
let draggedTableId = null;
let autoIncId = 1;
window.tableRowsCollapsedState = {};
window.toggleTableRowsData = function(tableId) {
window.tableRowsCollapsedState[tableId] = !window.tableRowsCollapsedState[tableId];
renderTables();
};
window.addBlankRecordInline = function(tableId) {
const table = tables.find(t => t.id === tableId);
if (!table) return;
const newRec = {};
table.columns.forEach(c => {
newRec[c.id] = '';
});
if (!table.records) table.records = [];
table.records.push(newRec);
// Expand the rows automatically so they can see the new row immediately
window.tableRowsCollapsedState[tableId] = false;
saveState();
renderTables();
// Auto focus on the first cell in that newly added row
setTimeout(() => {
const container = document.getElementById(`table-rows-data-container-${tableId}`);
if (container) {
const lastRow = container.querySelector('tbody tr:last-child');
if (lastRow) {
const firstCell = lastRow.querySelector('td');
if (firstCell) {
firstCell.click();
}
}
}
}, 100);
};
window.deleteRowInline = function(event, tableId, recIndex) {
event.stopPropagation();
const table = tables.find(t => t.id === tableId);
if (!table || !table.records) return;
table.records.splice(recIndex, 1);
saveState();
renderTables();
};
window.editCellInline = function(event, tableId, recordIndex, colId) {
event.stopPropagation();
const cell = event.currentTarget;
if (cell.querySelector('input')) return; // Already editing page cell
const currentVal = cell.textContent.trim() === 'null' ? '' : cell.textContent.trim();
// Create nice inline editing input field
const input = document.createElement('input');
input.type = 'text';
input.value = currentVal;
// Style input beautifully
input.className = 'bg-blue-50/80 dark:bg-slate-800 border border-blue-500 rounded px-1.5 py-0.5 text-[9.5px] font-mono text-gray-900 dark:text-white outline-none focus:ring-1 focus:ring-blue-400 transition-all';
// Set dynamic fitting width
input.style.width = Math.max(currentVal.length + 2, 8) + 'ch';
input.style.minWidth = '60px';
input.style.maxWidth = '280px';
input.oninput = () => {
input.style.width = Math.max(input.value.length + 2, 8) + 'ch';
};
cell.innerHTML = '';
cell.appendChild(input);
input.focus();
input.select();
let finished = false;
const saveAndExit = () => {
if (finished) return;
finished = true;
const newVal = input.value.trim();
const table = tables.find(t => t.id === tableId);
if (table && table.records && table.records[recordIndex]) {
table.records[recordIndex][colId] = newVal;
saveState();
renderTables();
}
};
input.onblur = () => {
saveAndExit();
};
input.onkeydown = (e) => {
if (e.key === 'Enter') {
e.stopPropagation();
saveAndExit();
} else if (e.key === 'Escape') {
e.stopPropagation();
finished = true;
renderTables(); // re-render to revert
}
};
input.onmousedown = (e) => {
e.stopPropagation(); // prevent drag
};
};
// Theme toggle
function toggleTheme() {
document.documentElement.classList.toggle('dark');
const isDark = document.documentElement.classList.contains('dark');
try { localStorage.setItem('theme', isDark ? 'dark' : 'light'); } catch(e){}
renderRelationships(); // Redraw lines for color config update
}
// Initial setup
window.onload = () => {
try {
if (localStorage.getItem('theme') === 'light') {
document.documentElement.classList.remove('dark');
} else {
document.documentElement.classList.add('dark');
}
} catch(e) {}
loadState();
// Listeners for zoom & pan
const mainCanvas = document.getElementById('main-canvas');
mainCanvas.addEventListener('wheel', handleWheel, { passive: false });
window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('mouseup', handleMouseUp);
window.addEventListener('keydown', handleKeyDown);
// Listen for theme changes from parent/other tabs
window.addEventListener('storage', (e) => {
if(e.key === 'theme') {
if(e.newValue === 'light') {
document.documentElement.classList.remove('dark');
} else {
document.documentElement.classList.add('dark');
}
renderRelationships();
}
});
// Try to communicate with parent for real-time theme switch (if iframe)
window.addEventListener('message', (e) => {
if (e.data && e.data.type === 'theme') {
if (e.data.theme === 'light') {
document.documentElement.classList.remove('dark');
} else {
document.documentElement.classList.add('dark');
}
renderRelationships();
} else if (e.data && e.data.type === 'request_sql') {
if (window.parent && window.parent !== window) {
window.parent.postMessage({
type: 'erd_updated',
sql: generateSQLText(),
tables: tables,
relationships: relationships
}, '*');
}
} else if (e.data && e.data.type === 'sync_from_sqlite') {
const dbState = e.data.state;
if (!dbState || !dbState.tables) return;
isSyncingFromParent = true;
const updatedTables = [];
let offsetY = 80;
let offsetX = 80;
dbState.tables.forEach(dbTable => {
const existingTbl = tables.find(t => t.name.toLowerCase() === dbTable.name.toLowerCase());
const tblId = existingTbl ? existingTbl.id : generateId();
let tblX = existingTbl ? existingTbl.x : offsetX;
let tblY = existingTbl ? existingTbl.y : offsetY;
if (!existingTbl) {
offsetX += 280;
if (offsetX > 900) {
offsetX = 80;
offsetY += 280;
}
}
// Map columns
const cols = dbTable.columns.map(dbCol => {
let colId = generateColId();
if (existingTbl) {
const existingCol = existingTbl.columns.find(c => c.name.toLowerCase() === dbCol.name.toLowerCase());
if (existingCol) colId = existingCol.id;
}
return {
id: colId,
name: dbCol.name,
type: (dbCol.type || 'INTEGER').toUpperCase(),
isPk: !!dbCol.pk
};
});
// Map records
const records = (dbTable.rows || []).map(row => {
const rec = {};
dbTable.columns.forEach((dbCol, idx) => {
const colItem = cols[idx];
rec[colItem.id] = row[idx];
});
return rec;
});
updatedTables.push({
id: tblId,
name: dbTable.name,
x: tblX,
y: tblY,
columns: cols,
records: records
});
});
tables = updatedTables;
// Reconstruct relationships
const newRelationships = [];
dbState.tables.forEach(dbTable => {
const fromTbl = tables.find(t => t.name.toLowerCase() === dbTable.name.toLowerCase());
if (!fromTbl || !dbTable.foreign_keys) return;
dbTable.foreign_keys.forEach(fk => {
const toTbl = tables.find(t => t.name.toLowerCase() === fk.table.toLowerCase());
if (!toTbl) return;
const fromColObj = fromTbl.columns.find(c => c.name.toLowerCase() === fk.from.toLowerCase());
const toColObj = toTbl.columns.find(c => c.name.toLowerCase() === fk.to.toLowerCase());
if (fromColObj && toColObj) {
newRelationships.push({
fromTable: fromTbl.id,
fromCol: fromColObj.id,
toTable: toTbl.id,
toCol: toColObj.id,
type: '1:N'
});
}
});
});
relationships = newRelationships;
saveState();
renderTables();
isSyncingFromParent = false;
}
});
};
function handleKeyDown(e) {
// Check if we are inside an input to prevent overriding text deletion
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') {
return;
}
if ((e.key === 'Backspace' || e.key === 'Delete') && selectedTableId) {
e.preventDefault();
deleteTable(selectedTableId);
}
}
function generateId() {
return 'tbl_' + Date.now() + '_' + Math.floor(Math.random() * 1000);
}
function generateColId() {
return 'col_' + Date.now() + '_' + Math.floor(Math.random() * 1000);
}
// Data Management
function saveState() {
renderTableList();
renderRelationships();
if (!isSyncingFromParent) {
if (window.parent && window.parent !== window) {
window.parent.postMessage({
type: 'erd_updated',
sql: generateSQLText(),
tables: tables,
relationships: relationships
}, '*');
}
}
}
function loadState() {
tables = [];
relationships = [];
pan = {x: 0, y: 0};
scale = 1;
autoIncId = 1;
updateCanvasTransform();
renderTables();
}
// Canvas Interactions
function handleWheel(e) {
if (e.ctrlKey || e.metaKey) {
e.preventDefault();
const zoomFactor = 0.05;
if (e.deltaY < 0) zoomIn(zoomFactor);
else zoomOut(zoomFactor);
}
}
function zoomIn(amount = 0.1) {
scale = Math.min(3, scale + amount);
updateCanvasTransform();
}
function zoomOut(amount = 0.1) {
scale = Math.max(0.2, scale - amount);
updateCanvasTransform();
}
function updateCanvasTransform() {
document.getElementById('canvas-container').style.transform = `translate(${pan.x}px, ${pan.y}px) scale(${scale})`;
document.getElementById('zoom-level').innerText = Math.round(scale * 100) + '%';
saveState();
}
function startPan(e) {
// Middle click or clicking on background
if (e.button === 1 || (!e.target.closest('.erd-table') && !e.target.closest('button'))) {
document.getElementById('main-canvas').focus();
isPanning = true;
startX = e.clientX - pan.x;
startY = e.clientY - pan.y;
document.getElementById('main-canvas').style.cursor = 'grabbing';
closeProperties();
selectedTableId = null;
renderTables();
}
}
function startTableDrag(e, tableId) {
if(e.button !== 0) return; // Only left click
// Don't drag if clicking buttons or inputs inside table
if(['BUTTON', 'INPUT', 'SELECT', 'I'].includes(e.target.tagName)) return;
document.getElementById('main-canvas').focus();
isDragging = true;
draggedTableId = tableId;
const t = tables.find(t => t.id === tableId);
startX = e.clientX / scale - t.x;
startY = e.clientY / scale - t.y;
selectedTableId = tableId;
renderTables();
openTableProperties(tableId);
e.stopPropagation();
}
function handleMouseMove(e) {
if (isPanning) {
pan.x = e.clientX - startX;
pan.y = e.clientY - startY;
updateCanvasTransform();
} else if (isDragging && draggedTableId) {
const t = tables.find(t => t.id === draggedTableId);
if (t) {
t.x = e.clientX / scale - startX;
t.y = e.clientY / scale - startY;
// Snap to grid (20px)
t.x = Math.round(t.x / 20) * 20;
t.y = Math.round(t.y / 20) * 20;
const el = document.getElementById(t.id);
if(el) {
el.style.left = t.x + 'px';
el.style.top = t.y + 'px';
}
renderRelationships();
}
}
}
function handleMouseUp() {
if (isPanning) {
isPanning = false;
document.getElementById('main-canvas').style.cursor = 'default';
}
if (isDragging) {
isDragging = false;
draggedTableId = null;
saveState();
}
}
// Table Operations
function addTable(isInit = false) {
const num = autoIncId++;
const t = {
id: generateId(),
name: 'table_' + num,
x: -pan.x / scale + 100,
y: -pan.y / scale + 100,
columns: [
{ id: generateColId(), name: 'id', type: 'INTEGER', isPk: true, isFk: null }
],
records: []
};
tables.push(t);
if(!isInit) {
selectedTableId = t.id;
saveState();
renderTables();
openTableProperties(t.id);
}
}
async function deleteTable(id) {
const confirmed = await AppModal.show({
type: 'confirm',
title: 'Delete Table',
body: 'Delete this table? This action cannot be undone.',
danger: true
});
if(confirmed) {
tables = tables.filter(t => t.id !== id);
relationships = relationships.filter(r => r.fromTable !== id && r.toTable !== id);
selectedTableId = null;
closeProperties();
saveState();
renderTables();
}
}
function addColumn(tableId) {
const table = tables.find(t => t.id === tableId);
if(table) {
table.columns.push({ id: generateColId(), name: 'new_col', type: 'VARCHAR', isPk: false, isFk: null });
saveState();
renderTables();
openTableProperties(tableId);
}
}
function deleteColumn(tableId, colId) {
const table = tables.find(t => t.id === tableId);
if(table) {
table.columns = table.columns.filter(c => c.id !== colId);
// Remove relationships
relationships = relationships.filter(r => !(r.fromTable === tableId && r.fromCol === colId) && !(r.toTable === tableId && r.toCol === colId));
saveState();
renderTables();
openTableProperties(tableId);
}
}
// UI Renderers
function renderTables() {
const container = document.getElementById('canvas-container');
// Remove existing tables
document.querySelectorAll('.erd-table').forEach(e => e.remove());
tables.forEach(t => {
const isSel = t.id === selectedTableId;
const el = document.createElement('div');
el.className = `erd-table flex flex-col ${isSel ? 'selected' : ''}`;
el.id = t.id;
el.style.left = t.x + 'px';
el.style.top = t.y + 'px';
el.onmousedown = (e) => startTableDrag(e, t.id);
let colsHtml = '';
t.columns.forEach(c => {
const fkRel = relationships.find(r => r.fromTable === t.id && r.fromCol === c.id);
let icons = '';
if(c.isPk) icons += '<i class="bx bxs-key text-yellow-500" title="Primary Key"></i>';
if(fkRel) icons += '<i class="bx bx-link text-blue-500" title="Foreign Key"></i>';
colsHtml += `
<div class="px-3 py-2 flex justify-between items-center border-t border-gray-100 dark:border-slate-700/50 text-[13px] hover:bg-blue-50/50 dark:hover:bg-slate-800/50 transition-colors col-row" data-colid="${c.id}">
<div class="flex items-center text-gray-700 dark:text-slate-300 font-medium tracking-tight whitespace-nowrap overflow-hidden text-ellipsis mr-2">
${icons} <span class="ml-1">${c.name}</span>
</div>
<div class="text-[10px] text-gray-500 dark:text-slate-400 font-mono bg-gray-100 dark:bg-slate-800 px-1.5 py-0.5 rounded uppercase tracking-wider">${c.type}</div>
</div>
`;
});
let rowsHtml = '';
if (t.records && t.records.length > 0) {
const isCollapsed = !!window.tableRowsCollapsedState[t.id];
rowsHtml = `
<div class="border-t border-gray-200 dark:border-slate-700/60 bg-gray-50/40 dark:bg-slate-900/25">
<button onclick="toggleTableRowsData('${t.id}'); event.stopPropagation();" class="w-full px-3 py-1.5 hover:bg-gray-100 dark:hover:bg-slate-800/40 text-[10px] text-gray-500 dark:text-slate-400 font-bold tracking-wider flex justify-between items-center transition-colors">
<span class="flex items-center gap-1 uppercase">
<i class="bx bx-grid-alt text-blue-500"></i> Records Data (${t.records.length})
</span>
<i class="bx ${isCollapsed ? 'bx-chevron-right' : 'bx-chevron-down'} text-xs"></i>
</button>
<div id="table-rows-data-container-${t.id}" class="${isCollapsed ? 'hidden' : 'block'} overflow-x-auto max-h-64 max-w-full border-t border-gray-200/50 dark:border-slate-800/50 custom-scrollbar font-sans">
<table class="w-full text-left border-collapse table-auto">
<thead class="bg-gray-100/60 dark:bg-slate-900/50 border-b border-gray-150 dark:border-slate-800">
<tr>
${t.columns.map(c => `
<th class="py-1.5 px-3.5 font-bold text-[9px] uppercase font-mono tracking-tight text-gray-500 dark:text-slate-400 border-r border-gray-200/50 dark:border-slate-800/50 whitespace-nowrap" style="min-width: 90px;">
${c.name}
</th>
`).join('')}
<th class="py-1 px-1 font-bold text-[9px] text-center uppercase tracking-tight text-gray-500 dark:text-slate-400 w-6"></th>
</tr>
</thead>
<tbody class="divide-y divide-gray-100 dark:divide-slate-800/40">
${t.records.map((rec, recIndex) => `
<tr class="hover:bg-blue-500/5 dark:hover:bg-blue-500/10 transition-colors group/row">
${t.columns.map(c => `
<td class="py-1.5 px-3.5 border-r border-gray-150/10 dark:border-slate-800/30 whitespace-nowrap font-mono text-[9.5px] text-gray-700 dark:text-slate-300 cursor-pointer hover:bg-blue-50 dark:hover:bg-slate-700/50 transition-all text-ellipsis overflow-hidden select-text"
style="min-width: 90px; max-width: 240px;"
onclick="editCellInline(event, '${t.id}', ${recIndex}, '${c.id}')"
title="Click to edit value">
${rec[c.id] !== null && rec[c.id] !== undefined && String(rec[c.id]).trim() !== '' ? String(rec[c.id]) : '<span class="text-gray-350 dark:text-slate-600 italic">null</span>'}
</td>
`).join('')}
<td class="py-1 px-1 text-center whitespace-nowrap">
<button onclick="deleteRowInline(event, '${t.id}', ${recIndex})" class="text-gray-400 hover:text-red-500 transition-colors opacity-0 group-hover/row:opacity-100 p-0.5 rounded" title="Delete row">
<i class="bx bx-trash text-[10px]"></i>
</button>
</td>
</tr>
`).join('')}
</tbody>
</table>
</div>
</div>
`;
} else {
rowsHtml = `
<div class="border-t border-gray-150 dark:border-slate-700/60 px-3 py-2.5 bg-gray-50/10 dark:bg-slate-900/5 text-[10px] text-gray-400 dark:text-slate-500 italic text-center flex flex-col items-center justify-center gap-1">
<span>No records yet in database</span>
<button onclick="addBlankRecordInline('${t.id}'); event.stopPropagation();" class="text-[9px] text-blue-500 hover:underline"><i class="bx bx-plus"></i> Add first record</button>
</div>
`;
}
el.innerHTML = `
<div class="px-3 py-2.5 bg-gradient-to-b from-gray-50 to-gray-100 dark:from-slate-800 dark:to-slate-900 border-b border-gray-200 dark:border-slate-700 flex justify-between items-center cursor-move">
<span class="font-bold text-sm text-gray-800 dark:text-gray-100 flex items-center gap-1.5 tracking-wide">
<i class="bx bx-table text-blue-500/80 dark:text-blue-400"></i> ${t.name}
</span>
<div class="flex gap-1">
<button onclick="addColumn('${t.id}'); event.stopPropagation();" class="text-[10px] text-gray-400 dark:text-gray-500 hover:text-blue-500 dark:hover:text-blue-400 transition-colors rounded px-1.5 py-0.5 border border-dashed border-gray-300 dark:border-slate-700 hover:border-blue-500/40 flex items-center gap-0.5" title="Add Column"><i class="bx bx-plus text-xs"></i>Col</button>
<button onclick="addBlankRecordInline('${t.id}'); event.stopPropagation();" class="text-[10px] text-gray-400 dark:text-gray-500 hover:text-emerald-500 dark:hover:text-emerald-400 transition-colors rounded px-1.5 py-0.5 border border-dashed border-gray-300 dark:border-slate-700 hover:border-emerald-500/40 flex items-center gap-0.5" title="Add Row/Record"><i class="bx bx-plus text-xs"></i>Row</button>
</div>
</div>
<div class="flex flex-col bg-white dark:bg-[#1e293b]">
${colsHtml}
</div>
${rowsHtml}
`;
container.appendChild(el);
});
renderTableList();
setTimeout(renderRelationships, 10);
}
function renderTableList() {
const list = document.getElementById('table-list');
list.innerHTML = '';
tables.forEach(t => {
const btn = document.createElement('button');
btn.className = `w-full text-left px-3 py-2 rounded flex items-center justify-between text-sm ${t.id === selectedTableId ? 'bg-blue-50 dark:bg-blue-500/10 text-blue-600 dark:text-blue-400' : 'text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-800'}`;
btn.innerHTML = `<span class="truncate"><i class="bx bx-table mr-2"></i>${t.name}</span> <span class="text-xs bg-gray-200 dark:bg-gray-700 rounded px-1.5 py-0.5">${t.columns.length}</span>`;
btn.onclick = () => {
selectedTableId = t.id;
// Center on table
pan.x = -t.x * scale + (window.innerWidth / 2) - 140;
pan.y = -t.y * scale + (window.innerHeight / 2);
updateCanvasTransform();
renderTables();
openTableProperties(t.id);
};
list.appendChild(btn);
});
}
function renderRelationships() {
const svg = document.getElementById('svg-layer');
svg.innerHTML = '';
const isDark = document.documentElement.classList.contains('dark');
const lineColor = isDark ? '#64748b' : '#94a3b8';
const activeColor = '#3b82f6';
// SVG Markers
svg.innerHTML = `
<defs>
<marker id="arrow-${isDark?'dark':'light'}" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
<path d="M 0 0 L 10 5 L 0 10 z" fill="${lineColor}"/>
</marker>
<marker id="arrow-active" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
<path d="M 0 0 L 10 5 L 0 10 z" fill="${activeColor}"/>
</marker>
<!-- Dot for One-to-Many -->
<marker id="dot-${isDark?'dark':'light'}" viewBox="0 0 10 10" refX="5" refY="5" markerWidth="5" markerHeight="5">
<circle cx="5" cy="5" r="5" fill="${lineColor}"/>
</marker>
</defs>
`;
relationships.forEach(rel => {
const fromTbl = document.getElementById(rel.fromTable);
const toTbl = document.getElementById(rel.toTable);
if(!fromTbl || !toTbl) return;
const fromColEl = fromTbl.querySelector(`[data-colid="${rel.fromCol}"]`);
const toColEl = toTbl.querySelector(`[data-colid="${rel.toCol}"]`);
if(!fromColEl || !toColEl) return;
// Calculate relative positions inside canvas
const r1 = fromColEl.getBoundingClientRect();
const r2 = toColEl.getBoundingClientRect();
const mainRect = document.getElementById('main-canvas').getBoundingClientRect();
// Convert back from scaled/panned screen coords to logical canvas coords
const getCanvasCoords = (rect, isRight) => {
return {
x: (rect.left - mainRect.left - pan.x) / scale + (isRight ? rect.width / scale: 0),
y: (rect.top - mainRect.top - pan.y) / scale + (rect.height / 2) / scale
};
};
const startIsRight = r1.left < r2.left; // simple heuristic
const p1 = getCanvasCoords(r1, startIsRight);
const p2 = getCanvasCoords(r2, !startIsRight);
// Curving
const cpOffset = Math.max(Math.abs(p2.x - p1.x) / 2, 50);
const pathStr = `M ${p1.x} ${p1.y} C ${p1.x + (startIsRight ? cpOffset : -cpOffset)} ${p1.y}, ${p2.x + (!startIsRight ? cpOffset : -cpOffset)} ${p2.y}, ${p2.x} ${p2.y}`;
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute('d', pathStr);
const isActive = (selectedTableId === rel.fromTable || selectedTableId === rel.toTable);
path.setAttribute('stroke', isActive ? activeColor : lineColor);
path.setAttribute('stroke-width', isActive ? '2' : '1.5');
path.setAttribute('fill', 'none');
if(rel.type === '1:N') {
path.setAttribute('marker-end', isActive ? 'url(#arrow-active)' : `url(#arrow-${isDark?'dark':'light'})`);
// we remove marker-start dot as we will draw actual nodes
} else if(rel.type === '1:1') {
path.setAttribute('stroke-dasharray', '5,5');
}
svg.appendChild(path);
// Draw solid connection nodes at both ends
const node1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
node1.setAttribute('cx', p1.x);
node1.setAttribute('cy', p1.y);
node1.setAttribute('r', '4');
node1.setAttribute('fill', isDark ? '#1a1c1a' : '#ffffff');
node1.setAttribute('stroke', isActive ? activeColor : lineColor);
node1.setAttribute('stroke-width', '2');
const node2 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
node2.setAttribute('cx', p2.x);
node2.setAttribute('cy', p2.y);
node2.setAttribute('r', '4');
node2.setAttribute('fill', isActive ? activeColor : lineColor);
svg.appendChild(node1);
svg.appendChild(node2);
});
}
// Properties Panel (Table/Column Edit)
function openTableProperties(tableId) {
const table = tables.find(t => t.id === tableId);
if(!table) return;
const panel = document.getElementById('properties-panel');
const body = document.getElementById('prop-body');
const foot = document.getElementById('prop-footer');
panel.classList.remove('hidden');
let types = ['INTEGER', 'VARCHAR', 'TEXT', 'DATE', 'DATETIME', 'BOOLEAN', 'FLOAT', 'DECIMAL'];
let html = `
<div class="mb-4">
<label class="block text-xs font-medium text-gray-500 mb-1">Table Name</label>
<input type="text" value="${table.name}" onchange="updateTableName('${table.id}', this.value)" class="w-full bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded px-3 py-1.5 text-sm dark:text-white outline-none focus:border-blue-500">
</div>
<div class="mb-2 flex justify-between items-center">
<label class="block text-xs font-bold text-gray-500 uppercase">Columns</label>
<button onclick="addColumn('${table.id}')" class="text-xs text-blue-500 hover:underline">+ Add Col</button>
</div>
<div class="space-y-3">
`;
table.columns.forEach((c) => {
let typeOpts = types.map(t => `<option value="${t}" ${c.type === t ? 'selected' : ''}>${t}</option>`).join('');
html += `