|
1 | | -import { BuiltinSimulatorHost, Node, IPublicTypePropChangeOptions } from '@alilc/lowcode-designer'; |
| 1 | +import { INode, IPublicTypePropChangeOptions } from '@alilc/lowcode-designer'; |
2 | 2 | import { GlobalEvent, IPublicEnumTransformStage, IPublicTypeNodeSchema, IPublicTypeEngineOptions } from '@alilc/lowcode-types'; |
3 | 3 | import { isReactComponent, cloneEnumerableProperty } from '@alilc/lowcode-utils'; |
4 | 4 | import { debounce } from '../utils/common'; |
@@ -16,15 +16,17 @@ export interface IComponentHocProps { |
16 | 16 | __tag: any; |
17 | 17 | componentId: any; |
18 | 18 | _leaf: any; |
19 | | - forwardedRef: any; |
| 19 | + forwardedRef?: any; |
20 | 20 | } |
21 | 21 |
|
22 | 22 | export interface IComponentHocState { |
23 | 23 | childrenInState: boolean; |
24 | 24 | nodeChildren: any; |
25 | 25 | nodeCacheProps: any; |
| 26 | + |
26 | 27 | /** 控制是否显示隐藏 */ |
27 | 28 | visible: boolean; |
| 29 | + |
28 | 30 | /** 控制是否渲染 */ |
29 | 31 | condition: boolean; |
30 | 32 | nodeProps: any; |
@@ -40,15 +42,17 @@ export interface IComponentHoc { |
40 | 42 | export type IComponentConstruct = (Comp: types.IBaseRenderComponent, info: IComponentHocInfo) => types.IGeneralConstructor; |
41 | 43 |
|
42 | 44 | interface IProps { |
43 | | - _leaf: Node | undefined; |
| 45 | + _leaf: INode | undefined; |
44 | 46 |
|
45 | 47 | visible: boolean; |
46 | 48 |
|
47 | | - componentId?: number; |
| 49 | + componentId: number; |
48 | 50 |
|
49 | | - children?: Node[]; |
| 51 | + children?: INode[]; |
50 | 52 |
|
51 | | - __tag?: number; |
| 53 | + __tag: number; |
| 54 | + |
| 55 | + forwardedRef?: any; |
52 | 56 | } |
53 | 57 |
|
54 | 58 | enum RerenderType { |
@@ -61,8 +65,7 @@ enum RerenderType { |
61 | 65 |
|
62 | 66 | // 缓存 Leaf 层组件,防止重新渲染问题 |
63 | 67 | class LeafCache { |
64 | | - constructor(public documentId: string, public device: string) { |
65 | | - } |
| 68 | + |
66 | 69 | /** 组件缓存 */ |
67 | 70 | component = new Map(); |
68 | 71 |
|
@@ -77,6 +80,9 @@ class LeafCache { |
77 | 80 | event = new Map(); |
78 | 81 |
|
79 | 82 | ref = new Map(); |
| 83 | + |
| 84 | + constructor(public documentId: string, public device: string) { |
| 85 | + } |
80 | 86 | } |
81 | 87 |
|
82 | 88 | let cache: LeafCache; |
@@ -155,11 +161,11 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
155 | 161 | const curDocumentId = baseRenderer.props?.documentId ?? ''; |
156 | 162 | const curDevice = baseRenderer.props?.device ?? ''; |
157 | 163 | const getNode = baseRenderer.props?.getNode; |
158 | | - const container: BuiltinSimulatorHost = baseRenderer.props?.__container; |
| 164 | + const container = baseRenderer.props?.__container; |
159 | 165 | const setSchemaChangedSymbol = baseRenderer.props?.setSchemaChangedSymbol; |
160 | 166 | const editor = host?.designer?.editor; |
161 | 167 | const runtime = adapter.getRuntime(); |
162 | | - const { forwardRef } = runtime; |
| 168 | + const { forwardRef, createElement } = runtime; |
163 | 169 | const Component = runtime.Component as types.IGeneralConstructor< |
164 | 170 | IComponentHocProps, IComponentHocState |
165 | 171 | >; |
@@ -192,14 +198,64 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
192 | 198 | recordInfo: { |
193 | 199 | startTime?: number | null; |
194 | 200 | type?: string; |
195 | | - node?: Node; |
| 201 | + node?: INode; |
196 | 202 | } = {}; |
| 203 | + |
| 204 | + private curEventLeaf: INode | undefined; |
| 205 | + |
197 | 206 | static displayName = schema.componentName; |
198 | 207 |
|
199 | 208 | disposeFunctions: Array<((() => void) | Function)> = []; |
200 | 209 |
|
201 | 210 | __component_tag = 'leafWrapper'; |
202 | 211 |
|
| 212 | + renderUnitInfo: { |
| 213 | + minimalUnitId?: string; |
| 214 | + minimalUnitName?: string; |
| 215 | + singleRender?: boolean; |
| 216 | + }; |
| 217 | + |
| 218 | + // 最小渲染单元做防抖处理 |
| 219 | + makeUnitRenderDebounced = debounce(() => { |
| 220 | + this.beforeRender(RerenderType.MinimalRenderUnit); |
| 221 | + const schema = this.leaf?.export?.(IPublicEnumTransformStage.Render); |
| 222 | + if (!schema) { |
| 223 | + return; |
| 224 | + } |
| 225 | + const nextProps = getProps(schema, scope, Comp, componentInfo); |
| 226 | + const children = getChildren(schema, scope, Comp); |
| 227 | + const nextState = { |
| 228 | + nodeProps: nextProps, |
| 229 | + nodeChildren: children, |
| 230 | + childrenInState: true, |
| 231 | + }; |
| 232 | + if ('children' in nextProps) { |
| 233 | + nextState.nodeChildren = nextProps.children; |
| 234 | + } |
| 235 | + |
| 236 | + __debug(`${this.leaf?.componentName}(${this.props.componentId}) MinimalRenderUnit Render!`); |
| 237 | + this.setState(nextState); |
| 238 | + }, 20); |
| 239 | + |
| 240 | + constructor(props: IProps, context: any) { |
| 241 | + super(props, context); |
| 242 | + // 监听以下事件,当变化时更新自己 |
| 243 | + __debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`); |
| 244 | + clearRerenderEvent(componentCacheId); |
| 245 | + this.curEventLeaf = this.leaf; |
| 246 | + |
| 247 | + cache.ref.set(componentCacheId, { |
| 248 | + makeUnitRender: this.makeUnitRender, |
| 249 | + }); |
| 250 | + |
| 251 | + let cacheState = cache.state.get(componentCacheId); |
| 252 | + if (!cacheState || cacheState.__tag !== props.__tag) { |
| 253 | + cacheState = this.getDefaultState(props); |
| 254 | + } |
| 255 | + |
| 256 | + this.state = cacheState; |
| 257 | + } |
| 258 | + |
203 | 259 | recordTime = () => { |
204 | 260 | if (!this.recordInfo.startTime) { |
205 | 261 | return; |
@@ -216,6 +272,14 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
216 | 272 | this.recordInfo.startTime = null; |
217 | 273 | }; |
218 | 274 |
|
| 275 | + makeUnitRender = () => { |
| 276 | + this.makeUnitRenderDebounced(); |
| 277 | + }; |
| 278 | + |
| 279 | + get autoRepaintNode() { |
| 280 | + return container?.autoRepaintNode; |
| 281 | + } |
| 282 | + |
219 | 283 | componentDidUpdate() { |
220 | 284 | this.recordTime(); |
221 | 285 | } |
@@ -243,27 +307,6 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
243 | 307 | }; |
244 | 308 | } |
245 | 309 |
|
246 | | - constructor(props: IProps, context: any) { |
247 | | - super(props, context); |
248 | | - // 监听以下事件,当变化时更新自己 |
249 | | - __debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`); |
250 | | - clearRerenderEvent(componentCacheId); |
251 | | - this.curEventLeaf = this.leaf; |
252 | | - |
253 | | - cache.ref.set(componentCacheId, { |
254 | | - makeUnitRender: this.makeUnitRender, |
255 | | - }); |
256 | | - |
257 | | - let cacheState = cache.state.get(componentCacheId); |
258 | | - if (!cacheState || cacheState.__tag !== props.__tag) { |
259 | | - cacheState = this.getDefaultState(props); |
260 | | - } |
261 | | - |
262 | | - this.state = cacheState; |
263 | | - } |
264 | | - |
265 | | - private curEventLeaf: Node | undefined; |
266 | | - |
267 | 310 | setState(state: any) { |
268 | 311 | cache.state.set(componentCacheId, { |
269 | 312 | ...this.state, |
@@ -274,23 +317,13 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
274 | 317 | } |
275 | 318 |
|
276 | 319 | /** 由于内部属性变化,在触发渲染前,会执行该函数 */ |
277 | | - beforeRender(type: string, node?: Node): void { |
| 320 | + beforeRender(type: string, node?: INode): void { |
278 | 321 | this.recordInfo.startTime = Date.now(); |
279 | 322 | this.recordInfo.type = type; |
280 | 323 | this.recordInfo.node = node; |
281 | 324 | setSchemaChangedSymbol?.(true); |
282 | 325 | } |
283 | 326 |
|
284 | | - renderUnitInfo: { |
285 | | - minimalUnitId?: string; |
286 | | - minimalUnitName?: string; |
287 | | - singleRender?: boolean; |
288 | | - }; |
289 | | - |
290 | | - get autoRepaintNode() { |
291 | | - return container.autoRepaintNode; |
292 | | - } |
293 | | - |
294 | 327 | judgeMiniUnitRender() { |
295 | 328 | if (!this.renderUnitInfo) { |
296 | 329 | this.getRenderUnitInfo(); |
@@ -308,7 +341,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
308 | 341 |
|
309 | 342 | if (!ref) { |
310 | 343 | __debug('Cant find minimalRenderUnit ref! This make rerender!'); |
311 | | - container.rerender(); |
| 344 | + container?.rerender(); |
312 | 345 | return; |
313 | 346 | } |
314 | 347 | __debug(`${this.leaf?.componentName}(${this.props.componentId}) need render, make its minimalRenderUnit ${renderUnitInfo.minimalUnitName}(${renderUnitInfo.minimalUnitId})`); |
@@ -321,7 +354,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
321 | 354 | return; |
322 | 355 | } |
323 | 356 |
|
324 | | - if (leaf.isRoot()) { |
| 357 | + if (leaf.isRootNode) { |
325 | 358 | this.renderUnitInfo = { |
326 | 359 | singleRender: true, |
327 | 360 | ...(this.renderUnitInfo || {}), |
@@ -347,32 +380,6 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
347 | 380 | } |
348 | 381 | } |
349 | 382 |
|
350 | | - // 最小渲染单元做防抖处理 |
351 | | - makeUnitRenderDebounced = debounce(() => { |
352 | | - this.beforeRender(RerenderType.MinimalRenderUnit); |
353 | | - const schema = this.leaf?.export?.(IPublicEnumTransformStage.Render); |
354 | | - if (!schema) { |
355 | | - return; |
356 | | - } |
357 | | - const nextProps = getProps(schema, scope, Comp, componentInfo); |
358 | | - const children = getChildren(schema, scope, Comp); |
359 | | - const nextState = { |
360 | | - nodeProps: nextProps, |
361 | | - nodeChildren: children, |
362 | | - childrenInState: true, |
363 | | - }; |
364 | | - if ('children' in nextProps) { |
365 | | - nextState.nodeChildren = nextProps.children; |
366 | | - } |
367 | | - |
368 | | - __debug(`${this.leaf?.componentName}(${this.props.componentId}) MinimalRenderUnit Render!`); |
369 | | - this.setState(nextState); |
370 | | - }, 20); |
371 | | - |
372 | | - makeUnitRender = () => { |
373 | | - this.makeUnitRenderDebounced(); |
374 | | - }; |
375 | | - |
376 | 383 | componentWillReceiveProps(nextProps: any) { |
377 | 384 | let { componentId } = nextProps; |
378 | 385 | if (nextProps.__tag === this.props.__tag) { |
@@ -420,7 +427,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
420 | 427 | // 目前多层循坏无法判断需要从哪一层开始渲染,故先粗暴解决 |
421 | 428 | if (key === '___loop___') { |
422 | 429 | __debug('key is ___loop___, render a page!'); |
423 | | - container.rerender(); |
| 430 | + container?.rerender(); |
424 | 431 | // 由于 scope 变化,需要清空缓存,使用新的 scope |
425 | 432 | cache.component.delete(componentCacheId); |
426 | 433 | return; |
@@ -536,7 +543,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
536 | 543 | return []; |
537 | 544 | } |
538 | 545 |
|
539 | | - get leaf(): Node | undefined { |
| 546 | + get leaf(): INode | undefined { |
540 | 547 | if (this.props._leaf?.isMock) { |
541 | 548 | // 低代码组件作为一个整体更新,其内部的组件不需要监听相关事件 |
542 | 549 | return undefined; |
@@ -570,13 +577,12 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
570 | 577 | } |
571 | 578 | } |
572 | 579 |
|
573 | | - let LeafWrapper = forwardRef((props: any, ref: any) => ( |
574 | | - // @ts-ignore |
575 | | - <LeafHoc |
576 | | - {...props} |
577 | | - forwardedRef={ref} |
578 | | - /> |
579 | | - )); |
| 580 | + let LeafWrapper = forwardRef((props: any, ref: any) => { |
| 581 | + return createElement(LeafHoc, { |
| 582 | + ...props, |
| 583 | + forwardedRef: ref, |
| 584 | + }); |
| 585 | + }); |
580 | 586 |
|
581 | 587 | LeafWrapper = cloneEnumerableProperty(LeafWrapper, Comp); |
582 | 588 |
|
|