@chamn/render 0.0.9 → 0.0.11
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/commonComponent/index.d.ts +12 -0
- package/dist/const/index.d.ts +2 -0
- package/dist/core/ReactErrorBoundary.d.ts +26 -0
- package/dist/core/adapter.d.ts +64 -0
- package/dist/core/adapterReact.d.ts +108 -0
- package/dist/core/designReactRender.d.ts +47 -0
- package/dist/core/refManager.d.ts +7 -0
- package/dist/core/render.d.ts +32 -0
- package/dist/core/storeManager.d.ts +11 -0
- package/dist/core/type.d.ts +9 -0
- package/dist/{index.cjs.js → index.js} +2 -2
- package/dist/index.js.map +1 -0
- package/dist/{index.es.js → index.mjs} +94 -83
- package/dist/index.mjs.map +1 -0
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/util/assetsLoader.d.ts +15 -0
- package/dist/util/index.d.ts +20 -0
- package/package.json +14 -9
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -30
- package/.prettierrc.json +0 -7
- package/CHANGELOG.md +0 -40
- package/__tests__/demo.test.ts +0 -3
- package/build.config.ts +0 -20
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.es.js.map +0 -1
- package/index.html +0 -16
- package/jest.config.js +0 -196
- package/public/vite.svg +0 -1
- package/src/_dev_/components.tsx +0 -12
- package/src/_dev_/dev.tsx +0 -12
- package/src/_dev_/index.css +0 -13
- package/src/_dev_/page/DesignerRenderDemo.tsx +0 -65
- package/src/_dev_/page/RenderDemo.tsx +0 -60
- package/src/_dev_/router.tsx +0 -15
- package/src/commonComponent/index.tsx +0 -184
- package/src/const/index.ts +0 -5
- package/src/core/ReactErrorBoundary.ts +0 -91
- package/src/core/adapter.ts +0 -133
- package/src/core/adapterReact.ts +0 -734
- package/src/core/designReactRender.ts +0 -325
- package/src/core/refManager.ts +0 -18
- package/src/core/render.ts +0 -123
- package/src/core/storeManager.ts +0 -57
- package/src/core/type.ts +0 -10
- package/src/util/assetsLoader.ts +0 -73
- package/src/util/index.ts +0 -164
- package/src/vite-env.d.ts +0 -1
- package/stats.html +0 -6177
- package/tsconfig.json +0 -26
- /package/{src/index.ts → dist/index.d.ts} +0 -0
|
@@ -1,325 +0,0 @@
|
|
|
1
|
-
/* eslint-disable react/no-find-dom-node */
|
|
2
|
-
import { CNode, ContainerConfig, CPage, CPageDataType, CRootNode, getRandomStr, InnerComponentNameEnum } from '@chamn/model';
|
|
3
|
-
import { isArray, isPlainObject, merge } from 'lodash-es';
|
|
4
|
-
import React, { useMemo, useRef } from 'react';
|
|
5
|
-
import { RenderPropsType, Render, UseRenderReturnType } from './render';
|
|
6
|
-
import * as ReactDOM from 'react-dom';
|
|
7
|
-
import ErrorBoundary from './ReactErrorBoundary';
|
|
8
|
-
import { RenderInstance } from './type';
|
|
9
|
-
|
|
10
|
-
export class ComponentInstanceManager {
|
|
11
|
-
private instanceMap = new Map<string, RenderInstance[]>();
|
|
12
|
-
|
|
13
|
-
get(id: string) {
|
|
14
|
-
return this.instanceMap.get(id);
|
|
15
|
-
}
|
|
16
|
-
add(id: string, handle: any) {
|
|
17
|
-
const val = this.instanceMap.get(id);
|
|
18
|
-
if (val) {
|
|
19
|
-
val.push(handle);
|
|
20
|
-
} else {
|
|
21
|
-
this.instanceMap.set(id, [handle]);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
remove(id: string, val?: any) {
|
|
26
|
-
const valList = this.instanceMap.get(id);
|
|
27
|
-
if (val !== undefined && Array.isArray(valList)) {
|
|
28
|
-
const newList = valList.filter((el) => el !== val);
|
|
29
|
-
this.instanceMap.set(id, newList);
|
|
30
|
-
} else {
|
|
31
|
-
this.instanceMap.delete(id);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
destroy() {
|
|
36
|
-
this.instanceMap.clear();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export type DesignRenderProp = Omit<RenderPropsType, 'ref' | 'render'> & {
|
|
41
|
-
ref?: React.MutableRefObject<DesignRender | null>;
|
|
42
|
-
render?: UseDesignRenderReturnType;
|
|
43
|
-
onMount?: (instance: DesignRender) => void;
|
|
44
|
-
dropPlaceholder?: React.ComponentClass<{ node: CNode | CRootNode }> | React.FunctionComponent<{ node: CNode | CRootNode }> | string;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const DefaultDropPlaceholder: React.FC<{ node: CNode | CRootNode }> = (props) => {
|
|
48
|
-
const { node } = props;
|
|
49
|
-
const configInfo = useMemo(() => {
|
|
50
|
-
const isContainer = node.material?.value?.isContainer;
|
|
51
|
-
if (isPlainObject(isContainer)) {
|
|
52
|
-
return isContainer as ContainerConfig;
|
|
53
|
-
} else {
|
|
54
|
-
return {
|
|
55
|
-
placeholder: 'Drag the component to place it',
|
|
56
|
-
width: '100%',
|
|
57
|
-
height: '100%',
|
|
58
|
-
style: {},
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
}, [props.node]);
|
|
62
|
-
|
|
63
|
-
const { placeholder, height, width, style } = configInfo;
|
|
64
|
-
|
|
65
|
-
return React.createElement(
|
|
66
|
-
'div',
|
|
67
|
-
{
|
|
68
|
-
style: {
|
|
69
|
-
display: 'flex',
|
|
70
|
-
alignItems: 'center',
|
|
71
|
-
justifyContent: 'center',
|
|
72
|
-
backgroundColor: 'rgba(200,200,200,0.1)',
|
|
73
|
-
border: '1px solid rgba(0,0,0,0.1)',
|
|
74
|
-
borderRadius: '2px',
|
|
75
|
-
fontSize: '14px',
|
|
76
|
-
color: 'gray',
|
|
77
|
-
cursor: 'default',
|
|
78
|
-
minWidth: '300px',
|
|
79
|
-
minHeight: '50px',
|
|
80
|
-
width: width,
|
|
81
|
-
height: height,
|
|
82
|
-
...style,
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
placeholder
|
|
86
|
-
);
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
export class DesignRender extends React.Component<DesignRenderProp> {
|
|
90
|
-
instanceManager = new ComponentInstanceManager();
|
|
91
|
-
renderRef: React.MutableRefObject<Render | null>;
|
|
92
|
-
dropPlaceholder: Required<DesignRenderProp>['dropPlaceholder'] = DefaultDropPlaceholder;
|
|
93
|
-
|
|
94
|
-
constructor(props: DesignRenderProp) {
|
|
95
|
-
super(props);
|
|
96
|
-
this.renderRef = React.createRef<Render>();
|
|
97
|
-
if (props.dropPlaceholder) {
|
|
98
|
-
this.dropPlaceholder = props.dropPlaceholder;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
componentDidMount(): void {
|
|
103
|
-
this.props.onMount?.(this);
|
|
104
|
-
}
|
|
105
|
-
getPageModel() {
|
|
106
|
-
return this.renderRef.current?.state.pageModel;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
onGetComponent = (comp: any, node: CNode | CRootNode) => {
|
|
110
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
111
|
-
const self = this;
|
|
112
|
-
class DesignWrap extends React.Component<any> {
|
|
113
|
-
_DESIGN_BOX = true;
|
|
114
|
-
_NODE_MODEL = node;
|
|
115
|
-
_NODE_ID = node.id;
|
|
116
|
-
_UNIQUE_ID = `${node.id}_${getRandomStr()}`;
|
|
117
|
-
_STATUS?: 'DESTROY';
|
|
118
|
-
|
|
119
|
-
componentDidMount(): void {
|
|
120
|
-
self.instanceManager.add(node.id, this);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
componentWillUnmount(): void {
|
|
124
|
-
this._STATUS = 'DESTROY';
|
|
125
|
-
self.instanceManager.remove(node.id, this);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
render() {
|
|
129
|
-
const { children = [], onlyRenderChild, ...restProps } = this.props;
|
|
130
|
-
let newChildren = children;
|
|
131
|
-
if (!isArray(children)) {
|
|
132
|
-
newChildren = [children];
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const hasChildren = Boolean(newChildren.filter(Boolean).length);
|
|
136
|
-
if (!hasChildren && (node.material?.value.isContainer || node.value.componentName === InnerComponentNameEnum.ROOT_CONTAINER)) {
|
|
137
|
-
newChildren.push(
|
|
138
|
-
React.createElement(self.dropPlaceholder, {
|
|
139
|
-
node: node,
|
|
140
|
-
})
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
if (onlyRenderChild) {
|
|
144
|
-
return newChildren;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return React.createElement(comp, restProps, ...newChildren);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return React.forwardRef(function ErrorWrap(props: any, ref) {
|
|
151
|
-
return React.createElement(
|
|
152
|
-
ErrorBoundary,
|
|
153
|
-
{
|
|
154
|
-
node,
|
|
155
|
-
targetComponent: DesignWrap,
|
|
156
|
-
},
|
|
157
|
-
React.createElement(DesignWrap, {
|
|
158
|
-
ref,
|
|
159
|
-
...props,
|
|
160
|
-
})
|
|
161
|
-
);
|
|
162
|
-
});
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
rerender(newPage?: CPageDataType | CPage) {
|
|
166
|
-
return this.renderRef.current?.rerender(newPage);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
getInstancesById(id: string, uniqueId?: string): RenderInstance[] {
|
|
170
|
-
let res = [...(this.instanceManager.get(id) || [])];
|
|
171
|
-
if (uniqueId !== undefined) {
|
|
172
|
-
res = res.filter((el) => {
|
|
173
|
-
return uniqueId === el?._UNIQUE_ID;
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
return res;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
getInstanceByDom(el: HTMLHtmlElement | Element): RenderInstance | null {
|
|
180
|
-
const fiberNode = findClosetFiberNode(el);
|
|
181
|
-
if (!fiberNode) {
|
|
182
|
-
return null;
|
|
183
|
-
}
|
|
184
|
-
const containerFiber = findClosetContainerFiberNode(fiberNode);
|
|
185
|
-
return containerFiber?.stateNode || null;
|
|
186
|
-
}
|
|
187
|
-
getDomsById(id: string, selector?: string) {
|
|
188
|
-
const instances = this.getInstancesById(id);
|
|
189
|
-
const doms: HTMLElement[] = [];
|
|
190
|
-
instances?.forEach((el) => {
|
|
191
|
-
if (el?._STATUS === 'DESTROY') {
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
const dom = ReactDOM.findDOMNode(el);
|
|
195
|
-
if (dom && !(dom instanceof Text)) {
|
|
196
|
-
if (selector) {
|
|
197
|
-
// 判断是不是数组
|
|
198
|
-
const list: HTMLElement[] = Array.from(dom.querySelectorAll(selector));
|
|
199
|
-
doms.push(...list);
|
|
200
|
-
} else {
|
|
201
|
-
doms.push(dom as unknown as HTMLElement);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
return doms;
|
|
207
|
-
}
|
|
208
|
-
getDomRectById(id: string, selector?: string) {
|
|
209
|
-
const domList = this.getDomsById(id, selector) as HTMLElement[];
|
|
210
|
-
// 判断是不是数组
|
|
211
|
-
const rectList = domList
|
|
212
|
-
.map((el) => {
|
|
213
|
-
return el?.getBoundingClientRect();
|
|
214
|
-
})
|
|
215
|
-
.filter(Boolean);
|
|
216
|
-
return rectList;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
render() {
|
|
220
|
-
const { props, onGetComponent } = this;
|
|
221
|
-
const { render, ...renderProps } = props;
|
|
222
|
-
if (render) {
|
|
223
|
-
render.ref.current = this;
|
|
224
|
-
}
|
|
225
|
-
return React.createElement(Render, {
|
|
226
|
-
onGetComponent,
|
|
227
|
-
...renderProps,
|
|
228
|
-
// 拦截特殊属性配置, 配合开发模式使用
|
|
229
|
-
processNodeConfigHook: (config, node) => {
|
|
230
|
-
if (node.nodeType !== 'NODE') {
|
|
231
|
-
return config;
|
|
232
|
-
}
|
|
233
|
-
const { props, condition } = config;
|
|
234
|
-
let newProps = { ...props };
|
|
235
|
-
const tempDevState = node.value.configure?.devState || {};
|
|
236
|
-
const fixedPropsObj = node.material?.value.fixedProps;
|
|
237
|
-
if (fixedPropsObj !== undefined) {
|
|
238
|
-
if (isPlainObject(fixedPropsObj)) {
|
|
239
|
-
newProps = {
|
|
240
|
-
...newProps,
|
|
241
|
-
...fixedPropsObj,
|
|
242
|
-
};
|
|
243
|
-
} else if (typeof fixedPropsObj === 'function') {
|
|
244
|
-
const tempProps = fixedPropsObj(newProps);
|
|
245
|
-
newProps = {
|
|
246
|
-
...newProps,
|
|
247
|
-
...tempProps,
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
let newCondition = condition;
|
|
252
|
-
if (tempDevState.condition === false) {
|
|
253
|
-
newCondition = tempDevState.condition as boolean;
|
|
254
|
-
}
|
|
255
|
-
return {
|
|
256
|
-
props: merge(newProps, tempDevState.props || {}),
|
|
257
|
-
condition: newCondition,
|
|
258
|
-
};
|
|
259
|
-
},
|
|
260
|
-
// renderMode: 'design',
|
|
261
|
-
ref: this.renderRef,
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
export type UseDesignRenderReturnType = Pick<UseRenderReturnType, 'rerender'> & {
|
|
267
|
-
ref: React.MutableRefObject<DesignRender | null>;
|
|
268
|
-
getInstancesById: (id: string, uid?: string) => RenderInstance[];
|
|
269
|
-
getInstanceByDom: (dom: HTMLHtmlElement | Element) => RenderInstance | null;
|
|
270
|
-
getDomsById: (id: string, selector?: string) => HTMLElement[];
|
|
271
|
-
getDomRectById: (id: string, selector?: string) => DOMRect | DOMRect[];
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
const findClosetFiberNode = (el: Element | null): SimpleFiberNodeType | null => {
|
|
275
|
-
if (!el) {
|
|
276
|
-
return null;
|
|
277
|
-
}
|
|
278
|
-
const REACT_KEY = Object.keys(el).find((key) => key.startsWith('__reactInternalInstance$') || key.startsWith('__reactFiber$')) || '';
|
|
279
|
-
|
|
280
|
-
if (REACT_KEY) {
|
|
281
|
-
return (el as any)[REACT_KEY];
|
|
282
|
-
} else {
|
|
283
|
-
return findClosetFiberNode(el.parentElement);
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
type SimpleFiberNodeType = {
|
|
288
|
-
return: SimpleFiberNodeType;
|
|
289
|
-
stateNode: (Element | HTMLElement) & RenderInstance;
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
const findClosetContainerFiberNode = (fiberNode: SimpleFiberNodeType): SimpleFiberNodeType | null => {
|
|
293
|
-
if (!fiberNode) {
|
|
294
|
-
return null;
|
|
295
|
-
}
|
|
296
|
-
if (fiberNode?.stateNode?._DESIGN_BOX) {
|
|
297
|
-
return fiberNode;
|
|
298
|
-
} else {
|
|
299
|
-
return findClosetContainerFiberNode(fiberNode.return);
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
export const useDesignRender = (): UseDesignRenderReturnType => {
|
|
304
|
-
const ref = useRef<DesignRender>(null);
|
|
305
|
-
return {
|
|
306
|
-
ref: ref,
|
|
307
|
-
rerender: function (...args) {
|
|
308
|
-
if (ref.current) {
|
|
309
|
-
ref.current.rerender(...args);
|
|
310
|
-
}
|
|
311
|
-
},
|
|
312
|
-
getInstancesById(id, uid) {
|
|
313
|
-
return ref.current?.getInstancesById(id, uid) || [];
|
|
314
|
-
},
|
|
315
|
-
getInstanceByDom(el) {
|
|
316
|
-
return ref.current?.getInstanceByDom(el) || null;
|
|
317
|
-
},
|
|
318
|
-
getDomsById(id: string, selector?: string) {
|
|
319
|
-
return ref.current?.getDomsById(id, selector) || [];
|
|
320
|
-
},
|
|
321
|
-
getDomRectById(id: string, selector?: string) {
|
|
322
|
-
return ref.current?.getDomRectById(id, selector) || [];
|
|
323
|
-
},
|
|
324
|
-
};
|
|
325
|
-
};
|
package/src/core/refManager.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export class RefManager {
|
|
2
|
-
private refMap = new Map();
|
|
3
|
-
|
|
4
|
-
get(id: string) {
|
|
5
|
-
return this.refMap.get(id);
|
|
6
|
-
}
|
|
7
|
-
add(id: string, handle: any) {
|
|
8
|
-
this.refMap.set(id, handle);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
remove(id: string) {
|
|
12
|
-
this.refMap.delete(id);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
destroy() {
|
|
16
|
-
this.refMap.clear();
|
|
17
|
-
}
|
|
18
|
-
}
|
package/src/core/render.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { checkPage, CPage, CPageDataType } from '@chamn/model';
|
|
2
|
-
import { isPlainObject } from 'lodash-es';
|
|
3
|
-
import React, { useRef } from 'react';
|
|
4
|
-
import { InnerComponent } from '../commonComponent';
|
|
5
|
-
import { AdapterOptionType, AdapterType } from './adapter';
|
|
6
|
-
import { RefManager } from './refManager';
|
|
7
|
-
import { RenderInstance } from './type';
|
|
8
|
-
|
|
9
|
-
export type RenderPropsType = {
|
|
10
|
-
page?: CPageDataType;
|
|
11
|
-
pageModel?: CPage;
|
|
12
|
-
adapter: AdapterType;
|
|
13
|
-
render?: UseRenderReturnType;
|
|
14
|
-
ref?: React.MutableRefObject<Render | null>;
|
|
15
|
-
renderMode?: 'design' | 'normal';
|
|
16
|
-
} & Partial<AdapterOptionType>;
|
|
17
|
-
|
|
18
|
-
export class Render extends React.Component<
|
|
19
|
-
RenderPropsType,
|
|
20
|
-
{
|
|
21
|
-
pageModel: CPage;
|
|
22
|
-
}
|
|
23
|
-
> {
|
|
24
|
-
refManager: RefManager;
|
|
25
|
-
// save component instance
|
|
26
|
-
dynamicComponentInstanceMap = new Map<string, RenderInstance>();
|
|
27
|
-
constructor(props: RenderPropsType) {
|
|
28
|
-
super(props);
|
|
29
|
-
this.state = {
|
|
30
|
-
pageModel: props.pageModel || new CPage(props.page!),
|
|
31
|
-
};
|
|
32
|
-
this.refManager = new RefManager();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
getPageModel() {
|
|
36
|
-
return this.state.pageModel;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
componentDidMount(): void {
|
|
40
|
-
const { render } = this.props;
|
|
41
|
-
if (render) {
|
|
42
|
-
render.ref.current = this;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
componentWillUnmount(): void {
|
|
47
|
-
this.refManager.destroy();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
onGetRef: AdapterOptionType['onGetRef'] = (ref, nodeModel, instance) => {
|
|
51
|
-
this.props.onGetRef?.(ref, nodeModel, instance);
|
|
52
|
-
this.dynamicComponentInstanceMap.set(nodeModel.id, instance);
|
|
53
|
-
this.refManager.add(nodeModel.value.refId || nodeModel.id, ref);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
render() {
|
|
57
|
-
const { props } = this;
|
|
58
|
-
const { adapter, onGetComponent, onComponentDestroy, onComponentMount } = props;
|
|
59
|
-
|
|
60
|
-
const { pageModel } = this.state;
|
|
61
|
-
// todo: 加载 page 资源
|
|
62
|
-
// todo: 收集所有的 第三方库
|
|
63
|
-
if (!pageModel) {
|
|
64
|
-
console.warn('pageModel is null');
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
const finalComponents = {
|
|
68
|
-
...InnerComponent,
|
|
69
|
-
...props.components,
|
|
70
|
-
};
|
|
71
|
-
const PageRoot = adapter.pageRender(pageModel, {
|
|
72
|
-
libs: {},
|
|
73
|
-
components: finalComponents,
|
|
74
|
-
onGetRef: this.onGetRef,
|
|
75
|
-
onGetComponent,
|
|
76
|
-
onComponentMount,
|
|
77
|
-
onComponentDestroy,
|
|
78
|
-
$$context: {
|
|
79
|
-
refs: this.refManager,
|
|
80
|
-
},
|
|
81
|
-
renderMode: props.renderMode,
|
|
82
|
-
processNodeConfigHook: props.processNodeConfigHook,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
return PageRoot;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
rerender = (newPage?: CPageDataType | CPage) => {
|
|
89
|
-
this.props.adapter.clear();
|
|
90
|
-
if ((newPage as CPage)?.nodeType === 'PAGE' && newPage) {
|
|
91
|
-
this.setState({
|
|
92
|
-
pageModel: newPage as CPage,
|
|
93
|
-
});
|
|
94
|
-
} else if (isPlainObject(newPage) && checkPage(newPage)) {
|
|
95
|
-
const newP = newPage as CPageDataType;
|
|
96
|
-
this.setState({
|
|
97
|
-
pageModel: new CPage(newP, {
|
|
98
|
-
materials: this.state.pageModel.materialsModel.rawValue,
|
|
99
|
-
}),
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export type UseRenderReturnType = {
|
|
106
|
-
ref: React.MutableRefObject<Render | null>;
|
|
107
|
-
rerender: (newPage: CPageDataType) => void;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
export const useRender = (): UseRenderReturnType => {
|
|
111
|
-
const ref = useRef<Render>(null);
|
|
112
|
-
|
|
113
|
-
return {
|
|
114
|
-
ref: ref,
|
|
115
|
-
rerender: function (...args) {
|
|
116
|
-
if (ref.current) {
|
|
117
|
-
ref.current.rerender(...args);
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
export * from './type';
|
package/src/core/storeManager.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import create, { StateCreator, StoreApi } from 'zustand/vanilla';
|
|
2
|
-
|
|
3
|
-
export class StoreManager {
|
|
4
|
-
storeMap: Map<string, StoreApi<any>> = new Map();
|
|
5
|
-
|
|
6
|
-
addStore(storeName: string, storeState: StateCreator<any>) {
|
|
7
|
-
const store = create(storeState);
|
|
8
|
-
this.storeMap.set(storeName, store);
|
|
9
|
-
(store as any).name = storeName;
|
|
10
|
-
|
|
11
|
-
return store;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
setStore(storeName: string, store: StoreApi<any>) {
|
|
15
|
-
this.storeMap.set(storeName, store);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
removeStore(storeName: string) {
|
|
19
|
-
this.storeMap.delete(storeName);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
getStore(storeName: string) {
|
|
23
|
-
return this.storeMap.get(storeName);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
connect<T extends Record<any, any> = any>(name: string, cb: (newState: T) => void) {
|
|
27
|
-
const store = this.storeMap.get(name);
|
|
28
|
-
if (store) {
|
|
29
|
-
return store.subscribe(cb);
|
|
30
|
-
} else {
|
|
31
|
-
console.warn('store not exits');
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
33
|
-
return () => {};
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// storeManger 赋值以及 获取需要做转换
|
|
38
|
-
getStateSnapshot() {
|
|
39
|
-
const res: Record<string, any> = {};
|
|
40
|
-
this.storeMap.forEach((val, key) => {
|
|
41
|
-
res[key] = {
|
|
42
|
-
state: val.getState(),
|
|
43
|
-
updateState: (newState: any) => {
|
|
44
|
-
val.setState(newState);
|
|
45
|
-
},
|
|
46
|
-
};
|
|
47
|
-
});
|
|
48
|
-
return res;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
destroy() {
|
|
52
|
-
this.storeMap.forEach((val) => {
|
|
53
|
-
val.destroy();
|
|
54
|
-
});
|
|
55
|
-
this.storeMap = new Map();
|
|
56
|
-
}
|
|
57
|
-
}
|
package/src/core/type.ts
DELETED
package/src/util/assetsLoader.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { AssetPackage, getRandomStr } from '@chamn/model';
|
|
2
|
-
import loadjs from 'loadjs';
|
|
3
|
-
|
|
4
|
-
export type Asset = AssetPackage;
|
|
5
|
-
|
|
6
|
-
export class AssetLoader {
|
|
7
|
-
assets: Asset[];
|
|
8
|
-
loadStatus: 'INIT' | 'SUCCESS' | 'ERROR';
|
|
9
|
-
win: Window = window;
|
|
10
|
-
private _onSuccessList: (() => void)[] = [];
|
|
11
|
-
private _onErrorList: ((depsNotFound: string[]) => void)[] = [];
|
|
12
|
-
constructor(
|
|
13
|
-
assets: Asset[],
|
|
14
|
-
options?: {
|
|
15
|
-
window?: Window;
|
|
16
|
-
}
|
|
17
|
-
) {
|
|
18
|
-
this.assets = assets;
|
|
19
|
-
this.loadStatus = 'INIT';
|
|
20
|
-
if (options?.window) {
|
|
21
|
-
this.win = options.window;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
load() {
|
|
26
|
-
const assets = this.assets || [];
|
|
27
|
-
const ids: string[] = [];
|
|
28
|
-
for (let i = 0; i < assets.length; i++) {
|
|
29
|
-
const item = assets[i];
|
|
30
|
-
if (!item.id) {
|
|
31
|
-
item.id = getRandomStr();
|
|
32
|
-
}
|
|
33
|
-
ids.push(item.id);
|
|
34
|
-
const srcList = item.resources.map((el) => el.src);
|
|
35
|
-
loadjs(srcList, item.id, {
|
|
36
|
-
async: false,
|
|
37
|
-
before: (_path, scriptEl) => {
|
|
38
|
-
this.win.document.body.appendChild(scriptEl);
|
|
39
|
-
/* return `false` to bypass default DOM insertion mechanism */
|
|
40
|
-
return false;
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
if (assets.length === 0) {
|
|
45
|
-
this._onSuccessList.forEach((el) => el());
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return new Promise((resolve, reject) => {
|
|
50
|
-
// 在下一个事件循环执行,确保 onSuccess 和 onError 被注册
|
|
51
|
-
loadjs.ready(ids, {
|
|
52
|
-
success: () => {
|
|
53
|
-
this._onSuccessList.forEach((el) => el());
|
|
54
|
-
resolve('');
|
|
55
|
-
},
|
|
56
|
-
error: (depsNotFound) => {
|
|
57
|
-
this._onErrorList.forEach((el) => el(depsNotFound));
|
|
58
|
-
reject(depsNotFound);
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
onSuccess(cb: () => void) {
|
|
65
|
-
this._onSuccessList.push(cb);
|
|
66
|
-
return this;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
onError(cb: () => void) {
|
|
70
|
-
this._onErrorList.push(cb);
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
}
|