feat: improve parser for component toelement

This commit is contained in:
chenshenhai 2023-06-03 21:41:59 +08:00
parent 6061cffa34
commit 58d9953eb1
3 changed files with 25 additions and 4 deletions

View file

@ -83,6 +83,7 @@ export function createButton(name: string) {
w: 800,
h: 400,
desc: {
bgColor: '#aaaaaa54',
default: createButtonItem('default'),
variants: [createButtonItem('primary'), createButtonItem('secondary')]
}

View file

@ -83,6 +83,7 @@ export function createCheckbox(name: string) {
w: 800,
h: 400,
desc: {
bgColor: '#aaaaaa54',
default: createCheckboxItem('default'),
variants: [createCheckboxItem('primary'), createCheckboxItem('secondary')]
}

View file

@ -1,7 +1,21 @@
import { deepClone } from '@idraw/util';
import type { Data, Element } from '@idraw/types';
import type { Data, Element, ElementType, ElementBaseDesc } from '@idraw/types';
import type { DesignComponent, DesignComponentItem } from '../types';
const baseDescKeys = ['borderWidth', 'borderColor', 'borderRadius', 'shadowColor', 'shadowOffsetX', 'shadowOffsetY', 'shadowBlur', 'color', 'bgColor'];
function parseElementBaseDesc(elem: DesignComponent | DesignComponentItem | Element<ElementType>): ElementBaseDesc {
const baseDesc: ElementBaseDesc = {};
if (elem?.desc) {
Object.keys(elem.desc).forEach((name: string) => {
if (baseDescKeys.includes(name)) {
baseDesc[name as keyof ElementBaseDesc] = (elem.desc as any)?.[name];
}
});
}
return baseDesc;
}
function parseComponentItemToElement(item: DesignComponentItem): Element<'group'> {
const elem: Element<'group'> = {
uuid: item.uuid,
@ -12,8 +26,10 @@ function parseComponentItemToElement(item: DesignComponentItem): Element<'group'
w: item.w,
h: item.h,
desc: {
...item.desc,
children: []
...parseElementBaseDesc(item),
...{
children: []
}
}
};
item.desc?.children?.forEach?.((child) => {
@ -38,7 +54,10 @@ function parseComponentToElement(comp: DesignComponent): Element<'group'> {
w: comp.w,
h: comp.h,
desc: {
children: []
...parseElementBaseDesc(comp),
...{
children: []
}
}
};