@knotx/react 0.4.6 → 0.4.8

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/dist/index.cjs CHANGED
@@ -7,6 +7,41 @@ const rxjs = require('rxjs');
7
7
  const operators = require('rxjs/operators');
8
8
  const pluginsBaseRender = require('@knotx/plugins-base-render');
9
9
 
10
+ const LayerWrapper = ({ children, layer }) => {
11
+ return /* @__PURE__ */ jsxRuntime.jsx(
12
+ "section",
13
+ {
14
+ style: {
15
+ position: "absolute",
16
+ left: 0,
17
+ top: 0,
18
+ zIndex: layer
19
+ },
20
+ children
21
+ }
22
+ );
23
+ };
24
+ function createLayer({ engine, layer }) {
25
+ return /* @__PURE__ */ jsxRuntime.jsx(LayerWrapper, { layer, children: engine.getLayerComponents(layer).map(({ name, render: Component }) => /* @__PURE__ */ jsxRuntime.jsx(Component, {}, name)) }, layer);
26
+ }
27
+
28
+ function LayerContent({ engineRef }) {
29
+ const engine = engineRef.current;
30
+ if (!engine) {
31
+ return null;
32
+ }
33
+ const layers = Array.from(engine.layers.keys()).sort((a, b) => a - b).filter((layer) => layer > core.Layer.Canvas);
34
+ return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: engine.getLayerComponents(core.Layer.Canvas).reduce(
35
+ (children, { name, render: CanvasComponent }) => /* @__PURE__ */ jsxRuntime.jsx(CanvasComponent, { children }, name),
36
+ layers.map(
37
+ (layer) => createLayer({
38
+ engine,
39
+ layer
40
+ })
41
+ )
42
+ ) });
43
+ }
44
+
10
45
  function initReactEngine({
11
46
  container,
12
47
  nodes = [],
@@ -49,64 +84,42 @@ function initReactEngine({
49
84
  }
50
85
 
51
86
  function useContainerRef(onResize) {
52
- const [container, setContainer] = react.useState();
53
- const [_, setSize] = react.useState();
54
- const onResizeRef = react.useRef(onResize);
87
+ const onResizeRef = react.useRef(null);
55
88
  onResizeRef.current = onResize;
56
- react.useLayoutEffect(() => {
57
- if (!container)
89
+ const observerRef = react.useRef();
90
+ react.useEffect(() => {
91
+ return () => {
92
+ if (observerRef.current) {
93
+ observerRef.current.disconnect();
94
+ observerRef.current = void 0;
95
+ }
96
+ };
97
+ }, []);
98
+ return react.useCallback((ref) => {
99
+ if (observerRef.current) {
100
+ observerRef.current.disconnect();
101
+ observerRef.current = void 0;
102
+ }
103
+ if (!ref) {
58
104
  return;
105
+ }
59
106
  const resizeObserver = new ResizeObserver((entries) => {
60
107
  var _a;
61
108
  const { width, height } = entries[0].contentRect;
62
109
  (_a = onResizeRef.current) == null ? void 0 : _a.call(onResizeRef, { width, height });
63
- setSize({ width, height });
64
110
  });
65
- resizeObserver.observe(container);
66
- return () => resizeObserver.disconnect();
67
- }, [container]);
68
- return setContainer;
69
- }
70
-
71
- const LayerWrapper = ({ children, layer }) => {
72
- return /* @__PURE__ */ jsxRuntime.jsx(
73
- "section",
74
- {
75
- style: {
76
- position: "absolute",
77
- left: 0,
78
- top: 0,
79
- zIndex: layer
80
- },
81
- children
82
- }
83
- );
84
- };
85
- function createLayer({ engine, layer }) {
86
- return /* @__PURE__ */ jsxRuntime.jsx(LayerWrapper, { layer, children: engine.getLayerComponents(layer).map(({ name, render: Component }) => /* @__PURE__ */ jsxRuntime.jsx(Component, {}, name)) }, layer);
87
- }
88
-
89
- function useLayerContent(engineRef) {
90
- const engine = engineRef.current;
91
- if (!engine) {
92
- return null;
93
- }
94
- const layers = Array.from(engine.layers.keys()).sort((a, b) => a - b).filter((layer) => layer > core.Layer.Canvas);
95
- return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: engine.getLayerComponents(core.Layer.Canvas).reduce(
96
- (children, { name, render: CanvasComponent }) => /* @__PURE__ */ jsxRuntime.jsx(CanvasComponent, { children }, name),
97
- layers.map(
98
- (layer) => createLayer({
99
- engine,
100
- layer
101
- })
102
- )
103
- ) });
111
+ resizeObserver.observe(ref);
112
+ observerRef.current = resizeObserver;
113
+ }, []);
104
114
  }
105
115
 
106
116
  function useDataUpdate(engineRef, { nodes, edges, initRef }) {
107
117
  react.useEffect(() => {
108
118
  const engine = engineRef.current;
109
- if (!engine || !initRef.current) {
119
+ if (
120
+ // 未初始化完成
121
+ !initRef.current || !(engine == null ? void 0 : engine.nodesManager.version)
122
+ ) {
110
123
  return;
111
124
  }
112
125
  const previousNodes = engine.nodesManager.dataMap$.value;
@@ -116,7 +129,10 @@ function useDataUpdate(engineRef, { nodes, edges, initRef }) {
116
129
  }, [nodes, nodes == null ? void 0 : nodes.length]);
117
130
  react.useEffect(() => {
118
131
  const engine = engineRef.current;
119
- if (!engine || !initRef.current) {
132
+ if (
133
+ // 未初始化完成
134
+ !initRef.current || !(engine == null ? void 0 : engine.edgesManager.version)
135
+ ) {
120
136
  return;
121
137
  }
122
138
  const previousEdges = engine.edgesManager.dataMap$.value;
@@ -183,6 +199,7 @@ var __spreadValues = (a, b) => {
183
199
  const Knotx = react.memo(react.forwardRef(({
184
200
  className,
185
201
  style,
202
+ getContainerStyle,
186
203
  initialNodes,
187
204
  initialEdges,
188
205
  nodes,
@@ -192,40 +209,43 @@ const Knotx = react.memo(react.forwardRef(({
192
209
  disablePresetPlugins = false,
193
210
  onInit
194
211
  }, ref) => {
212
+ const [, rerender] = react.useReducer((state) => state + 1, 0);
195
213
  const initRef = react.useRef(false);
196
214
  const engineRef = useEngineRef();
197
215
  const plugins = useMergedPlugins(_plugins, disablePresetPlugins);
216
+ if (!engineRef.current) {
217
+ engineRef.current = initReactEngine({
218
+ container: { width: 0, height: 0 },
219
+ nodes: nodes != null ? nodes : initialNodes,
220
+ edges: edges != null ? edges : initialEdges,
221
+ plugins,
222
+ pluginConfig
223
+ });
224
+ }
198
225
  const setContainer = useContainerRef((containerSize) => {
199
- if (engineRef.current) {
200
- engineRef.current.container = containerSize;
201
- } else {
202
- engineRef.current = initReactEngine({
203
- container: containerSize,
204
- nodes: nodes != null ? nodes : initialNodes,
205
- edges: edges != null ? edges : initialEdges,
206
- plugins,
207
- pluginConfig
208
- });
226
+ if (!engineRef.current) {
227
+ return;
209
228
  }
229
+ engineRef.current.container = containerSize;
230
+ if (!initRef.current) {
231
+ initRef.current = true;
232
+ onInit == null ? void 0 : onInit(engineRef.current);
233
+ }
234
+ rerender();
210
235
  });
211
- react.useImperativeHandle(ref, () => ({ engineRef }));
236
+ react.useImperativeHandle(ref, () => ({ engineRef, rerender }));
212
237
  useDataUpdate(engineRef, { nodes, edges, initRef });
213
238
  usePluginConfigUpdate(engineRef, { pluginConfig, initRef });
214
- const content = useLayerContent(engineRef);
215
- if (!initRef.current && engineRef.current && content) {
216
- initRef.current = true;
217
- onInit == null ? void 0 : onInit(engineRef.current);
218
- }
219
239
  return /* @__PURE__ */ jsxRuntime.jsx(
220
240
  "div",
221
241
  {
222
242
  ref: setContainer,
223
243
  className,
224
- style: __spreadValues({
244
+ style: __spreadValues(__spreadValues({
225
245
  overflow: "hidden",
226
246
  contain: "strict"
227
- }, style),
228
- children: content
247
+ }, style), getContainerStyle == null ? void 0 : getContainerStyle(engineRef.current)),
248
+ children: initRef.current && /* @__PURE__ */ jsxRuntime.jsx(LayerContent, { engineRef })
229
249
  }
230
250
  );
231
251
  }));
package/dist/index.d.cts CHANGED
@@ -5,11 +5,18 @@ import { FC, MutableRefObject, Ref, CSSProperties, ForwardedRef, ReactElement }
5
5
  type ReactEngine<TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> = Engine<FC<any>, TNode, TEdge>;
6
6
  interface KnotxInstance<TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> {
7
7
  engineRef: MutableRefObject<ReactEngine<TNode, TEdge> | null>;
8
+ rerender: () => void;
8
9
  }
9
10
  type KnotxProps<TPlugins extends Plugin[] = Plugin[], TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> = {
10
11
  ref?: Ref<KnotxInstance<TNode, TEdge>> | undefined;
11
12
  className?: string;
12
13
  style?: CSSProperties;
14
+ /**
15
+ * get container style, merge into default style
16
+ * @param engine
17
+ * @returns
18
+ */
19
+ getContainerStyle?: (engine: ReactEngine<TNode, TEdge>) => CSSProperties;
13
20
  /**
14
21
  * initial nodes used to create engine
15
22
  */
package/dist/index.d.mts CHANGED
@@ -5,11 +5,18 @@ import { FC, MutableRefObject, Ref, CSSProperties, ForwardedRef, ReactElement }
5
5
  type ReactEngine<TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> = Engine<FC<any>, TNode, TEdge>;
6
6
  interface KnotxInstance<TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> {
7
7
  engineRef: MutableRefObject<ReactEngine<TNode, TEdge> | null>;
8
+ rerender: () => void;
8
9
  }
9
10
  type KnotxProps<TPlugins extends Plugin[] = Plugin[], TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> = {
10
11
  ref?: Ref<KnotxInstance<TNode, TEdge>> | undefined;
11
12
  className?: string;
12
13
  style?: CSSProperties;
14
+ /**
15
+ * get container style, merge into default style
16
+ * @param engine
17
+ * @returns
18
+ */
19
+ getContainerStyle?: (engine: ReactEngine<TNode, TEdge>) => CSSProperties;
13
20
  /**
14
21
  * initial nodes used to create engine
15
22
  */
package/dist/index.d.ts CHANGED
@@ -5,11 +5,18 @@ import { FC, MutableRefObject, Ref, CSSProperties, ForwardedRef, ReactElement }
5
5
  type ReactEngine<TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> = Engine<FC<any>, TNode, TEdge>;
6
6
  interface KnotxInstance<TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> {
7
7
  engineRef: MutableRefObject<ReactEngine<TNode, TEdge> | null>;
8
+ rerender: () => void;
8
9
  }
9
10
  type KnotxProps<TPlugins extends Plugin[] = Plugin[], TNode extends IRecord = IRecord, TEdge extends IRecord = IRecord> = {
10
11
  ref?: Ref<KnotxInstance<TNode, TEdge>> | undefined;
11
12
  className?: string;
12
13
  style?: CSSProperties;
14
+ /**
15
+ * get container style, merge into default style
16
+ * @param engine
17
+ * @returns
18
+ */
19
+ getContainerStyle?: (engine: ReactEngine<TNode, TEdge>) => CSSProperties;
13
20
  /**
14
21
  * initial nodes used to create engine
15
22
  */
package/dist/index.js CHANGED
@@ -1,10 +1,45 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
- import { useRef, useState, useEffect, useLayoutEffect, useMemo, memo, forwardRef, useImperativeHandle } from 'react';
3
- import { Engine, Layer, buildDiffOperation } from '@knotx/core';
2
+ import { useRef, useState, useEffect, useCallback, useLayoutEffect, useMemo, memo, forwardRef, useReducer, useImperativeHandle } from 'react';
3
+ import { Layer, Engine, buildDiffOperation } from '@knotx/core';
4
4
  import { BehaviorSubject } from 'rxjs';
5
5
  import { map, distinctUntilChanged } from 'rxjs/operators';
6
6
  import { BaseRender } from '@knotx/plugins-base-render';
7
7
 
8
+ const LayerWrapper = ({ children, layer }) => {
9
+ return /* @__PURE__ */ jsx(
10
+ "section",
11
+ {
12
+ style: {
13
+ position: "absolute",
14
+ left: 0,
15
+ top: 0,
16
+ zIndex: layer
17
+ },
18
+ children
19
+ }
20
+ );
21
+ };
22
+ function createLayer({ engine, layer }) {
23
+ return /* @__PURE__ */ jsx(LayerWrapper, { layer, children: engine.getLayerComponents(layer).map(({ name, render: Component }) => /* @__PURE__ */ jsx(Component, {}, name)) }, layer);
24
+ }
25
+
26
+ function LayerContent({ engineRef }) {
27
+ const engine = engineRef.current;
28
+ if (!engine) {
29
+ return null;
30
+ }
31
+ const layers = Array.from(engine.layers.keys()).sort((a, b) => a - b).filter((layer) => layer > Layer.Canvas);
32
+ return /* @__PURE__ */ jsx(Fragment, { children: engine.getLayerComponents(Layer.Canvas).reduce(
33
+ (children, { name, render: CanvasComponent }) => /* @__PURE__ */ jsx(CanvasComponent, { children }, name),
34
+ layers.map(
35
+ (layer) => createLayer({
36
+ engine,
37
+ layer
38
+ })
39
+ )
40
+ ) });
41
+ }
42
+
8
43
  function initReactEngine({
9
44
  container,
10
45
  nodes = [],
@@ -47,64 +82,42 @@ function initReactEngine({
47
82
  }
48
83
 
49
84
  function useContainerRef(onResize) {
50
- const [container, setContainer] = useState();
51
- const [_, setSize] = useState();
52
- const onResizeRef = useRef(onResize);
85
+ const onResizeRef = useRef(null);
53
86
  onResizeRef.current = onResize;
54
- useLayoutEffect(() => {
55
- if (!container)
87
+ const observerRef = useRef();
88
+ useEffect(() => {
89
+ return () => {
90
+ if (observerRef.current) {
91
+ observerRef.current.disconnect();
92
+ observerRef.current = void 0;
93
+ }
94
+ };
95
+ }, []);
96
+ return useCallback((ref) => {
97
+ if (observerRef.current) {
98
+ observerRef.current.disconnect();
99
+ observerRef.current = void 0;
100
+ }
101
+ if (!ref) {
56
102
  return;
103
+ }
57
104
  const resizeObserver = new ResizeObserver((entries) => {
58
105
  var _a;
59
106
  const { width, height } = entries[0].contentRect;
60
107
  (_a = onResizeRef.current) == null ? void 0 : _a.call(onResizeRef, { width, height });
61
- setSize({ width, height });
62
108
  });
63
- resizeObserver.observe(container);
64
- return () => resizeObserver.disconnect();
65
- }, [container]);
66
- return setContainer;
67
- }
68
-
69
- const LayerWrapper = ({ children, layer }) => {
70
- return /* @__PURE__ */ jsx(
71
- "section",
72
- {
73
- style: {
74
- position: "absolute",
75
- left: 0,
76
- top: 0,
77
- zIndex: layer
78
- },
79
- children
80
- }
81
- );
82
- };
83
- function createLayer({ engine, layer }) {
84
- return /* @__PURE__ */ jsx(LayerWrapper, { layer, children: engine.getLayerComponents(layer).map(({ name, render: Component }) => /* @__PURE__ */ jsx(Component, {}, name)) }, layer);
85
- }
86
-
87
- function useLayerContent(engineRef) {
88
- const engine = engineRef.current;
89
- if (!engine) {
90
- return null;
91
- }
92
- const layers = Array.from(engine.layers.keys()).sort((a, b) => a - b).filter((layer) => layer > Layer.Canvas);
93
- return /* @__PURE__ */ jsx(Fragment, { children: engine.getLayerComponents(Layer.Canvas).reduce(
94
- (children, { name, render: CanvasComponent }) => /* @__PURE__ */ jsx(CanvasComponent, { children }, name),
95
- layers.map(
96
- (layer) => createLayer({
97
- engine,
98
- layer
99
- })
100
- )
101
- ) });
109
+ resizeObserver.observe(ref);
110
+ observerRef.current = resizeObserver;
111
+ }, []);
102
112
  }
103
113
 
104
114
  function useDataUpdate(engineRef, { nodes, edges, initRef }) {
105
115
  useEffect(() => {
106
116
  const engine = engineRef.current;
107
- if (!engine || !initRef.current) {
117
+ if (
118
+ // 未初始化完成
119
+ !initRef.current || !(engine == null ? void 0 : engine.nodesManager.version)
120
+ ) {
108
121
  return;
109
122
  }
110
123
  const previousNodes = engine.nodesManager.dataMap$.value;
@@ -114,7 +127,10 @@ function useDataUpdate(engineRef, { nodes, edges, initRef }) {
114
127
  }, [nodes, nodes == null ? void 0 : nodes.length]);
115
128
  useEffect(() => {
116
129
  const engine = engineRef.current;
117
- if (!engine || !initRef.current) {
130
+ if (
131
+ // 未初始化完成
132
+ !initRef.current || !(engine == null ? void 0 : engine.edgesManager.version)
133
+ ) {
118
134
  return;
119
135
  }
120
136
  const previousEdges = engine.edgesManager.dataMap$.value;
@@ -181,6 +197,7 @@ var __spreadValues = (a, b) => {
181
197
  const Knotx = memo(forwardRef(({
182
198
  className,
183
199
  style,
200
+ getContainerStyle,
184
201
  initialNodes,
185
202
  initialEdges,
186
203
  nodes,
@@ -190,40 +207,43 @@ const Knotx = memo(forwardRef(({
190
207
  disablePresetPlugins = false,
191
208
  onInit
192
209
  }, ref) => {
210
+ const [, rerender] = useReducer((state) => state + 1, 0);
193
211
  const initRef = useRef(false);
194
212
  const engineRef = useEngineRef();
195
213
  const plugins = useMergedPlugins(_plugins, disablePresetPlugins);
214
+ if (!engineRef.current) {
215
+ engineRef.current = initReactEngine({
216
+ container: { width: 0, height: 0 },
217
+ nodes: nodes != null ? nodes : initialNodes,
218
+ edges: edges != null ? edges : initialEdges,
219
+ plugins,
220
+ pluginConfig
221
+ });
222
+ }
196
223
  const setContainer = useContainerRef((containerSize) => {
197
- if (engineRef.current) {
198
- engineRef.current.container = containerSize;
199
- } else {
200
- engineRef.current = initReactEngine({
201
- container: containerSize,
202
- nodes: nodes != null ? nodes : initialNodes,
203
- edges: edges != null ? edges : initialEdges,
204
- plugins,
205
- pluginConfig
206
- });
224
+ if (!engineRef.current) {
225
+ return;
207
226
  }
227
+ engineRef.current.container = containerSize;
228
+ if (!initRef.current) {
229
+ initRef.current = true;
230
+ onInit == null ? void 0 : onInit(engineRef.current);
231
+ }
232
+ rerender();
208
233
  });
209
- useImperativeHandle(ref, () => ({ engineRef }));
234
+ useImperativeHandle(ref, () => ({ engineRef, rerender }));
210
235
  useDataUpdate(engineRef, { nodes, edges, initRef });
211
236
  usePluginConfigUpdate(engineRef, { pluginConfig, initRef });
212
- const content = useLayerContent(engineRef);
213
- if (!initRef.current && engineRef.current && content) {
214
- initRef.current = true;
215
- onInit == null ? void 0 : onInit(engineRef.current);
216
- }
217
237
  return /* @__PURE__ */ jsx(
218
238
  "div",
219
239
  {
220
240
  ref: setContainer,
221
241
  className,
222
- style: __spreadValues({
242
+ style: __spreadValues(__spreadValues({
223
243
  overflow: "hidden",
224
244
  contain: "strict"
225
- }, style),
226
- children: content
245
+ }, style), getContainerStyle == null ? void 0 : getContainerStyle(engineRef.current)),
246
+ children: initRef.current && /* @__PURE__ */ jsx(LayerContent, { engineRef })
227
247
  }
228
248
  );
229
249
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/react",
3
- "version": "0.4.6",
3
+ "version": "0.4.8",
4
4
  "description": "React for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -33,19 +33,19 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "rxjs": "^7.8.1",
36
- "@knotx/core": "0.4.6",
37
- "@knotx/jsx": "0.4.6",
38
- "@knotx/plugins-base-render": "0.4.6"
36
+ "@knotx/core": "0.4.8",
37
+ "@knotx/jsx": "0.4.8",
38
+ "@knotx/plugins-base-render": "0.4.8"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/react": "^17.0.0",
42
42
  "@types/react-dom": "^17.0.0",
43
43
  "react": "^17.0.0",
44
44
  "react-dom": "^17.0.0",
45
- "@knotx/build-config": "0.4.6",
46
- "@knotx/eslint-config": "0.4.6",
47
- "@knotx/jsx": "0.4.6",
48
- "@knotx/typescript-config": "0.4.6"
45
+ "@knotx/build-config": "0.4.8",
46
+ "@knotx/eslint-config": "0.4.8",
47
+ "@knotx/jsx": "0.4.8",
48
+ "@knotx/typescript-config": "0.4.8"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "unbuild",