6.0.1 • Published 2 years ago

flow-view v6.0.1

Weekly downloads
13
License
MIT
Repository
github
Last release
2 years ago

flow-view

is a visual editor for Dataflow programming

Installation

Using npm

With npm do

npm install flow-view

Using a CDN

Try this in your HTML page

<script type="module">
  import { FlowView } from 'https://unpkg.com/flow-view';

  const flowView = new FlowView(document.body);
</script>

Usage

GUI

Try demo here

Constructor

Create a FlowView instance and pass it a container. It will create a flow-view custom element and attach it to the container. Be aware that the flow-view custom element will fit the whole height of its container, so make sure to style properly to avoid a zero height container.

<!DOCTYPE html>
<html>
  <body>
    <script type="module">
      import { FlowView } from 'https://unpkg.com/flow-view';

      const flowView = new FlowView(document.body);
    </script>
  </body>
</html>

If some flow-view custom element is already in the page, it can be passed to the FlowView constructor. argument.

<!DOCTYPE html>
<html>
  <body>
    <flow-view id="my-view"></flow-view>

    <script type="module">
      import { FlowView } from 'https://unpkg.com/flow-view';

      const flowView = new FlowView(document.getElementById('my-view'));
    </script>
  </body>
</html>

Color schemes

Optionally set color scheme. If not provided it defaults to both light and dark according to system preferences.

Light scheme.

<flow-view light></flow-view>

Dark scheme.

<flow-view dark></flow-view>

addNodeDefinitions({ nodes?, types? })

Add a list to define which nodes are available. It is not required but it makes sense to be provided in the majority of use cases.

flowView.addNodeDefinitions({
	nodes: [
		{ name: "Marge", type: "parent" },
		{ name: "Homer", type: "parent" },
		{ name: "Bart", type: "child" },
		{ name: "Lisa", type: "child" },
		{ name: "Mr. Burns" },
	],
	types: {
		"parent": {
			inputs: [],
			outputs: [
				{ name: "out" },
			],
		},
		"child": {
			inputs: [
				{ name: "in1" },
				{ name: "in2" },
			],
			outputs: [],
		},
	},
});

node(id)

Get flow-view node by id.

const node = flowView.node("abc");

See also color schemes example.

edge(id)

Get flow-view edge by id.

const edge = flowView.edge("abc");

graph

Access current flow-view graph.

console.log(flowView.graph);

loadGraph({ nodes = [], edges = [] })

Load a flow-view graph.

flowView.loadGraph({
	nodes: [
		{
			id: "dad",
			text: "Homer",
			x: 60,
			y: 70,
			outs: [{ id: "children" }],
		},
		{
			id: "mom",
			text: "Marge",
			x: 160,
			y: 70,
			outs: [{ id: "children" }],
		},
		{
			id: "son",
			text: "Bart",
			x: 60,
			y: 240,
			ins: [{ id: "father" }, { id: "mother" }],
		},
		{
			id: "daughter",
			text: "Lisa",
			x: 220,
			y: 220,
			ins: [{ id: "father" }, { id: "mother" }],
		},
	],
	edges: [
		{ from: ["dad", "children"], to: ["son", "father"] },
		{ from: ["dad", "children"], to: ["daughter", "father"] },
		{ from: ["mom", "children"], to: ["son", "mother"] },
		{ from: ["mom", "children"], to: ["daughter", "mother"] },
	],
});

clearGraph()

Empty current graph.

flowView.clearGraph();

destroy()

Delete flow-view custom element.

flowView.destroy();

An use case for destroy() is the following. Suppose you are using Next.js, you need to load flow-view with an async import into a useEffect which needs to return a callback to be called when component is unmounted.

This is a sample code.

import type { FlowView } from "flow-view";
import { FC, useEffect, useRef } from "react";

const MyComponent: FC = () => {
	const flowViewContainerRef = useRef<HTMLDivElement | null>(null);
	const flowViewRef = useRef<FlowView | null>(null);

	useEffect(() => {
		let unmounted = false;

		const importFlowView = async () => {
			if (unmounted) return;
			if (flowViewContainerRef.current === null) return;
			if (flowViewRef.current !== null) return;

			const { FlowView } = await import("flow-view");

			const flowView = new FlowView({
				container: flowViewContainerRef.current,
			});
			flowViewRef.current = flowView;
		};

		importFlowView();

		return () => {
			unmounted = true;
			if (flowViewRef.current !== null) flowViewRef.current.destroy();
		};
	}, [flowViewRef, flowViewContainerRef]);

	return <div ref={flowViewContainerRef}></div>;
};

newNode() and newEdge()

Create nodes and edges programmatically. See programmatically example here.

// Create two nodes.

const node1 = flowView.newNode({
	text: "Hello",
	ins: [{}, {}],
	outs: [{ id: "output1" }],
	x: 100,
	y: 100,
	width: 80,
});
const node2 = flowView.newNode({
	text: "World",
	ins: [{ id: "input1" }],
	width: 100,
	x: 250,
	y: 400,
});

// Connect nodes with an edge.
flowView.newEdge({
	from: [node1.id, "output1"],
	to: [node2.id, "input1"],
});

deleteNode() and deleteEdge()

Delete nodes and edges programmatically. Notice that when a node is deleted, all its connected edges are deleted too.

const nodeId = "abc";
const edgeId = "123";

flowView.deleteNode(nodeId);
flowView.deleteEdge(edgeId);

addNodeClass(nodeType, NodeClass)

Can add custom node class. See custom node example here.

onChange(callback)

Set callback to be invoked on every view change. See demo code here.

Callback signature is ({ action, data }, info) => void, where

  • action can be CREATE_NODE, DELETE_NODE, etc.
  • data change based on action
  • info can contain { isLoadGraph: true } or other optional information.

nodeTextToType(func)

Set a function that will be invoked on node creation to resolve node type from node text.

License

MIT

6.0.1

2 years ago

6.0.0

3 years ago

5.8.0

3 years ago

5.7.0

3 years ago

5.6.1

3 years ago

5.6.0

3 years ago

5.5.0

3 years ago

5.3.0

3 years ago

5.4.0

3 years ago

5.2.0

3 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.19.0

8 years ago

2.18.0

8 years ago

2.17.1

8 years ago

2.17.0

8 years ago

2.16.0

8 years ago

2.15.2

8 years ago

2.15.1

8 years ago

2.15.0

8 years ago

2.14.3

8 years ago

2.14.1

8 years ago

2.14.0

8 years ago

2.12.0

8 years ago

2.11.0

8 years ago

2.10.1

8 years ago

2.10.0

8 years ago

2.9.1

8 years ago

2.9.0

9 years ago

2.8.1

9 years ago

2.8.0

9 years ago

2.7.1

9 years ago

2.7.0

9 years ago

2.6.1

9 years ago

2.6.0

9 years ago

2.5.0

9 years ago

2.4.0

9 years ago

2.3.0

9 years ago

2.2.0

9 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.10.0

9 years ago

0.9.0

10 years ago

0.8.1

10 years ago

0.8.0

10 years ago

0.7.2

10 years ago

0.7.1

10 years ago

0.7.0

10 years ago

0.6.2

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago