feat(editor): 组件表单配置支持函数

This commit is contained in:
roymondchen 2024-06-12 14:54:15 +08:00
parent a30e8a3eea
commit 9eb99ed176
2 changed files with 25 additions and 7 deletions

View File

@ -26,7 +26,13 @@ import type { Id, MComponent, MNode } from '@tmagic/schema';
import { getNodePath, getValueByKeyPath, guid, setValueByKeyPath, toLine } from '@tmagic/utils'; import { getNodePath, getValueByKeyPath, guid, setValueByKeyPath, toLine } from '@tmagic/utils';
import editorService from '@editor/services/editor'; 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 { fillConfig } from '@editor/utils/props';
import BaseService from './BaseService'; import BaseService from './BaseService';
@ -60,7 +66,7 @@ class Props extends BaseService {
]); ]);
} }
public setPropsConfigs(configs: Record<string, FormConfig>) { public setPropsConfigs(configs: Record<string, FormConfig | PropsFormConfigFunction>) {
Object.keys(configs).forEach((type: string) => { Object.keys(configs).forEach((type: string) => {
this.setPropsConfig(toLine(type), configs[type]); this.setPropsConfig(toLine(type), configs[type]);
}); });
@ -71,8 +77,13 @@ class Props extends BaseService {
return fillConfig(config, typeof labelWidth !== 'function' ? labelWidth : '80px'); return fillConfig(config, typeof labelWidth !== 'function' ? labelWidth : '80px');
} }
public async setPropsConfig(type: string, config: FormConfig) { public async setPropsConfig(type: string, config: FormConfig | PropsFormConfigFunction) {
this.state.propsConfigMap[toLine(type)] = await this.fillConfig(Array.isArray(config) ? config : [config]); 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([]))); return cloneDeep(this.state.propsConfigMap[toLine(type)] || (await this.fillConfig([])));
} }
public setPropsValues(values: Record<string, Partial<MNode>>) { public setPropsValues(values: Record<string, Partial<MNode> | PropsFormValueFunction>) {
Object.keys(values).forEach((type: string) => { Object.keys(values).forEach((type: string) => {
this.setPropsValue(toLine(type), values[type]); this.setPropsValue(toLine(type), values[type]);
}); });
@ -99,8 +110,12 @@ class Props extends BaseService {
* @param type * @param type
* @param value * @param value
*/ */
public async setPropsValue(type: string, value: Partial<MNode>) { public async setPropsValue(type: string, value: Partial<MNode> | PropsFormValueFunction) {
this.state.propsValueMap[toLine(type)] = value; let v = value;
if (typeof value === 'function') {
v = value({ editorService });
}
this.state.propsValueMap[toLine(type)] = v;
} }
/** /**

View File

@ -744,3 +744,6 @@ export interface EventBus extends EventEmitter {
): this; ): this;
emit<Name extends keyof EventBusEvent, Param extends EventBusEvent[Name]>(eventName: Name, ...args: Param): boolean; emit<Name extends keyof EventBusEvent, Param extends EventBusEvent[Name]>(eventName: Name, ...args: Param): boolean;
} }
export type PropsFormConfigFunction = (data: { editorService: EditorService }) => FormConfig;
export type PropsFormValueFunction = (data: { editorService: EditorService }) => Partial<MNode>;