Skip to content

Commit

Permalink
feat(editor): 组件表单配置支持函数
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Jun 12, 2024
1 parent a30e8a3 commit 9eb99ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
29 changes: 22 additions & 7 deletions packages/editor/src/services/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) => {
this.setPropsConfig(toLine(type), configs[type]);
});
Expand All @@ -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]);
}

/**
Expand All @@ -88,7 +99,7 @@ class Props extends BaseService {
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) => {
this.setPropsValue(toLine(type), values[type]);
});
Expand All @@ -99,8 +110,12 @@ class Props extends BaseService {
* @param type 组件类型
* @param value 组件初始值
*/
public async setPropsValue(type: string, value: Partial<MNode>) {
this.state.propsValueMap[toLine(type)] = value;
public async setPropsValue(type: string, value: Partial<MNode> | PropsFormValueFunction) {
let v = value;
if (typeof value === 'function') {
v = value({ editorService });
}
this.state.propsValueMap[toLine(type)] = v;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/editor/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,6 @@ export interface EventBus extends EventEmitter {
): this;
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>;

0 comments on commit 9eb99ed

Please sign in to comment.