feat: @idraw/core limit element num and angle

This commit is contained in:
chenshenhai 2021-06-16 15:06:23 +08:00
parent 528644273d
commit 42e0fb609a
8 changed files with 107 additions and 5 deletions

View file

@ -1,6 +1,6 @@
# @idraw/core
* [] Reset board's size
* [x] Reset board's size
* [] Keep two decimals
* [] Limit element's angle between -360~360 deg
* [] Check if the element and elements' attribute of desc is supported

21
jest.cover.js Normal file
View file

@ -0,0 +1,21 @@
const config = require('./jest.config');
module.exports = {
...config,
...{
"collectCoverage": true,
"coverageReporters": [
"clover",
// "html",
"text-summary"
],
"coverageThreshold": {
"global": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
}
},
}
}

View file

@ -11,6 +11,7 @@
"init": "lerna bootstrap --npm-client=cnpm",
"clear": "rm -rf ./packages/*/dist/ & rm -rf ./packages/*/node_modules/",
"jest": "jest --config jest.config.js",
"cover": "jest --config jest.cover.js",
"beforetest": "lerna bootstrap --no-ci && npm run build",
"test": "npm run e2e && npm run jest",
"serve": "http-server ./",

View file

@ -0,0 +1,33 @@
import { TypeData } from '@idraw/types';
import Core from './../../src';
import { getData } from './../data';
import { Element } from './../../src/lib/element';
describe("./lib/element", () => {
document.body.innerHTML = `
<div id="mount"></div>
`;
const opts = {
width: 600,
height: 400,
contextWidth: 600,
contextHeight: 400,
devicePixelRatio: 4
}
const mount = document.querySelector('#mount') as HTMLDivElement;
const core = new Core(mount, opts);
const ctx = core.__getBoardContext();
const data = getData();
test('initData', async () => {
const element = new Element(ctx);
const newData = element.initData(data as TypeData);
expect(newData.elements[0].uuid.length).toStrictEqual(36);
});
});

View file

@ -0,0 +1,33 @@
import { limitNum, limitAngle } from './../../src/lib/value';
describe("./lib/value", () => {
test('limitNum', () => {
const num1 = limitNum(1 / 3);
expect(num1).toStrictEqual(0.33);
const num2 = limitNum(1 / 6);
expect(num2).toStrictEqual(0.17);
const num3 = limitNum(1234 * 1);
expect(num3).toStrictEqual(1234);
});
test('limitAngle', () => {
const num1 = limitAngle(372);
expect(num1).toStrictEqual(12);
const num2 = limitAngle(-372);
expect(num2).toStrictEqual(-12);
const num3 = limitAngle(-372.3333333);
expect(num3).toStrictEqual(-12.33);
const num4 = limitAngle(372.66666666);
expect(num4).toStrictEqual(12.67);
});
})

View file

@ -2,7 +2,7 @@ import {
TypeData, TypePoint, TypeBoardSizeOptions,
TypeHelperWrapperDotDirection,
TypeConfig, TypeConfigStrict,
TypeElement, TypeElemDesc,
TypeElement, TypeElemDesc, TypeContext,
TypeCoreOptions, TypeScreenContext,
} from '@idraw/types';
import Board from '@idraw/board';
@ -215,6 +215,10 @@ class Core {
this[_coreEvent].off(key, callback);
}
__getBoardContext(): TypeContext {
return this[_board].getContext();
}
__getDisplayContext(): CanvasRenderingContext2D {
return this[_board].getDisplayContext()
}

View file

@ -9,6 +9,7 @@ import {
import util from '@idraw/util';
import { rotateElement } from './transform';
import { calcRadian, calcElementCenter, parseRadianToAngle } from './calculate';
import { limitAngle, limitNum } from './value';
const { createUUID } = util.uuid;
@ -167,9 +168,9 @@ export class Element {
}
return {
width: elem.w,
height: elem.h,
angle: elem.angle || 0,
width: limitNum(elem.w),
height: limitNum(elem.h),
angle: limitAngle(elem.angle || 0),
}
}

View file

@ -0,0 +1,9 @@
export function limitNum(num: number): number {
const numStr: string = num.toFixed(2);
return parseFloat(numStr);
}
export function limitAngle(angle: number): number {
return limitNum(angle % 360);
}