Spaces:
Running
Running
Update static/canvas.js
Browse files- static/canvas.js +20 -17
static/canvas.js
CHANGED
@@ -99,26 +99,29 @@ function clearCanvas() {
|
|
99 |
function createNodesFromParsedData(parsedNodes, parsedConnections) {
|
100 |
const scopePositions = {}; // Track x-position per scope
|
101 |
parsedNodes.forEach(nodeData => {
|
102 |
-
|
|
|
|
|
103 |
if (!scopePositions[scope]) {
|
104 |
scopePositions[scope] = { x: 50, count: 0 };
|
105 |
}
|
106 |
// Stack left to right: increment x based on level and scope
|
107 |
-
const
|
|
|
108 |
const y = scopePositions[scope].count * 80 + 50; // Slight y offset for readability
|
109 |
scopePositions[scope].count += 1;
|
110 |
|
111 |
const node = createNode(
|
112 |
x,
|
113 |
y,
|
114 |
-
nodeData.label,
|
115 |
-
nodeData.type,
|
116 |
-
nodeData.inputs,
|
117 |
-
nodeData.outputs,
|
118 |
-
nodeData.id
|
119 |
-
nodeData.source,
|
120 |
-
|
121 |
-
|
122 |
);
|
123 |
nodes.push(node);
|
124 |
layer.add(node);
|
@@ -129,7 +132,7 @@ function createNodesFromParsedData(parsedNodes, parsedConnections) {
|
|
129 |
}
|
130 |
|
131 |
// Create a node with inputs, outputs, and code segment
|
132 |
-
function createNode(x, y, label, type, inputs = [], outputs = [], id, source = '', parent_path = '', level = 0) {
|
133 |
const node = new Konva.Group({
|
134 |
x: x,
|
135 |
y: y,
|
@@ -314,7 +317,6 @@ function createNode(x, y, label, type, inputs = [], outputs = [], id, source = '
|
|
314 |
// Create textarea for editing
|
315 |
codeTextarea = document.createElement('textarea');
|
316 |
codeTextarea.style.position = 'absolute';
|
317 |
-
// Calculate position relative to canvas
|
318 |
const canvasRect = stage.container().getBoundingClientRect();
|
319 |
const textareaX = (nodePos.x + stage.x()) / scale + canvasRect.left;
|
320 |
const textareaY = (nodePos.y + height + 10 + stage.y()) / scale + canvasRect.top;
|
@@ -439,7 +441,7 @@ function autoConnect() {
|
|
439 |
|
440 |
const sortedNodes = [...nodes].sort((a, b) => {
|
441 |
if (a.data.level !== b.data.level) return a.data.level - b.data.level;
|
442 |
-
return a.data.x - b.data.x;
|
443 |
});
|
444 |
|
445 |
const hierarchy = {};
|
@@ -537,7 +539,7 @@ function updateProgram() {
|
|
537 |
function reconstructProgram() {
|
538 |
const sortedNodes = [...nodes].sort((a, b) => {
|
539 |
if (a.data.level !== b.data.level) return a.data.level - b.data.level;
|
540 |
-
return a.data.x - b.data.x;
|
541 |
});
|
542 |
|
543 |
let program = '';
|
@@ -626,9 +628,10 @@ function saveNodes() {
|
|
626 |
})),
|
627 |
connections: connections
|
628 |
})
|
629 |
-
})
|
630 |
-
|
631 |
-
|
|
|
632 |
}
|
633 |
|
634 |
// Initial draw
|
|
|
99 |
function createNodesFromParsedData(parsedNodes, parsedConnections) {
|
100 |
const scopePositions = {}; // Track x-position per scope
|
101 |
parsedNodes.forEach(nodeData => {
|
102 |
+
// Fallback for parent_path to prevent undefined error
|
103 |
+
const parentPath = nodeData.parent_path || 'global';
|
104 |
+
const scope = parentPath.split(' -> ')[0] || 'global';
|
105 |
if (!scopePositions[scope]) {
|
106 |
scopePositions[scope] = { x: 50, count: 0 };
|
107 |
}
|
108 |
// Stack left to right: increment x based on level and scope
|
109 |
+
const level = nodeData.level || 0;
|
110 |
+
const x = 50 + level * 150 + scopePositions[scope].count * 200;
|
111 |
const y = scopePositions[scope].count * 80 + 50; // Slight y offset for readability
|
112 |
scopePositions[scope].count += 1;
|
113 |
|
114 |
const node = createNode(
|
115 |
x,
|
116 |
y,
|
117 |
+
nodeData.label || 'Unnamed',
|
118 |
+
nodeData.type || 'other',
|
119 |
+
nodeData.inputs || [],
|
120 |
+
nodeData.outputs || [],
|
121 |
+
nodeData.id || `node_${nodes.length}`,
|
122 |
+
nodeData.source || '',
|
123 |
+
parentPath,
|
124 |
+
level
|
125 |
);
|
126 |
nodes.push(node);
|
127 |
layer.add(node);
|
|
|
132 |
}
|
133 |
|
134 |
// Create a node with inputs, outputs, and code segment
|
135 |
+
function createNode(x, y, label, type, inputs = [], outputs = [], id, source = '', parent_path = 'global', level = 0) {
|
136 |
const node = new Konva.Group({
|
137 |
x: x,
|
138 |
y: y,
|
|
|
317 |
// Create textarea for editing
|
318 |
codeTextarea = document.createElement('textarea');
|
319 |
codeTextarea.style.position = 'absolute';
|
|
|
320 |
const canvasRect = stage.container().getBoundingClientRect();
|
321 |
const textareaX = (nodePos.x + stage.x()) / scale + canvasRect.left;
|
322 |
const textareaY = (nodePos.y + height + 10 + stage.y()) / scale + canvasRect.top;
|
|
|
441 |
|
442 |
const sortedNodes = [...nodes].sort((a, b) => {
|
443 |
if (a.data.level !== b.data.level) return a.data.level - b.data.level;
|
444 |
+
return a.data.x - b.data.x;
|
445 |
});
|
446 |
|
447 |
const hierarchy = {};
|
|
|
539 |
function reconstructProgram() {
|
540 |
const sortedNodes = [...nodes].sort((a, b) => {
|
541 |
if (a.data.level !== b.data.level) return a.data.level - b.data.level;
|
542 |
+
return a.data.x - b.data.x;
|
543 |
});
|
544 |
|
545 |
let program = '';
|
|
|
628 |
})),
|
629 |
connections: connections
|
630 |
})
|
631 |
+
})
|
632 |
+
.then(response => response.json())
|
633 |
+
.then(data => console.log('Saved:', data))
|
634 |
+
.catch(error => console.error('Error:', error));
|
635 |
}
|
636 |
|
637 |
// Initial draw
|