@lyx-pj/primflow-react 2.0.1 → 2.1.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/README.md CHANGED
@@ -109,25 +109,7 @@ The application connects them: read `graph.toJSON()`, assemble standalone subgra
109
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
110
  - `GraphView` passes view options to the mounting process; changing a prop does not guarantee instance reconstruction.
111
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.
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.
131
113
 
132
114
  ## License
133
115
 
package/README.zh-CN.md CHANGED
@@ -107,25 +107,7 @@ export const { EngineProvider, useEngine } = createEngineContext(() => new Engin
107
107
  - 工厂函数为 Provider 创建和配置引擎。挂载后由应用启动引擎,画布选项通过 `graph.setOptions()` 更新。
108
108
  - `GraphView` 的视图选项交给挂载流程,不保证修改 prop 就自动重建实例。
109
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`。
110
+ `GraphView` 支持 `onNodeClick`、`onNodeDoubleClick`、`onNodeSelection`、`onBlankClick` 等回调,完整属性见 [GraphViewProps](src/components/graph-view/graph-view.tsx)。端口和画布交互通过 Graph 的 API 使用。
129
111
 
130
112
  ## 许可
131
113
 
package/build/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { CanvasUIMounter } from '@lyx-pj/primflow-canvas';
2
+ import { CanvasUIMountFn } from '@lyx-pj/primflow-canvas';
1
3
  import { EditorRegistry } from '@lyx-pj/primflow-canvas';
2
4
  import { Engine } from '@lyx-pj/primflow-engine';
3
5
  import { Graph } from '@lyx-pj/primflow-canvas';
@@ -11,6 +13,11 @@ import { NodeRegistry } from '@lyx-pj/primflow-canvas';
11
13
  import { NodeRendererRegistry } from '@lyx-pj/primflow-canvas';
12
14
  import { ReactNode } from 'react';
13
15
 
16
+ /** Renders all active portals owned by a `ReactPortalRegistry`. */
17
+ export declare function CanvasPortalHost({ registry }: {
18
+ registry: ReactPortalRegistry;
19
+ }): JSX.Element;
20
+
14
21
  export declare function createEngineContext<NodeDefs extends Record<string, NodeClassType_2>>(createEngine: () => Engine<NodeDefs>): {
15
22
  EngineProvider: ({ children }: {
16
23
  children: ReactNode;
@@ -32,7 +39,26 @@ export declare function createGraphContext<NodeDefs extends Record<string, NodeC
32
39
  useGraph: () => Graph<NodeDefs>;
33
40
  };
34
41
 
35
- declare interface GraphContextOptions {
42
+ /**
43
+ * Create a standalone React `CanvasUIMountFn` backed by `createRoot`.
44
+ *
45
+ * Works anywhere (including module-level static `GraphOptions` outside a React
46
+ * tree). For components that need access to outer React Context, prefer
47
+ * `useReactMount()` inside `GraphProvider`.
48
+ */
49
+ export declare function createReactMount<TCtx>(render: (context: TCtx) => ReactNode): CanvasUIMountFn<TCtx>;
50
+
51
+ export declare function createReactPortalRegistry(): ReactPortalRegistry;
52
+
53
+ /** Convenience wrapper returning `{ mount }` for `CanvasUISlot` / `NodePanelOptions`. */
54
+ export declare function createReactSlot<TCtx>(render: (context: TCtx) => ReactNode): CanvasUIMounter<TCtx>;
55
+
56
+ export declare interface GraphContextHelpers {
57
+ /** Mount React JSX into canvas containers via portals inside `GraphProvider`. */
58
+ reactMount: ReactMountFactory;
59
+ }
60
+
61
+ export declare interface GraphContextOptions {
36
62
  /** Canvas options forwarded to the Graph constructor. */
37
63
  canvas?: GraphOptions;
38
64
  /**
@@ -41,19 +67,19 @@ declare interface GraphContextOptions {
41
67
  * Editors decide how a port value is edited, so they must be in place before
42
68
  * the canvas builds any port control.
43
69
  */
44
- editors?: (editors: EditorRegistry) => void;
70
+ editors?: (editors: EditorRegistry, helpers: GraphContextHelpers) => void;
45
71
  /**
46
72
  * Register application node renderers before the first node renders.
47
73
  *
48
74
  * A renderer decides how a node definition looks, so it must be in place
49
75
  * before the canvas builds the node markup.
50
76
  */
51
- nodeRenderers?: (renderers: NodeRendererRegistry) => void;
77
+ nodeRenderers?: (renderers: NodeRendererRegistry, helpers: GraphContextHelpers) => void;
52
78
  }
53
79
 
54
80
  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
81
 
56
- declare type GraphViewProps<NodeDefs extends Record<string, NodeClassType>> = {
82
+ export declare type GraphViewProps<NodeDefs extends Record<string, NodeClassType>> = {
57
83
  graph: Graph<NodeDefs>;
58
84
  width?: number | string;
59
85
  height?: number | string;
@@ -81,4 +107,38 @@ declare type GraphViewProps<NodeDefs extends Record<string, NodeClassType>> = {
81
107
  };
82
108
  };
83
109
 
110
+ declare interface PortalEntry {
111
+ id: number;
112
+ container: HTMLElement;
113
+ element: ReactNode;
114
+ }
115
+
116
+ export declare type ReactMountFactory = <TCtx>(render: (context: TCtx) => ReactNode) => CanvasUIMountFn<TCtx>;
117
+
118
+ /**
119
+ * External store tracking active React portals mounted into canvas DOM containers.
120
+ *
121
+ * Rendering through portals keeps custom port editors, node cards, and panel
122
+ * sections inside the host React tree so they inherit React Context (theme,
123
+ * i18n, router, store, `useGraph`, `useEngine`).
124
+ */
125
+ export declare class ReactPortalRegistry {
126
+ private nextId;
127
+ private entries;
128
+ private readonly listeners;
129
+ subscribe: (listener: () => void) => (() => void);
130
+ getSnapshot: () => readonly PortalEntry[];
131
+ mount<TCtx>(render: (context: TCtx) => ReactNode): CanvasUIMountFn<TCtx>;
132
+ private upsert;
133
+ private remove;
134
+ private emit;
135
+ }
136
+
137
+ /**
138
+ * Returns a `reactMount` helper that mounts React JSX into canvas containers
139
+ * via portals attached to the enclosing `GraphProvider` (or falls back to
140
+ * `createReactMount` when called outside a `GraphProvider`).
141
+ */
142
+ export declare function useReactMount(): ReactMountFactory;
143
+
84
144
  export { }
package/build/index.es.js CHANGED
@@ -1,95 +1,187 @@
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;
1
+ import { jsx as i, Fragment as y, jsxs as G } from "react/jsx-runtime";
2
+ import { useSyncExternalStore as j, Fragment as q, useContext as k, createContext as C, useCallback as B, useState as S, useEffect as R, useRef as v } from "react";
3
+ import { Graph as M } from "@lyx-pj/primflow-canvas";
4
+ import { createPortal as H } from "react-dom";
5
+ import { createRoot as O } from "react-dom/client";
6
+ function f({
7
+ render: t,
8
+ context: e
9
+ }) {
10
+ return /* @__PURE__ */ i(y, { children: t(e) });
11
+ }
12
+ function V({ registry: t }) {
13
+ const e = j(t.subscribe, t.getSnapshot, t.getSnapshot);
14
+ return /* @__PURE__ */ i(y, { children: e.map((n) => /* @__PURE__ */ i(q, { children: H(n.element, n.container) }, n.id)) });
15
+ }
16
+ class $ {
17
+ nextId = 1;
18
+ entries = [];
19
+ listeners = /* @__PURE__ */ new Set();
20
+ subscribe = (e) => (this.listeners.add(e), () => {
21
+ this.listeners.delete(e);
22
+ });
23
+ getSnapshot = () => this.entries;
24
+ mount(e) {
25
+ return (n, o) => {
26
+ const r = this.nextId++;
27
+ return this.upsert(r, n, /* @__PURE__ */ i(f, { render: e, context: o })), {
28
+ update: (s) => {
29
+ this.upsert(r, n, /* @__PURE__ */ i(f, { render: e, context: s }));
30
+ },
31
+ destroy: () => {
32
+ this.remove(r);
33
+ }
34
+ };
35
+ };
36
+ }
37
+ upsert(e, n, o) {
38
+ const r = this.entries.findIndex((s) => s.id === e);
39
+ if (r === -1)
40
+ this.entries = [...this.entries, { id: e, container: n, element: o }];
41
+ else {
42
+ const s = this.entries.slice();
43
+ s[r] = { id: e, container: n, element: o }, this.entries = s;
44
+ }
45
+ this.emit();
46
+ }
47
+ remove(e) {
48
+ const n = this.entries.filter((o) => o.id !== e);
49
+ n.length !== this.entries.length && (this.entries = n, this.emit());
50
+ }
51
+ emit() {
52
+ for (const e of this.listeners) e();
53
+ }
54
+ }
55
+ function z() {
56
+ return new $();
57
+ }
58
+ const I = C(null);
59
+ function F(t) {
60
+ return (e, n) => {
61
+ let o = O(e);
62
+ return o.render(/* @__PURE__ */ i(f, { render: t, context: n })), {
63
+ update: (r) => {
64
+ o?.render(/* @__PURE__ */ i(f, { render: t, context: r }));
65
+ },
66
+ destroy: () => {
67
+ const r = o;
68
+ o = null, r && queueMicrotask(() => {
69
+ r.unmount();
70
+ });
71
+ }
72
+ };
73
+ };
74
+ }
75
+ function T(t) {
76
+ return { mount: F(t) };
77
+ }
78
+ function U() {
79
+ const t = k(I);
80
+ return B(
81
+ (e) => t ? t.mount(e) : F(e),
82
+ [t]
83
+ );
84
+ }
85
+ function W(t, e) {
86
+ const n = C(null), o = e && "canvas" in e ? e.canvas : e, r = e && "editors" in e ? e.editors : void 0, s = e && "nodeRenderers" in e ? e.nodeRenderers : void 0;
87
+ function m({ children: c }) {
88
+ const [{ graph: x, portals: a }] = S(() => {
89
+ const d = z(), l = {
90
+ reactMount: (p) => d.mount(p)
91
+ }, u = new M(t, o);
92
+ return r?.(u.editors, l), s?.(u.nodeRenderers, l), { graph: u, portals: d };
10
93
  });
11
- return /* @__PURE__ */ h(o.Provider, { value: f, children: c });
94
+ return /* @__PURE__ */ i(I.Provider, { value: a, children: /* @__PURE__ */ G(n.Provider, { value: x, children: [
95
+ c,
96
+ /* @__PURE__ */ i(V, { registry: a })
97
+ ] }) });
12
98
  }
13
- function a() {
14
- const c = p(o);
99
+ function g() {
100
+ const c = k(n);
15
101
  if (!c) throw new Error("GraphContext not found");
16
102
  return c;
17
103
  }
18
- return { GraphProvider: u, useGraph: a };
104
+ return { GraphProvider: m, useGraph: g };
19
105
  }
20
- function z(t) {
21
- const e = m(null);
22
- function o({ children: n }) {
23
- const [r] = R(t);
24
- return v(
106
+ function X(t) {
107
+ const e = C(null);
108
+ function n({ children: r }) {
109
+ const [s] = S(t);
110
+ return R(
25
111
  () => () => {
26
- r.stop();
112
+ s.stop();
27
113
  },
28
- [r]
29
- ), /* @__PURE__ */ h(e.Provider, { value: r, children: n });
114
+ [s]
115
+ ), /* @__PURE__ */ i(e.Provider, { value: s, children: r });
30
116
  }
31
- function i() {
32
- const n = p(e);
33
- if (!n) throw new Error("EngineContext not found");
34
- return n;
117
+ function o() {
118
+ const r = k(e);
119
+ if (!r) throw new Error("EngineContext not found");
120
+ return r;
35
121
  }
36
- return { EngineProvider: o, useEngine: i };
122
+ return { EngineProvider: n, useEngine: o };
37
123
  }
38
- function y(t) {
39
- return t.filter((e) => e instanceof w.NodeInstance);
124
+ function A(t) {
125
+ return t.filter((e) => e instanceof M.NodeInstance);
40
126
  }
41
- function A({
127
+ function Y({
42
128
  width: t = "100%",
43
129
  height: e = "100%",
44
- graph: o,
45
- className: i,
46
- children: n,
47
- options: r,
48
- onNodeClick: u,
49
- onNodeDoubleClick: a,
130
+ graph: n,
131
+ className: o,
132
+ children: r,
133
+ options: s,
134
+ onNodeClick: m,
135
+ onNodeDoubleClick: g,
50
136
  onNodeSelection: c,
51
- onBlankClick: f,
52
- onBlankDoubleClick: d,
53
- onLinkClick: C,
54
- onDragStart: G,
55
- onDrag: b,
56
- onDragEnd: P,
57
- onStartConnecting: x,
137
+ onBlankClick: x,
138
+ onBlankDoubleClick: a,
139
+ onLinkClick: d,
140
+ onDragStart: l,
141
+ onDrag: u,
142
+ onDragEnd: p,
143
+ onStartConnecting: E,
58
144
  onKeyDown: N
59
145
  }) {
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
146
+ const P = v(null), b = v({}), w = v(s);
147
+ return w.current = s, R(() => {
148
+ b.current = {
149
+ "node:click": m,
150
+ "node:dblclick": g,
151
+ "selection:changed": (h) => c?.(A(h)),
152
+ "blank:click": x,
153
+ "blank:dblclick": a,
154
+ "link:click": d,
155
+ "node:dragstart": l,
156
+ "node:dragmove": u,
157
+ "node:dragend": p,
158
+ "start:connecting": E ? () => E() : void 0
73
159
  };
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(
160
+ }), R(() => {
161
+ const h = P.current;
162
+ if (h)
163
+ return n.mount(h, () => b.current, w.current);
164
+ }, [n]), /* @__PURE__ */ G("div", { style: { position: "relative", width: t, height: e }, children: [
165
+ /* @__PURE__ */ i(
80
166
  "div",
81
167
  {
82
168
  style: { width: "100%", height: "100%" },
83
- className: i,
169
+ className: o,
84
170
  onKeyDown: N,
85
- ref: g
171
+ ref: P
86
172
  }
87
173
  ),
88
- n
174
+ r
89
175
  ] });
90
176
  }
91
177
  export {
92
- A as GraphView,
93
- z as createEngineContext,
94
- q as createGraphContext
178
+ V as CanvasPortalHost,
179
+ Y as GraphView,
180
+ $ as ReactPortalRegistry,
181
+ X as createEngineContext,
182
+ W as createGraphContext,
183
+ F as createReactMount,
184
+ z as createReactPortalRegistry,
185
+ T as createReactSlot,
186
+ U as useReactMount
95
187
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lyx-pj/primflow-react",
3
3
  "private": false,
4
- "version": "2.0.1",
4
+ "version": "2.1.0",
5
5
  "description": "React provider and GraphView that bind the Primflow canvas and workflow engine to React components.",
6
6
  "type": "module",
7
7
  "main": "build/index.es.js",
@@ -32,14 +32,14 @@
32
32
  "directory": "packages/primflow-react"
33
33
  },
34
34
  "peerDependencies": {
35
- "@lyx-pj/primflow-canvas": "2.0.1",
36
- "@lyx-pj/primflow-engine": "2.0.1",
35
+ "@lyx-pj/primflow-canvas": "2.1.0",
36
+ "@lyx-pj/primflow-engine": "2.1.0",
37
37
  "react": "^18.0.0 || ^19.0.0",
38
38
  "react-dom": "^18.0.0 || ^19.0.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@lyx-pj/primflow-canvas": "2.0.1",
42
- "@lyx-pj/primflow-engine": "2.0.1",
41
+ "@lyx-pj/primflow-canvas": "2.1.0",
42
+ "@lyx-pj/primflow-engine": "2.1.0",
43
43
  "@types/react": "^19.1.10",
44
44
  "@types/react-dom": "^19.1.7",
45
45
  "@vitejs/plugin-react-swc": "^4.0.1",
@@ -1,68 +0,0 @@
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.
@@ -1,68 +0,0 @@
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。