From 68ddca93f5ead30110a4f6a98dfe724c9fe7d983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=9B=E7=9A=93?= Date: Tue, 1 Jun 2021 10:01:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20initial=20/=20initi?= =?UTF-8?q?alValue=20=E5=9C=A8=E5=A4=8D=E5=88=B6=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E4=BC=9A=E8=A2=AB=E9=87=8D=E6=96=B0=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 1 + .../src/bundle/upgrade-metadata.ts | 124 +++++++++++++----- .../src/props-reducers/init-node-reducer.ts | 28 +--- 3 files changed, 91 insertions(+), 62 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e3c2be803..9f3523efb 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -28,5 +28,6 @@ module.exports = { 'eol-last': 0, 'react/no-find-dom-node': 0, 'no-case-declarations': 0, + '@typescript-eslint/indent': 0, } }; diff --git a/packages/vision-polyfill/src/bundle/upgrade-metadata.ts b/packages/vision-polyfill/src/bundle/upgrade-metadata.ts index 8d2d762b5..e38ac7599 100644 --- a/packages/vision-polyfill/src/bundle/upgrade-metadata.ts +++ b/packages/vision-polyfill/src/bundle/upgrade-metadata.ts @@ -1,6 +1,16 @@ import { ComponentType, ReactElement, isValidElement, ComponentClass } from 'react'; -import { isPlainObject, uniqueId } from '@ali/lowcode-utils'; -import { isI18nData, SettingTarget, InitialItem, FilterItem, isJSSlot, ProjectSchema, AutorunItem, isJSBlock } from '@ali/lowcode-types'; +import { isPlainObject, uniqueId, isVariable } from '@ali/lowcode-utils'; +import { + isI18nData, + SettingTarget, + InitialItem, + FilterItem, + isJSSlot, + ProjectSchema, + AutorunItem, + isJSBlock, + isJSExpression, +} from '@ali/lowcode-types'; import { editorCabin, designerCabin } from '@ali/lowcode-engine'; const { SettingField } = designerCabin; @@ -100,11 +110,8 @@ export interface OldPropConfig { } type ResizeHandler = (dragment: any, triggerDirection: string) => boolean; -type ResizeCompositeHandler = { handle: ResizeHandler, availableDirects: string[] | undefined }; -type CanResize = - | boolean - | ResizeHandler - | ResizeCompositeHandler; +type ResizeCompositeHandler = { handle: ResizeHandler; availableDirects: string[] | undefined }; +type CanResize = boolean | ResizeHandler | ResizeCompositeHandler; function isResizeCompositeHandler(resize: CanResize): resize is ResizeCompositeHandler { if ((resize as any).handle) { @@ -185,7 +192,13 @@ export interface OldPrototypeConfig { // => ? canResizing?: CanResize; onResizeStart?: (e: MouseEvent, triggerDirection: string, dragment: Node) => void; - onResize?: (e: MouseEvent, triggerDirection: string, dragment: Node, moveX: number, moveY: number) => void; + onResize?: ( + e: MouseEvent, + triggerDirection: string, + dragment: Node, + moveX: number, + moveY: number, + ) => void; onResizeEnd?: (e: MouseEvent, triggerDirection: string, dragment: Node) => void; devMode?: string; schema?: ProjectSchema; @@ -202,6 +215,44 @@ type SetterGetter = (this: Field, value: any) => ComponentClass; type ReturnBooleanFunction = (this: Field, value: any) => boolean; +/** + * 获取剥去 variable / JSExpression 结构后的值 + * @param propValue + * @returns + */ +function getRawValue(propValue: any) { + if (isVariable(propValue)) { + return propValue.value; + } + if (isJSExpression(propValue)) { + return propValue.mock; + } + return propValue; +} + +/** + * 根据原始值的数据结构(JSExpression / variable / 普通)转换 value,保持相同结构 + * @param originalValue + * @param value + * @returns + */ +function formatPropValue(originalValue: any, value: any) { + if (isJSExpression(originalValue)) { + return { + type: originalValue.type, + value: originalValue.value, + mock: value, + }; + } else if (isVariable(originalValue)) { + return { + type: originalValue.type, + variable: originalValue.variable, + value, + }; + } + return value; +} + export function upgradePropConfig(config: OldPropConfig, collector: ConfigCollector) { const { type, @@ -366,13 +417,14 @@ export function upgradePropConfig(config: OldPropConfig, collector: ConfigCollec initialFn = defaultInitial; } - const v = initialFn.call(field, currentValue, defaults); + const rawValue = getRawValue(currentValue); + const v = initialFn.call(field, rawValue, defaults); if (setterInitial) { - return setterInitial.call(field, v, defaults); + return formatPropValue(currentValue, setterInitial.call(field, v, defaults)); } - return v; + return formatPropValue(currentValue, v); }, }); } @@ -438,8 +490,7 @@ export function upgradePropConfig(config: OldPropConfig, collector: ConfigCollec if (type === 'composite') { const initials: InitialItem[] = []; const objItems = items - ? upgradeConfigure(items, - { + ? upgradeConfigure(items, { addInitial: (item) => { initials.push(item); }, @@ -492,8 +543,8 @@ export function upgradePropConfig(config: OldPropConfig, collector: ConfigCollec componentName: setter, condition: condition ? (field: Field) => { - return condition.call(field, field.getValue()); - } + return condition.call(field, field.getValue()); + } : null, }; }); @@ -540,17 +591,19 @@ type ConfigCollector = { }; function getInitialFromSetter(setter: any) { - return setter && ( - setter.initial || setter.Initial - || (setter.type && (setter.type.initial || setter.type.Initial)) - ) || null; // eslint-disable-line + return ( + (setter && + (setter.initial || + setter.Initial || + (setter.type && (setter.type.initial || setter.type.Initial)))) || + null + ); // eslint-disable-line } function defaultInitial(value: any, defaultValue: any) { return value == null ? defaultValue : value; } - export function upgradeConfigure(items: OldPropConfig[], collector: ConfigCollector) { const configure: any[] = []; let ignoreSlotName: any = null; @@ -569,7 +622,9 @@ export function upgradeConfigure(items: OldPropConfig[], collector: ConfigCollec return configure; } -export function upgradeActions(actions?: Array | ReactElement> | (() => ReactElement)) { +export function upgradeActions( + actions?: Array | ReactElement> | (() => ReactElement), +) { if (!actions) { return null; } @@ -744,8 +799,8 @@ export function upgradeMetadata(oldConfig: OldPrototypeConfig) { experimental.initialChildren = typeof initialChildren === 'function' ? (node: any) => { - return initialChildren.call(node, node.settingEntry); - } + return initialChildren.call(node, node.settingEntry); + } : initialChildren; } if (view) { @@ -827,18 +882,17 @@ export function upgradeMetadata(oldConfig: OldPrototypeConfig) { const initials: InitialItem[] = []; const filters: FilterItem[] = []; const autoruns: AutorunItem[] = []; - const props = upgradeConfigure(configure || [], - { - addInitial: (item) => { - initials.push(item); - }, - addFilter: (item) => { - filters.push(item); - }, - addAutorun: (item) => { - autoruns.push(item); - }, - }); + const props = upgradeConfigure(configure || [], { + addInitial: (item) => { + initials.push(item); + }, + addFilter: (item) => { + filters.push(item); + }, + addAutorun: (item) => { + autoruns.push(item); + }, + }); experimental.initials = initials; experimental.filters = filters; experimental.autoruns = autoruns; diff --git a/packages/vision-polyfill/src/props-reducers/init-node-reducer.ts b/packages/vision-polyfill/src/props-reducers/init-node-reducer.ts index f5f5e66e0..9fa3770bd 100644 --- a/packages/vision-polyfill/src/props-reducers/init-node-reducer.ts +++ b/packages/vision-polyfill/src/props-reducers/init-node-reducer.ts @@ -24,36 +24,10 @@ export function initNodeReducer(props, node) { const initials = node.componentMeta.getMetadata().experimental?.initials; if (initials) { - const getRealValue = (propValue: any) => { - if (isVariable(propValue)) { - return propValue.value; - } - if (isJSExpression(propValue)) { - return propValue.mock; - } - return propValue; - }; initials.forEach(item => { - // FIXME! this implements SettingTarget try { // FIXME! item.name could be 'xxx.xxx' - const ov = newProps[item.name]; - const v = item.initial(node as any, getRealValue(ov)); - if (ov === undefined && v !== undefined) { - newProps[item.name] = v; - } - // 兼容 props 中的属性为 i18n 类型,但是仅提供了一个字符串值,非变量绑定 - if ( - isUseI18NSetter(node.componentMeta.prototype, item.name) && - !isI18NObject(ov) && - !isJSExpression(ov) && - !isJSBlock(ov) && - !isJSSlot(ov) && - !isVariable(ov) && - (isString(v) || isI18NObject(v)) - ) { - newProps[item.name] = convertToI18NObject(v); - } + newProps[item.name] = item.initial(node as any, newProps[item.name]); } catch (e) { if (hasOwnProperty(props, item.name)) { newProps[item.name] = props[item.name];