@idraw/util 0.4.0-alpha.6 → 0.4.0-alpha.7
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/index.d.ts +3 -3
- package/dist/esm/index.js +3 -3
- package/dist/esm/lib/canvas.d.ts +6 -2
- package/dist/esm/lib/canvas.js +61 -18
- package/dist/esm/lib/context2d.d.ts +9 -3
- package/dist/esm/lib/context2d.js +101 -70
- package/dist/esm/lib/element.d.ts +2 -4
- package/dist/esm/lib/element.js +18 -94
- package/dist/esm/lib/handle-element.d.ts +4 -0
- package/dist/esm/lib/handle-element.js +108 -1
- package/dist/index.global.js +294 -176
- package/dist/index.global.min.js +1 -1
- package/package.json +1 -1
package/dist/esm/lib/element.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { rotateElementVertexes } from './rotate';
|
|
2
2
|
import { isAssetId, createAssetId } from './uuid';
|
|
3
|
-
import { istype } from './istype';
|
|
4
3
|
function getGroupUUIDs(elements, index) {
|
|
5
4
|
var _a;
|
|
6
5
|
const uuids = [];
|
|
@@ -208,54 +207,6 @@ export function getGroupQueueFromList(uuid, elements) {
|
|
|
208
207
|
_scan(uuid, elements);
|
|
209
208
|
return groupQueue;
|
|
210
209
|
}
|
|
211
|
-
function mergeElement(originElem, updateContent) {
|
|
212
|
-
var _a;
|
|
213
|
-
const commonKeys = Object.keys(updateContent);
|
|
214
|
-
for (let i = 0; i < commonKeys.length; i++) {
|
|
215
|
-
const commonKey = commonKeys[i];
|
|
216
|
-
if (['x', 'y', 'w', 'h', 'angle', 'name'].includes(commonKey)) {
|
|
217
|
-
originElem[commonKey] = updateContent[commonKey];
|
|
218
|
-
}
|
|
219
|
-
else if (['detail', 'operations'].includes(commonKey)) {
|
|
220
|
-
if (istype.json(updateContent[commonKey])) {
|
|
221
|
-
if (!(originElem === null || originElem === void 0 ? void 0 : originElem.hasOwnProperty(commonKey))) {
|
|
222
|
-
originElem[commonKey] = {};
|
|
223
|
-
}
|
|
224
|
-
if (istype.json(originElem[commonKey])) {
|
|
225
|
-
originElem[commonKey] = Object.assign(Object.assign({}, originElem[commonKey]), updateContent[commonKey]);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
else if (istype.array(updateContent[commonKey])) {
|
|
229
|
-
if (!(originElem === null || originElem === void 0 ? void 0 : originElem.hasOwnProperty(commonKey))) {
|
|
230
|
-
originElem[commonKey] = [];
|
|
231
|
-
}
|
|
232
|
-
if (istype.array(originElem[commonKey])) {
|
|
233
|
-
(_a = updateContent === null || updateContent === void 0 ? void 0 : updateContent[commonKey]) === null || _a === void 0 ? void 0 : _a.forEach((item, i) => {
|
|
234
|
-
originElem[commonKey][i] = item;
|
|
235
|
-
});
|
|
236
|
-
originElem[commonKey] = [...originElem[commonKey], ...updateContent[commonKey]];
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
return originElem;
|
|
242
|
-
}
|
|
243
|
-
export function updateElementInList(uuid, updateContent, elements) {
|
|
244
|
-
var _a;
|
|
245
|
-
let targetElement = null;
|
|
246
|
-
for (let i = 0; i < elements.length; i++) {
|
|
247
|
-
const elem = elements[i];
|
|
248
|
-
if (elem.uuid === uuid) {
|
|
249
|
-
mergeElement(elem, updateContent);
|
|
250
|
-
targetElement = elem;
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
253
|
-
else if (elem.type === 'group') {
|
|
254
|
-
targetElement = updateElementInList(uuid, updateContent, ((_a = elem === null || elem === void 0 ? void 0 : elem.detail) === null || _a === void 0 ? void 0 : _a.children) || []);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return targetElement;
|
|
258
|
-
}
|
|
259
210
|
export function getElementSize(elem) {
|
|
260
211
|
const { x, y, w, h, angle } = elem;
|
|
261
212
|
const size = { x, y, w, h, angle };
|
|
@@ -356,57 +307,30 @@ export function findElementFromListByPosition(position, list) {
|
|
|
356
307
|
}
|
|
357
308
|
return result;
|
|
358
309
|
}
|
|
359
|
-
export function
|
|
360
|
-
let result =
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
else if (position.length > 1) {
|
|
367
|
-
let tempList = list;
|
|
368
|
-
for (let i = 0; i < position.length; i++) {
|
|
369
|
-
const pos = position[i];
|
|
370
|
-
const item = tempList[pos];
|
|
371
|
-
if (i === position.length - 1) {
|
|
372
|
-
const pos = position[i];
|
|
373
|
-
tempList.splice(pos, 0, element);
|
|
374
|
-
result = true;
|
|
375
|
-
}
|
|
376
|
-
else if (i < position.length - 1 && item.type === 'group') {
|
|
377
|
-
tempList = item.detail.children;
|
|
378
|
-
}
|
|
379
|
-
else {
|
|
310
|
+
export function getElementPositionFromList(uuid, elements) {
|
|
311
|
+
let result = [];
|
|
312
|
+
let over = false;
|
|
313
|
+
const _loop = (list) => {
|
|
314
|
+
var _a;
|
|
315
|
+
for (let i = 0; i < list.length; i++) {
|
|
316
|
+
if (over === true) {
|
|
380
317
|
break;
|
|
381
318
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
let result = false;
|
|
388
|
-
if (position.length === 1) {
|
|
389
|
-
const pos = position[0];
|
|
390
|
-
list.splice(pos, 1);
|
|
391
|
-
result = true;
|
|
392
|
-
}
|
|
393
|
-
else if (position.length > 1) {
|
|
394
|
-
let tempList = list;
|
|
395
|
-
for (let i = 0; i < position.length; i++) {
|
|
396
|
-
const pos = position[i];
|
|
397
|
-
const item = tempList[pos];
|
|
398
|
-
if (i === position.length - 1) {
|
|
399
|
-
const pos = position[i];
|
|
400
|
-
tempList.splice(pos, 1);
|
|
401
|
-
result = true;
|
|
319
|
+
result.push(i);
|
|
320
|
+
const elem = list[i];
|
|
321
|
+
if (elem.uuid === uuid) {
|
|
322
|
+
over = true;
|
|
323
|
+
break;
|
|
402
324
|
}
|
|
403
|
-
else if (
|
|
404
|
-
|
|
325
|
+
else if (elem.type === 'group') {
|
|
326
|
+
_loop(((_a = elem === null || elem === void 0 ? void 0 : elem.detail) === null || _a === void 0 ? void 0 : _a.children) || []);
|
|
405
327
|
}
|
|
406
|
-
|
|
328
|
+
if (over) {
|
|
407
329
|
break;
|
|
408
330
|
}
|
|
331
|
+
result.pop();
|
|
409
332
|
}
|
|
410
|
-
}
|
|
333
|
+
};
|
|
334
|
+
_loop(elements);
|
|
411
335
|
return result;
|
|
412
336
|
}
|
|
@@ -4,7 +4,11 @@ export declare function createElement<T extends ElementType>(type: T, baseElem:
|
|
|
4
4
|
viewSizeInfo: ViewSizeInfo;
|
|
5
5
|
limitRatio?: boolean;
|
|
6
6
|
}): Element<T>;
|
|
7
|
+
export declare function insertElementToListByPosition(element: Element, position: ElementPosition, list: Element[]): boolean;
|
|
8
|
+
export declare function deleteElementInListByPosition(position: ElementPosition, list: Element[]): boolean;
|
|
9
|
+
export declare function deleteElementInList(uuid: string, list: Element[]): boolean;
|
|
7
10
|
export declare function moveElementPosition(elements: Elements, opts: {
|
|
8
11
|
from: ElementPosition;
|
|
9
12
|
to: ElementPosition;
|
|
10
13
|
}): Elements;
|
|
14
|
+
export declare function updateElementInList(uuid: string, updateContent: RecursivePartial<Element<ElementType>>, elements: Element[]): Element | null;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createUUID } from './uuid';
|
|
2
2
|
import { getDefaultElementDetailConfig, getDefaultElementRectDetail, getDefaultElementCircleDetail, getDefaultElementTextDetail, getDefaultElementSVGDetail, getDefaultElementImageDetail, getDefaultElementGroupDetail } from './config';
|
|
3
|
-
import {
|
|
3
|
+
import { istype } from './istype';
|
|
4
|
+
import { findElementFromListByPosition, getElementPositionFromList } from './element';
|
|
4
5
|
const defaultViewWidth = 200;
|
|
5
6
|
const defaultViewHeight = 200;
|
|
6
7
|
const defaultDetail = getDefaultElementDetailConfig();
|
|
@@ -74,6 +75,64 @@ export function createElement(type, baseElem, opts) {
|
|
|
74
75
|
const elem = Object.assign(Object.assign(Object.assign({}, elemSize), baseElem), { uuid: createUUID(), type, detail: Object.assign(Object.assign({}, detail), (baseElem.detail || {})) });
|
|
75
76
|
return elem;
|
|
76
77
|
}
|
|
78
|
+
export function insertElementToListByPosition(element, position, list) {
|
|
79
|
+
let result = false;
|
|
80
|
+
if (position.length === 1) {
|
|
81
|
+
const pos = position[0];
|
|
82
|
+
list.splice(pos, 0, element);
|
|
83
|
+
result = true;
|
|
84
|
+
}
|
|
85
|
+
else if (position.length > 1) {
|
|
86
|
+
let tempList = list;
|
|
87
|
+
for (let i = 0; i < position.length; i++) {
|
|
88
|
+
const pos = position[i];
|
|
89
|
+
const item = tempList[pos];
|
|
90
|
+
if (i === position.length - 1) {
|
|
91
|
+
const pos = position[i];
|
|
92
|
+
tempList.splice(pos, 0, element);
|
|
93
|
+
result = true;
|
|
94
|
+
}
|
|
95
|
+
else if (i < position.length - 1 && item.type === 'group') {
|
|
96
|
+
tempList = item.detail.children;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
export function deleteElementInListByPosition(position, list) {
|
|
106
|
+
let result = false;
|
|
107
|
+
if (position.length === 1) {
|
|
108
|
+
const pos = position[0];
|
|
109
|
+
list.splice(pos, 1);
|
|
110
|
+
result = true;
|
|
111
|
+
}
|
|
112
|
+
else if (position.length > 1) {
|
|
113
|
+
let tempList = list;
|
|
114
|
+
for (let i = 0; i < position.length; i++) {
|
|
115
|
+
const pos = position[i];
|
|
116
|
+
const item = tempList[pos];
|
|
117
|
+
if (i === position.length - 1) {
|
|
118
|
+
const pos = position[i];
|
|
119
|
+
tempList.splice(pos, 1);
|
|
120
|
+
result = true;
|
|
121
|
+
}
|
|
122
|
+
else if (i < position.length - 1 && item.type === 'group') {
|
|
123
|
+
tempList = item.detail.children;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
export function deleteElementInList(uuid, list) {
|
|
133
|
+
const position = getElementPositionFromList(uuid, list);
|
|
134
|
+
return deleteElementInListByPosition(position, list);
|
|
135
|
+
}
|
|
77
136
|
export function moveElementPosition(elements, opts) {
|
|
78
137
|
const { from, to } = opts;
|
|
79
138
|
if (from.length === 0 || to.length === 0) {
|
|
@@ -117,3 +176,51 @@ export function moveElementPosition(elements, opts) {
|
|
|
117
176
|
}
|
|
118
177
|
return elements;
|
|
119
178
|
}
|
|
179
|
+
function mergeElement(originElem, updateContent) {
|
|
180
|
+
var _a;
|
|
181
|
+
const commonKeys = Object.keys(updateContent);
|
|
182
|
+
for (let i = 0; i < commonKeys.length; i++) {
|
|
183
|
+
const commonKey = commonKeys[i];
|
|
184
|
+
if (['x', 'y', 'w', 'h', 'angle', 'name'].includes(commonKey)) {
|
|
185
|
+
originElem[commonKey] = updateContent[commonKey];
|
|
186
|
+
}
|
|
187
|
+
else if (['detail', 'operations'].includes(commonKey)) {
|
|
188
|
+
if (istype.json(updateContent[commonKey])) {
|
|
189
|
+
if (!(originElem === null || originElem === void 0 ? void 0 : originElem.hasOwnProperty(commonKey))) {
|
|
190
|
+
originElem[commonKey] = {};
|
|
191
|
+
}
|
|
192
|
+
if (istype.json(originElem[commonKey])) {
|
|
193
|
+
originElem[commonKey] = Object.assign(Object.assign({}, originElem[commonKey]), updateContent[commonKey]);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else if (istype.array(updateContent[commonKey])) {
|
|
197
|
+
if (!(originElem === null || originElem === void 0 ? void 0 : originElem.hasOwnProperty(commonKey))) {
|
|
198
|
+
originElem[commonKey] = [];
|
|
199
|
+
}
|
|
200
|
+
if (istype.array(originElem[commonKey])) {
|
|
201
|
+
(_a = updateContent === null || updateContent === void 0 ? void 0 : updateContent[commonKey]) === null || _a === void 0 ? void 0 : _a.forEach((item, i) => {
|
|
202
|
+
originElem[commonKey][i] = item;
|
|
203
|
+
});
|
|
204
|
+
originElem[commonKey] = [...originElem[commonKey], ...updateContent[commonKey]];
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return originElem;
|
|
210
|
+
}
|
|
211
|
+
export function updateElementInList(uuid, updateContent, elements) {
|
|
212
|
+
var _a;
|
|
213
|
+
let targetElement = null;
|
|
214
|
+
for (let i = 0; i < elements.length; i++) {
|
|
215
|
+
const elem = elements[i];
|
|
216
|
+
if (elem.uuid === uuid) {
|
|
217
|
+
mergeElement(elem, updateContent);
|
|
218
|
+
targetElement = elem;
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
else if (elem.type === 'group') {
|
|
222
|
+
targetElement = updateElementInList(uuid, updateContent, ((_a = elem === null || elem === void 0 ? void 0 : elem.detail) === null || _a === void 0 ? void 0 : _a.children) || []);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return targetElement;
|
|
226
|
+
}
|