From 9eb99ed1768def2817718da3e54d4e8424ee0aaf Mon Sep 17 00:00:00 2001 From: roymondchen Date: Wed, 12 Jun 2024 14:54:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(editor):=20=E7=BB=84=E4=BB=B6=E8=A1=A8?= =?UTF-8?q?=E5=8D=95=E9=85=8D=E7=BD=AE=E6=94=AF=E6=8C=81=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/editor/src/services/props.ts | 29 ++++++++++++++++++++------- packages/editor/src/type.ts | 3 +++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/editor/src/services/props.ts b/packages/editor/src/services/props.ts index 51efc24ca..c0eecb05a 100644 --- a/packages/editor/src/services/props.ts +++ b/packages/editor/src/services/props.ts @@ -26,7 +26,13 @@ import type { Id, MComponent, MNode } from '@tmagic/schema'; import { getNodePath, getValueByKeyPath, guid, setValueByKeyPath, toLine } from '@tmagic/utils'; import editorService from '@editor/services/editor'; -import type { AsyncHookPlugin, PropsState, SyncHookPlugin } from '@editor/type'; +import type { + AsyncHookPlugin, + PropsFormConfigFunction, + PropsFormValueFunction, + PropsState, + SyncHookPlugin, +} from '@editor/type'; import { fillConfig } from '@editor/utils/props'; import BaseService from './BaseService'; @@ -60,7 +66,7 @@ class Props extends BaseService { ]); } - public setPropsConfigs(configs: Record) { + public setPropsConfigs(configs: Record) { Object.keys(configs).forEach((type: string) => { this.setPropsConfig(toLine(type), configs[type]); }); @@ -71,8 +77,13 @@ class Props extends BaseService { return fillConfig(config, typeof labelWidth !== 'function' ? labelWidth : '80px'); } - public async setPropsConfig(type: string, config: FormConfig) { - this.state.propsConfigMap[toLine(type)] = await this.fillConfig(Array.isArray(config) ? config : [config]); + public async setPropsConfig(type: string, config: FormConfig | PropsFormConfigFunction) { + let c = config; + if (typeof config === 'function') { + c = config({ editorService }); + } + + this.state.propsConfigMap[toLine(type)] = await this.fillConfig(Array.isArray(c) ? c : [c]); } /** @@ -88,7 +99,7 @@ class Props extends BaseService { return cloneDeep(this.state.propsConfigMap[toLine(type)] || (await this.fillConfig([]))); } - public setPropsValues(values: Record>) { + public setPropsValues(values: Record | PropsFormValueFunction>) { Object.keys(values).forEach((type: string) => { this.setPropsValue(toLine(type), values[type]); }); @@ -99,8 +110,12 @@ class Props extends BaseService { * @param type 组件类型 * @param value 组件初始值 */ - public async setPropsValue(type: string, value: Partial) { - this.state.propsValueMap[toLine(type)] = value; + public async setPropsValue(type: string, value: Partial | PropsFormValueFunction) { + let v = value; + if (typeof value === 'function') { + v = value({ editorService }); + } + this.state.propsValueMap[toLine(type)] = v; } /** diff --git a/packages/editor/src/type.ts b/packages/editor/src/type.ts index 897a162c9..e4bcfcfe2 100644 --- a/packages/editor/src/type.ts +++ b/packages/editor/src/type.ts @@ -744,3 +744,6 @@ export interface EventBus extends EventEmitter { ): this; emit(eventName: Name, ...args: Param): boolean; } + +export type PropsFormConfigFunction = (data: { editorService: EditorService }) => FormConfig; +export type PropsFormValueFunction = (data: { editorService: EditorService }) => Partial;