1
1
import { INode , IPublicTypePropChangeOptions } from '@alilc/lowcode-designer' ;
2
- import { GlobalEvent , IPublicEnumTransformStage , IPublicTypeNodeSchema , IPublicTypeEngineOptions } from '@alilc/lowcode-types' ;
2
+ import {
3
+ GlobalEvent ,
4
+ IPublicEnumTransformStage ,
5
+ IPublicTypeNodeSchema ,
6
+ IPublicTypeEngineOptions ,
7
+ } from '@alilc/lowcode-types' ;
3
8
import { isReactComponent , cloneEnumerableProperty } from '@alilc/lowcode-utils' ;
4
9
import { debounce } from '../utils/common' ;
5
10
import adapter from '../adapter' ;
@@ -40,7 +45,10 @@ export interface IComponentHoc {
40
45
hoc : IComponentConstruct ;
41
46
}
42
47
43
- export type IComponentConstruct = ( Comp : types . IBaseRenderComponent , info : IComponentHocInfo ) => types . IGeneralConstructor ;
48
+ export type IComponentConstruct = (
49
+ Comp : types . IBaseRenderComponent ,
50
+ info : IComponentHocInfo ,
51
+ ) => types . IGeneralConstructor ;
44
52
45
53
interface IProps {
46
54
_leaf : INode | undefined ;
@@ -66,7 +74,6 @@ enum RerenderType {
66
74
67
75
// 缓存 Leaf 层组件,防止重新渲染问题
68
76
class LeafCache {
69
-
70
77
/** 组件缓存 */
71
78
component = new Map ( ) ;
72
79
@@ -82,24 +89,15 @@ class LeafCache {
82
89
83
90
ref = new Map ( ) ;
84
91
85
- constructor ( public documentId : string , public device : string ) {
86
- }
92
+ constructor ( public documentId : string , public device : string ) { }
87
93
}
88
94
89
95
let cache : LeafCache ;
90
96
91
97
/** 部分没有渲染的 node 节点进行兜底处理 or 渲染方式没有渲染 LeafWrapper */
92
- function initRerenderEvent ( {
93
- schema,
94
- __debug,
95
- container,
96
- getNode,
97
- } : any ) {
98
+ function initRerenderEvent ( { schema, __debug, container, getNode } : any ) {
98
99
const leaf = getNode ?.( schema . id ) ;
99
- if ( ! leaf
100
- || cache . event . get ( schema . id ) ?. clear
101
- || leaf === cache . event . get ( schema . id )
102
- ) {
100
+ if ( ! leaf || cache . event . get ( schema . id ) ?. clear || leaf === cache . event . get ( schema . id ) ) {
103
101
return ;
104
102
}
105
103
cache . event . get ( schema . id ) ?. dispose . forEach ( ( disposeFn : any ) => disposeFn && disposeFn ( ) ) ;
@@ -114,21 +112,27 @@ function initRerenderEvent({
114
112
if ( ! container . autoRepaintNode ) {
115
113
return ;
116
114
}
117
- __debug ( `${ schema . componentName } [${ schema . id } ] leaf not render in SimulatorRendererView, leaf onPropsChange make rerender` ) ;
115
+ __debug (
116
+ `${ schema . componentName } [${ schema . id } ] leaf not render in SimulatorRendererView, leaf onPropsChange make rerender` ,
117
+ ) ;
118
118
debounceRerender ( ) ;
119
119
} ) ,
120
120
leaf ?. onChildrenChange ?.( ( ) => {
121
121
if ( ! container . autoRepaintNode ) {
122
122
return ;
123
123
}
124
- __debug ( `${ schema . componentName } [${ schema . id } ] leaf not render in SimulatorRendererView, leaf onChildrenChange make rerender` ) ;
124
+ __debug (
125
+ `${ schema . componentName } [${ schema . id } ] leaf not render in SimulatorRendererView, leaf onChildrenChange make rerender` ,
126
+ ) ;
125
127
debounceRerender ( ) ;
126
128
} ) as Function ,
127
129
leaf ?. onVisibleChange ?.( ( ) => {
128
130
if ( ! container . autoRepaintNode ) {
129
131
return ;
130
132
}
131
- __debug ( `${ schema . componentName } [${ schema . id } ] leaf not render in SimulatorRendererView, leaf onVisibleChange make rerender` ) ;
133
+ __debug (
134
+ `${ schema . componentName } [${ schema . id } ] leaf not render in SimulatorRendererView, leaf onVisibleChange make rerender` ,
135
+ ) ;
132
136
debounceRerender ( ) ;
133
137
} ) ,
134
138
] ,
@@ -148,12 +152,10 @@ function clearRerenderEvent(id: string): void {
148
152
}
149
153
150
154
// 给每个组件包裹一个 HOC Leaf,支持组件内部属性变化,自响应渲染
151
- export function leafWrapper ( Comp : types . IBaseRenderComponent , {
152
- schema,
153
- baseRenderer,
154
- componentInfo,
155
- scope,
156
- } : IComponentHocInfo ) {
155
+ export function leafWrapper (
156
+ Comp : types . IBaseRenderComponent ,
157
+ { schema, baseRenderer, componentInfo, scope } : IComponentHocInfo ,
158
+ ) {
157
159
const {
158
160
__debug,
159
161
__getComponentProps : getProps ,
@@ -171,13 +173,18 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
171
173
const runtime = adapter . getRuntime ( ) ;
172
174
const { forwardRef, createElement } = runtime ;
173
175
const Component = runtime . Component as types . IGeneralConstructor <
174
- IComponentHocProps , IComponentHocState
176
+ IComponentHocProps ,
177
+ IComponentHocState
175
178
> ;
176
179
177
180
const componentCacheId = schema . id ;
178
181
179
- if ( ! cache || ( curDocumentId && curDocumentId !== cache . documentId ) || ( curDevice && curDevice !== cache . device ) ) {
180
- cache ?. event . forEach ( event => {
182
+ if (
183
+ ! cache ||
184
+ ( curDocumentId && curDocumentId !== cache . documentId ) ||
185
+ ( curDevice && curDevice !== cache . device )
186
+ ) {
187
+ cache ?. event . forEach ( ( event ) => {
181
188
event . dispose ?. forEach ( ( disposeFn : any ) => disposeFn && disposeFn ( ) ) ;
182
189
} ) ;
183
190
cache = new LeafCache ( curDocumentId , curDevice ) ;
@@ -194,7 +201,11 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
194
201
getNode,
195
202
} ) ;
196
203
197
- if ( curDocumentId && cache . component . has ( componentCacheId ) && ( cache . component . get ( componentCacheId ) . Comp === Comp ) ) {
204
+ if (
205
+ curDocumentId &&
206
+ cache . component . has ( componentCacheId ) &&
207
+ cache . component . get ( componentCacheId ) . Comp === Comp
208
+ ) {
198
209
return cache . component . get ( componentCacheId ) . LeafWrapper ;
199
210
}
200
211
@@ -209,7 +220,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
209
220
210
221
static displayName = schema . componentName ;
211
222
212
- disposeFunctions : Array < ( ( ( ) => void ) | Function ) > = [ ] ;
223
+ disposeFunctions : Array < ( ( ) => void ) | Function > = [ ] ;
213
224
214
225
__component_tag = 'leafWrapper' ;
215
226
@@ -244,7 +255,9 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
244
255
constructor ( props : IProps , context : any ) {
245
256
super ( props , context ) ;
246
257
// 监听以下事件,当变化时更新自己
247
- __debug ( `${ schema . componentName } [${ this . props . componentId } ] leaf render in SimulatorRendererView` ) ;
258
+ __debug (
259
+ `${ schema . componentName } [${ this . props . componentId } ] leaf render in SimulatorRendererView` ,
260
+ ) ;
248
261
clearRerenderEvent ( componentCacheId ) ;
249
262
this . curEventLeaf = this . leaf ;
250
263
@@ -266,7 +279,8 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
266
279
}
267
280
const endTime = Date . now ( ) ;
268
281
const nodeCount = host ?. designer ?. currentDocument ?. getNodeCount ?.( ) ;
269
- const componentName = this . recordInfo . node ?. componentName || this . leaf ?. componentName || 'UnknownComponent' ;
282
+ const componentName =
283
+ this . recordInfo . node ?. componentName || this . leaf ?. componentName || 'UnknownComponent' ;
270
284
editor ?. eventBus . emit ( GlobalEvent . Node . Rerender , {
271
285
componentName,
272
286
time : endTime - this . recordInfo . startTime ,
@@ -297,10 +311,8 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
297
311
}
298
312
299
313
getDefaultState ( nextProps : any ) {
300
- const {
301
- hidden = false ,
302
- condition = true ,
303
- } = nextProps . __inner__ || this . leaf ?. export ?.( IPublicEnumTransformStage . Render ) || { } ;
314
+ const { hidden = false , condition = true } =
315
+ nextProps . __inner__ || this . leaf ?. export ?.( IPublicEnumTransformStage . Render ) || { } ;
304
316
return {
305
317
nodeChildren : null ,
306
318
childrenInState : false ,
@@ -317,6 +329,12 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
317
329
...state ,
318
330
__tag : this . props . __tag ,
319
331
} ) ;
332
+ let style = state . nodeProps . style ?? { } ;
333
+ const { dynamicStyle } = state . nodeProps ;
334
+ if ( typeof dynamicStyle === 'object' ) {
335
+ style = { ...style , ...dynamicStyle } ;
336
+ }
337
+ if ( style && Object . keys ( style ) . length ) state . nodeProps . style = style ;
320
338
super . setState ( state ) ;
321
339
}
322
340
@@ -348,7 +366,9 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
348
366
container ?. rerender ( ) ;
349
367
return ;
350
368
}
351
- __debug ( `${ this . leaf ?. componentName } (${ this . props . componentId } ) need render, make its minimalRenderUnit ${ renderUnitInfo . minimalUnitName } (${ renderUnitInfo . minimalUnitId } )` ) ;
369
+ __debug (
370
+ `${ this . leaf ?. componentName } (${ this . props . componentId } ) need render, make its minimalRenderUnit ${ renderUnitInfo . minimalUnitName } (${ renderUnitInfo . minimalUnitId } )` ,
371
+ ) ;
352
372
ref . makeUnitRender ( ) ;
353
373
}
354
374
@@ -400,20 +420,14 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
400
420
this . curEventLeaf = _leaf ;
401
421
}
402
422
403
- const {
404
- visible,
405
- ...resetState
406
- } = this . getDefaultState ( nextProps ) ;
423
+ const { visible, ...resetState } = this . getDefaultState ( nextProps ) ;
407
424
this . setState ( resetState ) ;
408
425
}
409
426
410
427
/** 监听参数变化 */
411
428
initOnPropsChangeEvent ( leaf = this . leaf ) : void {
412
429
const handlePropsChange = debounce ( ( propChangeInfo : IPublicTypePropChangeOptions ) => {
413
- const {
414
- key,
415
- newValue = null ,
416
- } = propChangeInfo ;
430
+ const { key, newValue = null } = propChangeInfo ;
417
431
const node = leaf ;
418
432
419
433
if ( key === '___condition___' ) {
@@ -439,21 +453,36 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
439
453
this . beforeRender ( RerenderType . PropsChanged ) ;
440
454
const { state } = this ;
441
455
const { nodeCacheProps } = state ;
442
- const nodeProps = getProps ( node ?. export ?.( IPublicEnumTransformStage . Render ) as IPublicTypeNodeSchema , scope , Comp , componentInfo ) ;
443
- if ( key && ! ( key in nodeProps ) && ( key in this . props ) ) {
456
+ const nodeProps = getProps (
457
+ node ?. export ?.( IPublicEnumTransformStage . Render ) as IPublicTypeNodeSchema ,
458
+ scope ,
459
+ Comp ,
460
+ componentInfo ,
461
+ ) ;
462
+ if ( key && ! ( key in nodeProps ) && key in this . props ) {
444
463
// 当 key 在 this.props 中时,且不存在在计算值中,需要用 newValue 覆盖掉 this.props 的取值
445
464
nodeCacheProps [ key ] = newValue ;
446
465
}
447
- __debug ( `${ leaf ?. componentName } [${ this . props . componentId } ] component trigger onPropsChange!` , nodeProps , nodeCacheProps , key , newValue ) ;
448
- this . setState ( 'children' in nodeProps ? {
449
- nodeChildren : nodeProps . children ,
450
- nodeProps,
451
- childrenInState : true ,
452
- nodeCacheProps,
453
- } : {
466
+ __debug (
467
+ `${ leaf ?. componentName } [${ this . props . componentId } ] component trigger onPropsChange!` ,
454
468
nodeProps ,
455
469
nodeCacheProps ,
456
- } ) ;
470
+ key ,
471
+ newValue ,
472
+ ) ;
473
+ this . setState (
474
+ 'children' in nodeProps
475
+ ? {
476
+ nodeChildren : nodeProps . children ,
477
+ nodeProps,
478
+ childrenInState : true ,
479
+ nodeCacheProps,
480
+ }
481
+ : {
482
+ nodeProps,
483
+ nodeCacheProps,
484
+ } ,
485
+ ) ;
457
486
458
487
this . judgeMiniUnitRender ( ) ;
459
488
} ) ;
@@ -479,7 +508,9 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
479
508
return ;
480
509
}
481
510
482
- __debug ( `${ leaf ?. componentName } [${ this . props . componentId } ] component trigger onVisibleChange(${ flag } ) event` ) ;
511
+ __debug (
512
+ `${ leaf ?. componentName } [${ this . props . componentId } ] component trigger onVisibleChange(${ flag } ) event` ,
513
+ ) ;
483
514
this . beforeRender ( RerenderType . VisibleChanged ) ;
484
515
this . setState ( {
485
516
visible : flag ,
@@ -498,16 +529,20 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
498
529
if ( ! this . autoRepaintNode ) {
499
530
return ;
500
531
}
501
- const {
502
- type,
503
- node,
504
- } = param || { } ;
532
+ const { type, node } = param || { } ;
505
533
this . beforeRender ( `${ RerenderType . ChildChanged } -${ type } ` , node ) ;
506
534
// TODO: 缓存同级其他元素的 children。
507
535
// 缓存二级 children Next 查询筛选组件有问题
508
536
// 缓存一级 children Next Tab 组件有问题
509
- const nextChild = getChildren ( leaf ?. export ?.( IPublicEnumTransformStage . Render ) as types . ISchema , scope , Comp ) ;
510
- __debug ( `${ schema . componentName } [${ this . props . componentId } ] component trigger onChildrenChange event` , nextChild ) ;
537
+ const nextChild = getChildren (
538
+ leaf ?. export ?.( IPublicEnumTransformStage . Render ) as types . ISchema ,
539
+ scope ,
540
+ Comp ,
541
+ ) ;
542
+ __debug (
543
+ `${ schema . componentName } [${ this . props . componentId } ] component trigger onChildrenChange event` ,
544
+ nextChild ,
545
+ ) ;
511
546
this . setState ( {
512
547
nodeChildren : nextChild ,
513
548
childrenInState : true ,
@@ -518,7 +553,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
518
553
}
519
554
520
555
componentWillUnmount ( ) {
521
- this . disposeFunctions . forEach ( fn => fn ( ) ) ;
556
+ this . disposeFunctions . forEach ( ( fn ) => fn ( ) ) ;
522
557
}
523
558
524
559
get hasChildren ( ) : boolean {
@@ -556,10 +591,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
556
591
return null ;
557
592
}
558
593
559
- const {
560
- forwardedRef,
561
- ...rest
562
- } = this . props ;
594
+ const { forwardedRef, ...rest } = this . props ;
563
595
564
596
const compProps = {
565
597
...rest ,
@@ -597,4 +629,4 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
597
629
} ) ;
598
630
599
631
return LeafWrapper ;
600
- }
632
+ }
0 commit comments