@@ -29,7 +29,7 @@ import { |
29 | 29 | isVariable, |
30 | 30 | isJSSlot, |
31 | 31 | } from '../utils'; |
32 | | -import { IBaseRendererProps, IInfo, IBaseRenderComponent, IBaseRendererContext, IGeneralConstructor, IRendererAppHelper, DataSource } from '../types'; |
| 32 | +import { IBaseRendererProps, INodeInfo, IBaseRenderComponent, IBaseRendererContext, IGeneralConstructor, IRendererAppHelper, DataSource } from '../types'; |
33 | 33 | import { compWrapper } from '../hoc'; |
34 | 34 | import { IComponentConstruct, IComponentHoc, leafWrapper } from '../hoc/leaf'; |
35 | 35 | import logger from '../utils/logger'; |
@@ -59,6 +59,8 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
59 | 59 | PREVIEW: 'preview', |
60 | 60 | }; |
61 | 61 | const OVERLAY_LIST = ['Dialog', 'Overlay', 'Animate', 'ConfigProvider']; |
| 62 | + const DEFAULT_LOOP_ARG_ITEM = 'item'; |
| 63 | + const DEFAULT_LOOP_ARG_INDEX = 'index'; |
62 | 64 | let scopeIdx = 0; |
63 | 65 |
|
64 | 66 | return class BaseRenderer extends Component { |
@@ -377,7 +379,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
377 | 379 | if (engine) { |
378 | 380 | engine.props.onCompGetCtx(schema, this); |
379 | 381 | // 画布场景才需要每次渲染bind自定义方法 |
380 | | - if (engine.props.designMode === 'design') { |
| 382 | + if (this.__designModeIsDesign) { |
381 | 383 | this.__bindCustomMethods(this.props); |
382 | 384 | this.dataSourceMap = this.__dataHelper?.updateConfig(schema.dataSource); |
383 | 385 | } |
@@ -432,7 +434,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
432 | 434 | return this.__createVirtualDom(_children, scope, ({ |
433 | 435 | schema: __schema, |
434 | 436 | Comp: this.__getHocComp(Comp, __schema, scope), |
435 | | - } as IInfo)); |
| 437 | + } as INodeInfo)); |
436 | 438 | }; |
437 | 439 |
|
438 | 440 | /** |
@@ -442,7 +444,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
442 | 444 | * @param parentInfo 父组件的信息,包含schema和Comp |
443 | 445 | * @param idx 为循环渲染的循环Index |
444 | 446 | */ |
445 | | - __createVirtualDom = (originalSchema: NodeData | NodeData[] | undefined, originalScope: any, parentInfo: IInfo, idx: string | number = ''): any => { |
| 447 | + __createVirtualDom = (originalSchema: NodeData | NodeData[] | undefined, originalScope: any, parentInfo: INodeInfo, idx: string | number = ''): any => { |
446 | 448 | if (!originalSchema) { |
447 | 449 | return null; |
448 | 450 | } |
@@ -527,7 +529,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
527 | 529 |
|
528 | 530 | if (schema.loop != null) { |
529 | 531 | const loop = this.__parseData(schema.loop, scope); |
530 | | - const useLoop = isUseLoop(loop, this._designModeIsDesign); |
| 532 | + const useLoop = isUseLoop(loop, this.__designModeIsDesign); |
531 | 533 | if (useLoop) { |
532 | 534 | return this.__createLoopVirtualDom( |
533 | 535 | { |
@@ -543,7 +545,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
543 | 545 | const condition = schema.condition == null ? true : this.__parseData(schema.condition, scope); |
544 | 546 |
|
545 | 547 | // DesignMode 为 design 情况下,需要进入 leaf Hoc,进行相关事件注册 |
546 | | - const displayInHook = engine.props?.designMode === 'design'; |
| 548 | + const displayInHook = this.__designModeIsDesign; |
547 | 549 | if (!condition && !displayInHook) { |
548 | 550 | return null; |
549 | 551 | } |
@@ -579,7 +581,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
579 | 581 | if (engine.props?.designMode) { |
580 | 582 | otherProps.__designMode = engine.props.designMode; |
581 | 583 | } |
582 | | - if (this._designModeIsDesign) { |
| 584 | + if (this.__designModeIsDesign) { |
583 | 585 | otherProps.__tag = Math.random(); |
584 | 586 | } |
585 | 587 | const componentInfo: any = {}; |
@@ -588,7 +590,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
588 | 590 | props: transformArrayToMap(componentInfo.props, 'name'), |
589 | 591 | }) || {}; |
590 | 592 |
|
591 | | - this.componentHoc.forEach((ComponentConstruct: IComponentConstruct) => { |
| 593 | + this.__componentHoc.forEach((ComponentConstruct: IComponentConstruct) => { |
592 | 594 | Comp = ComponentConstruct(Comp, { |
593 | 595 | schema, |
594 | 596 | componentInfo, |
@@ -671,7 +673,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
671 | 673 | } |
672 | 674 | }; |
673 | 675 |
|
674 | | - get componentHoc(): IComponentConstruct[] { |
| 676 | + get __componentHoc(): IComponentConstruct[] { |
675 | 677 | const componentHoc: IComponentHoc[] = [ |
676 | 678 | { |
677 | 679 | designMode: 'design', |
@@ -691,31 +693,31 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
691 | 693 | } |
692 | 694 |
|
693 | 695 | __getSchemaChildrenVirtualDom = (schema: NodeSchema | undefined, scope: any, Comp: any) => { |
694 | | - let _children = this.__getSchemaChildren(schema); |
| 696 | + let children = this.__getSchemaChildren(schema); |
695 | 697 |
|
696 | 698 | // @todo 补完这里的 Element 定义 @承虎 |
697 | | - let children: any = []; |
698 | | - if (/*! isFileSchema(schema) && */_children) { |
699 | | - if (!Array.isArray(_children)) { |
700 | | - _children = [_children]; |
| 699 | + let result: any = []; |
| 700 | + if (children) { |
| 701 | + if (!Array.isArray(children)) { |
| 702 | + children = [children]; |
701 | 703 | } |
702 | 704 |
|
703 | | - _children.forEach((_child: any) => { |
704 | | - const _childVirtualDom = this.__createVirtualDom( |
705 | | - isJSExpression(_child) ? this.parseExpression(_child, scope) : _child, |
| 705 | + children.forEach((child: any) => { |
| 706 | + const childVirtualDom = this.__createVirtualDom( |
| 707 | + isJSExpression(child) ? this.parseExpression(child, scope) : child, |
706 | 708 | scope, |
707 | 709 | { |
708 | 710 | schema, |
709 | 711 | Comp, |
710 | 712 | }, |
711 | 713 | ); |
712 | 714 |
|
713 | | - children.push(_childVirtualDom); |
| 715 | + result.push(childVirtualDom); |
714 | 716 | }); |
715 | 717 | } |
716 | 718 |
|
717 | | - if (children && children.length) { |
718 | | - return children; |
| 719 | + if (result && result.length > 0) { |
| 720 | + return result; |
719 | 721 | } |
720 | 722 | return null; |
721 | 723 | }; |
@@ -734,14 +736,16 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
734 | 736 | }) || {}; |
735 | 737 | }; |
736 | 738 |
|
737 | | - __createLoopVirtualDom = (schema: NodeSchema, scope: any, parentInfo: IInfo, idx: number | string) => { |
| 739 | + __createLoopVirtualDom = (schema: NodeSchema, scope: any, parentInfo: INodeInfo, idx: number | string) => { |
738 | 740 | if (isFileSchema(schema)) { |
739 | 741 | console.warn('file type not support Loop'); |
740 | 742 | return null; |
741 | 743 | } |
742 | | - if (!Array.isArray(schema.loop)) return null; |
743 | | - const itemArg = (schema.loopArgs && schema.loopArgs[0]) || 'item'; |
744 | | - const indexArg = (schema.loopArgs && schema.loopArgs[1]) || 'index'; |
| 744 | + if (!Array.isArray(schema.loop)) { |
| 745 | + return null; |
| 746 | + } |
| 747 | + const itemArg = (schema.loopArgs && schema.loopArgs[0]) || DEFAULT_LOOP_ARG_ITEM; |
| 748 | + const indexArg = (schema.loopArgs && schema.loopArgs[1]) || DEFAULT_LOOP_ARG_INDEX; |
745 | 749 | const { loop } = schema; |
746 | 750 | return loop.map((item: JSONValue | CompositeValue, i: number) => { |
747 | 751 | const loopSelf: any = { |
@@ -761,26 +765,29 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
761 | 765 | }); |
762 | 766 | }; |
763 | 767 |
|
764 | | - get _designModeIsDesign() { |
| 768 | + get __designModeIsDesign() { |
765 | 769 | const { engine } = this.context || {}; |
766 | 770 | return engine?.props?.designMode === 'design'; |
767 | 771 | } |
768 | 772 |
|
769 | | - __parseProps = (originalProps: any, scope: any, path: string, info: IInfo): any => { |
| 773 | + __parseProps = (originalProps: any, scope: any, path: string, info: INodeInfo): any => { |
770 | 774 | let props = originalProps; |
771 | 775 | const { schema, Comp, componentInfo = {} } = info; |
772 | 776 | const propInfo = getValue(componentInfo.props, path); |
773 | | - // FIXME! 将这行逻辑外置,解耦,线上环境不要验证参数,调试环境可以有,通过传参自定义 |
| 777 | + // FIXME: 将这行逻辑外置,解耦,线上环境不要验证参数,调试环境可以有,通过传参自定义 |
774 | 778 | const propType = propInfo?.extra?.propType; |
775 | | - const ignoreParse = schema?.__ignoreParse || []; |
| 779 | + |
776 | 780 | const checkProps = (value: any) => { |
777 | | - if (!propType) return value; |
| 781 | + if (!propType) { |
| 782 | + return value; |
| 783 | + } |
778 | 784 | return checkPropTypes(value, path, propType, componentInfo.name) ? value : undefined; |
779 | 785 | }; |
780 | 786 |
|
781 | 787 | const parseReactNode = (data: any, params: any) => { |
782 | 788 | if (isEmpty(params)) { |
783 | | - return checkProps(this.__createVirtualDom(data, scope, ({ schema, Comp } as IInfo))); |
| 789 | + const virtualDom = this.__createVirtualDom(data, scope, ({ schema, Comp } as INodeInfo)); |
| 790 | + return checkProps(virtualDom); |
784 | 791 | } |
785 | 792 | return checkProps((...argValues: any[]) => { |
786 | 793 | const args: any = {}; |
@@ -794,44 +801,35 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
794 | 801 | }); |
795 | 802 | } |
796 | 803 | args.__proto__ = scope; |
797 | | - return scope.__createVirtualDom(data, args, { schema, Comp }); |
| 804 | + return scope.__createVirtualDom(data, args, ({ schema, Comp } as INodeInfo)); |
798 | 805 | }); |
799 | 806 | }; |
800 | 807 |
|
801 | | - // 判断是否需要解析变量 |
802 | | - if ( |
803 | | - ignoreParse.some((item: any) => { |
804 | | - if (item instanceof RegExp) { |
805 | | - return item.test(path); |
806 | | - } |
807 | | - return item === path; |
808 | | - }) |
809 | | - ) { |
810 | | - return checkProps(props); |
811 | | - } |
812 | 808 | if (isJSExpression(props)) { |
813 | 809 | props = this.parseExpression(props, scope); |
814 | 810 | // 只有当变量解析出来为模型结构的时候才会继续解析 |
815 | | - if (!isSchema(props) && !isJSSlot(props)) return checkProps(props); |
| 811 | + if (!isSchema(props) && !isJSSlot(props)) { |
| 812 | + return checkProps(props); |
| 813 | + } |
816 | 814 | } |
817 | 815 |
|
818 | | - const handleLegaoI18n = (innerProps: any) => innerProps[innerProps.use || 'zh_CN']; |
| 816 | + const handleI18nData = (innerProps: any) => innerProps[innerProps.use || 'zh_CN']; |
819 | 817 |
|
820 | | - // 兼容乐高设计态 i18n 数据 |
| 818 | + // @LEGACY 兼容老平台设计态 i18n 数据 |
821 | 819 | if (isI18nData(props)) { |
822 | | - const i18nProp = handleLegaoI18n(props); |
| 820 | + const i18nProp = handleI18nData(props); |
823 | 821 | if (i18nProp) { |
824 | 822 | props = i18nProp; |
825 | 823 | } else { |
826 | 824 | return parseI18n(props, scope); |
827 | 825 | } |
828 | 826 | } |
829 | 827 |
|
830 | | - // 兼容乐高设计态的变量绑定 |
| 828 | + // @LEGACY 兼容老平台设计态的变量绑定 |
831 | 829 | if (isVariable(props)) { |
832 | 830 | props = props.value; |
833 | 831 | if (isI18nData(props)) { |
834 | | - props = handleLegaoI18n(props); |
| 832 | + props = handleI18nData(props); |
835 | 833 | } |
836 | 834 | } |
837 | 835 |
|
@@ -840,15 +838,15 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
840 | 838 | } |
841 | 839 | if (isJSSlot(props)) { |
842 | 840 | const { params, value } = props; |
843 | | - if (!isSchema(value) || isEmpty(value)) return undefined; |
| 841 | + if (!isSchema(value) || isEmpty(value)) { |
| 842 | + return undefined; |
| 843 | + } |
844 | 844 | return parseReactNode(value, params); |
845 | 845 | } |
| 846 | + |
846 | 847 | // 兼容通过componentInfo判断的情况 |
847 | 848 | if (isSchema(props)) { |
848 | | - const isReactNodeFunction = !!( |
849 | | - propInfo?.type === 'ReactNode' |
850 | | - && propInfo?.props?.type === 'function' |
851 | | - ); |
| 849 | + const isReactNodeFunction = !!(propInfo?.type === 'ReactNode' && propInfo?.props?.type === 'function'); |
852 | 850 |
|
853 | 851 | const isMixinReactNodeFunction = !!( |
854 | 852 | propInfo?.type === 'Mixin' |
@@ -874,7 +872,9 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
874 | 872 | return checkProps(props.bind(scope)); |
875 | 873 | } |
876 | 874 | if (props && typeof props === 'object') { |
877 | | - if (props.$$typeof) return checkProps(props); |
| 875 | + if (props.$$typeof) { |
| 876 | + return checkProps(props); |
| 877 | + } |
878 | 878 | const res: any = {}; |
879 | 879 | forEach(props, (val: any, key: string) => { |
880 | 880 | if (key.startsWith('__')) { |
@@ -921,7 +921,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
921 | 921 |
|
922 | 922 | __getHocComp(OriginalComp: any, schema: any, scope: any) { |
923 | 923 | let Comp = OriginalComp; |
924 | | - this.componentHoc.forEach((ComponentConstruct: IComponentConstruct) => { |
| 924 | + this.__componentHoc.forEach((ComponentConstruct: IComponentConstruct) => { |
925 | 925 | Comp = ComponentConstruct(Comp || Div, { |
926 | 926 | schema, |
927 | 927 | componentInfo: {}, |
@@ -935,8 +935,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
935 | 935 |
|
936 | 936 | __renderComp(OriginalComp: any, ctxProps: object) { |
937 | 937 | let Comp = OriginalComp; |
938 | | - const { __schema } = this.props; |
939 | | - const { __ctx } = this.props; |
| 938 | + const { __schema, __ctx } = this.props; |
940 | 939 | const scope: any = {}; |
941 | 940 | scope.__proto__ = __ctx || this; |
942 | 941 | Comp = this.__getHocComp(Comp, __schema, scope); |
@@ -952,7 +951,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
952 | 951 | return null; |
953 | 952 | } |
954 | 953 |
|
955 | | - if (this._designModeIsDesign) { |
| 954 | + if (this.__designModeIsDesign) { |
956 | 955 | otherProps.__tag = Math.random(); |
957 | 956 | } |
958 | 957 |
|
@@ -973,13 +972,15 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
973 | 972 |
|
974 | 973 | __renderContent(children: any) { |
975 | 974 | const { __schema } = this.props; |
976 | | - const props = this.__parseData(__schema.props); |
977 | | - const { id, className, style = {} } = props; |
| 975 | + const parsedProps = this.__parseData(__schema.props); |
| 976 | + const className = classnames(`lce-${this.__namespace}`, getFileCssName(__schema.fileName), parsedProps.className, this.props.className); |
| 977 | + const style = { ...(parsedProps.style || {}), ...(typeof this.props.style === 'object' ? this.props.style : {}) }; |
| 978 | + const id = this.props.id || parsedProps.id; |
978 | 979 | return createElement('div', { |
979 | 980 | ref: this.__getRef, |
980 | | - className: classnames(`lce-${this.__namespace}`, getFileCssName(__schema.fileName), className, this.props.className), |
981 | | - id: this.props.id || id, |
982 | | - style: { ...style, ...(typeof this.props.style === 'object' ? this.props.style : {}) }, |
| 981 | + className, |
| 982 | + id, |
| 983 | + style, |
983 | 984 | }, children); |
984 | 985 | } |
985 | 986 |
|
@@ -989,8 +990,8 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
989 | 990 | extraComponents = [extraComponents]; |
990 | 991 | } |
991 | 992 |
|
992 | | - const buitin = capitalizeFirstLetter(this.__namespace); |
993 | | - const componentNames = [buitin, ...extraComponents]; |
| 993 | + const builtin = capitalizeFirstLetter(this.__namespace); |
| 994 | + const componentNames = [builtin, ...extraComponents]; |
994 | 995 | return !isSchema(schema) || !componentNames.includes(schema?.componentName ?? ''); |
995 | 996 | }; |
996 | 997 |
|
|