File size: 3,137 Bytes
3ff344a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Initialize Konva stage
const stage = new Konva.Stage({
    container: 'container',
    width: 800,
    height: 600
});

const layer = new Konva.Layer();
stage.add(layer);

// Store nodes and connections
let nodes = [];
let connections = [];
let selectedNode = null;

// Add a new node
function addNode() {
    const node = new Konva.Group({
        x: Math.random() * (stage.width() - 100),
        y: Math.random() * (stage.height() - 100),
        draggable: true
    });

    // Node rectangle
    const box = new Konva.Rect({
        width: 100,
        height: 50,
        fill: '#ffeb3b',
        stroke: 'black',
        strokeWidth: 2,
        cornerRadius: 5
    });

    // Node text (function name)
    const text = new Konva.Text({
        text: 'Function',
        fontSize: 12,
        fontFamily: 'Arial',
        fill: 'black',
        width: 100,
        align: 'center',
        y: 20
    });

    node.add(box);
    node.add(text);

    // Node data
    node.data = {
        id: nodes.length,
        function: 'sampleFunction', // Placeholder function name
        x: node.x(),
        y: node.y()
    };

    // Handle node click for connections
    node.on('click', () => {
        if (!selectedNode) {
            selectedNode = node;
        } else {
            // Create connection
            const line = new Konva.Line({
                points: [
                    selectedNode.x() + 50, selectedNode.y() + 25,
                    node.x() + 50, node.y() + 25
                ],
                stroke: 'black',
                strokeWidth: 2
            });
            layer.add(line);
            connections.push({
                from: selectedNode.data.id36
                to: node.data.id
            });
            selectedNode = null;
            saveNodes();
        }
    });

    // Update position on drag
    node.on('dragmove', () => {
        node.data.x = node.x();
        node.data.y = node.y();
        updateConnections();
        saveNodes();
    });

    nodes.push(node);
    layer.add(node);
    layer.draw();
}

// Update connection lines when nodes move
function updateConnections() {
    connections.forEach((conn, index) => {
        const fromNode = nodes.find(n => n.data.id === conn.from);
        const toNode = nodes.find(n => n.data.id === conn.to);
        if (fromNode && toNode) {
            const line = layer.find('Line')[index];
            if (line) {
                line.points([
                    fromNode.x() + 50, fromNode.y() + 25,
                    toNode.x() + 50, toNode.y() + 25
                ]);
            }
        }
    });
    layer.draw();
}

// Save nodes and connections to backend
function saveNodes() {
    fetch('/save_nodes', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            nodes: nodes.map(n => n.data),
            connections: connections
        })
    }).then(response => response.json())
      .then(data => console.log('Saved:', data))
      .catch(error => console.error('Error:', error));
}

// Initial draw
layer.draw();