Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="rete"></div>

<a class="github-fork-ribbon" href="https://github.com/retejs/rete/tree/next" target="_blank" data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>
              
            
!

CSS

              
                html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  overflow: hidden;
}

#rete {
  width: 100vw;
  height: 100vh;
}


.github-fork-ribbon {
  position: absolute;
  top: 0;
  right: 0;
  border: 0
}
.github-fork-ribbon:before {
  background-color: #f5f5f5;
}
.github-fork-ribbon:after {
  color: #000;
  text-shadow: 0 -0.08em rgb(255 255 255);
  border-color: rgb(73 73 73 / 70%);
  border-width: 0.12em;
  padding: 0.09em 0;
  margin: 0.09em 0;
}
              
            
!

JS

              
                const { ClassicPreset: Classic, NodeEditor } = window.Rete
const { createRoot } = window.ReactDOM
const { AreaExtensions, AreaPlugin } = window.ReteAreaPlugin
const { ConnectionPlugin, Presets: ConnectionPresets } = window.ReteConnectionPlugin
const { ReactRenderPlugin, Presets: ReactPresets } = window.ReteReactRenderPlugin
const { DataflowEngine } = window.ReteEngine
const { ContextMenuPlugin, Presets: ContextMenuPresets } = window.ReteContextMenuPlugin

class Connection extends Classic.Connection {}

class NumberNode extends Classic.Node {
  constructor(initial, change) {
    super('Number');

    this.addOutput('value', new Classic.Output(socket, 'Number'));
    this.addControl(
      'value',
      new Classic.InputControl('number', { initial, change })
    );
  }
  data() {
    const value = this.controls['value'].value;

    return {
      value,
    };
  }
  clone = () => {
    return new NumberNode(this.controls['value'].value, this.change)
  }
}

class AddNode extends Classic.Node {
  constructor(change) {
    super('Add');
    const left = new Classic.Input(socket, 'Left')
    const right = new Classic.Input(socket, 'Right')
    
    left.addControl(new Classic.InputControl('number', { initial: 0, change }))
    right.addControl(new Classic.InputControl('number', { initial: 0, change }))

    this.addInput('left', left);
    this.addInput('right', right);
    this.addOutput('value', new Classic.Output(socket, 'Number'));
    this.addControl('result', new Classic.InputControl('number', { initial: 0, readonly: true }));
  }
  data(inputs) {
    const { left = [], right = [] } = inputs;
    const leftControl = this.inputs['left'].control
    const rightControl = this.inputs['right'].control
    const sum = (left[0] || leftControl.value) + (right[0] || rightControl.value);

    this.controls['result'].setValue(sum);

    return {
      value: sum,
    };
  }
  clone = () => {
    return new AddNode(this.change)
  }
}

const socket = new Classic.Socket('socket');
console.log('document', document)
const editor = new NodeEditor();
const area = new AreaPlugin(document.querySelector('#rete'));
const connection = new ConnectionPlugin();
const reactRender = new ReactRenderPlugin({ createRoot });
const contextMenu = new ContextMenuPlugin({
  items: ContextMenuPresets.classic.setup([
    ['Number', () => new NumberNode(1, process)],
    ['Add', () => new AddNode(process)]
  ])
})

connection.addPreset(ConnectionPresets.classic.setup())

editor.use(area);

area.use(contextMenu)
area.use(reactRender);
area.use(connection);

reactRender.addPreset(ReactPresets.classic.setup({ area }));
reactRender.addPreset(ReactPresets.contextMenu.setup())


AreaExtensions.simpleNodesOrder(area);
AreaExtensions.showInputControl(area)

AreaExtensions.selectableNodes(area, AreaExtensions.selector(), {
  accumulating: AreaExtensions.accumulateOnCtrl(),
});

const dataflow = new DataflowEngine();

editor.use(dataflow);

void async function () {
  const a = new NumberNode(1, process);
  const b = new NumberNode(1, process);
  const add = new AddNode(process);

  await editor.addNode(a);
  await editor.addNode(b);
  await editor.addNode(add);

  await editor.addConnection(new Connection(a, 'value', add, 'left'));
  await editor.addConnection(new Connection(b, 'value', add, 'right'));

  await area.translate(a.id, { x: 80, y: 200 });
  await area.translate(b.id, { x: 80, y: 400 });
  await area.translate(add.id, { x: 500, y: 240 });

  setTimeout(() => { //wait for nodes to be rendered
    AreaExtensions.zoomAt(area, editor.getNodes());
  }, 100)

  async function process() {
    dataflow.reset();
    for (const node of editor.getNodes()) {
      if (node instanceof AddNode) {
        await dataflow.fetch(node.id);

        area.update('control', node.controls['result'].id);
      }
    }
  }
  await process();

  editor.addPipe((context) => {
    if (
      context.type === 'connectioncreated' ||
      context.type === 'connectionremoved'
    ) {
      process();
    }
    return context;
  });

}()
              
            
!
999px

Console