fix(editor): 新增组件id不对

This commit is contained in:
roymondchen 2022-07-27 16:44:30 +08:00 committed by jia000
parent ec1bf1dcb7
commit fff587d9eb
5 changed files with 51 additions and 35 deletions

View File

@ -506,7 +506,7 @@ class Editor extends BaseService {
return;
}
await propsService.setNewItemId(config, this.get('root'));
config = await propsService.setNewItemId(config, this.get('root'));
if (config.style) {
config.style = {
...config.style,

View File

@ -25,7 +25,7 @@ import { NodeType } from '@tmagic/schema';
import { isPop, toLine } from '@tmagic/utils';
import type { PropsState } from '@editor/type';
import { DEFAULT_CONFIG, fillConfig, getDefaultPropsValue } from '@editor/utils/props';
import { DEFAULT_CONFIG, fillConfig } from '@editor/utils/props';
import BaseService from './BaseService';
@ -36,7 +36,15 @@ class Props extends BaseService {
});
constructor() {
super(['setPropsConfig', 'getPropsConfig', 'setPropsValue', 'getPropsValue', 'createId', 'setNewItemId']);
super([
'setPropsConfig',
'getPropsConfig',
'setPropsValue',
'getPropsValue',
'createId',
'setNewItemId',
'getDefaultPropsValue',
]);
}
public setPropsConfigs(configs: Record<string, FormConfig>) {
@ -99,12 +107,20 @@ class Props extends BaseService {
return value;
}
const data = cloneDeep(defaultValue as any);
await this.setNewItemId(data);
const [id, defaultPropsValue, data] = await Promise.all([
this.createId(type),
this.getDefaultPropsValue(type),
this.setNewItemId(
cloneDeep({
type,
...defaultValue,
} as any),
),
]);
return {
...getDefaultPropsValue(type, await this.createId(type)),
id,
...defaultPropsValue,
...mergeWith(cloneDeep(this.state.propsValueMap[type] || {}), data),
};
}
@ -133,6 +149,29 @@ class Props extends BaseService {
await this.setNewItemId(item, config as MPage);
}
}
return config;
}
/**
*
* @param type
* @returns Object
*/
public async getDefaultPropsValue(type: string) {
return ['page', 'container'].includes(type)
? {
type,
layout: 'absolute',
style: {},
name: type,
items: [],
}
: {
type,
style: {},
name: type,
};
}
}

View File

@ -237,25 +237,3 @@ export const fillConfig = (config: FormConfig = []) => [
// 默认组件属性表单配置
export const DEFAULT_CONFIG: FormConfig = fillConfig([]);
/**
*
* @param type
* @returns Object
*/
export const getDefaultPropsValue = (type: string, id: string) =>
['page', 'container'].includes(type)
? {
type,
id,
layout: 'absolute',
style: {},
name: type,
items: [],
}
: {
type,
id,
style: {},
name: type,
};

View File

@ -61,3 +61,8 @@ describe('setNewItemId', () => {
expect(config.items[1].id === config.items[0].pop).toBeTruthy();
});
});
test('getDefaultValue', async () => {
const value = await props.getDefaultPropsValue('text');
expect(value.type).toBe('text');
});

View File

@ -42,10 +42,4 @@ describe('util form', () => {
expect(config[0].items[0].items.length - defaultConfig[0].items[0].items.length).toBe(1);
});
test('getDefaultValue', () => {
const value = props.getDefaultPropsValue('text', '1');
expect(value.id).toBe('1');
expect(value.type).toBe('text');
});
});