@idraw/util 0.4.3 → 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.
Files changed (96) hide show
  1. package/dist/esm/converter/data-svg.d.ts +2 -0
  2. package/dist/esm/converter/data-svg.js +19 -0
  3. package/dist/esm/converter/material-svg.d.ts +2 -0
  4. package/dist/esm/converter/material-svg.js +63 -0
  5. package/dist/esm/converter/svg-material.d.ts +2 -0
  6. package/dist/esm/converter/svg-material.js +188 -0
  7. package/dist/esm/index.d.ts +32 -21
  8. package/dist/esm/index.js +32 -21
  9. package/dist/esm/static.d.ts +1 -0
  10. package/dist/esm/static.js +1 -0
  11. package/dist/esm/tool/file.js +2 -2
  12. package/dist/esm/tool/html.js +27 -27
  13. package/dist/esm/tool/id.d.ts +1 -0
  14. package/dist/esm/tool/id.js +4 -0
  15. package/dist/esm/tool/image.js +6 -6
  16. package/dist/esm/tool/path-to-box.d.ts +3 -0
  17. package/dist/esm/tool/path-to-box.js +353 -0
  18. package/dist/esm/tool/path-to-command.d.ts +4 -0
  19. package/dist/esm/tool/path-to-command.js +392 -0
  20. package/dist/esm/tool/path.d.ts +18 -0
  21. package/dist/esm/tool/path.js +421 -0
  22. package/dist/esm/tool/svg-path.d.ts +3 -0
  23. package/dist/esm/{view → tool}/svg-path.js +4 -16
  24. package/dist/esm/tool/time.d.ts +2 -2
  25. package/dist/esm/tool/time.js +4 -4
  26. package/dist/esm/tool/uuid.d.ts +1 -1
  27. package/dist/esm/tool/uuid.js +6 -6
  28. package/dist/esm/view/box.d.ts +0 -2
  29. package/dist/esm/view/box.js +1 -173
  30. package/dist/esm/view/canvas.js +3 -3
  31. package/dist/esm/view/check.d.ts +8 -7
  32. package/dist/esm/view/check.js +34 -34
  33. package/dist/esm/view/config.d.ts +0 -9
  34. package/dist/esm/view/config.js +1 -71
  35. package/dist/esm/view/context2d.d.ts +10 -3
  36. package/dist/esm/view/context2d.js +39 -18
  37. package/dist/esm/view/controller.d.ts +5 -5
  38. package/dist/esm/view/controller.js +105 -102
  39. package/dist/esm/view/data.d.ts +2 -2
  40. package/dist/esm/view/data.js +65 -65
  41. package/dist/esm/view/dom.d.ts +23 -0
  42. package/dist/esm/view/dom.js +190 -0
  43. package/dist/esm/view/flat.d.ts +2 -2
  44. package/dist/esm/view/flat.js +53 -53
  45. package/dist/esm/view/group.d.ts +3 -3
  46. package/dist/esm/view/group.js +25 -28
  47. package/dist/esm/view/handle-material.d.ts +24 -0
  48. package/dist/esm/view/{handle-element.js → handle-material.js} +98 -75
  49. package/dist/esm/view/is.d.ts +10 -12
  50. package/dist/esm/view/is.js +15 -19
  51. package/dist/esm/view/load.d.ts +2 -2
  52. package/dist/esm/view/load.js +7 -7
  53. package/dist/esm/view/material.d.ts +40 -0
  54. package/dist/esm/view/material.js +476 -0
  55. package/dist/esm/view/middleware.d.ts +1 -0
  56. package/dist/esm/view/middleware.js +14 -2
  57. package/dist/esm/view/modify-record.d.ts +4 -4
  58. package/dist/esm/view/modify-record.js +2 -2
  59. package/dist/esm/view/parser.d.ts +2 -2
  60. package/dist/esm/view/parser.js +3 -3
  61. package/dist/esm/view/path.d.ts +2 -0
  62. package/dist/esm/view/path.js +30 -0
  63. package/dist/esm/view/point-move-material.d.ts +5 -0
  64. package/dist/esm/view/{point-move-element.js → point-move-material.js} +6 -6
  65. package/dist/esm/view/point.d.ts +3 -8
  66. package/dist/esm/view/point.js +1 -21
  67. package/dist/esm/view/position.d.ts +9 -9
  68. package/dist/esm/view/position.js +1 -1
  69. package/dist/esm/view/rect.d.ts +2 -2
  70. package/dist/esm/view/rect.js +4 -4
  71. package/dist/esm/view/resize-material.d.ts +4 -0
  72. package/dist/esm/view/resize-material.js +266 -0
  73. package/dist/esm/view/rotate.d.ts +11 -11
  74. package/dist/esm/view/rotate.js +24 -24
  75. package/dist/esm/view/static.d.ts +13 -0
  76. package/dist/esm/view/static.js +89 -0
  77. package/dist/esm/view/styles.d.ts +11 -0
  78. package/dist/esm/view/styles.js +78 -0
  79. package/dist/esm/view/vertex.d.ts +8 -8
  80. package/dist/esm/view/vertex.js +28 -28
  81. package/dist/esm/view/view-box.d.ts +2 -6
  82. package/dist/esm/view/view-box.js +8 -64
  83. package/dist/esm/view/view-calc.d.ts +23 -20
  84. package/dist/esm/view/view-calc.js +104 -93
  85. package/dist/esm/view/view-content.d.ts +2 -2
  86. package/dist/esm/view/view-content.js +34 -34
  87. package/dist/index.global.js +3144 -1548
  88. package/dist/index.global.min.js +1 -1
  89. package/package.json +1 -1
  90. package/dist/esm/view/element.d.ts +0 -35
  91. package/dist/esm/view/element.js +0 -471
  92. package/dist/esm/view/handle-element.d.ts +0 -24
  93. package/dist/esm/view/point-move-element.d.ts +0 -5
  94. package/dist/esm/view/resize-element.d.ts +0 -4
  95. package/dist/esm/view/resize-element.js +0 -255
  96. 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
