@knotx/react 0.4.6 → 0.4.7

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,58 +84,33 @@ 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 }) {
@@ -183,6 +193,7 @@ var __spreadValues = (a, b) => {
183
193
  const Knotx = react.memo(react.forwardRef(({
184
194
  className,
185
195
  style,
196
+ getContainerStyle,
186
197
  initialNodes,
187
198
  initialEdges,
188
199
  nodes,
@@ -192,40 +203,45 @@ const Knotx = react.memo(react.forwardRef(({
192
203
  disablePresetPlugins = false,
193
204
  onInit
194
205
  }, ref) => {
206
+ const [, rerender] = react.useReducer((state) => state + 1, 0);
195
207
  const initRef = react.useRef(false);
196
208
  const engineRef = useEngineRef();
197
209
  const plugins = useMergedPlugins(_plugins, disablePresetPlugins);
210
+ if (!engineRef.current) {
211
+ engineRef.current = initReactEngine({
212
+ container: { width: 0, height: 0 },
213
+ nodes: nodes != null ? nodes : initialNodes,
214
+ edges: edges != null ? edges : initialEdges,
215
+ plugins,
216
+ pluginConfig
217
+ });
218
+ }
198
219
  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
- });
220
+ if (!engineRef.current) {
221
+ return;
209
222
  }
223
+ engineRef.current.container = containerSize;
224
+ initRef.current = true;
225
+ rerender();
210
226
  });
211
- react.useImperativeHandle(ref, () => ({ engineRef }));
227
+ react.useImperativeHandle(ref, () => ({ engineRef, rerender }));
212
228
  useDataUpdate(engineRef, { nodes, edges, initRef });
213
229
  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
- }
230
+ react.useEffect(() => {
231
+ if (initRef.current) {
232
+ onInit == null ? void 0 : onInit(engineRef.current);
233
+ }
234
+ }, [initRef.current]);
219
235
  return /* @__PURE__ */ jsxRuntime.jsx(
220
236
  "div",
221
237
  {
222
238
  ref: setContainer,
223
239
  className,
224
- style: __spreadValues({
240
+ style: __spreadValues(__spreadValues({
225
241
  overflow: "hidden",
226
242
  contain: "strict"
227
- }, style),
228
- children: content
243
+ }, style), getContainerStyle == null ? void 0 : getContainerStyle(engineRef.current)),
244
+ children: initRef.current && /* @__PURE__ */ jsxRuntime.jsx(LayerContent, { engineRef })
229
245
  }
230
246
  );
231
247
  }));
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,58 +82,33 @@ 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 }) {
@@ -181,6 +191,7 @@ var __spreadValues = (a, b) => {
181
191
  const Knotx = memo(forwardRef(({
182
192
  className,
183
193
  style,
194
+ getContainerStyle,
184
195
  initialNodes,
185
196
  initialEdges,
186
197
  nodes,
@@ -190,40 +201,45 @@ const Knotx = memo(forwardRef(({
190
201
  disablePresetPlugins = false,
191
202
  onInit
192
203
  }, ref) => {
204
+ const [, rerender] = useReducer((state) => state + 1, 0);
193
205
  const initRef = useRef(false);
194
206
  const engineRef = useEngineRef();
195
207
  const plugins = useMergedPlugins(_plugins, disablePresetPlugins);
208
+ if (!engineRef.current) {
209
+ engineRef.current = initReactEngine({
210
+ container: { width: 0, height: 0 },
211
+ nodes: nodes != null ? nodes : initialNodes,
212
+ edges: edges != null ? edges : initialEdges,
213
+ plugins,
214
+ pluginConfig
215
+ });
216
+ }
196
217
  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
- });
218
+ if (!engineRef.current) {
219
+ return;
207
220
  }
221
+ engineRef.current.container = containerSize;
222
+ initRef.current = true;
223
+ rerender();
208
224
  });
209
- useImperativeHandle(ref, () => ({ engineRef }));
225
+ useImperativeHandle(ref, () => ({ engineRef, rerender }));
210
226
  useDataUpdate(engineRef, { nodes, edges, initRef });
211
227
  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
- }
228
+ useEffect(() => {
229
+ if (initRef.current) {
230
+ onInit == null ? void 0 : onInit(engineRef.current);
231
+ }
232
+ }, [initRef.current]);
217
233
  return /* @__PURE__ */ jsx(
218
234
  "div",
219
235
  {
220
236
  ref: setContainer,
221
237
  className,
222
- style: __spreadValues({
238
+ style: __spreadValues(__spreadValues({
223
239
  overflow: "hidden",
224
240
  contain: "strict"
225
- }, style),
226
- children: content
241
+ }, style), getContainerStyle == null ? void 0 : getContainerStyle(engineRef.current)),
242
+ children: initRef.current && /* @__PURE__ */ jsx(LayerContent, { engineRef })
227
243
  }
228
244
  );
229
245
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/react",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
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.7",
37
+ "@knotx/jsx": "0.4.7",
38
+ "@knotx/plugins-base-render": "0.4.7"
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.7",
46
+ "@knotx/eslint-config": "0.4.7",
47
+ "@knotx/jsx": "0.4.7",
48
+ "@knotx/typescript-config": "0.4.7"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "unbuild",