@idraw/util 0.2.0-alpha.9 → 0.3.0-0.3.0-beta.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/esm/index.d.ts +71 -0
- package/dist/esm/index.js +29 -0
- package/dist/esm/lib/check.d.ts +17 -0
- package/dist/esm/lib/check.js +115 -0
- package/dist/esm/lib/color.d.ts +3 -0
- package/dist/esm/lib/color.js +9 -0
- package/dist/esm/lib/context.d.ts +80 -0
- package/dist/esm/lib/context.js +194 -0
- package/dist/esm/lib/data.d.ts +1 -0
- package/dist/esm/lib/data.js +27 -0
- package/dist/esm/lib/file.d.ts +6 -0
- package/dist/esm/lib/file.js +10 -0
- package/dist/esm/lib/is.d.ts +45 -0
- package/dist/esm/lib/is.js +81 -0
- package/dist/esm/lib/istype.d.ts +13 -0
- package/dist/esm/lib/istype.js +39 -0
- package/dist/esm/lib/loader.d.ts +6 -0
- package/dist/esm/lib/loader.js +41 -0
- package/dist/esm/lib/parser.d.ts +5 -0
- package/dist/esm/lib/parser.js +41 -0
- package/dist/esm/lib/time.d.ts +5 -0
- package/dist/esm/lib/time.js +38 -0
- package/dist/esm/lib/uuid.d.ts +1 -0
- package/dist/esm/lib/uuid.js +6 -0
- package/dist/index.global.js +710 -284
- package/dist/index.global.min.js +1 -0
- package/package.json +10 -9
- package/dist/index.cjs.js +0 -285
- package/dist/index.d.ts +0 -75
- package/dist/index.es.js +0 -283
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { isColorStr } from './color';
|
|
2
|
+
function number(value) {
|
|
3
|
+
return (typeof value === 'number' && (value > 0 || value <= 0));
|
|
4
|
+
}
|
|
5
|
+
function x(value) {
|
|
6
|
+
return number(value);
|
|
7
|
+
}
|
|
8
|
+
function y(value) {
|
|
9
|
+
return number(value);
|
|
10
|
+
}
|
|
11
|
+
function w(value) {
|
|
12
|
+
return (typeof value === 'number' && value >= 0);
|
|
13
|
+
}
|
|
14
|
+
function h(value) {
|
|
15
|
+
return (typeof value === 'number' && value >= 0);
|
|
16
|
+
}
|
|
17
|
+
function angle(value) {
|
|
18
|
+
return (typeof value === 'number' && value >= -360 && value <= 360);
|
|
19
|
+
}
|
|
20
|
+
function borderWidth(value) {
|
|
21
|
+
return w(value);
|
|
22
|
+
}
|
|
23
|
+
function borderRadius(value) {
|
|
24
|
+
return number(value) && value >= 0;
|
|
25
|
+
}
|
|
26
|
+
function color(value) {
|
|
27
|
+
return isColorStr(value);
|
|
28
|
+
}
|
|
29
|
+
function imageURL(value) {
|
|
30
|
+
return (typeof value === 'string' && /^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${value}`));
|
|
31
|
+
}
|
|
32
|
+
function imageBase64(value) {
|
|
33
|
+
return (typeof value === 'string' && /^(data:image\/)/.test(`${value}`));
|
|
34
|
+
}
|
|
35
|
+
function imageSrc(value) {
|
|
36
|
+
return (imageBase64(value) || imageURL(value));
|
|
37
|
+
}
|
|
38
|
+
function svg(value) {
|
|
39
|
+
return (typeof value === 'string' && /^(<svg[\s]{1,}|<svg>)/i.test(`${value}`.trim()) && /<\/[\s]{0,}svg>$/i.test(`${value}`.trim()));
|
|
40
|
+
}
|
|
41
|
+
function html(value) {
|
|
42
|
+
let result = false;
|
|
43
|
+
if (typeof value === 'string') {
|
|
44
|
+
let div = document.createElement('div');
|
|
45
|
+
div.innerHTML = value;
|
|
46
|
+
if (div.children.length > 0) {
|
|
47
|
+
result = true;
|
|
48
|
+
}
|
|
49
|
+
div = null;
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
function text(value) {
|
|
54
|
+
return typeof value === 'string';
|
|
55
|
+
}
|
|
56
|
+
function fontSize(value) {
|
|
57
|
+
return number(value) && value > 0;
|
|
58
|
+
}
|
|
59
|
+
function lineHeight(value) {
|
|
60
|
+
return number(value) && value > 0;
|
|
61
|
+
}
|
|
62
|
+
function strokeWidth(value) {
|
|
63
|
+
return number(value) && value > 0;
|
|
64
|
+
}
|
|
65
|
+
function textAlign(value) {
|
|
66
|
+
return ['center', 'left', 'right'].includes(value);
|
|
67
|
+
}
|
|
68
|
+
function fontFamily(value) {
|
|
69
|
+
return typeof value === 'string' && value.length > 0;
|
|
70
|
+
}
|
|
71
|
+
function fontWeight(value) {
|
|
72
|
+
return ['bold'].includes(value);
|
|
73
|
+
}
|
|
74
|
+
const is = {
|
|
75
|
+
x, y, w, h, angle, number,
|
|
76
|
+
borderWidth, borderRadius, color,
|
|
77
|
+
imageSrc, imageURL, imageBase64, svg, html,
|
|
78
|
+
text, fontSize, lineHeight, textAlign, fontFamily, fontWeight,
|
|
79
|
+
strokeWidth,
|
|
80
|
+
};
|
|
81
|
+
export default is;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const istype: {
|
|
2
|
+
type(data: any, lowerCase?: boolean): string;
|
|
3
|
+
array(data: any): boolean;
|
|
4
|
+
json(data: any): boolean;
|
|
5
|
+
function(data: any): boolean;
|
|
6
|
+
asyncFunction(data: any): boolean;
|
|
7
|
+
string(data: any): boolean;
|
|
8
|
+
number(data: any): boolean;
|
|
9
|
+
undefined(data: any): boolean;
|
|
10
|
+
null(data: any): boolean;
|
|
11
|
+
promise(data: any): boolean;
|
|
12
|
+
};
|
|
13
|
+
export default istype;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
function parsePrototype(data) {
|
|
2
|
+
const typeStr = Object.prototype.toString.call(data) || '';
|
|
3
|
+
const result = typeStr.replace(/(\[object|\])/ig, '').trim();
|
|
4
|
+
return result;
|
|
5
|
+
}
|
|
6
|
+
const istype = {
|
|
7
|
+
type(data, lowerCase) {
|
|
8
|
+
const result = parsePrototype(data);
|
|
9
|
+
return lowerCase === true ? result.toLocaleLowerCase() : result;
|
|
10
|
+
},
|
|
11
|
+
array(data) {
|
|
12
|
+
return parsePrototype(data) === 'Array';
|
|
13
|
+
},
|
|
14
|
+
json(data) {
|
|
15
|
+
return parsePrototype(data) === 'Object';
|
|
16
|
+
},
|
|
17
|
+
function(data) {
|
|
18
|
+
return parsePrototype(data) === 'Function';
|
|
19
|
+
},
|
|
20
|
+
asyncFunction(data) {
|
|
21
|
+
return parsePrototype(data) === 'AsyncFunction';
|
|
22
|
+
},
|
|
23
|
+
string(data) {
|
|
24
|
+
return parsePrototype(data) === 'String';
|
|
25
|
+
},
|
|
26
|
+
number(data) {
|
|
27
|
+
return parsePrototype(data) === 'Number';
|
|
28
|
+
},
|
|
29
|
+
undefined(data) {
|
|
30
|
+
return parsePrototype(data) === 'Undefined';
|
|
31
|
+
},
|
|
32
|
+
null(data) {
|
|
33
|
+
return parsePrototype(data) === 'Null';
|
|
34
|
+
},
|
|
35
|
+
promise(data) {
|
|
36
|
+
return parsePrototype(data) === 'Promise';
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
export default istype;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function loadImage(src: string): Promise<HTMLImageElement>;
|
|
2
|
+
export declare function loadSVG(svg: string): Promise<HTMLImageElement>;
|
|
3
|
+
export declare function loadHTML(html: string, opts: {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
}): Promise<HTMLImageElement>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { parseHTMLToDataURL, parseSVGToDataURL } from './parser';
|
|
11
|
+
const { Image } = window;
|
|
12
|
+
export function loadImage(src) {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const img = new Image;
|
|
15
|
+
img.crossOrigin = 'anonymous';
|
|
16
|
+
img.onload = function () {
|
|
17
|
+
resolve(img);
|
|
18
|
+
};
|
|
19
|
+
img.onabort = reject;
|
|
20
|
+
img.onerror = reject;
|
|
21
|
+
img.src = src;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export function loadSVG(svg) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const dataURL = yield parseSVGToDataURL(svg);
|
|
27
|
+
const image = yield loadImage(dataURL);
|
|
28
|
+
return image;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function filterAmpersand(str) {
|
|
32
|
+
return str.replace(/\&/ig, '&');
|
|
33
|
+
}
|
|
34
|
+
export function loadHTML(html, opts) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
html = filterAmpersand(html);
|
|
37
|
+
const dataURL = yield parseHTMLToDataURL(html, opts);
|
|
38
|
+
const image = yield loadImage(dataURL);
|
|
39
|
+
return image;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export function parseHTMLToDataURL(html, opts) {
|
|
2
|
+
const { width, height } = opts;
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
const _svg = `
|
|
5
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${width || ''}" height = "${height || ''}">
|
|
6
|
+
<foreignObject width="100%" height="100%">
|
|
7
|
+
<div xmlns = "http://www.w3.org/1999/xhtml">
|
|
8
|
+
${html}
|
|
9
|
+
</div>
|
|
10
|
+
</foreignObject>
|
|
11
|
+
</svg>
|
|
12
|
+
`;
|
|
13
|
+
const blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
|
|
14
|
+
const reader = new FileReader();
|
|
15
|
+
reader.readAsDataURL(blob);
|
|
16
|
+
reader.onload = function (event) {
|
|
17
|
+
var _a;
|
|
18
|
+
const base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
|
|
19
|
+
resolve(base64);
|
|
20
|
+
};
|
|
21
|
+
reader.onerror = function (err) {
|
|
22
|
+
reject(err);
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export function parseSVGToDataURL(svg) {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const _svg = svg;
|
|
29
|
+
const blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
|
|
30
|
+
const reader = new FileReader();
|
|
31
|
+
reader.readAsDataURL(blob);
|
|
32
|
+
reader.onload = function (event) {
|
|
33
|
+
var _a;
|
|
34
|
+
const base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
|
|
35
|
+
resolve(base64);
|
|
36
|
+
};
|
|
37
|
+
reader.onerror = function (err) {
|
|
38
|
+
reject(err);
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
type Middleware = (ctx: any, next: Middleware) => any;
|
|
2
|
+
export declare function compose(middleware: Middleware[]): (context: any, next?: Middleware) => any;
|
|
3
|
+
export declare function delay(time: number): Promise<void>;
|
|
4
|
+
export declare function throttle(fn: (...args: any[]) => any, timeout: number): (...args: any[]) => any;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export function compose(middleware) {
|
|
2
|
+
return function (context, next) {
|
|
3
|
+
return dispatch(0);
|
|
4
|
+
function dispatch(i) {
|
|
5
|
+
let fn = middleware[i];
|
|
6
|
+
if (i === middleware.length && next) {
|
|
7
|
+
fn = next;
|
|
8
|
+
}
|
|
9
|
+
if (!fn)
|
|
10
|
+
return Promise.resolve();
|
|
11
|
+
try {
|
|
12
|
+
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
return Promise.reject(err);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function delay(time) {
|
|
21
|
+
return new Promise((resolve) => {
|
|
22
|
+
setTimeout(() => {
|
|
23
|
+
resolve();
|
|
24
|
+
}, time);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
export function throttle(fn, timeout) {
|
|
28
|
+
let timer = -1;
|
|
29
|
+
return function (...args) {
|
|
30
|
+
if (timer > 0) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
timer = setTimeout(() => {
|
|
34
|
+
fn(...args);
|
|
35
|
+
timer = -1;
|
|
36
|
+
}, timeout);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createUUID(): string;
|