npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@au-re/ramen

v0.7.2

Published

Node editor for visual programming

Downloads

24

Readme

ramen

Ramen is a simple but extensible react library for building node editors for visual programming.

npm commitizen friendly Quality Gate Status Build Status dependencies Status size

Getting Started

You can find examples and the complete documentation here.

Installation

Ramen can be installed via npm.

$ npm i -S @au-re/ramen

Example Usage

import Ramen from "@au-re/ramen";

Start by creating a schema. The schema defines the types of nodes, fields and connections that can exist in your graph.

const schema = {
  nodeTypes: {
    number: {
      fields: [
        {
          id: "number",
          dataType: "number",
          output: true,
        }
      ]
    },
    add: {
      fields: [
        {
          id: "number1",
          dataType: "number",
          input: true,
        },
        {
          id: "number2",
          dataType: "number",
          input: true,
        },
        {
          id: "result",
          dataType: "number",
          output: true,
        }
      ]
    }
  },
  dataTypes: {
    number: {
      name: "Number",
      color: "#333",
      validTargets: [
        "number",
      ],
    },
  },
};

The graph state is persisted in the following format:

const graph = {
  xPos: 100,
  yPos: 200,
  nodes: [
    {
      id: "0",
      x: 100,
      y: 50,
      type: "number",
    },
    {
      id: "1",
      x: 100,
      y: 200,
      type: "number",
    },
    {
      id: "2",
      x: 450,
      y: 50,
      type: "add",
    },
  ],
  connections: [
    {
      originNode: "0",
      originPin: "number",
      targetNode: "2",
      targetPin: "number1",
    },
    {
      originNode: "1",
      originPin: "number",
      targetNode: "2",
      targetPin: "number2",
    },
  ],
}

To initialize a node editor you can pass both the schema and the graph. Note that connections in graph that are not allowed by the schema will be ignored.

<Ramen
  schema={schema}
  initialGraph={graph}
/>

The NodeEditor can be either controlled or uncontrolled. By adding a graph property to the editor, it becomes controlled: Passing a new graph value to the editor will cause the editor to rerender and the graph will no longer update its own state internally. Instead you should use a callback to check changes in the graph.

<Ramen
  graph={graph}
/>

You can listen to changes to the graph structure, e.g. a new connection is created, removed.

<Ramen
  onNodeCreated
  onNodeDeleted
  onConnectionCreated
  onConnectionDeleted
  onGraphChange
  onControlChange
/>

Field Controls

In order to update the state of a node it is often necessary to provide an input of some sorts for the case that no connection to a field exists. For that reason you can pass custom inputs to ramen:


function NumberControl(props) {
  const { defaultValue } = props;
  return <input type="number" />
}

const schema = {
  nodeTypes: {
    type1: {
      fields: [
        {
          id: "input",
          dataType: "number",
          controlType: "numberControl"
        }
      ]
    }
  }
};

<NodeEditor
  schema={schema}
  controls={{
    numberControl: NumberControl,
  }}
  onControlChange={(nodeId, fieldId, data) => {}}
/>

You can initialize the state of controls with the initialGraph value, or the graph value if you are using the editor controlled.

const graph = {
  nodes: [
    {
      id: "0",
      type: "add",
      defaultValues: {
        number1: 10
      }
    },
  ],
};

Customization

You can customize every aspect of the editor by either passing a custom theme or custom components.

The simplest way to customize the look of the editor is by swapping the theme. Ramen ships with two themes, dark and light.

Ramen uses the styled-components library for styling. You can use styled-components ThemeProvider to pass a new theme:

import { ThemeProvider } from "styled-components";

// example theme
const theme = {};

<ThemeProvider theme={theme}>
  <NodeEditor />
<ThemeProvider

You can find more information on how to use the ThemeProvider and how to switch themes here.

Custom Components

You can also provide custom components:

function Background() {
  return (<div></div>);
}

<NodeEditor
  Background={Background}
/>

Custom components can be styled to your liking, and you can modify their internal behavior.

// custom node with an input field instead of a title
function Node(props) {
  const { name } = props;
  return (
    <div>
      <input value={name} />
      {children}
    </div>)
}

<NodeEditor
  Node={Node}
/>

The following components can be passed as properties:

<NodeEditor
  Background
  Node
  Noodle
  Field
  ContextMenu
  BoxSelection
/>

Extending Functionality

Children of the node editor will be displayed overlaying the graph editor. You can access the internal state of the graph editor through the editorContext.

import { editorContext } from "@au-re/ramen";

function MiniMap(props) {
  const { graph, setGraph } = React.useContext(editorContext);
  // render a minimap
}

<NodeEditor>
  <MiniMap />
</NodeEditor>

License

MIT