@@ -2,8 +2,10 @@ import { BuiltinSimulatorHost, Node, PropChangeOptions } from '@alilc/lowcode-de |
2 | 2 | import { GlobalEvent, TransformStage, NodeSchema } from '@alilc/lowcode-types'; |
3 | 3 | import { isReactComponent, cloneEnumerableProperty } from '@alilc/lowcode-utils'; |
4 | 4 | import { EngineOptions } from '@alilc/lowcode-editor-core'; |
| 5 | +import { debounce } from '../utils/common'; |
5 | 6 | import adapter from '../adapter'; |
6 | 7 | import * as types from '../types/index'; |
| 8 | +import { parseData } from '../utils'; |
7 | 9 |
|
8 | 10 | export interface IComponentHocInfo { |
9 | 11 | schema: any; |
@@ -23,7 +25,10 @@ export interface IComponentHocState { |
23 | 25 | childrenInState: boolean; |
24 | 26 | nodeChildren: any; |
25 | 27 | nodeCacheProps: any; |
| 28 | + /** 控制是否显示隐藏 */ |
26 | 29 | visible: boolean; |
| 30 | + /** 控制是否渲染 */ |
| 31 | + condition: boolean; |
27 | 32 | nodeProps: any; |
28 | 33 | } |
29 | 34 |
|
@@ -58,7 +63,7 @@ enum RerenderType { |
58 | 63 |
|
59 | 64 | // 缓存 Leaf 层组件,防止重新渲染问题 |
60 | 65 | class LeafCache { |
61 | | - constructor(public documentId: string) { |
| 66 | + constructor(public documentId: string, public device: string) { |
62 | 67 | } |
63 | 68 | /** 组件缓存 */ |
64 | 69 | component = new Map(); |
@@ -140,6 +145,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
140 | 145 | const { engine } = baseRenderer.context; |
141 | 146 | const host = baseRenderer.props?.__host; |
142 | 147 | const curDocumentId = baseRenderer.props?.documentId ?? ''; |
| 148 | + const curDevice = baseRenderer.props?.device ?? ''; |
143 | 149 | const getNode = baseRenderer.props?.getNode; |
144 | 150 | const container: BuiltinSimulatorHost = baseRenderer.props?.__container; |
145 | 151 | const setSchemaChangedSymbol = baseRenderer.props?.setSchemaChangedSymbol; |
@@ -152,11 +158,11 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
152 | 158 |
|
153 | 159 | const componentCacheId = schema.id; |
154 | 160 |
|
155 | | - if (!cache || (curDocumentId && curDocumentId !== cache.documentId)) { |
156 | | - cache?.event.forEach((event) => { |
| 161 | + if (!cache || (curDocumentId && curDocumentId !== cache.documentId) || (curDevice && curDevice !== cache.device)) { |
| 162 | + cache?.event.forEach(event => { |
157 | 163 | event.dispose?.forEach((disposeFn: any) => disposeFn && disposeFn()); |
158 | 164 | }); |
159 | | - cache = new LeafCache(curDocumentId); |
| 165 | + cache = new LeafCache(curDocumentId, curDevice); |
160 | 166 | } |
161 | 167 |
|
162 | 168 | if (!isReactComponent(Comp)) { |
@@ -231,11 +237,13 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
231 | 237 | get defaultState() { |
232 | 238 | const { |
233 | 239 | hidden = false, |
| 240 | + condition = true, |
234 | 241 | } = this.leaf?.schema || {}; |
235 | 242 | return { |
236 | 243 | nodeChildren: null, |
237 | 244 | childrenInState: false, |
238 | 245 | visible: !hidden, |
| 246 | + condition: parseData(condition, scope), |
239 | 247 | nodeCacheProps: {}, |
240 | 248 | nodeProps: {}, |
241 | 249 | }; |
@@ -285,58 +293,64 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
285 | 293 | singleRender?: boolean; |
286 | 294 | }; |
287 | 295 |
|
288 | | - shouldRenderSingleNode(): boolean { |
| 296 | + judgeMiniUnitRender() { |
289 | 297 | if (!this.renderUnitInfo) { |
290 | 298 | this.getRenderUnitInfo(); |
291 | 299 | } |
292 | 300 |
|
293 | | - const { renderUnitInfo } = this; |
| 301 | + const renderUnitInfo = this.renderUnitInfo || { |
| 302 | + singleRender: true, |
| 303 | + }; |
294 | 304 |
|
295 | 305 | if (renderUnitInfo.singleRender) { |
296 | | - return true; |
| 306 | + return; |
297 | 307 | } |
298 | 308 |
|
299 | 309 | const ref = cache.ref.get(renderUnitInfo.minimalUnitId); |
300 | 310 |
|
301 | 311 | if (!ref) { |
302 | 312 | __debug('Cant find minimalRenderUnit ref! This make rerender!'); |
303 | 313 | container.rerender(); |
304 | | - return false; |
| 314 | + return; |
305 | 315 | } |
306 | 316 | __debug(`${this.leaf?.componentName}(${this.props.componentId}) need render, make its minimalRenderUnit ${renderUnitInfo.minimalUnitName}(${renderUnitInfo.minimalUnitId})`); |
307 | 317 | ref.makeUnitRender(); |
308 | | - |
309 | | - return false; |
310 | 318 | } |
311 | 319 |
|
312 | 320 | getRenderUnitInfo(leaf = this.leaf) { |
313 | | - if (leaf?.isRoot()) { |
| 321 | + // leaf 在低代码组件中存在 mock 的情况,退出最小渲染单元判断 |
| 322 | + if (!leaf || typeof leaf.isRoot !== 'function') { |
| 323 | + return; |
| 324 | + } |
| 325 | + |
| 326 | + if (leaf.isRoot()) { |
314 | 327 | this.renderUnitInfo = { |
315 | 328 | singleRender: true, |
316 | 329 | ...(this.renderUnitInfo || {}), |
317 | 330 | }; |
318 | 331 | } |
319 | | - if (leaf?.componentMeta.isMinimalRenderUnit) { |
| 332 | + if (leaf.componentMeta.isMinimalRenderUnit) { |
320 | 333 | this.renderUnitInfo = { |
321 | 334 | minimalUnitId: leaf.id, |
322 | 335 | minimalUnitName: leaf.componentName, |
323 | 336 | singleRender: false, |
324 | 337 | }; |
325 | 338 | } |
326 | | - if (leaf?.hasLoop()) { |
| 339 | + if (leaf.hasLoop()) { |
327 | 340 | // 含有循环配置的元素,父元素是最小渲染单元 |
328 | 341 | this.renderUnitInfo = { |
329 | 342 | minimalUnitId: leaf?.parent?.id, |
330 | 343 | minimalUnitName: leaf?.parent?.componentName, |
331 | 344 | singleRender: false, |
332 | 345 | }; |
333 | 346 | } |
334 | | - if (leaf?.parent) { |
| 347 | + if (leaf.parent) { |
335 | 348 | this.getRenderUnitInfo(leaf.parent); |
336 | 349 | } |
337 | 350 | } |
338 | 351 |
|
339 | | - makeUnitRender = () => { |
| 352 | + // 最小渲染单元做防抖处理 |
| 353 | + makeUnitRenderDebounced = debounce(() => { |
340 | 354 | this.beforeRender(RerenderType.MinimalRenderUnit); |
341 | 355 | const schema = this.leaf?.export?.(TransformStage.Render); |
342 | 356 | if (!schema) { |
@@ -355,6 +369,10 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
355 | 369 |
|
356 | 370 | __debug(`${this.leaf?.componentName}(${this.props.componentId}) MinimalRenderUnit Render!`); |
357 | 371 | this.setState(nextState); |
| 372 | + }, 20); |
| 373 | + |
| 374 | + makeUnitRender = () => { |
| 375 | + this.makeUnitRenderDebounced(); |
358 | 376 | }; |
359 | 377 |
|
360 | 378 | componentWillReceiveProps(nextProps: any) { |
@@ -389,6 +407,16 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
389 | 407 | } = propChangeInfo; |
390 | 408 | const node = leaf; |
391 | 409 |
|
| 410 | + if (key === '___condition___') { |
| 411 | + const condition = parseData(newValue, scope); |
| 412 | + __debug(`key is ___condition___, change condition value to [${condition}]`); |
| 413 | + // 条件表达式改变 |
| 414 | + this.setState({ |
| 415 | + condition, |
| 416 | + }); |
| 417 | + return; |
| 418 | + } |
| 419 | + |
392 | 420 | // 如果循坏条件变化,从根节点重新渲染 |
393 | 421 | // 目前多层循坏无法判断需要从哪一层开始渲染,故先粗暴解决 |
394 | 422 | if (key === '___loop___') { |
@@ -398,9 +426,6 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
398 | 426 | cache.component.delete(componentCacheId); |
399 | 427 | return; |
400 | 428 | } |
401 | | - if (!this.shouldRenderSingleNode()) { |
402 | | - return; |
403 | | - } |
404 | 429 | this.beforeRender(RerenderType.PropsChanged); |
405 | 430 | const { state } = this; |
406 | 431 | const { nodeCacheProps } = state; |
@@ -419,6 +444,8 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
419 | 444 | nodeProps, |
420 | 445 | nodeCacheProps, |
421 | 446 | }); |
| 447 | + |
| 448 | + this.judgeMiniUnitRender(); |
422 | 449 | }); |
423 | 450 |
|
424 | 451 | dispose && this.disposeFunctions.push(dispose); |
@@ -433,15 +460,12 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
433 | 460 | return; |
434 | 461 | } |
435 | 462 |
|
436 | | - if (!this.shouldRenderSingleNode()) { |
437 | | - return; |
438 | | - } |
439 | | - |
440 | 463 | __debug(`${leaf?.componentName}[${this.props.componentId}] component trigger onVisibleChange(${flag}) event`); |
441 | 464 | this.beforeRender(RerenderType.VisibleChanged); |
442 | 465 | this.setState({ |
443 | 466 | visible: flag, |
444 | 467 | }); |
| 468 | + this.judgeMiniUnitRender(); |
445 | 469 | }); |
446 | 470 |
|
447 | 471 | dispose && this.disposeFunctions.push(dispose); |
@@ -456,26 +480,23 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
456 | 480 | type, |
457 | 481 | node, |
458 | 482 | } = param || {}; |
459 | | - if (!this.shouldRenderSingleNode()) { |
460 | | - return; |
461 | | - } |
462 | 483 | this.beforeRender(`${RerenderType.ChildChanged}-${type}`, node); |
463 | 484 | // TODO: 缓存同级其他元素的 children。 |
464 | 485 | // 缓存二级 children Next 查询筛选组件有问题 |
465 | 486 | // 缓存一级 children Next Tab 组件有问题 |
466 | | - const nextChild = getChildren(leaf?.export?.(TransformStage.Render) as NodeSchema, scope, Comp); // this.childrenMap |
| 487 | + const nextChild = getChildren(leaf?.export?.(TransformStage.Render) as types.ISchema, scope, Comp); // this.childrenMap |
467 | 488 | __debug(`${schema.componentName}[${this.props.componentId}] component trigger onChildrenChange event`, nextChild); |
468 | 489 | this.setState({ |
469 | 490 | nodeChildren: nextChild, |
470 | 491 | childrenInState: true, |
471 | 492 | }); |
| 493 | + this.judgeMiniUnitRender(); |
472 | 494 | }); |
473 | | - |
474 | 495 | dispose && this.disposeFunctions.push(dispose); |
475 | 496 | } |
476 | 497 |
|
477 | 498 | componentWillUnmount() { |
478 | | - this.disposeFunctions.forEach((fn) => fn()); |
| 499 | + this.disposeFunctions.forEach(fn => fn()); |
479 | 500 | } |
480 | 501 |
|
481 | 502 | get hasChildren(): boolean { |
@@ -509,7 +530,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, { |
509 | 530 | } |
510 | 531 |
|
511 | 532 | render() { |
512 | | - if (!this.state.visible) { |
| 533 | + if (!this.state.visible || !this.state.condition) { |
513 | 534 | return null; |
514 | 535 | } |
515 | 536 |
|
|