+ }
@@ -1,2 +1,2 @@
1
- import type { Elements } from '@idraw/types';
2
- export declare function flatElementList(list: Elements): Elements;
1
+ import type { StrictMaterial } from '@idraw/types';
2
+ export declare function flatMaterialList(list: StrictMaterial[]): StrictMaterial[];
@@ -9,12 +9,12 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- import { calcElementVertexesInGroup } from './vertex';
13
- import { limitAngle, parseAngleToRadian, calcElementCenterFromVertexes, rotatePoint } from './rotate';
14
- function flatElementSize(elemSize, opts) {
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 } = elemSize;
17
- const { w, h, angle = 0 } = elemSize;
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
- w,
30
- h,
31
- angle
29
+ width,
30
+ height,
31
+ angle,
32
32
  };
33
33
  }
34
- totalAngle += elemSize.angle || 0;
34
+ totalAngle += mtrlSize.angle || 0;
35
35
  totalAngle = limitAngle(totalAngle);
36
- const vertexes = calcElementVertexesInGroup(elemSize, { groupQueue });
37
- const center = calcElementCenterFromVertexes(vertexes);
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
- w,
45
- h,
46
- angle: totalAngle
44
+ width,
45
+ height,
46
+ angle: totalAngle,
47
47
  };
48
48
  }
