Skip to content

Commit

Permalink
Feat remote node (#16)
Browse files Browse the repository at this point in the history
* feat: remote node

* chore: 1.3.0
  • Loading branch information
lixiao1022 authored Jun 9, 2022
1 parent d35a3e1 commit c7843e4
Show file tree
Hide file tree
Showing 15 changed files with 2,948 additions and 3,030 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export default Demo;
| nodes | The nodes of FlowBuilder | [Node](#node)[] || - | |
| readonly | Readonly mode, cannot add, remove, configure. | `boolean` | | false | |
| registerNodes | The registered nodes | [RegisterNode](#registernode)[] || - | |
| registerRemoteNodes | The registered remote nodes | [RegisterRemoteNode](#registerremotenode)[] | | - | 1.3.0 |
| showPracticalBranchNode | - | `boolean` | - | false | 1.1.0 |
| showPracticalBranchRemove | - | `boolean` | - | false | 1.1.0 |
| spaceX | Horizontal spacing between nodes | `number` | | `16` | |
Expand Down Expand Up @@ -241,6 +242,13 @@ export default Demo;
| showPracticalBranchRemove | - | `boolean` | - | false | 1.1.0 |
| type | The type of node, promise `start` is start node type and `end` is end node type | `string` || - |

### RegisterRemoteNode

| Property | Description | Type | Required | Default | Version |
| :------- | :------------- | :------- | :------- | :------ | :------ |
| url | remote url | `string` || - | 1.3.0 |
| cssUrl | remote css url | `string` | | - | 1.3.0 |

#### DisplayComponent

| Property | Description | Type |
Expand Down
8 changes: 8 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export default Demo;
| nodes | 流程引擎的节点 | [Node](#node)[] || - | |
| readonly | 只读模式,不能进行节点的增加、删除、配置 | `boolean` | | false | |
| registerNodes | 注册节点 | [RegisterNode](#registernode)[] || - | |
| registerRemoteNodes | 注册远程节点 | [RegisterRemoteNode](#registerremotenode)[] | | - | 1.3.0 |
| spaceX | 节点之间水平方向的间距 | `number` | | 16 | |
| showPracticalBranchNode | - | `boolean` | - | false | 1.1.0 |
| showPracticalBranchRemove | - | `boolean` | - | false | 1.1.0 |
Expand Down Expand Up @@ -241,6 +242,13 @@ export default Demo;
| showPracticalBranchRemove | - | `boolean` | - | false | 1.1.0 |
| type | 节点类型 | `string` || - |

### RegisterRemoteNode

| 参数 | 说明 | 类型 | 必须 | 默认值 | 版本 |
| :----- | :----------------- | :------- | :--- | :----- | :---- |
| url | 节点的远程地址 | `string` || - | 1.3.0 |
| cssUrl | 节点样式的远程地址 | `string` | | - | 1.3.0 |

#### DisplayComponent

| 参数 | 说明 | 类型 |
Expand Down
10 changes: 9 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 6
order: 7
---

# API
Expand All @@ -21,6 +21,7 @@ order: 6
| nodes | 流程引擎的节点 | [Node](#node)[] || - | |
| readonly | 只读模式,不能进行节点的增加、删除、配置 | `boolean` | | false | |
| registerNodes | 注册节点 | [RegisterNode](#registernode)[] || - | |
| registerRemoteNodes | 注册远程节点 | [RegisterRemoteNode](#registerremotenode)[] | | - | 1.3.0 |
| showPracticalBranchNode | - | `boolean` | - | false | 1.1.0 |
| showPracticalBranchRemove | - | `boolean` | - | false | 1.1.0 |
| spaceX | 节点之间水平方向的间距 | `number` | | 16 | |
Expand Down Expand Up @@ -101,6 +102,13 @@ order: 6
| showPracticalBranchRemove | - | `boolean` | - | false | 1.1.0 |
| type | 节点类型 | `string` || - |

## RegisterRemoteNode

| 参数 | 说明 | 类型 | 必须 | 默认值 | 版本 |
| :----- | :----------------- | :------- | :--- | :----- | :---- |
| url | 节点的远程地址 | `string` || - | 1.3.0 |
| cssUrl | 节点样式的远程地址 | `string` | | - | 1.3.0 |

### DisplayComponent

| 参数 | 说明 | 类型 |
Expand Down
8 changes: 7 additions & 1 deletion docs/changeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 6
order: 8
---

# Change Log
Expand All @@ -15,3 +15,9 @@ order: 6
`2022-05-24`

- 节点的默认 id 从 `node-` 开头修改为 `节点类型-` 开头

## 1.3.0

`2022-06-09`

- 支持远程节点注册,通过 System.js 加载对应的 js / css 资源
36 changes: 36 additions & 0 deletions docs/demo/remote/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.start-node, .end-node {
height: 64px;
width: 64px;
border-radius: 50%;
line-height: 64px;
color: #fff;
text-align: center;
}

.start-node {
background-color: #338aff;
}

.end-node {
background-color: #666;
}

.other-node, .condition-node {
width: 224px;
border-radius: 4px;
color: #666;
background: #fff;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.08);
}

.other-node {
height: 118px;
padding: 16px;
display: flex;
flex-direction: column;
}

.condition-node {
height: 44px;
padding: 12px 16px;
}
136 changes: 136 additions & 0 deletions docs/demo/remote/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import React, { useState, useContext } from 'react';
import FlowBuilder, {
NodeContext,
INode,
IRegisterNode,
} from 'react-flow-builder';

import './index.css';

window.React = React;

const StartNodeDisplay: React.FC = () => {
const node = useContext(NodeContext);
return <div className="start-node">{node.name}</div>;
};

const EndNodeDisplay: React.FC = () => {
const node = useContext(NodeContext);
return <div className="end-node">{node.name}</div>;
};

const NodeDisplay: React.FC = () => {
const node = useContext(NodeContext);
return <div className="other-node">{node.name}</div>;
};

const ConditionNodeDisplay: React.FC = () => {
const node = useContext(NodeContext);
return <div className="condition-node">{node.name}</div>;
};

const registerNodes: IRegisterNode[] = [
{
type: 'start',
name: '开始节点',
displayComponent: StartNodeDisplay,
isStart: true,
},
{
type: 'end',
name: '结束节点',
displayComponent: EndNodeDisplay,
isEnd: true,
},
{
type: 'node',
name: '普通节点',
displayComponent: NodeDisplay,
},
{
type: 'condition',
name: '条件节点',
displayComponent: ConditionNodeDisplay,
},
{
type: 'branch',
name: '分支节点',
conditionNodeType: 'condition',
},
];

const defaultNodes = [
{
id: 'node-0d9d4733-e48c-41fd-a41f-d93cc4718d97',
type: 'start',
name: 'start',
path: ['0'],
},
{
id: 'node-b2ffe834-c7c2-4f29-a370-305adc03c010',
type: 'branch',
name: '分支节点',
children: [
{
id: 'node-cf9c8f7e-26dd-446c-b3fa-b2406fc7821a',
type: 'condition',
name: '条件节点',
children: [
{
id: 'node-f227cd08-a503-48b7-babf-b4047fc9dfa5',
type: 'node',
name: '普通节点',
path: ['1', 'children', '0', 'children', '0'],
},
],
path: ['1', 'children', '0'],
},
{
id: 'node-9d393627-24c0-469f-818a-319d9a678707',
type: 'condition',
name: '条件节点',
children: [],
path: ['1', 'children', '1'],
},
],
path: ['1'],
},
{
id: 'node-972401ca-c4db-4268-8780-5607876d8372',
type: 'node',
name: '普通节点',
path: ['2'],
},
{
id: 'node-b106675a-5148-4a2e-aa86-8e06abd692d1',
type: 'end',
name: 'end',
path: ['3'],
},
];

const Remote = () => {
const [nodes, setNodes] = useState<INode[]>(defaultNodes);

const handleChange = (nodes: INode[]) => {
console.log('nodes change', nodes);
setNodes(nodes);
};

return (
<FlowBuilder
nodes={nodes}
onChange={handleChange}
registerNodes={registerNodes}
registerRemoteNodes={[
{
url: 'https://unpkg.com/[email protected]/dist/index.umd.js',
cssUrl:
'https://unpkg.com/[email protected]/dist/index.umd.css',
},
]}
/>
);
};

export default Remote;
2 changes: 1 addition & 1 deletion docs/dragdrop.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 5
order: 6
---

# 拖拽
Expand Down
2 changes: 1 addition & 1 deletion docs/history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 4
order: 5
---

# 撤销、重做
Expand Down
84 changes: 84 additions & 0 deletions docs/remote.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
order: 3
---

# 远程节点注册

通过 System.js 从远程加载 js / css 资源

## js 资源

需要按照 umd 规范默认导出节点注册所需要的各个属性。注意:react、antd 等组件可作为 external,不必直接打包到 umd 资源中,否则会导致远程节点资源体积过大,影响加载速度和多个实例出现的其他异常现象

## css 资源

在 css 内容不是特别多的场景下,建议将 css 样式合并 js 资源中,减少 http 请求数量

### 远程组件示例

```javascript
import React from 'react';

import './index.css';

const DisplayComponent = () => {
return <div className="remote-node">display component</div>;
};

const RemoteDemo = {
type: 'remote-node',
name: 'remote-node',
displayComponent: DisplayComponent,
};

export default RemoteDemo;
```

### umd 格式示例

```javascript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? (module.exports = factory(require('react')))
: typeof define === 'function' && define.amd
? define(['react'], factory)
: ((global =
typeof globalThis !== 'undefined' ? globalThis : global || self),
(global.reactFlowBuilderPkgDemo = factory(global.React)));
})(this, function (React) {
'use strict';

function _interopDefaultLegacy(e) {
return e && typeof e === 'object' && 'default' in e ? e : { default: e };
}

var React__default = /*#__PURE__*/ _interopDefaultLegacy(React);

var DisplayComponent = function DisplayComponent() {
return /*#__PURE__*/ React__default['default'].createElement(
'div',
{
className: 'remote-node',
},
'display component',
);
};

var RemoteDemo = {
type: 'remote-node',
name: 'remote-node',
displayComponent: DisplayComponent,
};

return RemoteDemo;
});
```

## RegisterRemoteNode

| 参数 | 说明 | 类型 | 必须 | 默认值 | 版本 |
| :----- | :----------------- | :------- | :--- | :----- | :---- |
| url | 节点的远程地址 | `string` || - | 1.3.0 |
| cssUrl | 节点样式的远程地址 | `string` | | - | 1.3.0 |

<code src="./demo/remote/index.tsx" />
2 changes: 1 addition & 1 deletion docs/zoom.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 3
order: 4
---

# 缩放
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-flow-builder",
"version": "1.2.1",
"version": "1.3.0",
"keywords": [
"flow",
"builder",
Expand Down Expand Up @@ -51,11 +51,12 @@
"prettier": "^2.2.1",
"react": ">=16",
"react-dom": ">=16",
"react-flow-builder": "^1.2.1",
"react-flow-builder": "1.3.0",
"yorkie": "^2.0.0"
},
"dependencies": {
"lodash.get": "^4.4.2",
"systemjs": "^6.12.1",
"uuid": "^8.3.2"
}
}
Loading

0 comments on commit c7843e4

Please sign in to comment.