Repository navigation
Expand file tree
/
Copy pathnode-editor.html
More file actions
908 lines (783 loc) · 31.9 KB
/
Copy pathnode-editor.html
File metadata and controls
908 lines (783 loc) · 31.9 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
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Node Flow Editor - Interactive</title>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500;600;700&display=swap" rel="stylesheet">
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
theme: {
extend: {
fontFamily: {
sans: ['IBM Plex Sans', 'system-ui', 'sans-serif'],
mono: ['JetBrains Mono', 'Consolas', 'Monaco', 'monospace'],
},
}
}
}
</script>
<style>
body {
font-family: 'IBM Plex Sans', system-ui, sans-serif;
}
.font-mono {
font-family: 'JetBrains Mono', 'Consolas', 'Monaco', monospace;
}
.canvas-grid {
background-image: radial-gradient(circle, #1A1A1A 1px, transparent 1px);
background-size: 20px 20px;
}
.node {
position: absolute;
min-width: 192px;
background: #1A1A1A;
border: 1px solid #2E2E2E;
border-radius: 12px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
transition: all 200ms cubic-bezier(0.16, 1, 0.3, 1);
cursor: move;
user-select: none;
}
.node:hover {
background: #252525;
border-color: #3A3A3A;
}
.node.selected {
border: 2px solid #3B82F6;
box-shadow: 0 10px 30px rgba(59, 130, 246, 0.2);
}
.node.dragging {
opacity: 0.7;
cursor: grabbing;
}
.port {
width: 12px;
height: 12px;
border-radius: 50%;
border: 2px solid #64748B;
background: #0D0D0D;
transition: all 150ms ease-out;
cursor: crosshair;
pointer-events: all;
position: relative;
z-index: 10;
}
.port:hover {
border-color: #3B82F6;
transform: scale(1.3);
box-shadow: 0 0 8px rgba(59, 130, 246, 0.5);
}
.port.valid {
border-color: #10B981;
background: rgba(16, 185, 129, 0.3);
transform: scale(1.3);
box-shadow: 0 0 8px rgba(16, 185, 129, 0.5);
}
.port-output:hover {
border-color: #F59E0B;
}
.connection {
stroke: #475569;
stroke-width: 2;
fill: none;
transition: stroke 200ms ease-out;
cursor: pointer;
}
.connection:hover {
stroke: #60A5FA;
stroke-width: 3;
}
.connection.active {
stroke: #3B82F6;
stroke-width: 3;
}
.file-item {
transition: all 150ms ease-out;
cursor: grab;
}
.file-item:hover {
background: #1A1A1A;
}
.file-item.dragging {
opacity: 0.5;
}
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: #0D0D0D;
}
::-webkit-scrollbar-thumb {
background: #2A2A2A;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: #3A3A3A;
}
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
.properties-panel {
transition: transform 300ms cubic-bezier(0.16, 1, 0.3, 1);
}
.properties-panel.hidden {
transform: translateX(100%);
}
</style>
</head>
<body class="bg-black text-slate-100 overflow-hidden">
<!-- Top Navbar -->
<nav class="h-14 bg-black/95 backdrop-blur-md border-b border-[#1E1E1E] fixed top-0 left-0 right-0 z-50">
<div class="h-full px-4 flex items-center justify-between">
<div class="flex items-center gap-3">
<div class="w-8 h-8 bg-gradient-to-br from-blue-500 to-blue-600 rounded-lg flex items-center justify-center">
<svg class="w-5 h-5 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M13 10V3L4 14h7v7l9-11h-7z" />
</svg>
</div>
<h1 class="text-lg font-semibold text-slate-100">Node Flow</h1>
</div>
<div class="flex items-center gap-2">
<button id="undoBtn" class="px-3 py-1.5 text-sm text-slate-400 hover:text-slate-100 transition-colors duration-200 rounded-lg hover:bg-[#1A1A1A]">
<span class="flex items-center gap-1.5">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6" />
</svg>
Undo
</span>
</button>
<button id="redoBtn" class="px-3 py-1.5 text-sm text-slate-400 hover:text-slate-100 transition-colors duration-200 rounded-lg hover:bg-[#1A1A1A]">
<span class="flex items-center gap-1.5">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M21 10h-10a8 8 0 00-8 8v2M21 10l-6 6m6-6l-6-6" />
</svg>
Redo
</span>
</button>
<div class="w-px h-6 bg-[#2A2A2A] mx-1"></div>
<button id="saveBtn" class="px-4 py-1.5 bg-blue-500 hover:bg-blue-600 text-white text-sm font-medium rounded-lg transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-black">
Save
</button>
<button id="exportBtn" class="px-3 py-1.5 text-sm text-slate-400 hover:text-slate-100 transition-colors duration-200 rounded-lg hover:bg-[#1A1A1A]">
Export
</button>
</div>
</div>
</nav>
<!-- Main Layout -->
<div class="flex h-screen pt-14">
<!-- Left Sidebar -->
<aside class="w-64 bg-[#121212] border-r border-[#1E1E1E] flex flex-col">
<div class="p-4 border-b border-[#1E1E1E]">
<h2 class="text-sm font-semibold text-slate-300 mb-3">Components</h2>
<div class="relative">
<svg class="w-4 h-4 text-slate-500 absolute left-3 top-1/2 -translate-y-1/2" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
</svg>
<input
id="searchInput"
type="text"
placeholder="Search files..."
class="w-full pl-9 pr-3 py-2 bg-[#0D0D0D] border border-[#2A2A2A] rounded-lg text-sm text-slate-100 placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-blue-500 transition-all duration-150"
/>
</div>
</div>
<div class="flex-1 overflow-y-auto p-4">
<div id="fileList" class="space-y-1"></div>
</div>
</aside>
<!-- Canvas -->
<main class="flex-1 relative overflow-hidden bg-[#0D0D0D] canvas-grid">
<!-- Canvas Controls -->
<div class="absolute top-4 right-4 z-20 flex items-center gap-2 bg-[#1A1A1A] border border-[#2A2A2A] rounded-lg p-1">
<button id="zoomInBtn" class="p-1.5 text-slate-400 hover:text-slate-100 hover:bg-[#252525] rounded transition-colors duration-150">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4" />
</svg>
</button>
<button id="zoomOutBtn" class="p-1.5 text-slate-400 hover:text-slate-100 hover:bg-[#252525] rounded transition-colors duration-150">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M20 12H4" />
</svg>
</button>
<div class="w-px h-5 bg-[#2A2A2A]"></div>
<span id="zoomLevel" class="px-2 py-1 text-xs text-slate-400 font-mono">100%</span>
<div class="w-px h-5 bg-[#2A2A2A]"></div>
<button id="fitBtn" class="p-1.5 text-slate-400 hover:text-slate-100 hover:bg-[#252525] rounded transition-colors duration-150">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M4 8V4m0 0h4M4 4l5 5m11-1V4m0 0h-4m4 0l-5 5M4 16v4m0 0h4m-4 0l5-5m11 5l-5-5m5 5v-4m0 4h-4" />
</svg>
</button>
</div>
<!-- Canvas Container -->
<div id="canvasContainer" class="absolute inset-0" style="cursor: grab;">
<svg id="connectionsSvg" class="absolute inset-0 pointer-events-none" style="z-index: 5; width: 100%; height: 100%;"></svg>
<div id="nodesContainer" style="position: absolute; left: 0; top: 0; width: 100%; height: 100%;"></div>
</div>
<!-- Instructions -->
<div class="absolute bottom-4 left-4 bg-[#1A1A1A]/90 backdrop-blur-sm border border-[#2A2A2A] rounded-lg px-4 py-3 max-w-sm">
<p class="text-xs text-slate-400 mb-2 font-medium">Quick Tips:</p>
<ul class="text-xs text-slate-500 space-y-1">
<li>• Drag files from sidebar to canvas</li>
<li>• Click nodes to edit parameters</li>
<li>• Drag between ports to connect</li>
<li>• Space + Drag to pan canvas</li>
<li>• Delete key to remove selected</li>
</ul>
</div>
</main>
<!-- Right Sidebar (Properties) -->
<aside id="propertiesPanel" class="properties-panel hidden w-80 bg-[#121212] border-l border-[#1E1E1E] flex flex-col">
<div class="p-4 border-b border-[#1E1E1E]">
<div class="flex items-center justify-between mb-2">
<h2 class="text-lg font-semibold text-slate-100">Properties</h2>
<button id="closePanelBtn" class="text-slate-400 hover:text-slate-100 transition-colors duration-150 p-1 hover:bg-[#1A1A1A] rounded">
<svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<p id="selectedNodeName" class="text-sm text-slate-500 font-mono"></p>
</div>
<div id="parametersContainer" class="flex-1 overflow-y-auto p-4">
<!-- Parameters will be dynamically inserted here -->
</div>
<div class="p-4 border-t border-[#1E1E1E]">
<button id="saveChangesBtn" class="w-full px-4 py-2.5 bg-blue-500 hover:bg-blue-600 text-white rounded-lg font-medium transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-[#121212]">
Save Changes
</button>
<button id="deleteNodeBtn" class="w-full mt-2 px-4 py-2 text-red-400 hover:text-red-300 text-sm transition-colors duration-200 hover:bg-red-500/10 rounded-lg">
Delete Node
</button>
</div>
</aside>
</div>
<script>
// Component data loaded from API
let componentTemplates = [];
const API_URL = 'http://localhost:5000/api/components';
// Load components from API
async function loadComponents() {
try {
const response = await fetch(API_URL);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
componentTemplates = await response.json();
console.log(`Loaded ${componentTemplates.length} components from database`);
initFileList();
} catch (error) {
console.error('Failed to load components:', error);
alert('Failed to load components. Make sure the API server is running (python3 api.py)');
}
}
// State
let nodes = [];
let connections = [];
let selectedNode = null;
let nextNodeId = 1;
let nextConnectionId = 1;
let zoom = 1;
let panX = 0;
let panY = 0;
let isPanning = false;
let draggedPort = null;
let tempConnection = null;
// DOM Elements
const fileList = document.getElementById('fileList');
const nodesContainer = document.getElementById('nodesContainer');
const connectionsSvg = document.getElementById('connectionsSvg');
const canvasContainer = document.getElementById('canvasContainer');
const propertiesPanel = document.getElementById('propertiesPanel');
const parametersContainer = document.getElementById('parametersContainer');
const selectedNodeName = document.getElementById('selectedNodeName');
const zoomLevel = document.getElementById('zoomLevel');
// Initialize file list
function initFileList() {
fileList.innerHTML = componentTemplates.map(template => `
<div class="file-item px-3 py-2 rounded-lg" draggable="true" data-component="${template.name}">
<div class="flex items-center gap-2">
<svg class="w-4 h-4 text-blue-400 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
<div class="flex-1 min-w-0">
<p class="font-mono text-xs text-slate-300 truncate">${template.name}.json</p>
<p class="text-xs text-slate-500 mt-0.5">${template.parameters.length} params</p>
</div>
</div>
</div>
`).join('');
// Add drag listeners
document.querySelectorAll('.file-item').forEach(item => {
item.addEventListener('dragstart', handleFileDragStart);
item.addEventListener('dragend', handleFileDragEnd);
});
}
// File drag handlers
function handleFileDragStart(e) {
e.target.classList.add('dragging');
e.dataTransfer.effectAllowed = 'copy';
e.dataTransfer.setData('component', e.target.dataset.component);
}
function handleFileDragEnd(e) {
e.target.classList.remove('dragging');
}
// Canvas drop handler
canvasContainer.addEventListener('dragover', (e) => {
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
canvasContainer.addEventListener('drop', (e) => {
e.preventDefault();
const componentName = e.dataTransfer.getData('component');
if (componentName) {
const template = componentTemplates.find(t => t.name === componentName);
const rect = canvasContainer.getBoundingClientRect();
const x = (e.clientX - rect.left - panX) / zoom;
const y = (e.clientY - rect.top - panY) / zoom;
createNode(template, x, y);
}
});
// Create node
function createNode(template, x, y) {
const node = {
id: nextNodeId++,
name: template.name,
x: x,
y: y,
parameters: template.parameters.map(p => ({...p, value: p.defaultValue}))
};
nodes.push(node);
renderNode(node);
saveState();
}
// Render node
function renderNode(node) {
const nodeEl = document.createElement('div');
nodeEl.className = 'node';
nodeEl.dataset.nodeId = node.id;
nodeEl.style.left = `${node.x}px`;
nodeEl.style.top = `${node.y}px`;
nodeEl.innerHTML = `
<div class="node-header px-4 py-3 border-b border-[#2A2A2A]" style="cursor: move;">
<h3 class="text-sm font-semibold text-slate-100">${node.name}</h3>
<p class="text-xs text-slate-500 font-mono mt-0.5">${node.parameters.length} parameters</p>
</div>
<div class="px-4 py-3">
<div class="flex items-center justify-between mb-3">
<div class="flex items-center gap-2">
<div class="port port-input" data-node-id="${node.id}" data-type="input"></div>
<span class="text-xs text-slate-400">Input</span>
</div>
</div>
<div class="flex items-center justify-end gap-2">
<span class="text-xs text-slate-400">Output</span>
<div class="port port-output" data-node-id="${node.id}" data-type="output"></div>
</div>
</div>
`;
nodesContainer.appendChild(nodeEl);
// Node click to select
nodeEl.addEventListener('click', (e) => {
if (!e.target.classList.contains('port')) {
selectNode(node.id);
}
});
// Node drag
const header = nodeEl.querySelector('.node-header');
let isDragging = false;
let startX, startY, initialX, initialY;
header.addEventListener('mousedown', (e) => {
if (e.button !== 0) return;
isDragging = true;
nodeEl.classList.add('dragging');
startX = e.clientX;
startY = e.clientY;
initialX = node.x;
initialY = node.y;
e.preventDefault();
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const dx = (e.clientX - startX) / zoom;
const dy = (e.clientY - startY) / zoom;
node.x = initialX + dx;
node.y = initialY + dy;
nodeEl.style.left = `${node.x}px`;
nodeEl.style.top = `${node.y}px`;
updateConnections();
});
document.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
nodeEl.classList.remove('dragging');
saveState();
}
});
// Port connections
const ports = nodeEl.querySelectorAll('.port');
ports.forEach(port => {
port.addEventListener('mousedown', handlePortMouseDown);
});
}
// Port connection handling
function handlePortMouseDown(e) {
e.stopPropagation();
e.preventDefault();
if (e.button !== 0) return;
const portType = e.target.dataset.type;
if (portType !== 'output') return; // Only start from output
draggedPort = {
nodeId: parseInt(e.target.dataset.nodeId),
type: portType,
element: e.target
};
// Create temporary connection
const rect = e.target.getBoundingClientRect();
const canvasRect = canvasContainer.getBoundingClientRect();
const startX = rect.left + rect.width / 2 - canvasRect.left;
const startY = rect.top + rect.height / 2 - canvasRect.top;
tempConnection = {
startX,
startY,
endX: startX,
endY: startY
};
console.log('Started connection from node:', draggedPort.nodeId);
document.addEventListener('mousemove', handleConnectionDrag);
document.addEventListener('mouseup', handleConnectionEnd);
}
function handleConnectionDrag(e) {
if (!tempConnection) return;
e.preventDefault();
const canvasRect = canvasContainer.getBoundingClientRect();
tempConnection.endX = e.clientX - canvasRect.left;
tempConnection.endY = e.clientY - canvasRect.top;
console.log('Dragging connection to:', tempConnection.endX, tempConnection.endY);
canvasContainer.style.cursor = 'crosshair';
renderTempConnection();
// Highlight valid ports
document.querySelectorAll('.port-input').forEach(port => {
const rect = port.getBoundingClientRect();
const distance = Math.hypot(e.clientX - (rect.left + rect.width / 2), e.clientY - (rect.top + rect.height / 2));
if (distance < 30) {
port.classList.add('valid');
} else {
port.classList.remove('valid');
}
});
}
function handleConnectionEnd(e) {
document.removeEventListener('mousemove', handleConnectionDrag);
document.removeEventListener('mouseup', handleConnectionEnd);
canvasContainer.style.cursor = 'grab';
// Check if dropped on valid input port
const target = document.elementFromPoint(e.clientX, e.clientY);
console.log('Connection ended on:', target);
if (target && target.classList.contains('port-input')) {
const targetNodeId = parseInt(target.dataset.nodeId);
console.log('Connecting:', draggedPort.nodeId, '->', targetNodeId);
if (targetNodeId !== draggedPort.nodeId) {
createConnection(draggedPort.nodeId, targetNodeId);
}
} else {
console.log('No valid target found');
}
// Cleanup
draggedPort = null;
tempConnection = null;
document.querySelectorAll('.port').forEach(p => p.classList.remove('valid'));
renderConnections();
}
function renderTempConnection() {
if (!tempConnection) return;
const path = createBezierPath(tempConnection.startX, tempConnection.startY, tempConnection.endX, tempConnection.endY);
connectionsSvg.innerHTML = connections.map(renderConnection).join('') +
`<path d="${path}" stroke="#3B82F6" stroke-width="3" fill="none" class="connection active" />`;
console.log('Rendering temp connection:', path);
}
// Create connection
function createConnection(fromNodeId, toNodeId) {
// Check if connection already exists
const exists = connections.some(c => c.from === fromNodeId && c.to === toNodeId);
if (exists) return;
connections.push({
id: nextConnectionId++,
from: fromNodeId,
to: toNodeId
});
renderConnections();
saveState();
}
// Render connections
function renderConnections() {
connectionsSvg.innerHTML = connections.map(renderConnection).join('');
// Add click handlers to delete connections
connectionsSvg.querySelectorAll('.connection').forEach((path, index) => {
path.style.pointerEvents = 'stroke';
path.addEventListener('click', () => {
if (confirm('Delete this connection?')) {
connections.splice(index, 1);
renderConnections();
saveState();
}
});
});
}
function renderConnection(conn) {
const fromNode = nodes.find(n => n.id === conn.from);
const toNode = nodes.find(n => n.id === conn.to);
if (!fromNode || !toNode) return '';
const fromEl = document.querySelector(`[data-node-id="${conn.from}"] .port-output`);
const toEl = document.querySelector(`[data-node-id="${conn.to}"] .port-input`);
if (!fromEl || !toEl) return '';
const fromRect = fromEl.getBoundingClientRect();
const toRect = toEl.getBoundingClientRect();
const canvasRect = canvasContainer.getBoundingClientRect();
// Get center of ports in screen coordinates, then convert to canvas coordinates
const x1 = fromRect.left + fromRect.width / 2 - canvasRect.left;
const y1 = fromRect.top + fromRect.height / 2 - canvasRect.top;
const x2 = toRect.left + toRect.width / 2 - canvasRect.left;
const y2 = toRect.top + toRect.height / 2 - canvasRect.top;
const path = createBezierPath(x1, y1, x2, y2);
return `<path d="${path}" class="connection" />`;
}
function createBezierPath(x1, y1, x2, y2) {
const dx = Math.abs(x2 - x1) * 0.5;
return `M ${x1},${y1} C ${x1 + dx},${y1} ${x2 - dx},${y2} ${x2},${y2}`;
}
function updateConnections() {
renderConnections();
}
// Select node
function selectNode(nodeId) {
// Deselect previous
document.querySelectorAll('.node').forEach(n => n.classList.remove('selected'));
selectedNode = nodes.find(n => n.id === nodeId);
if (!selectedNode) return;
// Select new
const nodeEl = document.querySelector(`[data-node-id="${nodeId}"]`);
nodeEl.classList.add('selected');
// Show properties panel
showProperties(selectedNode);
}
// Show properties
function showProperties(node) {
propertiesPanel.classList.remove('hidden');
selectedNodeName.textContent = `${node.name}.json`;
parametersContainer.innerHTML = node.parameters.map((param, index) => {
let inputHtml = '';
if (param.datatype === 'float' || param.datatype === 'int') {
const step = param.datatype === 'float' ? '0.001' : '1';
inputHtml = `
<input
type="number"
step="${step}"
value="${param.value}"
data-param-index="${index}"
class="param-input w-full px-3 py-2 bg-[#0D0D0D] border border-[#2A2A2A] rounded-lg text-sm text-slate-100 font-mono focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-150"
/>
`;
} else if (param.datatype === 'boolean') {
inputHtml = `
<div class="flex items-center gap-3">
<label class="flex items-center cursor-pointer">
<input type="radio" name="param-${index}" value="true" ${param.value ? 'checked' : ''} data-param-index="${index}" class="param-input w-4 h-4">
<span class="ml-2 text-sm text-slate-300">True</span>
</label>
<label class="flex items-center cursor-pointer">
<input type="radio" name="param-${index}" value="false" ${!param.value ? 'checked' : ''} data-param-index="${index}" class="param-input w-4 h-4">
<span class="ml-2 text-sm text-slate-300">False</span>
</label>
</div>
`;
} else if (param.datatype === 'string' && param.options) {
inputHtml = `
<select data-param-index="${index}" class="param-input w-full px-3 py-2 bg-[#0D0D0D] border border-[#2A2A2A] rounded-lg text-sm text-slate-100 font-mono focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-150">
${param.options.map(opt => `<option value="${opt}" ${opt === param.value ? 'selected' : ''}>${opt}</option>`).join('')}
</select>
`;
} else {
inputHtml = `
<input
type="text"
value="${param.value}"
data-param-index="${index}"
class="param-input w-full px-3 py-2 bg-[#0D0D0D] border border-[#2A2A2A] rounded-lg text-sm text-slate-100 font-mono focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-150"
/>
`;
}
return `
<div class="mb-4">
<label class="block text-xs font-medium text-slate-400 mb-1.5 font-mono">
${param.name} (${param.datatype})
</label>
${inputHtml}
${param.description ? `<p class="text-xs text-slate-500 mt-1.5">${param.description}</p>` : ''}
</div>
`;
}).join('');
}
// Close properties panel
document.getElementById('closePanelBtn').addEventListener('click', () => {
propertiesPanel.classList.add('hidden');
selectedNode = null;
document.querySelectorAll('.node').forEach(n => n.classList.remove('selected'));
});
// Save changes
document.getElementById('saveChangesBtn').addEventListener('click', () => {
if (!selectedNode) return;
document.querySelectorAll('.param-input').forEach(input => {
const index = parseInt(input.dataset.paramIndex);
const param = selectedNode.parameters[index];
if (input.type === 'radio') {
if (input.checked) {
param.value = input.value === 'true';
}
} else if (param.datatype === 'float') {
param.value = parseFloat(input.value) || 0;
} else if (param.datatype === 'int') {
param.value = parseInt(input.value) || 0;
} else {
param.value = input.value;
}
});
saveState();
alert('Parameters saved!');
});
// Delete node
document.getElementById('deleteNodeBtn').addEventListener('click', () => {
if (!selectedNode) return;
if (!confirm('Delete this node?')) return;
// Remove connections
connections = connections.filter(c => c.from !== selectedNode.id && c.to !== selectedNode.id);
// Remove node
const nodeEl = document.querySelector(`[data-node-id="${selectedNode.id}"]`);
nodeEl.remove();
nodes = nodes.filter(n => n.id !== selectedNode.id);
// Close panel
propertiesPanel.classList.add('hidden');
selectedNode = null;
renderConnections();
saveState();
});
// Canvas panning
canvasContainer.addEventListener('mousedown', (e) => {
if (e.button !== 0 || e.target.classList.contains('node') || e.target.closest('.node')) return;
if (e.target.classList.contains('port')) return;
isPanning = true;
canvasContainer.style.cursor = 'grabbing';
const startX = e.clientX - panX;
const startY = e.clientY - panY;
function onMouseMove(e) {
if (!isPanning) return;
panX = e.clientX - startX;
panY = e.clientY - startY;
updateCanvasTransform();
}
function onMouseUp() {
isPanning = false;
canvasContainer.style.cursor = 'grab';
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
}
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
});
// Zoom
canvasContainer.addEventListener('wheel', (e) => {
e.preventDefault();
const delta = e.deltaY > 0 ? 0.9 : 1.1;
zoom = Math.max(0.25, Math.min(2, zoom * delta));
updateCanvasTransform();
});
document.getElementById('zoomInBtn').addEventListener('click', () => {
zoom = Math.min(2, zoom * 1.1);
updateCanvasTransform();
});
document.getElementById('zoomOutBtn').addEventListener('click', () => {
zoom = Math.max(0.25, zoom * 0.9);
updateCanvasTransform();
});
document.getElementById('fitBtn').addEventListener('click', () => {
zoom = 1;
panX = 0;
panY = 0;
updateCanvasTransform();
});
function updateCanvasTransform() {
nodesContainer.style.transform = `translate(${panX}px, ${panY}px) scale(${zoom})`;
nodesContainer.style.transformOrigin = '0 0';
zoomLevel.textContent = `${Math.round(zoom * 100)}%`;
renderConnections();
}
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
if (e.key === 'Delete' || e.key === 'Backspace') {
if (selectedNode && !e.target.classList.contains('param-input')) {
e.preventDefault();
document.getElementById('deleteNodeBtn').click();
}
}
if (e.key === 'Escape') {
document.getElementById('closePanelBtn').click();
}
if (e.ctrlKey || e.metaKey) {
if (e.key === 's') {
e.preventDefault();
document.getElementById('saveBtn').click();
}
}
});
// Save/Export
document.getElementById('saveBtn').addEventListener('click', () => {
localStorage.setItem('nodeFlowState', JSON.stringify({ nodes, connections }));
alert('Canvas saved!');
});
document.getElementById('exportBtn').addEventListener('click', () => {
const data = JSON.stringify({ nodes, connections }, null, 2);
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'node-flow-export.json';
a.click();
});
function saveState() {
localStorage.setItem('nodeFlowState', JSON.stringify({ nodes, connections }));
}
function loadState() {
const saved = localStorage.getItem('nodeFlowState');
if (saved) {
const state = JSON.parse(saved);
nodes = state.nodes || [];
connections = state.connections || [];
nextNodeId = Math.max(...nodes.map(n => n.id), 0) + 1;
nextConnectionId = Math.max(...connections.map(c => c.id), 0) + 1;
nodes.forEach(node => renderNode(node));
renderConnections();
}
}
// Search
document.getElementById('searchInput').addEventListener('input', (e) => {
const query = e.target.value.toLowerCase();
document.querySelectorAll('.file-item').forEach(item => {
const name = item.dataset.component.toLowerCase();
item.style.display = name.includes(query) ? 'block' : 'none';
});
});
// Initialize
loadComponents().then(() => {
loadState();
});
</script>
</body>
</html>