@lyx-pj/primflow-react 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +133 -2
- package/README.zh-CN.md +132 -0
- package/build/index.d.ts +84 -0
- package/build/index.es.js +95 -0
- package/docs/adapter-contract.md +68 -0
- package/docs/adapter-contract.zh-CN.md +68 -0
- package/package.json +65 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 L.Y.X
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,134 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lyx-pj/primflow-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
English | [简体中文](README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Use the canvas and engine in React applications. This package provides `GraphProvider`, `EngineProvider`, and `GraphView` to manage mounting and unmounting. Menus, node panels, and the debug panel come from [canvas](../primflow-canvas/README.md); the application handles file storage and execution controls.
|
|
6
|
+
|
|
7
|
+
## Mount the canvas
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { NodeRegistry } from '@lyx-pj/primflow-canvas'
|
|
11
|
+
import { createGraphContext, GraphView } from '@lyx-pj/primflow-react'
|
|
12
|
+
|
|
13
|
+
const registry = new NodeRegistry()
|
|
14
|
+
const { GraphProvider, useGraph } = createGraphContext(registry, {
|
|
15
|
+
background: '#18181b',
|
|
16
|
+
gridSize: 10,
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
function Editor() {
|
|
20
|
+
const graph = useGraph()
|
|
21
|
+
return <GraphView graph={graph} height={480} />
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default function App() {
|
|
25
|
+
return (
|
|
26
|
+
<GraphProvider>
|
|
27
|
+
<Editor />
|
|
28
|
+
</GraphProvider>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- `createGraphContext` takes a node registry and canvas options, returning `GraphProvider` and `useGraph`.
|
|
34
|
+
- `GraphView` requires an explicit `graph` prop; it does not read a Context you created yourself.
|
|
35
|
+
- The container needs a defined height. `GraphView` defaults to `height="100%"`, which requires a parent with a computable height.
|
|
36
|
+
|
|
37
|
+
This example mounts only the editor. Add the engine when you need workflow execution.
|
|
38
|
+
|
|
39
|
+
To register application editors, pass the object form instead of plain canvas options. The editors are registered before the first node renders, so a port control always exists when the canvas builds it.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
const { GraphProvider, useGraph } = createGraphContext(registry, {
|
|
43
|
+
canvas: { background: '#18181b' },
|
|
44
|
+
editors: (editors) => {
|
|
45
|
+
editors.register('media.image@1', {
|
|
46
|
+
port: {
|
|
47
|
+
render: (ctx) => html`
|
|
48
|
+
<button
|
|
49
|
+
@click=${(evt) => ctx.overlay.open({
|
|
50
|
+
anchor: evt.currentTarget,
|
|
51
|
+
render: () => html`<input type="file" />`,
|
|
52
|
+
})}
|
|
53
|
+
>Pick</button>
|
|
54
|
+
`,
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
See [canvas custom editors](../primflow-canvas/README.md#custom-editors) for the editor API.
|
|
62
|
+
|
|
63
|
+
The same object form takes `nodeRenderers` to replace the built-in content of a
|
|
64
|
+
node definition. Registering both is common: editors decide how a port value is
|
|
65
|
+
edited, and renderers decide how the node looks.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const { GraphProvider, useGraph } = createGraphContext(registry, {
|
|
69
|
+
canvas: { background: '#18181b' },
|
|
70
|
+
nodeRenderers: (renderers) => {
|
|
71
|
+
renderers.register('ImageFile', {
|
|
72
|
+
size: { width: 196, height: 104 },
|
|
73
|
+
render: ({ node }) => html`<div class="image-card">${node?.title}</div>`,
|
|
74
|
+
})
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
See [canvas custom node renderers](../primflow-canvas/README.md#custom-node-renderers) for the renderer API.
|
|
80
|
+
|
|
81
|
+
## Connect the engine
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { Engine, NodeRegistry } from '@lyx-pj/primflow-engine'
|
|
85
|
+
import { createEngineContext } from '@lyx-pj/primflow-react'
|
|
86
|
+
|
|
87
|
+
const registry = new NodeRegistry()
|
|
88
|
+
export const { EngineProvider, useEngine } = createEngineContext(() => new Engine(registry))
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Wrap components that need execution with `EngineProvider`. Children use `useEngine()` to access the same engine instance. The canvas and engine must register the same custom node types.
|
|
92
|
+
|
|
93
|
+
The application connects them: read `graph.toJSON()`, assemble standalone subgraphs, call `engine.loadGraph()` and `engine.start({ debug })`, and convert engine events into canvas display states. See the [example's events.ts](../../examples/primflow-react/packages/workflow/events.ts) for a complete integration.
|
|
94
|
+
|
|
95
|
+
`EngineProvider` does not automatically execute workflows or display execution states on the canvas.
|
|
96
|
+
|
|
97
|
+
## API and lifecycle
|
|
98
|
+
|
|
99
|
+
| API | Purpose |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| `createGraphContext(registry, options?)` | Create `GraphProvider` and `useGraph`; `options` takes canvas options, or `{ canvas, editors, nodeRenderers }` to also register application editors and node renderers |
|
|
102
|
+
| `createEngineContext(createEngine)` | Create `EngineProvider` and `useEngine` |
|
|
103
|
+
| `GraphView` | Mount the canvas; accepts `graph`, dimensions, className, event callbacks, and view options |
|
|
104
|
+
|
|
105
|
+
- Provider instances stay stable across renders. Do not repeatedly call `createGraphContext` / `createEngineContext` inside component render functions.
|
|
106
|
+
- `useGraph()` / `useEngine()` must run within the corresponding Provider or they throw.
|
|
107
|
+
- `EngineProvider` calls `engine.stop()` on unmount. Use `engine.whenStopped()` when asynchronous resource cleanup must finish.
|
|
108
|
+
- `GraphView` reads the latest callbacks when events fire, avoiding stale props.
|
|
109
|
+
- The engine factory creates and configures an engine for the Provider. Start the engine in application code after mounting. Use `graph.setOptions()` to update canvas options.
|
|
110
|
+
- `GraphView` passes view options to the mounting process; changing a prop does not guarantee instance reconstruction.
|
|
111
|
+
|
|
112
|
+
`GraphView` supports callbacks such as `onNodeClick`, `onNodeDoubleClick`, `onNodeSelection`, and `onBlankClick`. See [GraphViewProps](src/components/graph-view/graph-view.tsx) for all props. Use Graph APIs for port and canvas interactions without manipulating JointJS directly.
|
|
113
|
+
|
|
114
|
+
## Build
|
|
115
|
+
|
|
116
|
+
Consumers install canvas, engine, and React. This package keeps them external during bundling to avoid duplicate node classes.
|
|
117
|
+
|
|
118
|
+
`pnpm build` checks package boundaries. See [React integration](docs/adapter-contract.md) for the rules and [verify-boundaries.mjs](../../scripts/verify-boundaries.mjs) for the checks.
|
|
119
|
+
|
|
120
|
+
## Development and tests
|
|
121
|
+
|
|
122
|
+
Run from the repository root:
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
pnpm --filter @lyx-pj/primflow-react test
|
|
126
|
+
pnpm --filter @lyx-pj/primflow-react typecheck
|
|
127
|
+
pnpm --filter @lyx-pj/primflow-react lint
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The existing [index.test.ts](tests/index.test.ts) covers instance stability in Strict Mode and engine cleanup on unmount. Add corresponding tests for new interactions. Run the root `pnpm build` when changing bundling or public exports.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @lyx-pj/primflow-react
|
|
2
|
+
|
|
3
|
+
[English](README.md) | 简体中文
|
|
4
|
+
|
|
5
|
+
在 React 应用里使用画布和引擎。它提供 `GraphProvider`、`EngineProvider` 和 `GraphView`,负责组件的挂载与卸载;菜单、节点面板和调试面板仍由 [canvas](../primflow-canvas/README.zh-CN.md) 提供,文件保存和运行控制由应用自己处理。
|
|
6
|
+
|
|
7
|
+
## 挂载画布
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { NodeRegistry } from '@lyx-pj/primflow-canvas'
|
|
11
|
+
import { createGraphContext, GraphView } from '@lyx-pj/primflow-react'
|
|
12
|
+
|
|
13
|
+
const registry = new NodeRegistry()
|
|
14
|
+
const { GraphProvider, useGraph } = createGraphContext(registry, {
|
|
15
|
+
background: '#18181b',
|
|
16
|
+
gridSize: 10,
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
function Editor() {
|
|
20
|
+
const graph = useGraph()
|
|
21
|
+
return <GraphView graph={graph} height={480} />
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default function App() {
|
|
25
|
+
return (
|
|
26
|
+
<GraphProvider>
|
|
27
|
+
<Editor />
|
|
28
|
+
</GraphProvider>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- `createGraphContext` 接收节点注册表和画布配置,返回 `GraphProvider` 和 `useGraph`。
|
|
34
|
+
- `GraphView` 必须显式传入 `graph`,不会去读你自己创建的 Context。
|
|
35
|
+
- 容器要有明确高度。`GraphView` 默认 `height="100%"`,这时父元素必须有可计算的高度。
|
|
36
|
+
|
|
37
|
+
上面的例子只挂载编辑器,不连接执行。需要运行工作流时,再接入引擎。
|
|
38
|
+
|
|
39
|
+
要注册应用编辑器,改用对象形式传入配置。编辑器会在首个节点渲染前完成注册,因此画布构建端口控件时它已经就位。
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
const { GraphProvider, useGraph } = createGraphContext(registry, {
|
|
43
|
+
canvas: { background: '#18181b' },
|
|
44
|
+
editors: (editors) => {
|
|
45
|
+
editors.register('media.image@1', {
|
|
46
|
+
port: {
|
|
47
|
+
render: (ctx) => html`
|
|
48
|
+
<button
|
|
49
|
+
@click=${(evt) => ctx.overlay.open({
|
|
50
|
+
anchor: evt.currentTarget,
|
|
51
|
+
render: () => html`<input type="file" />`,
|
|
52
|
+
})}
|
|
53
|
+
>选择图片</button>
|
|
54
|
+
`,
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
编辑器 API 见 [画布自定义编辑器](../primflow-canvas/README.zh-CN.md#自定义编辑器)。
|
|
62
|
+
|
|
63
|
+
同一个对象形式还接受 `nodeRenderers`,用于替换某个节点定义内置的内容。两者常一起注册:编辑器决定端口值怎么编辑,渲染器决定节点长什么样。
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const { GraphProvider, useGraph } = createGraphContext(registry, {
|
|
67
|
+
canvas: { background: '#18181b' },
|
|
68
|
+
nodeRenderers: (renderers) => {
|
|
69
|
+
renderers.register('ImageFile', {
|
|
70
|
+
size: { width: 196, height: 104 },
|
|
71
|
+
render: ({ node }) => html`<div class="image-card">${node?.title}</div>`,
|
|
72
|
+
})
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
渲染器 API 见 [画布自定义节点渲染](../primflow-canvas/README.zh-CN.md#自定义节点渲染)。
|
|
78
|
+
|
|
79
|
+
## 接入引擎
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { Engine, NodeRegistry } from '@lyx-pj/primflow-engine'
|
|
83
|
+
import { createEngineContext } from '@lyx-pj/primflow-react'
|
|
84
|
+
|
|
85
|
+
const registry = new NodeRegistry()
|
|
86
|
+
export const { EngineProvider, useEngine } = createEngineContext(() => new Engine(registry))
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
把 `EngineProvider` 放在需要运行的组件树外层,子组件用 `useEngine()` 拿到同一个引擎实例。画布和引擎要注册相同的自定义节点类型。
|
|
90
|
+
|
|
91
|
+
画布和引擎之间的连接由应用完成:读取 `graph.toJSON()`,组装独立子图,调用 `engine.loadGraph()` 和 `engine.start({ debug })`;再把引擎事件转换成画布要显示的状态。完整例子见 [示例的 events.ts](../../examples/primflow-react/packages/workflow/events.ts)。
|
|
92
|
+
|
|
93
|
+
`EngineProvider` 不会自动运行流程,也不会自动把执行状态画到画布上。
|
|
94
|
+
|
|
95
|
+
## API 和生命周期
|
|
96
|
+
|
|
97
|
+
| 接口 | 作用 |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| `createGraphContext(registry, options?)` | 创建 `GraphProvider` 和 `useGraph`;`options` 可传画布配置,也可用 `{ canvas, editors, nodeRenderers }` 同时注册应用编辑器和节点渲染器 |
|
|
100
|
+
| `createEngineContext(createEngine)` | 创建 `EngineProvider` 和 `useEngine` |
|
|
101
|
+
| `GraphView` | 挂载画布;接收 `graph`、尺寸、className、事件回调和视图选项 |
|
|
102
|
+
|
|
103
|
+
- Provider 实例在多次渲染之间保持不变;不要在组件 render 里反复调用 `createGraphContext` / `createEngineContext`。
|
|
104
|
+
- `useGraph()` / `useEngine()` 必须放在对应的 Provider 里,否则会抛错。
|
|
105
|
+
- `EngineProvider` 卸载时会调用 `engine.stop()`;需要等待异步资源释放时用 `engine.whenStopped()`。
|
|
106
|
+
- `GraphView` 在事件触发时读取最新回调,不会捕获旧的 props。
|
|
107
|
+
- 工厂函数为 Provider 创建和配置引擎。挂载后由应用启动引擎,画布选项通过 `graph.setOptions()` 更新。
|
|
108
|
+
- `GraphView` 的视图选项交给挂载流程,不保证修改 prop 就自动重建实例。
|
|
109
|
+
|
|
110
|
+
`GraphView` 支持 `onNodeClick`、`onNodeDoubleClick`、`onNodeSelection`、`onBlankClick` 等回调,完整属性见 [GraphViewProps](src/components/graph-view/graph-view.tsx)。端口和画布交互通过 Graph 的 API 使用,不需要直接操作 JointJS。
|
|
111
|
+
|
|
112
|
+
## 构建
|
|
113
|
+
|
|
114
|
+
canvas、engine 和 React 由使用方安装。这个包构建时不会把它们打包进来,避免出现两份节点类。
|
|
115
|
+
|
|
116
|
+
运行 `pnpm build` 会检查包之间的依赖是否符合要求,规则见 [React 接入](docs/adapter-contract.zh-CN.md),检查脚本见 [verify-boundaries.mjs](../../scripts/verify-boundaries.mjs)。
|
|
117
|
+
|
|
118
|
+
## 开发与测试
|
|
119
|
+
|
|
120
|
+
从仓库根运行:
|
|
121
|
+
|
|
122
|
+
```sh
|
|
123
|
+
pnpm --filter @lyx-pj/primflow-react test
|
|
124
|
+
pnpm --filter @lyx-pj/primflow-react typecheck
|
|
125
|
+
pnpm --filter @lyx-pj/primflow-react lint
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
现有 [index.test.ts](tests/index.test.ts) 覆盖严格模式下的实例稳定性和卸载后的引擎清理;新增交互需要补充对应测试。打包或公开导出变化时运行根 `pnpm build`。
|
|
129
|
+
|
|
130
|
+
## 许可
|
|
131
|
+
|
|
132
|
+
MIT
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { EditorRegistry } from '@lyx-pj/primflow-canvas';
|
|
2
|
+
import { Engine } from '@lyx-pj/primflow-engine';
|
|
3
|
+
import { Graph } from '@lyx-pj/primflow-canvas';
|
|
4
|
+
import { GraphOptions } from '@lyx-pj/primflow-canvas';
|
|
5
|
+
import { JSX } from 'react/jsx-runtime';
|
|
6
|
+
import { LinkInstanceType } from '@lyx-pj/primflow-canvas';
|
|
7
|
+
import { NodeClassType } from '@lyx-pj/primflow-canvas';
|
|
8
|
+
import { NodeClassType as NodeClassType_2 } from '@lyx-pj/primflow-engine';
|
|
9
|
+
import { NodeInstanceType } from '@lyx-pj/primflow-canvas';
|
|
10
|
+
import { NodeRegistry } from '@lyx-pj/primflow-canvas';
|
|
11
|
+
import { NodeRendererRegistry } from '@lyx-pj/primflow-canvas';
|
|
12
|
+
import { ReactNode } from 'react';
|
|
13
|
+
|
|
14
|
+
export declare function createEngineContext<NodeDefs extends Record<string, NodeClassType_2>>(createEngine: () => Engine<NodeDefs>): {
|
|
15
|
+
EngineProvider: ({ children }: {
|
|
16
|
+
children: ReactNode;
|
|
17
|
+
}) => JSX.Element;
|
|
18
|
+
useEngine: () => Engine<NodeDefs>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export declare function createGraphContext<NodeDefs extends Record<string, NodeClassType>>(registry: NodeRegistry<NodeDefs>, options?: GraphOptions): {
|
|
22
|
+
GraphProvider: (props: {
|
|
23
|
+
children: ReactNode;
|
|
24
|
+
}) => ReactNode;
|
|
25
|
+
useGraph: () => Graph<NodeDefs>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export declare function createGraphContext<NodeDefs extends Record<string, NodeClassType>>(registry: NodeRegistry<NodeDefs>, options: GraphContextOptions): {
|
|
29
|
+
GraphProvider: (props: {
|
|
30
|
+
children: ReactNode;
|
|
31
|
+
}) => ReactNode;
|
|
32
|
+
useGraph: () => Graph<NodeDefs>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
declare interface GraphContextOptions {
|
|
36
|
+
/** Canvas options forwarded to the Graph constructor. */
|
|
37
|
+
canvas?: GraphOptions;
|
|
38
|
+
/**
|
|
39
|
+
* Register application editors before the first node renders.
|
|
40
|
+
*
|
|
41
|
+
* Editors decide how a port value is edited, so they must be in place before
|
|
42
|
+
* the canvas builds any port control.
|
|
43
|
+
*/
|
|
44
|
+
editors?: (editors: EditorRegistry) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Register application node renderers before the first node renders.
|
|
47
|
+
*
|
|
48
|
+
* A renderer decides how a node definition looks, so it must be in place
|
|
49
|
+
* before the canvas builds the node markup.
|
|
50
|
+
*/
|
|
51
|
+
nodeRenderers?: (renderers: NodeRendererRegistry) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export declare function GraphView<NodeDefs extends Record<string, NodeClassType>>({ width, height, graph, className, children, options, onNodeClick, onNodeDoubleClick, onNodeSelection, onBlankClick, onBlankDoubleClick, onLinkClick, onDragStart, onDrag, onDragEnd, onStartConnecting, onKeyDown, }: GraphViewProps<NodeDefs>): JSX.Element;
|
|
55
|
+
|
|
56
|
+
declare type GraphViewProps<NodeDefs extends Record<string, NodeClassType>> = {
|
|
57
|
+
graph: Graph<NodeDefs>;
|
|
58
|
+
width?: number | string;
|
|
59
|
+
height?: number | string;
|
|
60
|
+
className?: string;
|
|
61
|
+
children?: React.ReactNode;
|
|
62
|
+
onNodeClick?: (node: NodeInstanceType, event: MouseEvent) => void;
|
|
63
|
+
onNodeDoubleClick?: (node: NodeInstanceType, event: MouseEvent) => void;
|
|
64
|
+
onNodeSelection?: (nodes: NodeInstanceType[]) => void;
|
|
65
|
+
onBlankClick?: (event: MouseEvent) => void;
|
|
66
|
+
onBlankDoubleClick?: (event: MouseEvent) => void;
|
|
67
|
+
onLinkClick?: (link: LinkInstanceType, event: MouseEvent) => void;
|
|
68
|
+
onDragStart?: (node: NodeInstanceType) => void;
|
|
69
|
+
onDrag?: (node: NodeInstanceType) => void;
|
|
70
|
+
onDragEnd?: (node: NodeInstanceType) => void;
|
|
71
|
+
onStartConnecting?: () => void;
|
|
72
|
+
onKeyDown?: (event: React.KeyboardEvent) => void;
|
|
73
|
+
options?: {
|
|
74
|
+
background?: string;
|
|
75
|
+
gridSize?: number;
|
|
76
|
+
drawGrid?: {
|
|
77
|
+
size?: number;
|
|
78
|
+
color?: string;
|
|
79
|
+
thickness?: number;
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export { }
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { jsx as h, jsxs as j } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as m, useState as R, useContext as p, useEffect as v, useRef as l } from "react";
|
|
3
|
+
import { Graph as w } from "@lyx-pj/primflow-canvas";
|
|
4
|
+
function q(t, e) {
|
|
5
|
+
const o = m(null), i = e && "canvas" in e ? e.canvas : e, n = e && "editors" in e ? e.editors : void 0, r = e && "nodeRenderers" in e ? e.nodeRenderers : void 0;
|
|
6
|
+
function u({ children: c }) {
|
|
7
|
+
const [f] = R(() => {
|
|
8
|
+
const d = new w(t, i);
|
|
9
|
+
return n?.(d.editors), r?.(d.nodeRenderers), d;
|
|
10
|
+
});
|
|
11
|
+
return /* @__PURE__ */ h(o.Provider, { value: f, children: c });
|
|
12
|
+
}
|
|
13
|
+
function a() {
|
|
14
|
+
const c = p(o);
|
|
15
|
+
if (!c) throw new Error("GraphContext not found");
|
|
16
|
+
return c;
|
|
17
|
+
}
|
|
18
|
+
return { GraphProvider: u, useGraph: a };
|
|
19
|
+
}
|
|
20
|
+
function z(t) {
|
|
21
|
+
const e = m(null);
|
|
22
|
+
function o({ children: n }) {
|
|
23
|
+
const [r] = R(t);
|
|
24
|
+
return v(
|
|
25
|
+
() => () => {
|
|
26
|
+
r.stop();
|
|
27
|
+
},
|
|
28
|
+
[r]
|
|
29
|
+
), /* @__PURE__ */ h(e.Provider, { value: r, children: n });
|
|
30
|
+
}
|
|
31
|
+
function i() {
|
|
32
|
+
const n = p(e);
|
|
33
|
+
if (!n) throw new Error("EngineContext not found");
|
|
34
|
+
return n;
|
|
35
|
+
}
|
|
36
|
+
return { EngineProvider: o, useEngine: i };
|
|
37
|
+
}
|
|
38
|
+
function y(t) {
|
|
39
|
+
return t.filter((e) => e instanceof w.NodeInstance);
|
|
40
|
+
}
|
|
41
|
+
function A({
|
|
42
|
+
width: t = "100%",
|
|
43
|
+
height: e = "100%",
|
|
44
|
+
graph: o,
|
|
45
|
+
className: i,
|
|
46
|
+
children: n,
|
|
47
|
+
options: r,
|
|
48
|
+
onNodeClick: u,
|
|
49
|
+
onNodeDoubleClick: a,
|
|
50
|
+
onNodeSelection: c,
|
|
51
|
+
onBlankClick: f,
|
|
52
|
+
onBlankDoubleClick: d,
|
|
53
|
+
onLinkClick: C,
|
|
54
|
+
onDragStart: G,
|
|
55
|
+
onDrag: b,
|
|
56
|
+
onDragEnd: P,
|
|
57
|
+
onStartConnecting: x,
|
|
58
|
+
onKeyDown: N
|
|
59
|
+
}) {
|
|
60
|
+
const g = l(null), E = l({}), k = l(r);
|
|
61
|
+
return k.current = r, v(() => {
|
|
62
|
+
E.current = {
|
|
63
|
+
"node:click": u,
|
|
64
|
+
"node:dblclick": a,
|
|
65
|
+
"selection:changed": (s) => c?.(y(s)),
|
|
66
|
+
"blank:click": f,
|
|
67
|
+
"blank:dblclick": d,
|
|
68
|
+
"link:click": C,
|
|
69
|
+
"node:dragstart": G,
|
|
70
|
+
"node:dragmove": b,
|
|
71
|
+
"node:dragend": P,
|
|
72
|
+
"start:connecting": x ? () => x() : void 0
|
|
73
|
+
};
|
|
74
|
+
}), v(() => {
|
|
75
|
+
const s = g.current;
|
|
76
|
+
if (s)
|
|
77
|
+
return o.mount(s, () => E.current, k.current);
|
|
78
|
+
}, [o]), /* @__PURE__ */ j("div", { style: { position: "relative", width: t, height: e }, children: [
|
|
79
|
+
/* @__PURE__ */ h(
|
|
80
|
+
"div",
|
|
81
|
+
{
|
|
82
|
+
style: { width: "100%", height: "100%" },
|
|
83
|
+
className: i,
|
|
84
|
+
onKeyDown: N,
|
|
85
|
+
ref: g
|
|
86
|
+
}
|
|
87
|
+
),
|
|
88
|
+
n
|
|
89
|
+
] });
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
A as GraphView,
|
|
93
|
+
z as createEngineContext,
|
|
94
|
+
q as createGraphContext
|
|
95
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# React and canvas integration
|
|
2
|
+
|
|
3
|
+
English | [简体中文](adapter-contract.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
The React package connects the canvas and engine to component mounting, updates, and unmounting. [canvas](../../primflow-canvas/README.md) provides menus, node panels, and the debug panel. The application handles file storage and execution controls.
|
|
6
|
+
|
|
7
|
+
See the [React README](../README.md) for usage and [Architecture](../../../docs/architecture.md) for module relationships.
|
|
8
|
+
|
|
9
|
+
## Layer responsibilities
|
|
10
|
+
|
|
11
|
+
| Layer | Responsibility |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| canvas | Create the canvas, manage nodes and links, bind canvas events, and display menus and panels |
|
|
14
|
+
| react | Provide `GraphProvider`, `EngineProvider`, and `GraphView`, retaining instances used by components |
|
|
15
|
+
| Application | Register business nodes, execute workflows, save files, and pass engine results to the canvas |
|
|
16
|
+
|
|
17
|
+
The canvas does not call the engine directly. The application supplies node states, logs, and history.
|
|
18
|
+
|
|
19
|
+
## Mounting and unmounting
|
|
20
|
+
|
|
21
|
+
`GraphView` requires a `graph` prop and a defined height, either directly or through its parent container. It calls `graph.mount` internally and invokes the returned cleanup function on unmount.
|
|
22
|
+
|
|
23
|
+
The core approach is shown below; see [GraphView](../src/components/graph-view/graph-view.tsx) for the complete implementation:
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const container = containerRef.current
|
|
28
|
+
if (!container) return
|
|
29
|
+
return graph.mount(container, () => handlersRef.current, optionsRef.current)
|
|
30
|
+
}, [graph])
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- `GraphProvider` retains the same `Graph` instance during a mount.
|
|
34
|
+
- `EngineProvider` retains the same `Engine` and calls `stop()` on unmount. Use `whenStopped()` to await asynchronous cleanup.
|
|
35
|
+
- Event handlers read the latest callbacks rather than stale props.
|
|
36
|
+
- Do not reimplement canvas menus in React or manipulate JointJS internals directly.
|
|
37
|
+
|
|
38
|
+
## Canvas capabilities
|
|
39
|
+
|
|
40
|
+
canvas owns mounting, event names, serialization, zoom, subgraph navigation, replay, and UI configuration. See the [canvas README](../../primflow-canvas/README.md) for the full API:
|
|
41
|
+
|
|
42
|
+
| Action | API |
|
|
43
|
+
| --- | --- |
|
|
44
|
+
| Mount the canvas | `graph.mount(container, handlers, options)`, returning a cleanup function |
|
|
45
|
+
| Save or load a graph | `graph.toJSON()` / `graph.fromJSON(json)` |
|
|
46
|
+
| Zoom and select nodes | `graph.zoom(scale)`, `graph.select(node)`, `graph.clearSelection()` |
|
|
47
|
+
| Enter or leave an embedded subgraph | `graph.enterSubgraph()` / `graph.exitSubgraph()` |
|
|
48
|
+
| Return to a source file | `graph.setOptions({ navigation })`; see [Subgraphs and navigation](../../primflow-canvas/docs/subgraphs.md) |
|
|
49
|
+
| Replay history | `graph.enterReplay(json, options)` / `graph.exitReplay()` |
|
|
50
|
+
| Menus and panels | Configure `palette`, `contextMenu`, `nodePanel`, `debugPanel`, and `toolbar`; use `false` to disable |
|
|
51
|
+
|
|
52
|
+
`handlers` uses `Graph` event names such as `node:click` and `selection:changed`, not JointJS event names. It can be a callback map or a function returning the latest callback map.
|
|
53
|
+
|
|
54
|
+
The application should pause autosave when entering replay and resume it on exit. The canvas displays historical content and restores the editing graph; it does not store task history.
|
|
55
|
+
|
|
56
|
+
## Build
|
|
57
|
+
|
|
58
|
+
Packages import through public entry points. Consumers install canvas, engine, and React. The React package keeps them external during bundling to avoid duplicate node classes.
|
|
59
|
+
|
|
60
|
+
`pnpm build` checks source dependencies, type declarations, and build output. See [verify-boundaries.mjs](../../../scripts/verify-boundaries.mjs). Run `pnpm verify` to execute the checks separately.
|
|
61
|
+
|
|
62
|
+
## Related tests
|
|
63
|
+
|
|
64
|
+
- [React tests](../tests/index.test.ts): instance stability and engine resource cleanup on unmount.
|
|
65
|
+
- [Canvas interaction tests](../../primflow-canvas/tests/ui.test.ts): menus, panels, replay, and interaction cleanup.
|
|
66
|
+
- Shared-class tests in [engine](../../primflow-engine/tests/boundaries.test.ts) and [canvas](../../primflow-canvas/tests/boundaries.test.ts): ensure packages use the same node classes.
|
|
67
|
+
|
|
68
|
+
Add tests for new `GraphView` behavior. Existing React tests mainly cover Providers.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# React 与画布接入
|
|
2
|
+
|
|
3
|
+
[English](adapter-contract.md) | 简体中文
|
|
4
|
+
|
|
5
|
+
React 包负责让画布和引擎配合组件的挂载、更新和卸载。菜单、节点面板、调试面板等界面由 [canvas](../../primflow-canvas/README.zh-CN.md) 提供,文件保存和运行控制由应用负责。
|
|
6
|
+
|
|
7
|
+
完整用法见 [React README](../README.zh-CN.md),模块关系见 [架构说明](../../../docs/architecture.zh-CN.md)。
|
|
8
|
+
|
|
9
|
+
## 各层分别做什么
|
|
10
|
+
|
|
11
|
+
| 位置 | 负责的工作 |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| canvas | 创建画布、处理节点和连线、绑定画布事件、显示菜单与面板 |
|
|
14
|
+
| react | 提供 `GraphProvider`、`EngineProvider` 和 `GraphView`,保存组件使用的实例 |
|
|
15
|
+
| 应用 | 注册业务节点、执行流程、保存文件,把引擎结果传给画布 |
|
|
16
|
+
|
|
17
|
+
画布不会直接调用引擎;节点状态、日志和历史记录都由应用传入。
|
|
18
|
+
|
|
19
|
+
## 挂载和卸载
|
|
20
|
+
|
|
21
|
+
`GraphView` 必须接收 `graph` 属性,并设置明确高度,或让父容器提供高度。它内部调用 `graph.mount`,组件卸载时调用返回的清理函数。
|
|
22
|
+
|
|
23
|
+
核心做法如下,完整实现见 [GraphView](../src/components/graph-view/graph-view.tsx):
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const container = containerRef.current
|
|
28
|
+
if (!container) return
|
|
29
|
+
return graph.mount(container, () => handlersRef.current, optionsRef.current)
|
|
30
|
+
}, [graph])
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- `GraphProvider` 在同一次挂载期间保留同一个 `Graph` 实例。
|
|
34
|
+
- `EngineProvider` 保留同一个 `Engine`,卸载时调用 `stop()`;需要等待异步清理时使用 `whenStopped()`。
|
|
35
|
+
- 事件触发时读取最新回调,避免使用已经过期的 props。
|
|
36
|
+
- 不在 React 中重新实现画布菜单,也不直接操作 JointJS 内部对象。
|
|
37
|
+
|
|
38
|
+
## 画布能力
|
|
39
|
+
|
|
40
|
+
画布的挂载、事件名、保存加载、缩放、子图导航、回溯和 UI 配置由 canvas 维护,完整接口见 [canvas README](../../primflow-canvas/README.zh-CN.md):
|
|
41
|
+
|
|
42
|
+
| 需要做什么 | 调用方式 |
|
|
43
|
+
| --- | --- |
|
|
44
|
+
| 挂载画布 | `graph.mount(container, handlers, options)`,返回清理函数 |
|
|
45
|
+
| 保存或加载图 | `graph.toJSON()` / `graph.fromJSON(json)` |
|
|
46
|
+
| 缩放和选择节点 | `graph.zoom(scale)`、`graph.select(node)`、`graph.clearSelection()` |
|
|
47
|
+
| 进入或退出内嵌子图 | `graph.enterSubgraph()` / `graph.exitSubgraph()` |
|
|
48
|
+
| 返回来源文件 | `graph.setOptions({ navigation })`,见 [子图与导航](../../primflow-canvas/docs/subgraphs.zh-CN.md) |
|
|
49
|
+
| 历史回溯 | `graph.enterReplay(json, options)` / `graph.exitReplay()` |
|
|
50
|
+
| 菜单和面板 | `palette`、`contextMenu`、`nodePanel`、`debugPanel`、`toolbar` 配置;设为 `false` 关闭 |
|
|
51
|
+
|
|
52
|
+
`handlers` 使用 `Graph` 的事件名,例如 `node:click`、`selection:changed`,不需要使用 JointJS 的事件名。它可以是回调表,也可以是每次返回最新回调表的函数。
|
|
53
|
+
|
|
54
|
+
进入回溯后,应用应暂停自动保存,退出后恢复。画布只负责显示历史内容并恢复编辑图,不保存任务历史。
|
|
55
|
+
|
|
56
|
+
## 构建
|
|
57
|
+
|
|
58
|
+
包之间从公开入口导入。canvas、engine 和 React 由使用方安装;React 包构建时不会把它们打包进来,避免出现两份节点类。
|
|
59
|
+
|
|
60
|
+
运行 `pnpm build` 会检查源码依赖、类型声明和构建文件。检查脚本见 [verify-boundaries.mjs](../../../scripts/verify-boundaries.mjs),也可以单独运行 `pnpm verify`。
|
|
61
|
+
|
|
62
|
+
## 相关测试
|
|
63
|
+
|
|
64
|
+
- [React 测试](../tests/index.test.ts):实例稳定、组件卸载后清理引擎资源。
|
|
65
|
+
- [画布交互测试](../../primflow-canvas/tests/ui.test.ts):菜单、面板、回溯和交互清理。
|
|
66
|
+
- [engine](../../primflow-engine/tests/boundaries.test.ts) 和 [canvas](../../primflow-canvas/tests/boundaries.test.ts) 的共享类测试:确认各包使用相同的节点类。
|
|
67
|
+
|
|
68
|
+
`GraphView` 的新行为需要补充对应测试;现有 React 测试主要覆盖 Provider。
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lyx-pj/primflow-react",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "2.0.0",
|
|
5
|
+
"description": "React provider and GraphView that bind the Primflow canvas and workflow engine to React components.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "build/index.es.js",
|
|
8
|
+
"types": "build/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"@lyx-pj/source": "./src/index.ts",
|
|
12
|
+
"types": "./build/index.d.ts",
|
|
13
|
+
"import": "./build/index.es.js",
|
|
14
|
+
"default": "./build/index.es.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"build/**/*",
|
|
20
|
+
"README.md",
|
|
21
|
+
"README.zh-CN.md",
|
|
22
|
+
"docs/**/*.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@lyx-pj/primflow-canvas": "2.0.0",
|
|
31
|
+
"@lyx-pj/primflow-engine": "2.0.0",
|
|
32
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
33
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@lyx-pj/primflow-canvas": "2.0.0",
|
|
37
|
+
"@lyx-pj/primflow-engine": "2.0.0",
|
|
38
|
+
"@types/react": "^19.1.10",
|
|
39
|
+
"@types/react-dom": "^19.1.7",
|
|
40
|
+
"@vitejs/plugin-react-swc": "^4.0.1",
|
|
41
|
+
"react": "^19.1.1",
|
|
42
|
+
"react-dom": "^19.1.1",
|
|
43
|
+
"vite": "^7.1.5",
|
|
44
|
+
"vite-plugin-css-injected-by-js": "^3.5.2",
|
|
45
|
+
"vite-plugin-dts": "^4.5.4"
|
|
46
|
+
},
|
|
47
|
+
"keywords": [
|
|
48
|
+
"workflow",
|
|
49
|
+
"react",
|
|
50
|
+
"flow",
|
|
51
|
+
"orchestration",
|
|
52
|
+
"node-based",
|
|
53
|
+
"visual-programming",
|
|
54
|
+
"pipeline",
|
|
55
|
+
"automation",
|
|
56
|
+
"graph",
|
|
57
|
+
"editor",
|
|
58
|
+
"ui"
|
|
59
|
+
],
|
|
60
|
+
"scripts": {
|
|
61
|
+
"dev": "vite build --watch",
|
|
62
|
+
"build": "vite build",
|
|
63
|
+
"typecheck": "tsc -p tsconfig.app.json --noEmit",
|
|
64
|
+
"clean": "rimraf build",
|
|
65
|
+
"lint": "oxlint --config ../../.oxlintrc.json --deny-warnings .",
|
|
66
|
+
"test": "vitest run --reporter verbose"
|
|
67
|
+
}
|
|
6
68
|
}
|