chore(editor): 删除无用代码

This commit is contained in:
roymondchen 2022-08-22 19:46:05 +08:00 committed by jia000
parent 9b0db4a807
commit 3cdcca3b0b
3 changed files with 18 additions and 72 deletions

View File

@ -20,9 +20,8 @@ import { reactive } from 'vue';
import { cloneDeep, mergeWith } from 'lodash-es';
import type { FormConfig } from '@tmagic/form';
import type { Id, MComponent, MNode, MPage } from '@tmagic/schema';
import { NodeType } from '@tmagic/schema';
import { isPop, toLine } from '@tmagic/utils';
import type { MComponent, MNode } from '@tmagic/schema';
import { toLine } from '@tmagic/utils';
import type { PropsState } from '../type';
import { DEFAULT_CONFIG, fillConfig } from '../utils/props';
@ -125,19 +124,6 @@ class Props extends BaseService {
};
}
/**
* GUID-
* @param digit 8
* @returns
*/
guid(digit = 8): string {
return 'x'.repeat(digit).replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
public async createId(type: string | number): Promise<string> {
return `${type}_${this.guid()}`;
}
@ -147,19 +133,12 @@ class Props extends BaseService {
* @param {Object} config
*/
/* eslint no-param-reassign: ["error", { "props": false }] */
public async setNewItemId(config: MNode, parent?: MPage) {
const oldId = config.id;
public async setNewItemId(config: MNode) {
config.id = await this.createId(config.type || 'component');
// 只有弹窗在页面下的一级子元素才有效
if (isPop(config) && parent?.type === NodeType.PAGE) {
updatePopId(oldId, config.id, parent);
}
if (config.items && Array.isArray(config.items)) {
for (const item of config.items) {
await this.setNewItemId(item, config as MPage);
await this.setNewItemId(item);
}
}
@ -186,32 +165,21 @@ class Props extends BaseService {
name: type,
};
}
/**
* GUID-
* @param digit 8
* @returns
*/
private guid(digit = 8): string {
return 'x'.repeat(digit).replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
}
/**
* id换测复制出来的弹窗的id
* @param {number} oldId id
* @param {number} popId id
* @param {Object} pageConfig
*/
const updatePopId = (oldId: Id, popId: Id, pageConfig: MPage) => {
pageConfig.items?.forEach((config) => {
if (config.pop === oldId) {
config.pop = popId;
return;
}
if (config.popId === oldId) {
config.popId = popId;
return;
}
if (Array.isArray(config.items)) {
updatePopId(oldId, popId, config as MPage);
}
});
};
export type PropsService = Props;
export default new Props();

View File

@ -42,7 +42,7 @@ export const beforePaste = async (position: PastePosition, config: MNode[]): Pro
pastePosition.top = configItem.style?.top - referenceTop + pastePosition.top;
}
const pasteConfig = await propsService.setNewItemId(configItem, editorService.get('root'));
const pasteConfig = await propsService.setNewItemId(configItem);
if (pasteConfig.style) {
const { left, top } = pasteConfig.style;

View File

@ -38,28 +38,6 @@ describe('setNewItemId', () => {
expect(config.id === 1).toBeFalsy();
expect(config.items[0].id === 2).toBeFalsy();
});
test('pop', async () => {
const config = {
id: 1,
type: NodeType.PAGE,
items: [
{
type: 'button',
id: 2,
pop: 3,
},
{
type: 'pop',
id: 3,
},
],
};
await props.setNewItemId(config);
expect(config.items[0].pop === 3).toBeFalsy();
expect(config.items[1].id === 3).toBeFalsy();
expect(config.items[1].id === config.items[0].pop).toBeTruthy();
});
});
test('getDefaultValue', async () => {