feat: @idraw/core add mixin func

This commit is contained in:
chenshenhai 2021-06-16 17:27:52 +08:00
parent be53d035ec
commit 88e8172875
7 changed files with 170 additions and 7 deletions

View file

@ -0,0 +1,32 @@
import Core from './../../src';
describe("@idraw/core static check", () => {
test('Core.check.attrs', () => {
expect(Core.check.attrs({
x: 0,
y: 100,
w: 200,
h: 200,
angle: 0
})).toStrictEqual(true);
expect(Core.check.attrs({
x: 0,
y: 100,
w: -200,
h: 200,
angle: 0
})).toStrictEqual(false);
expect(Core.check.attrs({
x: 0,
y: 100,
w: 200,
h: 200,
angle: -99999
})).toStrictEqual(false);
});
})

View file

@ -0,0 +1,47 @@
import Core from './../../src';
describe("@idraw/core static is", () => {
test('Core.is.number', () => {
expect(Core.is.number(0)).toStrictEqual(true);
expect(Core.is.number(100)).toStrictEqual(true);
expect(Core.is.number(-100)).toStrictEqual(true);
expect(Core.is.number('abc')).toStrictEqual(false);
});
test('Core.is.x', () => {
expect(Core.is.x(0)).toStrictEqual(true);
expect(Core.is.x(100)).toStrictEqual(true);
expect(Core.is.x(-100)).toStrictEqual(true);
expect(Core.is.x('abc')).toStrictEqual(false);
});
test('Core.is.y', () => {
expect(Core.is.y(0)).toStrictEqual(true);
expect(Core.is.y(100)).toStrictEqual(true);
expect(Core.is.y(-100)).toStrictEqual(true);
expect(Core.is.y('abc')).toStrictEqual(false);
});
test('Core.is.w', () => {
expect(Core.is.w(0)).toStrictEqual(true);
expect(Core.is.w(100)).toStrictEqual(true);
expect(Core.is.w(-100)).toStrictEqual(false);
expect(Core.is.w('abc')).toStrictEqual(false);
});
test('Core.is.h', () => {
expect(Core.is.h(0)).toStrictEqual(true);
expect(Core.is.h(100)).toStrictEqual(true);
expect(Core.is.h(-100)).toStrictEqual(false);
expect(Core.is.h('abc')).toStrictEqual(false);
});
test('Core.is.angle', () => {
expect(Core.is.angle(0)).toStrictEqual(true);
expect(Core.is.angle(100)).toStrictEqual(true);
expect(Core.is.angle(-100)).toStrictEqual(true);
expect(Core.is.angle(-370)).toStrictEqual(false);
});
})

View file

@ -13,6 +13,8 @@ import { Helper } from './lib/helper';
import { mergeConfig } from './lib/config';
import { CoreEvent, TypeCoreEventArgMap } from './lib/core-event';
import { parseData } from './lib/parse';
import is from './lib/is';
import check from './lib/check';
const { time } = util;
const { deepClone } = util.data;
@ -58,6 +60,9 @@ class Core {
private [_prevPoint]: TypePoint | null = null;
private [_selectedDotDirection]: TypeHelperWrapperDotDirection | null = null;
static is = is;
static check = check;
constructor(mount: HTMLDivElement, opts: TypeCoreOptions, config?: TypeConfig) {
this[_data] = { elements: [] };
this[_opts] = opts;

View file

@ -0,0 +1,45 @@
import { TypeElementAttrs} from '@idraw/types';
import util from '@idraw/util';
import is from './is';
const { isColorStr } = util.color;
function attrs(
attrs: TypeElementAttrs
): boolean {
const { x, y, w, h, angle } = attrs;
if (!(is.x(x) && is.y(y) && is.w(w) && is.h(h) && is.angle(angle))) {
return false;
}
if (!(angle >= -360 && angle <= 360 )) {
return false;
}
return true;
}
function rectDesc(
desc: any
): boolean {
const { borderColor, borderRadius, borderWidth, color } = desc;
if (typeof borderColor === 'string' && !isColorStr(color)) {
return false;
}
if (typeof borderColor === 'string' && !isColorStr(borderColor)) {
return false;
}
if (typeof borderRadius === 'number' && !is.number(borderRadius)) {
return false;
}
if (typeof borderWidth === 'number' && !is.number(borderWidth)) {
return false;
}
return true;
}
const check = {
attrs,
rectDesc,
}
export default check;

View file

@ -0,0 +1,30 @@
function number(value: any) {
return (typeof value === 'number' && (value > 0 || value <= 0))
}
function x(value: any) {
return number(value);
}
function y(value: any) {
return number(value);
}
function w(value: any) {
return (typeof value === 'number' && value >= 0)
}
function h(value: any) {
return (typeof value === 'number' && value >= 0)
}
function angle(value: any) {
return (typeof value === 'number' && value >= -360 && value <= 360)
}
const is = {
x, y, w, h, angle, number,
}
export default is;

View file

@ -6,4 +6,4 @@ export function limitNum(num: number): number {
export function limitAngle(angle: number): number {
return limitNum(angle % 360);
}
}

View file

@ -1,14 +1,17 @@
// import { TypePaintData } from './paint';
type TypeElement<T extends keyof TypeElemDesc> = {
name?: string;
uuid: string;
type: T;
type TypeElementAttrs = {
x: number;
y: number;
w: number;
h: number;
angle?: number;
angle: number;
}
type TypeElement<T extends keyof TypeElemDesc> = TypeElementAttrs & {
name?: string;
uuid: string;
type: T;
desc: TypeElemDesc[T];
}
@ -28,7 +31,7 @@ type TypeElemDesc = {
}
type TypeElemDescRect = {
color: string;
color?: string;
} & TypeElemBoxDesc
type TypeElemDescText = {
@ -58,6 +61,7 @@ type TypeElemDescSVG = {
// type TypeElemDescPaint = TypePaintData
export {
TypeElementAttrs,
TypeElemDescText,
TypeElemDescRect,
TypeElemDescCircle,