mirror of
https://github.com/idrawjs/idraw
synced 2026-05-24 01:58:27 +00:00
feat: @idraw/core receive any type data
This commit is contained in:
parent
f04f66f092
commit
8cdffeb8ce
3 changed files with 46 additions and 4 deletions
6
packages/core/src/constant/element.ts
Normal file
6
packages/core/src/constant/element.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
export const elementTypes = {
|
||||
'text': true,
|
||||
'rect': true,
|
||||
'image': true,
|
||||
'svg': true,
|
||||
}
|
||||
|
|
@ -16,12 +16,12 @@ import { Element } from './lib/element';
|
|||
import { Helper } from './lib/helper';
|
||||
import { mergeConfig } from './lib/config';
|
||||
import { CoreEvent, TypeCoreEventArgMap } from './lib/core-event';
|
||||
import { parseData } from './lib/parse';
|
||||
|
||||
const { time } = util;
|
||||
const { deepClone } = util.data;
|
||||
const { createUUID } = util.uuid;
|
||||
|
||||
|
||||
enum Mode {
|
||||
NULL = 'null',
|
||||
SELECT_ELEMENT = 'select-element',
|
||||
|
|
@ -141,7 +141,7 @@ class Core {
|
|||
return deepClone(this[_data]);
|
||||
}
|
||||
|
||||
initData(data: TypeData): void {
|
||||
initData(data: any | TypeData): void {
|
||||
if (this[_hasInitedData] === true) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -150,8 +150,8 @@ class Core {
|
|||
this[_hasInitedData] = true;
|
||||
}
|
||||
|
||||
setData(data: TypeData): void {
|
||||
this[_data] = this[_element].initData(deepClone(data));
|
||||
setData(data: any | TypeData): void {
|
||||
this[_data] = this[_element].initData(deepClone(parseData(data)));
|
||||
this.draw();
|
||||
}
|
||||
|
||||
|
|
|
|||
36
packages/core/src/lib/parse.ts
Normal file
36
packages/core/src/lib/parse.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { TypeData, TypeElement, TypeElemDesc } from '@idraw/types';
|
||||
import { elementTypes } from './../constant/element';
|
||||
|
||||
export function parseData(data: any): TypeData {
|
||||
const result: TypeData = {
|
||||
elements: [],
|
||||
};
|
||||
if (Array.isArray(data?.elements)) {
|
||||
data?.elements.forEach((elem: any = {}) => {
|
||||
if (isElement(elem)) {
|
||||
result.elements.push(elem);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (typeof data.bgColor === 'string') {
|
||||
result.bgColor = data.bgColor;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function isElement(
|
||||
elem: TypeElement<keyof TypeElemDesc>
|
||||
): boolean{
|
||||
if (!(isNumber(elem.x) && isNumber(elem.y) && isNumber(elem.w) && isNumber(elem.h))) {
|
||||
return false;
|
||||
}
|
||||
if (!(typeof elem.type === 'string' && Object.keys(elementTypes).includes(elem.type))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
function isNumber(num: any) {
|
||||
return (num >= 0 || num < 0)
|
||||
}
|
||||
Loading…
Reference in a new issue