@idraw/util 0.4.2 → 1.0.0-alpha.1
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/dist/esm/converter/data-svg.d.ts +2 -0
- package/dist/esm/converter/data-svg.js +19 -0
- package/dist/esm/converter/material-svg.d.ts +2 -0
- package/dist/esm/converter/material-svg.js +63 -0
- package/dist/esm/converter/svg-material.d.ts +2 -0
- package/dist/esm/converter/svg-material.js +188 -0
- package/dist/esm/index.d.ts +32 -21
- package/dist/esm/index.js +32 -21
- package/dist/esm/static.d.ts +1 -0
- package/dist/esm/static.js +1 -0
- package/dist/esm/tool/file.js +2 -2
- package/dist/esm/tool/html.js +27 -27
- package/dist/esm/tool/id.d.ts +1 -0
- package/dist/esm/tool/id.js +4 -0
- package/dist/esm/tool/image.js +6 -6
- package/dist/esm/tool/path-to-box.d.ts +3 -0
- package/dist/esm/tool/path-to-box.js +353 -0
- package/dist/esm/tool/path-to-command.d.ts +4 -0
- package/dist/esm/tool/path-to-command.js +392 -0
- package/dist/esm/tool/path.d.ts +18 -0
- package/dist/esm/tool/path.js +421 -0
- package/dist/esm/tool/svg-path.d.ts +3 -0
- package/dist/esm/{view → tool}/svg-path.js +4 -16
- package/dist/esm/tool/time.d.ts +2 -2
- package/dist/esm/tool/time.js +4 -4
- package/dist/esm/tool/uuid.d.ts +1 -1
- package/dist/esm/tool/uuid.js +6 -6
- package/dist/esm/view/box.d.ts +0 -2
- package/dist/esm/view/box.js +1 -173
- package/dist/esm/view/canvas.js +3 -3
- package/dist/esm/view/check.d.ts +8 -7
- package/dist/esm/view/check.js +34 -34
- package/dist/esm/view/config.d.ts +0 -9
- package/dist/esm/view/config.js +1 -71
- package/dist/esm/view/context2d.d.ts +10 -3
- package/dist/esm/view/context2d.js +39 -18
- package/dist/esm/view/controller.d.ts +5 -5
- package/dist/esm/view/controller.js +105 -102
- package/dist/esm/view/data.d.ts +2 -2
- package/dist/esm/view/data.js +65 -65
- package/dist/esm/view/dom.d.ts +23 -0
- package/dist/esm/view/dom.js +190 -0
- package/dist/esm/view/flat.d.ts +2 -2
- package/dist/esm/view/flat.js +53 -53
- package/dist/esm/view/group.d.ts +3 -3
- package/dist/esm/view/group.js +25 -28
- package/dist/esm/view/handle-material.d.ts +24 -0
- package/dist/esm/view/{handle-element.js → handle-material.js} +98 -75
- package/dist/esm/view/is.d.ts +10 -12
- package/dist/esm/view/is.js +15 -19
- package/dist/esm/view/load.d.ts +2 -2
- package/dist/esm/view/load.js +7 -7
- package/dist/esm/view/material.d.ts +40 -0
- package/dist/esm/view/material.js +476 -0
- package/dist/esm/view/middleware.d.ts +1 -0
- package/dist/esm/view/middleware.js +14 -2
- package/dist/esm/view/modify-record.d.ts +4 -4
- package/dist/esm/view/modify-record.js +2 -2
- package/dist/esm/view/parser.d.ts +2 -2
- package/dist/esm/view/parser.js +3 -3
- package/dist/esm/view/path.d.ts +2 -0
- package/dist/esm/view/path.js +30 -0
- package/dist/esm/view/point-move-material.d.ts +5 -0
- package/dist/esm/view/{point-move-element.js → point-move-material.js} +6 -6
- package/dist/esm/view/point.d.ts +3 -8
- package/dist/esm/view/point.js +1 -21
- package/dist/esm/view/position.d.ts +9 -9
- package/dist/esm/view/position.js +1 -1
- package/dist/esm/view/rect.d.ts +2 -2
- package/dist/esm/view/rect.js +4 -4
- package/dist/esm/view/resize-material.d.ts +4 -0
- package/dist/esm/view/resize-material.js +266 -0
- package/dist/esm/view/rotate.d.ts +11 -11
- package/dist/esm/view/rotate.js +24 -24
- package/dist/esm/view/static.d.ts +13 -0
- package/dist/esm/view/static.js +89 -0
- package/dist/esm/view/styles.d.ts +11 -0
- package/dist/esm/view/styles.js +78 -0
- package/dist/esm/view/vertex.d.ts +8 -8
- package/dist/esm/view/vertex.js +28 -28
- package/dist/esm/view/view-box.d.ts +2 -6
- package/dist/esm/view/view-box.js +8 -64
- package/dist/esm/view/view-calc.d.ts +23 -20
- package/dist/esm/view/view-calc.js +104 -93
- package/dist/esm/view/view-content.d.ts +2 -2
- package/dist/esm/view/view-content.js +34 -34
- package/dist/index.global.js +3144 -1548
- package/dist/index.global.min.js +1 -1
- package/package.json +1 -1
- package/dist/esm/view/element.d.ts +0 -35
- package/dist/esm/view/element.js +0 -471
- package/dist/esm/view/handle-element.d.ts +0 -24
- package/dist/esm/view/point-move-element.d.ts +0 -5
- package/dist/esm/view/resize-element.d.ts +0 -4
- package/dist/esm/view/resize-element.js +0 -255
- package/dist/esm/view/svg-path.d.ts +0 -10
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { HTMLCSSProps, HTMLProps } from '@idraw/types';
|
|
2
|
+
type DOMElement = HTMLElement | SVGElement | Element;
|
|
3
|
+
export declare function addClassName<T extends DOMElement = DOMElement>(elem: T, classNameList: string[]): void;
|
|
4
|
+
export declare function removeClassName<T extends HTMLElement = HTMLElement>(elem: T, classNameList: string[]): void;
|
|
5
|
+
export declare function setHTMLCSSProps(elem: HTMLElement, cssProps: HTMLCSSProps): HTMLElement;
|
|
6
|
+
export declare function assembleHTMLElement<T extends DOMElement = HTMLElement>(root: T, props?: HTMLProps, children?: string | HTMLElement | SVGElement | Element | Array<HTMLElement | SVGElement | Element | string>): T;
|
|
7
|
+
export declare function createHTMLElement<K extends keyof HTMLElementTagNameMap = 'div'>(tagName: K, props?: HTMLProps, children?: Array<Element | HTMLElement>): HTMLElementTagNameMap[K];
|
|
8
|
+
export declare function sanitizeHTMLStr(htmlString: string): string;
|
|
9
|
+
export declare function parseHTMLStr(htmlString: string): HTMLElement;
|
|
10
|
+
export declare function getHTMLElementRectInPage(elem: HTMLElement): {
|
|
11
|
+
pageX: number;
|
|
12
|
+
pageY: number;
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
};
|
|
16
|
+
export declare const bubbleHTMLElement: ($active: HTMLElement, $root: HTMLElement, targetProps: {
|
|
17
|
+
[key: string]: string | true;
|
|
18
|
+
}) => HTMLElement | null;
|
|
19
|
+
export declare function isPointInMiddlewareElement(e: Event, opts: {
|
|
20
|
+
$root: HTMLElement | null;
|
|
21
|
+
rootClassName: string;
|
|
22
|
+
}): boolean;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { istype } from '../tool/istype';
|
|
13
|
+
const aliasKeys = {
|
|
14
|
+
className: 'class',
|
|
15
|
+
};
|
|
16
|
+
const defaultPixelKeys = [
|
|
17
|
+
'width',
|
|
18
|
+
'height',
|
|
19
|
+
'left',
|
|
20
|
+
'right',
|
|
21
|
+
'top',
|
|
22
|
+
'bottom',
|
|
23
|
+
'margin',
|
|
24
|
+
'marginLeft',
|
|
25
|
+
'marginRight',
|
|
26
|
+
'marginTop',
|
|
27
|
+
'marginBottom',
|
|
28
|
+
'padding',
|
|
29
|
+
'paddingLeft',
|
|
30
|
+
'paddingRight',
|
|
31
|
+
'paddingTop',
|
|
32
|
+
'paddingBottom',
|
|
33
|
+
];
|
|
34
|
+
const parseCSSValue = (key, value) => {
|
|
35
|
+
const str = typeof value === 'number' && defaultPixelKeys.includes(key) ? `${value}px` : value;
|
|
36
|
+
return str;
|
|
37
|
+
};
|
|
38
|
+
export function addClassName(elem, classNameList) {
|
|
39
|
+
classNameList.forEach((className) => {
|
|
40
|
+
if (!className) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const name = className.trim();
|
|
44
|
+
const strs = name.split(' ');
|
|
45
|
+
strs.forEach((str) => {
|
|
46
|
+
if (str && !elem.classList.contains(str)) {
|
|
47
|
+
elem.classList.add(str);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
export function removeClassName(elem, classNameList) {
|
|
53
|
+
classNameList.forEach((className) => {
|
|
54
|
+
if (!className) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (elem.classList.contains(className)) {
|
|
58
|
+
elem.classList.remove(className);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export function setHTMLCSSProps(elem, cssProps) {
|
|
63
|
+
Object.keys(cssProps).forEach((key) => {
|
|
64
|
+
const value = cssProps[key];
|
|
65
|
+
elem.style[key] = parseCSSValue(key, value);
|
|
66
|
+
});
|
|
67
|
+
return elem;
|
|
68
|
+
}
|
|
69
|
+
function appendPropsToHTMLElement(elem, props) {
|
|
70
|
+
Object.entries(props).forEach(([attrKey, attrValue]) => {
|
|
71
|
+
const key = aliasKeys[attrKey] ? aliasKeys[attrKey] : attrKey;
|
|
72
|
+
const attributeName = key.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
73
|
+
const value = attrValue;
|
|
74
|
+
if (key === 'style') {
|
|
75
|
+
if (istype.json(attrValue)) {
|
|
76
|
+
setHTMLCSSProps(elem, attrValue);
|
|
77
|
+
}
|
|
78
|
+
else if (typeof attrValue === 'string') {
|
|
79
|
+
elem.setAttribute(attributeName, attrValue);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else if (key === 'class' && typeof attrValue === 'string') {
|
|
83
|
+
addClassName(elem, attrValue.split(' '));
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
let v = value;
|
|
87
|
+
if (v === undefined || v === null) {
|
|
88
|
+
v = '';
|
|
89
|
+
}
|
|
90
|
+
elem.setAttribute(attributeName, String(v));
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return elem;
|
|
94
|
+
}
|
|
95
|
+
export function assembleHTMLElement(root, props, children) {
|
|
96
|
+
const _a = props || {}, { children: propsChildren } = _a, restProps = __rest(_a, ["children"]);
|
|
97
|
+
const widgetChildren = children || propsChildren;
|
|
98
|
+
appendPropsToHTMLElement(root, restProps || {});
|
|
99
|
+
if (Array.isArray(widgetChildren)) {
|
|
100
|
+
widgetChildren === null || widgetChildren === void 0 ? void 0 : widgetChildren.forEach((child) => {
|
|
101
|
+
if (child instanceof Element || typeof child === 'string') {
|
|
102
|
+
root.append(child);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
else if (widgetChildren instanceof Element || typeof widgetChildren === 'string') {
|
|
107
|
+
root.append(widgetChildren);
|
|
108
|
+
}
|
|
109
|
+
return root;
|
|
110
|
+
}
|
|
111
|
+
export function createHTMLElement(tagName, props, children) {
|
|
112
|
+
const elem = document.createElement(tagName);
|
|
113
|
+
return assembleHTMLElement(elem, props, children);
|
|
114
|
+
}
|
|
115
|
+
export function sanitizeHTMLStr(htmlString) {
|
|
116
|
+
const dangerousTagsRegex = /<(script|iframe|object|embed)\b[^<]*(?:(?!<\/\1>)<[^<]*)*<\/\1>/gi;
|
|
117
|
+
return htmlString.replace(dangerousTagsRegex, '');
|
|
118
|
+
}
|
|
119
|
+
export function parseHTMLStr(htmlString) {
|
|
120
|
+
const sanitizedString = sanitizeHTMLStr(htmlString);
|
|
121
|
+
const template = document.createElement('template');
|
|
122
|
+
template.innerHTML = sanitizedString.trim();
|
|
123
|
+
const element = template.content.firstElementChild;
|
|
124
|
+
return element;
|
|
125
|
+
}
|
|
126
|
+
export function getHTMLElementRectInPage(elem) {
|
|
127
|
+
const rect = elem.getBoundingClientRect();
|
|
128
|
+
const scrollX = window.pageXOffset || document.documentElement.scrollLeft;
|
|
129
|
+
const scrollY = window.pageYOffset || document.documentElement.scrollTop;
|
|
130
|
+
return {
|
|
131
|
+
pageX: rect.left + scrollX,
|
|
132
|
+
pageY: rect.top + scrollY,
|
|
133
|
+
width: rect.width,
|
|
134
|
+
height: rect.height,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
export const bubbleHTMLElement = ($active, $root, targetProps) => {
|
|
138
|
+
let $current = $active;
|
|
139
|
+
const propsKeys = Object.keys(targetProps);
|
|
140
|
+
const isTarget = () => {
|
|
141
|
+
if (!$current) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
for (let i = 0; i < propsKeys.length; i++) {
|
|
145
|
+
const key = propsKeys[i];
|
|
146
|
+
const value = targetProps[key];
|
|
147
|
+
if (typeof key === 'string' && key && typeof value === 'string' && value) {
|
|
148
|
+
if (key === 'className' && ($current === null || $current === void 0 ? void 0 : $current.classList.contains(value))) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if ($current.getAttribute(key) === value) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
else if (typeof key === 'string' && key && typeof value === 'boolean') {
|
|
157
|
+
if ($current.hasAttribute(key) && value === true) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
};
|
|
165
|
+
while ($current) {
|
|
166
|
+
if (isTarget()) {
|
|
167
|
+
return $current;
|
|
168
|
+
}
|
|
169
|
+
if ($root === $current) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
if (!$root.contains($current)) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
$current = $current.parentElement;
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
};
|
|
179
|
+
export function isPointInMiddlewareElement(e, opts) {
|
|
180
|
+
const { $root, rootClassName } = opts;
|
|
181
|
+
if (!$root) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
const $target = e.target;
|
|
185
|
+
const $elem = bubbleHTMLElement($target, $root, { className: rootClassName });
|
|
186
|
+
if ($elem) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
package/dist/esm/view/flat.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function
|
|
1
|
+
import type { StrictMaterial } from '@idraw/types';
|
|
2
|
+
export declare function flatMaterialList(list: StrictMaterial[]): StrictMaterial[];
|
package/dist/esm/view/flat.js
CHANGED
|
@@ -9,12 +9,12 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
9
9
|
}
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
|
-
import {
|
|
13
|
-
import { limitAngle, parseAngleToRadian,
|
|
14
|
-
function
|
|
12
|
+
import { calcMaterialVertexesInGroup } from './vertex';
|
|
13
|
+
import { limitAngle, parseAngleToRadian, calcMaterialCenterFromVertexes, rotatePoint } from './rotate';
|
|
14
|
+
function flatMaterialSize(mtrlSize, opts) {
|
|
15
15
|
const { groupQueue } = opts;
|
|
16
|
-
let { x, y } =
|
|
17
|
-
const {
|
|
16
|
+
let { x, y } = mtrlSize;
|
|
17
|
+
const { width, height, angle = 0 } = mtrlSize;
|
|
18
18
|
let totalAngle = 0;
|
|
19
19
|
groupQueue.forEach((group) => {
|
|
20
20
|
x += group.x;
|
|
@@ -26,108 +26,108 @@ function flatElementSize(elemSize, opts) {
|
|
|
26
26
|
return {
|
|
27
27
|
x,
|
|
28
28
|
y,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
angle
|
|
29
|
+
width,
|
|
30
|
+
height,
|
|
31
|
+
angle,
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
|
-
totalAngle +=
|
|
34
|
+
totalAngle += mtrlSize.angle || 0;
|
|
35
35
|
totalAngle = limitAngle(totalAngle);
|
|
36
|
-
const vertexes =
|
|
37
|
-
const center =
|
|
36
|
+
const vertexes = calcMaterialVertexesInGroup(mtrlSize, { groupQueue });
|
|
37
|
+
const center = calcMaterialCenterFromVertexes(vertexes);
|
|
38
38
|
const start = rotatePoint(center, vertexes[0], parseAngleToRadian(0 - totalAngle));
|
|
39
39
|
x = start.x;
|
|
40
40
|
y = start.y;
|
|
41
41
|
return {
|
|
42
42
|
x,
|
|
43
43
|
y,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
angle: totalAngle
|
|
44
|
+
width,
|
|
45
|
+
height,
|
|
46
|
+
angle: totalAngle,
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
|
-
function
|
|
49
|
+
function isValidMaterial(mtrl) {
|
|
50
50
|
var _a;
|
|
51
|
-
if (['rect', 'circle'].includes(
|
|
52
|
-
const
|
|
53
|
-
if (!
|
|
51
|
+
if (['rect', 'circle'].includes(mtrl.type)) {
|
|
52
|
+
const attributes = mtrl;
|
|
53
|
+
if (!attributes.fill && !attributes.strokeWidth) {
|
|
54
54
|
return false;
|
|
55
55
|
}
|
|
56
|
-
if (
|
|
56
|
+
if (attributes.fill === 'transparent' && !attributes.strokeWidth) {
|
|
57
57
|
return false;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
-
if (['group'].includes(
|
|
61
|
-
const
|
|
62
|
-
const { children } =
|
|
63
|
-
if (!(children.length > 0) && !
|
|
60
|
+
if (['group'].includes(mtrl.type)) {
|
|
61
|
+
const attributes = mtrl;
|
|
62
|
+
const { children } = attributes;
|
|
63
|
+
if (!(children.length > 0) && !attributes.fill && !attributes.strokeWidth) {
|
|
64
64
|
return false;
|
|
65
65
|
}
|
|
66
|
-
if (!(children.length > 0) &&
|
|
66
|
+
if (!(children.length > 0) && attributes.fill === 'transparent' && !attributes.strokeWidth) {
|
|
67
67
|
return false;
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
if (
|
|
71
|
-
if (!
|
|
70
|
+
if (mtrl.type === 'text') {
|
|
71
|
+
if (!mtrl.text) {
|
|
72
72
|
return false;
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
-
if (
|
|
76
|
-
if (!
|
|
75
|
+
if (mtrl.type === 'image') {
|
|
76
|
+
if (!mtrl.href) {
|
|
77
77
|
return false;
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
|
-
if (
|
|
81
|
-
if (!
|
|
80
|
+
if (mtrl.type === 'foreignObject') {
|
|
81
|
+
if (!mtrl.content) {
|
|
82
82
|
return false;
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
if (
|
|
86
|
-
if (!
|
|
85
|
+
if (mtrl.type === 'svgCode') {
|
|
86
|
+
if (!mtrl.code) {
|
|
87
87
|
return false;
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
if (
|
|
91
|
-
const
|
|
92
|
-
if (!(((_a =
|
|
90
|
+
if (mtrl.type === 'path') {
|
|
91
|
+
const attributes = mtrl;
|
|
92
|
+
if (!(((_a = attributes === null || attributes === void 0 ? void 0 : attributes.commands) === null || _a === void 0 ? void 0 : _a.length) > 0)) {
|
|
93
93
|
return false;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
return true;
|
|
97
97
|
}
|
|
98
|
-
export function
|
|
99
|
-
const
|
|
98
|
+
export function flatMaterialList(list) {
|
|
99
|
+
const mtrleList = [];
|
|
100
100
|
const currentGroupQueue = [];
|
|
101
|
-
const
|
|
102
|
-
if (!
|
|
101
|
+
const _resetMtrlSize = (mtrl) => {
|
|
102
|
+
if (!isValidMaterial(mtrl)) {
|
|
103
103
|
return;
|
|
104
104
|
}
|
|
105
|
-
const newSize =
|
|
106
|
-
const
|
|
107
|
-
|
|
105
|
+
const newSize = flatMaterialSize(mtrl, { groupQueue: currentGroupQueue });
|
|
106
|
+
const resizeMtrl = Object.assign(Object.assign({}, mtrl), newSize);
|
|
107
|
+
mtrleList.push(resizeMtrl);
|
|
108
108
|
};
|
|
109
|
-
const _walk = (
|
|
109
|
+
const _walk = (mtrl) => {
|
|
110
110
|
var _a;
|
|
111
|
-
if (((_a =
|
|
111
|
+
if (((_a = mtrl === null || mtrl === void 0 ? void 0 : mtrl.operations) === null || _a === void 0 ? void 0 : _a.invisible) === true) {
|
|
112
112
|
return;
|
|
113
113
|
}
|
|
114
|
-
if (
|
|
115
|
-
const
|
|
116
|
-
const { children } =
|
|
117
|
-
|
|
118
|
-
currentGroupQueue.push(
|
|
114
|
+
if (mtrl.type === 'group') {
|
|
115
|
+
const attributes = mtrl;
|
|
116
|
+
const { children } = attributes, restAttributes = __rest(attributes, ["children"]);
|
|
117
|
+
_resetMtrlSize(Object.assign(Object.assign({}, mtrl), { attributes: Object.assign(Object.assign({}, restAttributes), { children: [] }) }));
|
|
118
|
+
currentGroupQueue.push(mtrl);
|
|
119
119
|
children.forEach((child) => {
|
|
120
120
|
_walk(child);
|
|
121
121
|
});
|
|
122
122
|
currentGroupQueue.pop();
|
|
123
123
|
}
|
|
124
124
|
else {
|
|
125
|
-
|
|
125
|
+
_resetMtrlSize(mtrl);
|
|
126
126
|
}
|
|
127
127
|
};
|
|
128
128
|
for (let i = 0; i < list.length; i++) {
|
|
129
|
-
const
|
|
130
|
-
_walk(
|
|
129
|
+
const mtrl = list[i];
|
|
130
|
+
_walk(mtrl);
|
|
131
131
|
}
|
|
132
|
-
return
|
|
132
|
+
return mtrleList;
|
|
133
133
|
}
|
package/dist/esm/view/group.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function
|
|
1
|
+
import type { StrictMaterial, MaterialPosition } from '@idraw/types';
|
|
2
|
+
export declare function groupMaterialsByPosition(list: StrictMaterial[], positions: MaterialPosition[]): StrictMaterial[];
|
|
3
|
+
export declare function ungroupMaterialsByPosition(list: StrictMaterial[], position: MaterialPosition): StrictMaterial[];
|
package/dist/esm/view/group.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { findMaterialFromListByPosition, calcMaterialListSize } from './material';
|
|
2
|
+
import { deleteMaterialInListByPosition, insertMaterialToListByPosition } from './handle-material';
|
|
3
3
|
import { createUUID } from '../tool/uuid';
|
|
4
|
-
export function
|
|
4
|
+
export function groupMaterialsByPosition(list, positions) {
|
|
5
5
|
if (positions.length > 1) {
|
|
6
6
|
let isValidPositions = true;
|
|
7
|
-
|
|
7
|
+
const lastIndexs = [];
|
|
8
8
|
for (let i = 1; i < positions.length; i++) {
|
|
9
9
|
const prevPosition = positions[i - 1];
|
|
10
10
|
const position = positions[i];
|
|
@@ -28,7 +28,7 @@ export function groupElementsByPosition(list, positions) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
if (isValidPositions !== true) {
|
|
31
|
-
console.error('[idraw]: The grouped
|
|
31
|
+
console.error('[idraw]: The grouped materials are not siblings!');
|
|
32
32
|
return list;
|
|
33
33
|
}
|
|
34
34
|
lastIndexs.sort((a, b) => a - b);
|
|
@@ -37,45 +37,42 @@ export function groupElementsByPosition(list, positions) {
|
|
|
37
37
|
const groupPosition = [...groupParentPosition, lastIndexs[0]];
|
|
38
38
|
for (let i = 0; i < lastIndexs.length; i++) {
|
|
39
39
|
const position = [...groupParentPosition, lastIndexs[i]];
|
|
40
|
-
const
|
|
41
|
-
if (
|
|
42
|
-
groupChildren.push(
|
|
40
|
+
const mtrl = findMaterialFromListByPosition(position, list);
|
|
41
|
+
if (mtrl) {
|
|
42
|
+
groupChildren.push(mtrl);
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
-
const groupSize =
|
|
45
|
+
const groupSize = calcMaterialListSize(groupChildren);
|
|
46
46
|
for (let i = 0; i < groupChildren.length; i++) {
|
|
47
|
-
const
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
const mtrl = groupChildren[i];
|
|
48
|
+
if (mtrl) {
|
|
49
|
+
mtrl.x -= groupSize.x;
|
|
50
|
+
mtrl.y -= groupSize.y;
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
for (let i = lastIndexs.length - 1; i >= 0; i--) {
|
|
54
54
|
const position = [...groupParentPosition, lastIndexs[i]];
|
|
55
|
-
|
|
55
|
+
deleteMaterialInListByPosition(position, list);
|
|
56
56
|
}
|
|
57
|
-
const group = Object.assign(Object.assign({ name: 'Group',
|
|
58
|
-
|
|
59
|
-
} });
|
|
60
|
-
insertElementToListByPosition(group, groupPosition, list);
|
|
57
|
+
const group = Object.assign(Object.assign({ name: 'Group', id: createUUID(), type: 'group' }, groupSize), { children: groupChildren });
|
|
58
|
+
insertMaterialToListByPosition(group, groupPosition, list);
|
|
61
59
|
}
|
|
62
60
|
return list;
|
|
63
61
|
}
|
|
64
|
-
export function
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
console.error('[idraw]: The ungrouped element is not a group element!');
|
|
62
|
+
export function ungroupMaterialsByPosition(list, position) {
|
|
63
|
+
const mtrl = findMaterialFromListByPosition(position, list);
|
|
64
|
+
if (!(mtrl && (mtrl === null || mtrl === void 0 ? void 0 : mtrl.type) === 'group' && Array.isArray(mtrl === null || mtrl === void 0 ? void 0 : mtrl.children))) {
|
|
65
|
+
console.error('[idraw]: The ungrouped material is not a group material!');
|
|
69
66
|
}
|
|
70
67
|
const groupParentPosition = [...position].splice(0, position.length - 1);
|
|
71
68
|
const groupLastIndex = position[position.length - 1];
|
|
72
|
-
const { x, y } =
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
const { x, y } = mtrl;
|
|
70
|
+
deleteMaterialInListByPosition(position, list);
|
|
71
|
+
mtrl.children.forEach((child, i) => {
|
|
75
72
|
child.x += x;
|
|
76
73
|
child.y += y;
|
|
77
|
-
const
|
|
78
|
-
|
|
74
|
+
const mtrlPosition = [...groupParentPosition, groupLastIndex + i];
|
|
75
|
+
insertMaterialToListByPosition(child, mtrlPosition, list);
|
|
79
76
|
});
|
|
80
77
|
return list;
|
|
81
78
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RecursivePartial, StrictMaterial, MaterialPosition, MaterialType, ViewScaleInfo, ViewSizeInfo } from '@idraw/types';
|
|
2
|
+
export declare function createMaterial<T extends MaterialType>(type: T, baseMtrl: RecursivePartial<StrictMaterial<T>>, opts?: {
|
|
3
|
+
viewScaleInfo: ViewScaleInfo;
|
|
4
|
+
viewSizeInfo: ViewSizeInfo;
|
|
5
|
+
limitRatio?: boolean;
|
|
6
|
+
}): StrictMaterial<T>;
|
|
7
|
+
export declare function insertMaterialToListByPosition(material: StrictMaterial, position: MaterialPosition, list: StrictMaterial[]): boolean;
|
|
8
|
+
export declare function deleteMaterialInListByPosition(position: MaterialPosition, list: StrictMaterial[]): boolean;
|
|
9
|
+
export declare function deleteMaterialInList(id: string, list: StrictMaterial[]): boolean;
|
|
10
|
+
export declare function moveMaterialPosition(materials: StrictMaterial[], opts: {
|
|
11
|
+
from: MaterialPosition;
|
|
12
|
+
to: MaterialPosition;
|
|
13
|
+
}): {
|
|
14
|
+
materials: StrictMaterial[];
|
|
15
|
+
from: MaterialPosition;
|
|
16
|
+
to: MaterialPosition;
|
|
17
|
+
};
|
|
18
|
+
export declare function mergeMaterial<T extends StrictMaterial<MaterialType> = StrictMaterial<MaterialType>>(originMtrl: T, updateContent: RecursivePartial<T>, opts?: {
|
|
19
|
+
onlyUpdateContent?: boolean;
|
|
20
|
+
}): T;
|
|
21
|
+
export declare function updateMaterialInList(id: string, updateContent: RecursivePartial<StrictMaterial<MaterialType>>, materials: StrictMaterial[]): StrictMaterial | null;
|
|
22
|
+
export declare function updateMaterialInListByPosition(position: MaterialPosition, updateContent: RecursivePartial<StrictMaterial<MaterialType>>, materials: StrictMaterial[], opts?: {
|
|
23
|
+
onlyUpdateContent?: boolean;
|
|
24
|
+
}): StrictMaterial | null;
|