49
- function isValidElement(elem) {
49
+ function isValidMaterial(mtrl) {
50
50
  var _a;
51
- if (['rect', 'circle'].includes(elem.type)) {
52
- const detail = elem.detail;
53
- if (!detail.background && !detail.borderWidth) {
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 (detail.background === 'transparent' && !detail.borderWidth) {
56
+ if (attributes.fill === 'transparent' && !attributes.strokeWidth) {
57
57
  return false;
58
58
  }
59
59
  }
60
- if (['group'].includes(elem.type)) {
61
- const detail = elem.detail || {};
62
- const { children } = detail;
63
- if (!(children.length > 0) && !detail.background && !detail.borderWidth) {
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) && detail.background === 'transparent' && !detail.borderWidth) {
66
+ if (!(children.length > 0) && attributes.fill === 'transparent' && !attributes.strokeWidth) {
67
67
  return false;
68
68
  }
69
69
  }
70
- if (elem.type === 'text') {
71
- if (!elem.detail.text) {
70
+ if (mtrl.type === 'text') {
71
+ if (!mtrl.text) {
72
72
  return false;
73
73
  }
74
74
  }
75
- if (elem.type === 'image') {
76
- if (!elem.detail.src) {
75
+ if (mtrl.type === 'image') {
76
+ if (!mtrl.href) {
77
77
  return false;
78
78
  }
79
79
  }
80
- if (elem.type === 'html') {
81
- if (!elem.detail.html) {
80
+ if (mtrl.type === 'foreignObject') {
81
+ if (!mtrl.content) {
82
82
  return false;
83
83
  }
84
84
  }
85
- if (elem.type === 'svg') {
86
- if (!elem.detail.svg) {
85
+ if (mtrl.type === 'svgCode') {
86
+ if (!mtrl.code) {
87
87
  return false;
88
88
  }
89
89
  }
90
- if (elem.type === 'path') {
91
- const detail = elem.detail;
92
- if (!(((_a = detail === null || detail === void 0 ? void 0 : detail.commands) === null || _a === void 0 ? void 0 : _a.length) > 0)) {
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 flatElementList(list) {
99
- const elemeList = [];
98
+ export function flatMaterialList(list) {
99
+ const mtrleList = [];
100
100
  const currentGroupQueue = [];
101
- const _resetElemSize = (elem) => {
102
- if (!isValidElement(elem)) {
101
+ const _resetMtrlSize = (mtrl) => {
102
+ if (!isValidMaterial(mtrl)) {
103
103
  return;
104
104
  }
105
- const newSize = flatElementSize(elem, { groupQueue: currentGroupQueue });
106
- const resizeElem = Object.assign(Object.assign({}, elem), newSize);
107
- elemeList.push(resizeElem);
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 = (elem) => {
109
+ const _walk = (mtrl) => {
110
110
  var _a;
111
- if (((_a = elem === null || elem === void 0 ? void 0 : elem.operations) === null || _a === void 0 ? void 0 : _a.invisible) === true) {
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 (elem.type === 'group') {
115
- const { detail } = elem;
116
- const { children } = detail, restDetail = __rest(detail, ["children"]);
117
- _resetElemSize(Object.assign(Object.assign({}, elem), { detail: Object.assign(Object.assign({}, restDetail), { children: [] }) }));
118
- currentGroupQueue.push(elem);
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
- _resetElemSize(elem);
125
+ _resetMtrlSize(mtrl);
126
126
  }
127
127
  };
128
128
  for (let i = 0; i < list.length; i++) {
129
- const elem = list[i];
130
- _walk(elem);
129
+ const mtrl = list[i];
130
+ _walk(mtrl);
131
131
  }
132
- return elemeList;
132
+ return mtrleList;
133
133
  }
@@ -1,3 +1,3 @@
1
- import type { Elements, ElementPosition } from '@idraw/types';
2
- export declare function groupElementsByPosition(list: Elements, positions: ElementPosition[]): Elements;
3
- export declare function ungroupElementsByPosition(list: Elements, position: ElementPosition): Elements;
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[];
@@ -1,10 +1,10 @@
1
- import { findElementFromListByPosition, calcElementListSize } from './element';
2
- import { deleteElementInListByPosition, insertElementToListByPosition } from './handle-element';
1
+ import { findMaterialFromListByPosition, calcMaterialListSize } from './material';
2
+ import { deleteMaterialInListByPosition, insertMaterialToListByPosition } from './handle-material';
3
3
  import { createUUID } from '../tool/uuid';
4
- export function groupElementsByPosition(list, positions) {
4
+ export function groupMaterialsByPosition(list, positions) {
5
5
  if (positions.length > 1) {
6
6
  let isValidPositions = true;
7
- let lastIndexs = [];
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 elements are not siblings!');
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 elem = findElementFromListByPosition(position, list);
41
- if (elem) {
42
- groupChildren.push(elem);
40
+ const mtrl = findMaterialFromListByPosition(position, list);
41
+ if (mtrl) {
42
+ groupChildren.push(mtrl);
43
43
  }
44
44
  }
45
- const groupSize = calcElementListSize(groupChildren);
45
+ const groupSize = calcMaterialListSize(groupChildren);
46
46
  for (let i = 0; i < groupChildren.length; i++) {
47
- const elem = groupChildren[i];
48
- if (elem) {
49
- elem.x -= groupSize.x;
50
- elem.y -= groupSize.y;
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
- deleteElementInListByPosition(position, list);
55
+ deleteMaterialInListByPosition(position, list);
56
56
  }
57
- const group = Object.assign(Object.assign({ name: 'Group', uuid: createUUID(), type: 'group' }, groupSize), { detail: {
58
- children: groupChildren
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 ungroupElementsByPosition(list, position) {
65
- var _a;
66
- const elem = findElementFromListByPosition(position, list);
67
- if (!(elem && (elem === null || elem === void 0 ? void 0 : elem.type) === 'group' && Array.isArray((_a = elem === null || elem === void 0 ? void 0 : elem.detail) === null || _a === void 0 ? void 0 : _a.children))) {
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 } = elem;
73
- deleteElementInListByPosition(position, list);
74
- elem.detail.children.forEach((child, i) => {
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 elemPosition = [...groupParentPosition, groupLastIndex + i];
78
- insertElementToListByPosition(child, elemPosition, list);
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;