@knotx/react 0.0.8 → 0.0.9
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 +115 -57
- package/dist/index.d.cts +16 -7
- package/dist/index.d.mts +16 -7
- package/dist/index.d.ts +16 -7
- package/dist/index.mjs +116 -58
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -2,10 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
const jsxRuntime = require('react/jsx-runtime');
|
|
4
4
|
const core = require('@knotx/core');
|
|
5
|
-
const pluginsBaseRender = require('@knotx/plugins-base-render');
|
|
6
5
|
const react = require('react');
|
|
7
6
|
const rxjs = require('rxjs');
|
|
8
7
|
const operators = require('rxjs/operators');
|
|
8
|
+
const pluginsBaseRender = require('@knotx/plugins-base-render');
|
|
9
|
+
|
|
10
|
+
function useContainerRef(onResize) {
|
|
11
|
+
const [container, setContainer] = react.useState();
|
|
12
|
+
const [_, setSize] = react.useState();
|
|
13
|
+
react.useLayoutEffect(() => {
|
|
14
|
+
if (!container)
|
|
15
|
+
return;
|
|
16
|
+
const resizeObserver = new ResizeObserver((entries) => {
|
|
17
|
+
const { width, height } = entries[0].contentRect;
|
|
18
|
+
onResize({ width, height });
|
|
19
|
+
setSize({ width, height });
|
|
20
|
+
});
|
|
21
|
+
resizeObserver.observe(container);
|
|
22
|
+
return () => resizeObserver.disconnect();
|
|
23
|
+
}, [container]);
|
|
24
|
+
return setContainer;
|
|
25
|
+
}
|
|
9
26
|
|
|
10
27
|
const LayerWrapper = ({ children, layer }) => {
|
|
11
28
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
@@ -25,6 +42,58 @@ function createLayer({ engine, layer }) {
|
|
|
25
42
|
return /* @__PURE__ */ jsxRuntime.jsx(LayerWrapper, { layer, children: engine.getLayerComponents(layer).map(({ name, render: Component }) => /* @__PURE__ */ jsxRuntime.jsx(Component, {}, name)) }, layer);
|
|
26
43
|
}
|
|
27
44
|
|
|
45
|
+
function useLayerContent(engineRef) {
|
|
46
|
+
const content = react.useMemo(() => {
|
|
47
|
+
const engine = engineRef.current;
|
|
48
|
+
if (!engine)
|
|
49
|
+
return null;
|
|
50
|
+
const layers = Array.from(engine.layers.keys()).sort((a, b) => a - b).filter((layer) => layer > core.Layer.Canvas);
|
|
51
|
+
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: engine.getLayerComponents(core.Layer.Canvas).reduce(
|
|
52
|
+
(children, { name, render: CanvasComponent }) => /* @__PURE__ */ jsxRuntime.jsx(CanvasComponent, { children }, name),
|
|
53
|
+
layers.map(
|
|
54
|
+
(layer) => createLayer({
|
|
55
|
+
engine,
|
|
56
|
+
layer
|
|
57
|
+
})
|
|
58
|
+
)
|
|
59
|
+
) });
|
|
60
|
+
}, [engineRef.current]);
|
|
61
|
+
return content;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function useEngineRef() {
|
|
65
|
+
const engineRef = react.useRef();
|
|
66
|
+
react.useEffect(() => () => {
|
|
67
|
+
var _a;
|
|
68
|
+
return (_a = engineRef.current) == null ? void 0 : _a.destroy();
|
|
69
|
+
}, []);
|
|
70
|
+
return engineRef;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function useMergedPlugins(plugins, disablePresetPlugins) {
|
|
74
|
+
return react.useMemo(() => {
|
|
75
|
+
if (disablePresetPlugins)
|
|
76
|
+
return plugins;
|
|
77
|
+
return [pluginsBaseRender.BaseRender, ...plugins || []];
|
|
78
|
+
}, [disablePresetPlugins, plugins]);
|
|
79
|
+
}
|
|
80
|
+
function usePluginConfigUpdate(engineRef, pluginConfig = {}) {
|
|
81
|
+
const pluginConfigRef = react.useRef(pluginConfig);
|
|
82
|
+
react.useLayoutEffect(() => {
|
|
83
|
+
if (!engineRef.current) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
for (const pluginName in pluginConfig) {
|
|
87
|
+
const pluginConfigValue = pluginConfig[pluginName];
|
|
88
|
+
if (Object.is(pluginConfigRef.current[pluginName], pluginConfigValue) || !pluginConfigValue) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
pluginConfigRef.current[pluginName] = pluginConfigValue;
|
|
92
|
+
engineRef.current.changePluginConfig(pluginName, pluginConfigValue);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
28
97
|
var __defProp = Object.defineProperty;
|
|
29
98
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
30
99
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
@@ -46,46 +115,22 @@ const Knotx = react.memo(react.forwardRef(({
|
|
|
46
115
|
style,
|
|
47
116
|
nodes,
|
|
48
117
|
edges,
|
|
49
|
-
plugins,
|
|
118
|
+
plugins: _plugins,
|
|
119
|
+
pluginConfig,
|
|
50
120
|
disablePresetPlugins = false
|
|
51
121
|
}, ref) => {
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (!container)
|
|
62
|
-
return;
|
|
63
|
-
const resizeObserver = new ResizeObserver((entries) => {
|
|
64
|
-
const { width, height } = entries[0].contentRect;
|
|
65
|
-
if (engineRef.current) {
|
|
66
|
-
engineRef.current.container = { width, height };
|
|
67
|
-
} else {
|
|
68
|
-
engineRef.current = initReactEngine({ width, height }, nodes, edges, enabledPlugins);
|
|
69
|
-
}
|
|
70
|
-
setSize({ width, height });
|
|
71
|
-
});
|
|
72
|
-
resizeObserver.observe(container);
|
|
73
|
-
return () => resizeObserver.disconnect();
|
|
74
|
-
}, [container]);
|
|
75
|
-
react.useEffect(() => () => {
|
|
76
|
-
var _a;
|
|
77
|
-
return (_a = engineRef.current) == null ? void 0 : _a.destroy();
|
|
78
|
-
}, []);
|
|
122
|
+
const engineRef = useEngineRef();
|
|
123
|
+
const plugins = useMergedPlugins(_plugins, disablePresetPlugins);
|
|
124
|
+
const setContainer = useContainerRef((containerSize) => {
|
|
125
|
+
if (engineRef.current) {
|
|
126
|
+
engineRef.current.container = containerSize;
|
|
127
|
+
} else {
|
|
128
|
+
engineRef.current = initReactEngine({ container: containerSize, nodes, edges, plugins, pluginConfig });
|
|
129
|
+
}
|
|
130
|
+
});
|
|
79
131
|
react.useImperativeHandle(ref, () => engineRef.current);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (!engine)
|
|
83
|
-
return null;
|
|
84
|
-
const layers = Array.from(engine.layers.keys()).sort((a, b) => a - b).filter((layer) => layer > core.Layer.Canvas);
|
|
85
|
-
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: engine.getLayerComponents(core.Layer.Canvas).reduce((children, { name, render: CanvasComponent }) => /* @__PURE__ */ jsxRuntime.jsx(CanvasComponent, { children }, name), layers.map(
|
|
86
|
-
(layer) => createLayer({ engine, layer })
|
|
87
|
-
)) });
|
|
88
|
-
}, [engineRef.current]);
|
|
132
|
+
usePluginConfigUpdate(engineRef, pluginConfig);
|
|
133
|
+
const content = useLayerContent(engineRef);
|
|
89
134
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
90
135
|
"div",
|
|
91
136
|
{
|
|
@@ -100,27 +145,40 @@ const Knotx = react.memo(react.forwardRef(({
|
|
|
100
145
|
);
|
|
101
146
|
}));
|
|
102
147
|
Knotx.displayName = "Knotx";
|
|
103
|
-
function initReactEngine(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
148
|
+
function initReactEngine({
|
|
149
|
+
container,
|
|
150
|
+
nodes = [],
|
|
151
|
+
edges = [],
|
|
152
|
+
plugins = [],
|
|
153
|
+
pluginConfig = {}
|
|
154
|
+
}) {
|
|
155
|
+
return new core.Engine({
|
|
156
|
+
container,
|
|
157
|
+
nodes,
|
|
158
|
+
edges,
|
|
159
|
+
plugins,
|
|
160
|
+
pluginConfig,
|
|
161
|
+
runtime: {
|
|
162
|
+
render: {
|
|
163
|
+
getValue: (subject, { selector, context }) => {
|
|
164
|
+
const contextRef = react.useRef(context);
|
|
165
|
+
contextRef.current = context;
|
|
166
|
+
const [state, setState] = react.useState(() => selector ? selector(subject instanceof rxjs.BehaviorSubject ? subject.value : subject, contextRef.current) : subject instanceof rxjs.BehaviorSubject ? subject.value : subject);
|
|
167
|
+
react.useEffect(() => {
|
|
168
|
+
if (!(subject instanceof rxjs.BehaviorSubject)) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const subscription = subject.pipe(
|
|
172
|
+
operators.map((value) => selector ? selector(value, contextRef.current) : value),
|
|
173
|
+
operators.distinctUntilChanged()
|
|
174
|
+
).subscribe(setState);
|
|
175
|
+
return () => subscription.unsubscribe();
|
|
176
|
+
}, [subject, selector]);
|
|
177
|
+
return state;
|
|
178
|
+
}
|
|
121
179
|
}
|
|
122
180
|
}
|
|
123
|
-
}
|
|
181
|
+
});
|
|
124
182
|
}
|
|
125
183
|
|
|
126
184
|
exports.Knotx = Knotx;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
|
-
import { Engine, NodeData, EdgeData
|
|
2
|
-
import { FC, CSSProperties,
|
|
1
|
+
import { Engine, Plugin, NodeData, EdgeData } from '@knotx/core';
|
|
2
|
+
import { FC, CSSProperties, ForwardedRef, ReactElement } from 'react';
|
|
3
3
|
|
|
4
4
|
type ReactEngine = Engine<FC<any>>;
|
|
5
|
-
|
|
5
|
+
type KnotxProps<TPlugins extends Plugin[] = Plugin[]> = {
|
|
6
6
|
className?: string;
|
|
7
7
|
style?: CSSProperties;
|
|
8
8
|
nodes?: NodeData[];
|
|
9
9
|
edges?: EdgeData[];
|
|
10
|
-
plugins?:
|
|
10
|
+
plugins?: TPlugins;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* disable preset plugins
|
|
13
13
|
* preset plugin includes:
|
|
14
14
|
* - base-render (@knotx/plugins-base-render)
|
|
15
15
|
* @default false
|
|
16
16
|
*/
|
|
17
17
|
disablePresetPlugins?: boolean;
|
|
18
|
-
}
|
|
18
|
+
} & ((TPlugins[number] extends Plugin<infer T> ? {
|
|
19
|
+
[TName in T as Extract<TPlugins[number], Plugin<TName>> extends Plugin<TName, undefined> ? never : TName]: Extract<TPlugins[number], Plugin<TName>> extends infer T ? T extends Plugin<TName, infer TConfig> ? TConfig : never : never;
|
|
20
|
+
} : {}) extends infer PluginConfig ? {} extends PluginConfig ? {
|
|
21
|
+
pluginConfig?: undefined;
|
|
22
|
+
} : {
|
|
23
|
+
pluginConfig: PluginConfig;
|
|
24
|
+
} : {});
|
|
19
25
|
|
|
20
|
-
declare const Knotx:
|
|
26
|
+
declare const Knotx: {
|
|
27
|
+
<TPlugins extends Plugin[]>(props: KnotxProps<TPlugins>, ref: ForwardedRef<ReactEngine>): ReactElement | null;
|
|
28
|
+
displayName?: string | undefined;
|
|
29
|
+
};
|
|
21
30
|
|
|
22
31
|
export { Knotx, type KnotxProps, type ReactEngine };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
|
-
import { Engine, NodeData, EdgeData
|
|
2
|
-
import { FC, CSSProperties,
|
|
1
|
+
import { Engine, Plugin, NodeData, EdgeData } from '@knotx/core';
|
|
2
|
+
import { FC, CSSProperties, ForwardedRef, ReactElement } from 'react';
|
|
3
3
|
|
|
4
4
|
type ReactEngine = Engine<FC<any>>;
|
|
5
|
-
|
|
5
|
+
type KnotxProps<TPlugins extends Plugin[] = Plugin[]> = {
|
|
6
6
|
className?: string;
|
|
7
7
|
style?: CSSProperties;
|
|
8
8
|
nodes?: NodeData[];
|
|
9
9
|
edges?: EdgeData[];
|
|
10
|
-
plugins?:
|
|
10
|
+
plugins?: TPlugins;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* disable preset plugins
|
|
13
13
|
* preset plugin includes:
|
|
14
14
|
* - base-render (@knotx/plugins-base-render)
|
|
15
15
|
* @default false
|
|
16
16
|
*/
|
|
17
17
|
disablePresetPlugins?: boolean;
|
|
18
|
-
}
|
|
18
|
+
} & ((TPlugins[number] extends Plugin<infer T> ? {
|
|
19
|
+
[TName in T as Extract<TPlugins[number], Plugin<TName>> extends Plugin<TName, undefined> ? never : TName]: Extract<TPlugins[number], Plugin<TName>> extends infer T ? T extends Plugin<TName, infer TConfig> ? TConfig : never : never;
|
|
20
|
+
} : {}) extends infer PluginConfig ? {} extends PluginConfig ? {
|
|
21
|
+
pluginConfig?: undefined;
|
|
22
|
+
} : {
|
|
23
|
+
pluginConfig: PluginConfig;
|
|
24
|
+
} : {});
|
|
19
25
|
|
|
20
|
-
declare const Knotx:
|
|
26
|
+
declare const Knotx: {
|
|
27
|
+
<TPlugins extends Plugin[]>(props: KnotxProps<TPlugins>, ref: ForwardedRef<ReactEngine>): ReactElement | null;
|
|
28
|
+
displayName?: string | undefined;
|
|
29
|
+
};
|
|
21
30
|
|
|
22
31
|
export { Knotx, type KnotxProps, type ReactEngine };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
|
-
import { Engine, NodeData, EdgeData
|
|
2
|
-
import { FC, CSSProperties,
|
|
1
|
+
import { Engine, Plugin, NodeData, EdgeData } from '@knotx/core';
|
|
2
|
+
import { FC, CSSProperties, ForwardedRef, ReactElement } from 'react';
|
|
3
3
|
|
|
4
4
|
type ReactEngine = Engine<FC<any>>;
|
|
5
|
-
|
|
5
|
+
type KnotxProps<TPlugins extends Plugin[] = Plugin[]> = {
|
|
6
6
|
className?: string;
|
|
7
7
|
style?: CSSProperties;
|
|
8
8
|
nodes?: NodeData[];
|
|
9
9
|
edges?: EdgeData[];
|
|
10
|
-
plugins?:
|
|
10
|
+
plugins?: TPlugins;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* disable preset plugins
|
|
13
13
|
* preset plugin includes:
|
|
14
14
|
* - base-render (@knotx/plugins-base-render)
|
|
15
15
|
* @default false
|
|
16
16
|
*/
|
|
17
17
|
disablePresetPlugins?: boolean;
|
|
18
|
-
}
|
|
18
|
+
} & ((TPlugins[number] extends Plugin<infer T> ? {
|
|
19
|
+
[TName in T as Extract<TPlugins[number], Plugin<TName>> extends Plugin<TName, undefined> ? never : TName]: Extract<TPlugins[number], Plugin<TName>> extends infer T ? T extends Plugin<TName, infer TConfig> ? TConfig : never : never;
|
|
20
|
+
} : {}) extends infer PluginConfig ? {} extends PluginConfig ? {
|
|
21
|
+
pluginConfig?: undefined;
|
|
22
|
+
} : {
|
|
23
|
+
pluginConfig: PluginConfig;
|
|
24
|
+
} : {});
|
|
19
25
|
|
|
20
|
-
declare const Knotx:
|
|
26
|
+
declare const Knotx: {
|
|
27
|
+
<TPlugins extends Plugin[]>(props: KnotxProps<TPlugins>, ref: ForwardedRef<ReactEngine>): ReactElement | null;
|
|
28
|
+
displayName?: string | undefined;
|
|
29
|
+
};
|
|
21
30
|
|
|
22
31
|
export { Knotx, type KnotxProps, type ReactEngine };
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
2
2
|
import { Layer, Engine } from '@knotx/core';
|
|
3
|
-
import {
|
|
4
|
-
import { memo, forwardRef, useState, useRef, useMemo, useLayoutEffect, useEffect, useImperativeHandle } from 'react';
|
|
3
|
+
import { useState, useLayoutEffect, useMemo, useRef, useEffect, memo, forwardRef, useImperativeHandle } from 'react';
|
|
5
4
|
import { BehaviorSubject } from 'rxjs';
|
|
6
5
|
import { map, distinctUntilChanged } from 'rxjs/operators';
|
|
6
|
+
import { BaseRender } from '@knotx/plugins-base-render';
|
|
7
|
+
|
|
8
|
+
function useContainerRef(onResize) {
|
|
9
|
+
const [container, setContainer] = useState();
|
|
10
|
+
const [_, setSize] = useState();
|
|
11
|
+
useLayoutEffect(() => {
|
|
12
|
+
if (!container)
|
|
13
|
+
return;
|
|
14
|
+
const resizeObserver = new ResizeObserver((entries) => {
|
|
15
|
+
const { width, height } = entries[0].contentRect;
|
|
16
|
+
onResize({ width, height });
|
|
17
|
+
setSize({ width, height });
|
|
18
|
+
});
|
|
19
|
+
resizeObserver.observe(container);
|
|
20
|
+
return () => resizeObserver.disconnect();
|
|
21
|
+
}, [container]);
|
|
22
|
+
return setContainer;
|
|
23
|
+
}
|
|
7
24
|
|
|
8
25
|
const LayerWrapper = ({ children, layer }) => {
|
|
9
26
|
return /* @__PURE__ */ jsx(
|
|
@@ -23,6 +40,58 @@ function createLayer({ engine, layer }) {
|
|
|
23
40
|
return /* @__PURE__ */ jsx(LayerWrapper, { layer, children: engine.getLayerComponents(layer).map(({ name, render: Component }) => /* @__PURE__ */ jsx(Component, {}, name)) }, layer);
|
|
24
41
|
}
|
|
25
42
|
|
|
43
|
+
function useLayerContent(engineRef) {
|
|
44
|
+
const content = useMemo(() => {
|
|
45
|
+
const engine = engineRef.current;
|
|
46
|
+
if (!engine)
|
|
47
|
+
return null;
|
|
48
|
+
const layers = Array.from(engine.layers.keys()).sort((a, b) => a - b).filter((layer) => layer > Layer.Canvas);
|
|
49
|
+
return /* @__PURE__ */ jsx(Fragment, { children: engine.getLayerComponents(Layer.Canvas).reduce(
|
|
50
|
+
(children, { name, render: CanvasComponent }) => /* @__PURE__ */ jsx(CanvasComponent, { children }, name),
|
|
51
|
+
layers.map(
|
|
52
|
+
(layer) => createLayer({
|
|
53
|
+
engine,
|
|
54
|
+
layer
|
|
55
|
+
})
|
|
56
|
+
)
|
|
57
|
+
) });
|
|
58
|
+
}, [engineRef.current]);
|
|
59
|
+
return content;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function useEngineRef() {
|
|
63
|
+
const engineRef = useRef();
|
|
64
|
+
useEffect(() => () => {
|
|
65
|
+
var _a;
|
|
66
|
+
return (_a = engineRef.current) == null ? void 0 : _a.destroy();
|
|
67
|
+
}, []);
|
|
68
|
+
return engineRef;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function useMergedPlugins(plugins, disablePresetPlugins) {
|
|
72
|
+
return useMemo(() => {
|
|
73
|
+
if (disablePresetPlugins)
|
|
74
|
+
return plugins;
|
|
75
|
+
return [BaseRender, ...plugins || []];
|
|
76
|
+
}, [disablePresetPlugins, plugins]);
|
|
77
|
+
}
|
|
78
|
+
function usePluginConfigUpdate(engineRef, pluginConfig = {}) {
|
|
79
|
+
const pluginConfigRef = useRef(pluginConfig);
|
|
80
|
+
useLayoutEffect(() => {
|
|
81
|
+
if (!engineRef.current) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
for (const pluginName in pluginConfig) {
|
|
85
|
+
const pluginConfigValue = pluginConfig[pluginName];
|
|
86
|
+
if (Object.is(pluginConfigRef.current[pluginName], pluginConfigValue) || !pluginConfigValue) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
pluginConfigRef.current[pluginName] = pluginConfigValue;
|
|
90
|
+
engineRef.current.changePluginConfig(pluginName, pluginConfigValue);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
26
95
|
var __defProp = Object.defineProperty;
|
|
27
96
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
28
97
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
@@ -44,46 +113,22 @@ const Knotx = memo(forwardRef(({
|
|
|
44
113
|
style,
|
|
45
114
|
nodes,
|
|
46
115
|
edges,
|
|
47
|
-
plugins,
|
|
116
|
+
plugins: _plugins,
|
|
117
|
+
pluginConfig,
|
|
48
118
|
disablePresetPlugins = false
|
|
49
119
|
}, ref) => {
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (!container)
|
|
60
|
-
return;
|
|
61
|
-
const resizeObserver = new ResizeObserver((entries) => {
|
|
62
|
-
const { width, height } = entries[0].contentRect;
|
|
63
|
-
if (engineRef.current) {
|
|
64
|
-
engineRef.current.container = { width, height };
|
|
65
|
-
} else {
|
|
66
|
-
engineRef.current = initReactEngine({ width, height }, nodes, edges, enabledPlugins);
|
|
67
|
-
}
|
|
68
|
-
setSize({ width, height });
|
|
69
|
-
});
|
|
70
|
-
resizeObserver.observe(container);
|
|
71
|
-
return () => resizeObserver.disconnect();
|
|
72
|
-
}, [container]);
|
|
73
|
-
useEffect(() => () => {
|
|
74
|
-
var _a;
|
|
75
|
-
return (_a = engineRef.current) == null ? void 0 : _a.destroy();
|
|
76
|
-
}, []);
|
|
120
|
+
const engineRef = useEngineRef();
|
|
121
|
+
const plugins = useMergedPlugins(_plugins, disablePresetPlugins);
|
|
122
|
+
const setContainer = useContainerRef((containerSize) => {
|
|
123
|
+
if (engineRef.current) {
|
|
124
|
+
engineRef.current.container = containerSize;
|
|
125
|
+
} else {
|
|
126
|
+
engineRef.current = initReactEngine({ container: containerSize, nodes, edges, plugins, pluginConfig });
|
|
127
|
+
}
|
|
128
|
+
});
|
|
77
129
|
useImperativeHandle(ref, () => engineRef.current);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (!engine)
|
|
81
|
-
return null;
|
|
82
|
-
const layers = Array.from(engine.layers.keys()).sort((a, b) => a - b).filter((layer) => layer > Layer.Canvas);
|
|
83
|
-
return /* @__PURE__ */ jsx(Fragment, { children: engine.getLayerComponents(Layer.Canvas).reduce((children, { name, render: CanvasComponent }) => /* @__PURE__ */ jsx(CanvasComponent, { children }, name), layers.map(
|
|
84
|
-
(layer) => createLayer({ engine, layer })
|
|
85
|
-
)) });
|
|
86
|
-
}, [engineRef.current]);
|
|
130
|
+
usePluginConfigUpdate(engineRef, pluginConfig);
|
|
131
|
+
const content = useLayerContent(engineRef);
|
|
87
132
|
return /* @__PURE__ */ jsx(
|
|
88
133
|
"div",
|
|
89
134
|
{
|
|
@@ -98,27 +143,40 @@ const Knotx = memo(forwardRef(({
|
|
|
98
143
|
);
|
|
99
144
|
}));
|
|
100
145
|
Knotx.displayName = "Knotx";
|
|
101
|
-
function initReactEngine(
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
146
|
+
function initReactEngine({
|
|
147
|
+
container,
|
|
148
|
+
nodes = [],
|
|
149
|
+
edges = [],
|
|
150
|
+
plugins = [],
|
|
151
|
+
pluginConfig = {}
|
|
152
|
+
}) {
|
|
153
|
+
return new Engine({
|
|
154
|
+
container,
|
|
155
|
+
nodes,
|
|
156
|
+
edges,
|
|
157
|
+
plugins,
|
|
158
|
+
pluginConfig,
|
|
159
|
+
runtime: {
|
|
160
|
+
render: {
|
|
161
|
+
getValue: (subject, { selector, context }) => {
|
|
162
|
+
const contextRef = useRef(context);
|
|
163
|
+
contextRef.current = context;
|
|
164
|
+
const [state, setState] = useState(() => selector ? selector(subject instanceof BehaviorSubject ? subject.value : subject, contextRef.current) : subject instanceof BehaviorSubject ? subject.value : subject);
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
if (!(subject instanceof BehaviorSubject)) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const subscription = subject.pipe(
|
|
170
|
+
map((value) => selector ? selector(value, contextRef.current) : value),
|
|
171
|
+
distinctUntilChanged()
|
|
172
|
+
).subscribe(setState);
|
|
173
|
+
return () => subscription.unsubscribe();
|
|
174
|
+
}, [subject, selector]);
|
|
175
|
+
return state;
|
|
176
|
+
}
|
|
119
177
|
}
|
|
120
178
|
}
|
|
121
|
-
}
|
|
179
|
+
});
|
|
122
180
|
}
|
|
123
181
|
|
|
124
182
|
export { Knotx };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knotx/react",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.9",
|
|
5
5
|
"description": "React for Knotx",
|
|
6
6
|
"author": "boenfu",
|
|
7
7
|
"license": "MIT",
|
|
@@ -34,19 +34,19 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"rxjs": "^7.8.1",
|
|
37
|
-
"@knotx/core": "0.0.
|
|
38
|
-
"@knotx/jsx": "0.0.
|
|
39
|
-
"@knotx/plugins-base-render": "0.0.
|
|
37
|
+
"@knotx/core": "0.0.7",
|
|
38
|
+
"@knotx/jsx": "0.0.7",
|
|
39
|
+
"@knotx/plugins-base-render": "0.0.9"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/react": "^17.0.0",
|
|
43
43
|
"@types/react-dom": "^17.0.0",
|
|
44
44
|
"react": "^17.0.0",
|
|
45
45
|
"react-dom": "^17.0.0",
|
|
46
|
-
"@knotx/build-config": "0.0.
|
|
47
|
-
"@knotx/eslint-config": "0.0.
|
|
48
|
-
"@knotx/jsx": "0.0.
|
|
49
|
-
"@knotx/typescript-config": "0.0.
|
|
46
|
+
"@knotx/build-config": "0.0.7",
|
|
47
|
+
"@knotx/eslint-config": "0.0.7",
|
|
48
|
+
"@knotx/jsx": "0.0.7",
|
|
49
|
+
"@knotx/typescript-config": "0.0.7"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "unbuild --failOnWarn=false",
|