mirror of
https://github.com/idrawjs/idraw
synced 2026-05-24 10:08:34 +00:00
feat: @idraw/core add mixin func
This commit is contained in:
parent
be53d035ec
commit
88e8172875
7 changed files with 170 additions and 7 deletions
32
packages/core/__tests__/lib/core-check.test.ts
Normal file
32
packages/core/__tests__/lib/core-check.test.ts
Normal 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);
|
||||
});
|
||||
|
||||
})
|
||||
47
packages/core/__tests__/lib/core-is.test.ts
Normal file
47
packages/core/__tests__/lib/core-is.test.ts
Normal 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);
|
||||
});
|
||||
|
||||
})
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
45
packages/core/src/lib/check.ts
Normal file
45
packages/core/src/lib/check.ts
Normal 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;
|
||||
30
packages/core/src/lib/is.ts
Normal file
30
packages/core/src/lib/is.ts
Normal 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;
|
||||
|
|
@ -6,4 +6,4 @@ export function limitNum(num: number): number {
|
|||
|
||||
export function limitAngle(angle: number): number {
|
||||
return limitNum(angle % 360);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue