Spaces:
Sleeping
Sleeping
Commit ·
aace3df
1
Parent(s): fb6241c
Fourth push
Browse files- index.html +2 -2
- index.js +49 -5
index.html
CHANGED
|
@@ -9,6 +9,6 @@
|
|
| 9 |
<div id="rete" style="height: 500px; width: 100%;"></div>
|
| 10 |
|
| 11 |
<!-- Include your JavaScript file -->
|
| 12 |
-
<script src="index.js" defer></script>
|
| 13 |
</body>
|
| 14 |
-
</html>
|
|
|
|
| 9 |
<div id="rete" style="height: 500px; width: 100%;"></div>
|
| 10 |
|
| 11 |
<!-- Include your JavaScript file -->
|
| 12 |
+
<script src="path/to/your/index.js" defer></script>
|
| 13 |
</body>
|
| 14 |
+
</html>
|
index.js
CHANGED
|
@@ -1,15 +1,59 @@
|
|
| 1 |
// index.js or app.js
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
const container = document.querySelector('#rete');
|
| 5 |
const editor = new Rete.NodeEditor('demo@0.1.0', container);
|
| 6 |
|
| 7 |
-
//
|
| 8 |
-
const
|
| 9 |
-
editor.register(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
editor.view.resize();
|
| 12 |
window.addEventListener('resize', () => editor.view.resize());
|
| 13 |
|
| 14 |
editor.trigger('process');
|
| 15 |
-
});
|
|
|
|
| 1 |
// index.js or app.js
|
| 2 |
|
| 3 |
+
// Define the NumComponent class
|
| 4 |
+
class NumComponent extends Rete.Component {
|
| 5 |
+
constructor() {
|
| 6 |
+
super("Number");
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
builder(node) {
|
| 10 |
+
const numSocket = new Rete.Socket('Number value'); // Define the socket here or globally
|
| 11 |
+
const out1 = new Rete.Output('num', "Number", numSocket);
|
| 12 |
+
|
| 13 |
+
node.addOutput(out1);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
worker(node, inputs, outputs) {
|
| 17 |
+
outputs['num'] = node.data.num;
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// Initialize the Rete.js editor
|
| 22 |
+
document.addEventListener('DOMContentLoaded', async function () {
|
| 23 |
const container = document.querySelector('#rete');
|
| 24 |
const editor = new Rete.NodeEditor('demo@0.1.0', container);
|
| 25 |
|
| 26 |
+
// Register the component
|
| 27 |
+
const numComponent = new NumComponent();
|
| 28 |
+
editor.register(numComponent);
|
| 29 |
+
|
| 30 |
+
// Setup the editor area and use plugins
|
| 31 |
+
const engine = new Rete.Engine('demo@0.1.0');
|
| 32 |
+
editor.use(Rete.ConnectionPlugin); // enables drawing connections
|
| 33 |
+
editor.use(Rete.ReactRenderPlugin); // enables rendering nodes (if using React)
|
| 34 |
+
editor.use(Rete.ContextMenuPlugin); // adds a context menu to add nodes
|
| 35 |
+
editor.use(Rete.AreaPlugin); // enables dragging and zooming
|
| 36 |
+
editor.use(Rete.CommentPlugin); // enables comments
|
| 37 |
+
|
| 38 |
+
// Add a simple node to the editor
|
| 39 |
+
const node1 = await numComponent.createNode({ num: 2 });
|
| 40 |
+
node1.position = [80, 200];
|
| 41 |
+
editor.addNode(node1);
|
| 42 |
+
|
| 43 |
+
const node2 = await numComponent.createNode({ num: 3 });
|
| 44 |
+
node2.position = [300, 200];
|
| 45 |
+
editor.addNode(node2);
|
| 46 |
+
|
| 47 |
+
// Draw a connection between nodes
|
| 48 |
+
editor.connect(node1.outputs.get('num'), node2.inputs.get('num'));
|
| 49 |
+
|
| 50 |
+
editor.on('process nodecreated noderemoved connectioncreated connectionremoved', async () => {
|
| 51 |
+
console.log('Process triggered');
|
| 52 |
+
await engine.process(editor.toJSON());
|
| 53 |
+
});
|
| 54 |
|
| 55 |
editor.view.resize();
|
| 56 |
window.addEventListener('resize', () => editor.view.resize());
|
| 57 |
|
| 58 |
editor.trigger('process');
|
| 59 |
+
});
|