@g9tm/lynxcontrols-canvas 2.5.10 → 2.5.11

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.
@@ -0,0 +1,897 @@
1
+ import { bump, compareOrder, indexAfter, indicesBetween, isFrame, isLinear, isPointsElement, measureText, newFrame, newFreeDraw, newImage, newLinear, newShape, newText, normalizeText, randomId, sizeOfPoints } from "./elements.js";
2
+ import { absoluteCoords, boundsContain, elementBounds, hitElement, intersectOutline, isBindable, outlineOf, rotate } from "./geometry.js";
3
+ const BINDING_GAP = 5;
4
+ function expandSelection(scene, ids) {
5
+ const result = new Set();
6
+ const visible = scene.visible();
7
+ for (const id of ids){
8
+ var _element_groupIds;
9
+ const element = scene.get(id);
10
+ if (!element || element.isDeleted) continue;
11
+ result.add(id);
12
+ const group = null == (_element_groupIds = element.groupIds) ? void 0 : _element_groupIds[element.groupIds.length - 1];
13
+ if (group) {
14
+ var _other_groupIds;
15
+ for (const other of visible)if (null == (_other_groupIds = other.groupIds) ? void 0 : _other_groupIds.includes(group)) result.add(other.id);
16
+ }
17
+ }
18
+ return [
19
+ ...result
20
+ ];
21
+ }
22
+ function movingSet(scene, ids) {
23
+ const result = new Set(ids);
24
+ for (const id of ids){
25
+ var _element_boundElements;
26
+ const element = scene.get(id);
27
+ if (element) {
28
+ for (const bound of null != (_element_boundElements = element.boundElements) ? _element_boundElements : [])if ('text' === bound.type) result.add(bound.id);
29
+ if (isFrame(element)) {
30
+ for (const child of scene.visible())if (child.frameId === id) result.add(child.id);
31
+ }
32
+ }
33
+ }
34
+ return [
35
+ ...result
36
+ ].filter((id)=>scene.get(id) && !scene.get(id).isDeleted);
37
+ }
38
+ function hitTest(scene, point, threshold, selected = new Set()) {
39
+ const visible = scene.visible();
40
+ for(let i = visible.length - 1; i >= 0; i--){
41
+ const element = visible[i];
42
+ if (!selected.has(element.id)) continue;
43
+ const [x1, y1, x2, y2, cx, cy] = absoluteCoords(element);
44
+ const p = rotate(point, [
45
+ cx,
46
+ cy
47
+ ], -(element.angle || 0));
48
+ if (p[0] >= x1 - threshold && p[0] <= x2 + threshold && p[1] >= y1 - threshold && p[1] <= y2 + threshold) return element;
49
+ }
50
+ for(let i = visible.length - 1; i >= 0; i--){
51
+ const element = visible[i];
52
+ if (!element.locked) {
53
+ if (hitElement(element, point, threshold)) {
54
+ var _scene_get;
55
+ if ('text' === element.type && element.containerId) return null != (_scene_get = scene.get(element.containerId)) ? _scene_get : element;
56
+ return element;
57
+ }
58
+ }
59
+ }
60
+ return null;
61
+ }
62
+ function inBox(scene, box) {
63
+ const ids = [];
64
+ for (const element of scene.visible())if (!element.locked) {
65
+ if ('text' !== element.type || !element.containerId) {
66
+ if (boundsContain(box, elementBounds(element))) ids.push(element.id);
67
+ }
68
+ }
69
+ return expandSelection(scene, ids);
70
+ }
71
+ function frameFor(scene, bounds) {
72
+ const visible = scene.visible();
73
+ for(let i = visible.length - 1; i >= 0; i--){
74
+ const frame = visible[i];
75
+ if (isFrame(frame) && boundsContain(elementBounds(frame), bounds)) return frame.id;
76
+ }
77
+ return null;
78
+ }
79
+ function createShape(scene, type, box, style) {
80
+ const options = {
81
+ x: box[0],
82
+ y: box[1],
83
+ width: box[2] - box[0],
84
+ height: box[3] - box[1],
85
+ style,
86
+ index: indexAfter(scene.lastIndex())
87
+ };
88
+ if ('frame' === type) return newFrame({
89
+ ...options,
90
+ index: options.index
91
+ });
92
+ return newShape(type, {
93
+ ...options,
94
+ frameId: frameFor(scene, box)
95
+ });
96
+ }
97
+ function createLinear(scene, type, absolute, style, zoom = 1) {
98
+ const [x, y] = absolute[0];
99
+ let element = newLinear(type, {
100
+ x,
101
+ y,
102
+ style,
103
+ index: indexAfter(scene.lastIndex()),
104
+ points: absolute.map(([px, py])=>[
105
+ px - x,
106
+ py - y
107
+ ]),
108
+ startArrowhead: style.startArrowhead,
109
+ endArrowhead: style.endArrowhead,
110
+ frameId: frameFor(scene, [
111
+ Math.min(...absolute.map((p)=>p[0])),
112
+ Math.min(...absolute.map((p)=>p[1])),
113
+ Math.max(...absolute.map((p)=>p[0])),
114
+ Math.max(...absolute.map((p)=>p[1]))
115
+ ])
116
+ });
117
+ if ('arrow' !== type) return [
118
+ element
119
+ ];
120
+ const touched = new Map();
121
+ for (const end of [
122
+ 'start',
123
+ 'end'
124
+ ]){
125
+ var _touched_get, _shape_boundElements, _shape_boundElements1;
126
+ const point = 'start' === end ? absolute[0] : absolute[absolute.length - 1];
127
+ const target = bindableAt(scene, point, 12 / zoom, element.id);
128
+ if (!target) continue;
129
+ const binding = {
130
+ elementId: target.id,
131
+ focus: 0,
132
+ gap: BINDING_GAP
133
+ };
134
+ element = {
135
+ ...element,
136
+ ['start' === end ? 'startBinding' : 'endBinding']: binding
137
+ };
138
+ const shape = null != (_touched_get = touched.get(target.id)) ? _touched_get : target;
139
+ if (!(null != (_shape_boundElements = shape.boundElements) ? _shape_boundElements : []).some((bound)=>bound.id === element.id)) touched.set(target.id, bump(shape, {
140
+ boundElements: [
141
+ ...null != (_shape_boundElements1 = shape.boundElements) ? _shape_boundElements1 : [],
142
+ {
143
+ id: element.id,
144
+ type: 'arrow'
145
+ }
146
+ ]
147
+ }));
148
+ }
149
+ const lookup = (id)=>{
150
+ var _touched_get;
151
+ return null != (_touched_get = touched.get(id)) ? _touched_get : scene.get(id);
152
+ };
153
+ element = followBindings(element, lookup);
154
+ return [
155
+ element,
156
+ ...touched.values()
157
+ ];
158
+ }
159
+ function bindableAt(scene, point, threshold, except) {
160
+ const visible = scene.visible();
161
+ for(let i = visible.length - 1; i >= 0; i--){
162
+ const element = visible[i];
163
+ if (element.id === except || !isBindable(element) || 'text' === element.type && element.containerId) continue;
164
+ const [x1, y1, x2, y2, cx, cy] = absoluteCoords(element);
165
+ const p = rotate(point, [
166
+ cx,
167
+ cy
168
+ ], -(element.angle || 0));
169
+ if (p[0] >= x1 - threshold && p[0] <= x2 + threshold && p[1] >= y1 - threshold && p[1] <= y2 + threshold) return element;
170
+ }
171
+ return null;
172
+ }
173
+ function followBindings(arrow, lookup) {
174
+ var _arrow_points;
175
+ const points = (null != (_arrow_points = arrow.points) ? _arrow_points : []).map(([px, py])=>[
176
+ px + arrow.x,
177
+ py + arrow.y
178
+ ]);
179
+ if (points.length < 2) return arrow;
180
+ const start = arrow.startBinding ? lookup(arrow.startBinding.elementId) : void 0;
181
+ const end = arrow.endBinding ? lookup(arrow.endBinding.elementId) : void 0;
182
+ const centre = (element)=>{
183
+ const coords = absoluteCoords(element);
184
+ return [
185
+ coords[4],
186
+ coords[5]
187
+ ];
188
+ };
189
+ const snap = (shape, from, gap)=>{
190
+ const c = centre(shape);
191
+ const hit = intersectOutline(outlineOf(shape), from, c);
192
+ if (!hit) return null;
193
+ const length = Math.hypot(hit[0] - from[0], hit[1] - from[1]);
194
+ if (length < 1e-6) return hit;
195
+ return [
196
+ hit[0] - (hit[0] - from[0]) / length * gap,
197
+ hit[1] - (hit[1] - from[1]) / length * gap
198
+ ];
199
+ };
200
+ let changed = false;
201
+ const last = points.length - 1;
202
+ if (start && !start.isDeleted) {
203
+ var _ref;
204
+ var _arrow_startBinding;
205
+ const from = 2 === points.length && end && !end.isDeleted ? centre(end) : points[1];
206
+ const next = snap(start, from, null != (_ref = null == (_arrow_startBinding = arrow.startBinding) ? void 0 : _arrow_startBinding.gap) ? _ref : BINDING_GAP);
207
+ if (next) {
208
+ points[0] = next;
209
+ changed = true;
210
+ }
211
+ }
212
+ if (end && !end.isDeleted) {
213
+ var _ref1;
214
+ var _arrow_endBinding;
215
+ const from = 2 === points.length && start && !start.isDeleted ? centre(start) : points[last - 1];
216
+ const next = snap(end, from, null != (_ref1 = null == (_arrow_endBinding = arrow.endBinding) ? void 0 : _arrow_endBinding.gap) ? _ref1 : BINDING_GAP);
217
+ if (next) {
218
+ points[last] = next;
219
+ changed = true;
220
+ }
221
+ }
222
+ if (!changed) return arrow;
223
+ const [ox, oy] = points[0];
224
+ const relative = points.map(([px, py])=>[
225
+ px - ox,
226
+ py - oy
227
+ ]);
228
+ const size = sizeOfPoints(relative);
229
+ return {
230
+ ...arrow,
231
+ x: ox,
232
+ y: oy,
233
+ points: relative,
234
+ width: size.width,
235
+ height: size.height
236
+ };
237
+ }
238
+ function createFreeDraw(scene, flat, pressures, style) {
239
+ const x = flat[0];
240
+ const y = flat[1];
241
+ const points = [];
242
+ for(let i = 0; i + 1 < flat.length; i += 2)points.push([
243
+ flat[i] - x,
244
+ flat[i + 1] - y
245
+ ]);
246
+ if (1 === points.length) points.push([
247
+ 0.0001,
248
+ 0.0001
249
+ ]);
250
+ const bounds = [
251
+ Math.min(...points.map((p)=>p[0])) + x,
252
+ Math.min(...points.map((p)=>p[1])) + y,
253
+ Math.max(...points.map((p)=>p[0])) + x,
254
+ Math.max(...points.map((p)=>p[1])) + y
255
+ ];
256
+ return newFreeDraw({
257
+ x,
258
+ y,
259
+ style: {
260
+ ...style,
261
+ roundness: 'sharp'
262
+ },
263
+ index: indexAfter(scene.lastIndex()),
264
+ points,
265
+ pressures: pressures ? [
266
+ ...pressures
267
+ ].slice(0, points.length) : [],
268
+ simulatePressure: !pressures,
269
+ frameId: frameFor(scene, bounds)
270
+ });
271
+ }
272
+ function createText(scene, x, y, text, style, container) {
273
+ var _container_index, _container_boundElements;
274
+ if (!container) {
275
+ const element = newText({
276
+ x,
277
+ y,
278
+ text,
279
+ style,
280
+ index: indexAfter(scene.lastIndex())
281
+ });
282
+ return [
283
+ {
284
+ ...element,
285
+ frameId: frameFor(scene, elementBounds(element))
286
+ }
287
+ ];
288
+ }
289
+ const label = newText({
290
+ x,
291
+ y,
292
+ text,
293
+ style: {
294
+ ...style,
295
+ textAlign: 'center'
296
+ },
297
+ verticalAlign: 'middle',
298
+ containerId: container.id,
299
+ index: indexAfter(null != (_container_index = container.index) ? _container_index : scene.lastIndex()),
300
+ autoResize: true
301
+ });
302
+ const placed = placeLabel(label, container);
303
+ return [
304
+ placed,
305
+ bump(container, {
306
+ boundElements: [
307
+ ...(null != (_container_boundElements = container.boundElements) ? _container_boundElements : []).filter((bound)=>'text' !== bound.type),
308
+ {
309
+ id: label.id,
310
+ type: 'text'
311
+ }
312
+ ]
313
+ })
314
+ ];
315
+ }
316
+ function placeLabel(label, container) {
317
+ const [x1, y1, x2, y2] = absoluteCoords(container);
318
+ const cx = (x1 + x2) / 2;
319
+ const cy = (y1 + y2) / 2;
320
+ return {
321
+ ...label,
322
+ x: cx - label.width / 2,
323
+ y: cy - label.height / 2,
324
+ angle: container.angle || 0
325
+ };
326
+ }
327
+ function editText(scene, id, value) {
328
+ var _element_fontSize, _element_fontFamily, _element_lineHeight;
329
+ const element = scene.get(id);
330
+ if (!element) return [];
331
+ const text = normalizeText(value);
332
+ if (!text.trim()) return deleteElements(scene, [
333
+ id
334
+ ]);
335
+ const metrics = measureText(text, null != (_element_fontSize = element.fontSize) ? _element_fontSize : 20, null != (_element_fontFamily = element.fontFamily) ? _element_fontFamily : 5, null != (_element_lineHeight = element.lineHeight) ? _element_lineHeight : 1.25);
336
+ let next = bump(element, {
337
+ text,
338
+ originalText: text,
339
+ width: metrics.width,
340
+ height: metrics.height
341
+ });
342
+ if ('center' !== element.textAlign || element.containerId) {
343
+ if ('right' === element.textAlign && !element.containerId) next = {
344
+ ...next,
345
+ x: element.x + element.width - metrics.width
346
+ };
347
+ } else next = {
348
+ ...next,
349
+ x: element.x + (element.width - metrics.width) / 2
350
+ };
351
+ const container = element.containerId ? scene.get(element.containerId) : void 0;
352
+ if (container) next = placeLabel(next, container);
353
+ return [
354
+ next
355
+ ];
356
+ }
357
+ function createImage(scene, fileId, x, y, width, height, style) {
358
+ return newImage({
359
+ x,
360
+ y,
361
+ width,
362
+ height,
363
+ style,
364
+ fileId,
365
+ index: indexAfter(scene.lastIndex()),
366
+ frameId: frameFor(scene, [
367
+ x,
368
+ y,
369
+ x + width,
370
+ y + height
371
+ ])
372
+ });
373
+ }
374
+ function followers(scene, updated, moving) {
375
+ var _element_boundElements;
376
+ const lookup = (id)=>{
377
+ var _updated_get;
378
+ return null != (_updated_get = updated.get(id)) ? _updated_get : scene.get(id);
379
+ };
380
+ const arrows = new Set();
381
+ for (const element of updated.values())for (const bound of null != (_element_boundElements = element.boundElements) ? _element_boundElements : [])if ('arrow' === bound.type && !moving.has(bound.id)) arrows.add(bound.id);
382
+ const result = [];
383
+ for (const id of arrows){
384
+ const arrow = scene.get(id);
385
+ if (!arrow || arrow.isDeleted) continue;
386
+ const next = followBindings(arrow, lookup);
387
+ if (next !== arrow) result.push(bump(arrow, {
388
+ x: next.x,
389
+ y: next.y,
390
+ points: next.points,
391
+ width: next.width,
392
+ height: next.height
393
+ }));
394
+ }
395
+ for (const element of updated.values()){
396
+ var _element_boundElements1;
397
+ for (const bound of null != (_element_boundElements1 = element.boundElements) ? _element_boundElements1 : []){
398
+ if ('text' !== bound.type || updated.has(bound.id)) continue;
399
+ const label = scene.get(bound.id);
400
+ if (label && !label.isDeleted) {
401
+ const placed = placeLabel(label, element);
402
+ if (placed.x !== label.x || placed.y !== label.y || placed.angle !== label.angle) result.push(bump(label, {
403
+ x: placed.x,
404
+ y: placed.y,
405
+ angle: placed.angle
406
+ }));
407
+ }
408
+ }
409
+ }
410
+ return result;
411
+ }
412
+ function unbindMoved(scene, arrow, moving) {
413
+ const shapes = [];
414
+ let next = arrow;
415
+ for (const end of [
416
+ 'startBinding',
417
+ 'endBinding'
418
+ ]){
419
+ var _shape_boundElements;
420
+ var _arrow_endBinding;
421
+ const binding = arrow[end];
422
+ if (!binding || moving.has(binding.elementId)) continue;
423
+ next = {
424
+ ...next,
425
+ [end]: null
426
+ };
427
+ const shape = scene.get(binding.elementId);
428
+ const stillBound = 'startBinding' === end ? (null == (_arrow_endBinding = arrow.endBinding) ? void 0 : _arrow_endBinding.elementId) === binding.elementId && !moving.has(binding.elementId) : false;
429
+ if (shape && !stillBound && !shapes.some((s)=>s.id === shape.id)) shapes.push(bump(shape, {
430
+ boundElements: (null != (_shape_boundElements = shape.boundElements) ? _shape_boundElements : []).filter((bound)=>bound.id !== arrow.id)
431
+ }));
432
+ }
433
+ return {
434
+ arrow: next,
435
+ shapes
436
+ };
437
+ }
438
+ function moveElements(scene, ids, dx, dy) {
439
+ const moving = new Set(movingSet(scene, ids));
440
+ const updated = new Map();
441
+ const extra = [];
442
+ for (const id of moving){
443
+ const element = scene.get(id);
444
+ if (!element || element.locked) continue;
445
+ let next = {
446
+ ...element,
447
+ x: element.x + dx,
448
+ y: element.y + dy
449
+ };
450
+ if ('arrow' === element.type) {
451
+ const result = unbindMoved(scene, next, moving);
452
+ next = result.arrow;
453
+ extra.push(...result.shapes);
454
+ }
455
+ updated.set(id, next);
456
+ }
457
+ for (const [id, element] of updated){
458
+ if (isFrame(element) || element.containerId) continue;
459
+ if (element.frameId && moving.has(element.frameId)) continue;
460
+ const frame = frameFor(scene, elementBounds(element));
461
+ if (frame !== element.frameId && frame !== id) updated.set(id, {
462
+ ...element,
463
+ frameId: frame
464
+ });
465
+ }
466
+ const bumped = new Map([
467
+ ...updated
468
+ ].map(([id, element])=>[
469
+ id,
470
+ bump(scene.get(id), element)
471
+ ]));
472
+ for (const shape of extra)if (!bumped.has(shape.id)) bumped.set(shape.id, shape);
473
+ return [
474
+ ...bumped.values(),
475
+ ...followers(scene, bumped, moving)
476
+ ];
477
+ }
478
+ function resizeElements(scene, ids, from, to, angle = 0) {
479
+ const fw = from[2] - from[0] || 1;
480
+ const fh = from[3] - from[1] || 1;
481
+ const sx = (to[2] - to[0]) / fw;
482
+ const sy = (to[3] - to[1]) / fh;
483
+ if (!Number.isFinite(sx) || !Number.isFinite(sy) || Math.abs(sx) < 1e-3 || Math.abs(sy) < 1e-3) return [];
484
+ const moving = new Set(movingSet(scene, ids));
485
+ const oldCentre = [
486
+ (from[0] + from[2]) / 2,
487
+ (from[1] + from[3]) / 2
488
+ ];
489
+ const updated = new Map();
490
+ for (const id of moving){
491
+ const element = scene.get(id);
492
+ if (!element || element.locked || 'text' === element.type && element.containerId && moving.has(element.containerId)) continue;
493
+ const [x1, y1, x2, y2, cx, cy] = absoluteCoords(element);
494
+ const local = angle ? rotate([
495
+ cx,
496
+ cy
497
+ ], oldCentre, -angle) : [
498
+ cx,
499
+ cy
500
+ ];
501
+ const mapped = [
502
+ to[0] + (local[0] - from[0]) * sx,
503
+ to[1] + (local[1] - from[1]) * sy
504
+ ];
505
+ const newCentre = angle ? rotate(mapped, oldCentre, angle) : mapped;
506
+ const w = Math.abs((x2 - x1) * sx);
507
+ const h = Math.abs((y2 - y1) * sy);
508
+ let next;
509
+ if (isPointsElement(element)) {
510
+ var _element_points;
511
+ const points = (null != (_element_points = element.points) ? _element_points : []).map(([px, py])=>[
512
+ px * sx,
513
+ py * sy
514
+ ]);
515
+ const size = sizeOfPoints(points);
516
+ const minX = Math.min(...points.map((p)=>p[0]));
517
+ const minY = Math.min(...points.map((p)=>p[1]));
518
+ next = {
519
+ ...element,
520
+ points,
521
+ width: size.width,
522
+ height: size.height,
523
+ x: newCentre[0] - size.width / 2 - minX,
524
+ y: newCentre[1] - size.height / 2 - minY
525
+ };
526
+ } else if ('text' === element.type) {
527
+ var _element_fontSize, _element_text, _element_fontFamily, _element_lineHeight;
528
+ const scale = Math.max(Math.abs(sx), Math.abs(sy)) === Math.abs(sx) && 1 === ids.length ? Math.abs(sx) : Math.min(Math.abs(sx), Math.abs(sy));
529
+ const fontSize = Math.max(1, (null != (_element_fontSize = element.fontSize) ? _element_fontSize : 20) * scale);
530
+ const metrics = measureText(null != (_element_text = element.text) ? _element_text : '', fontSize, null != (_element_fontFamily = element.fontFamily) ? _element_fontFamily : 5, null != (_element_lineHeight = element.lineHeight) ? _element_lineHeight : 1.25);
531
+ next = {
532
+ ...element,
533
+ fontSize,
534
+ width: metrics.width,
535
+ height: metrics.height,
536
+ x: newCentre[0] - metrics.width / 2,
537
+ y: newCentre[1] - metrics.height / 2
538
+ };
539
+ } else next = {
540
+ ...element,
541
+ width: w,
542
+ height: h,
543
+ x: newCentre[0] - w / 2,
544
+ y: newCentre[1] - h / 2
545
+ };
546
+ updated.set(id, next);
547
+ }
548
+ for (const [id, element] of updated){
549
+ var _element_boundElements;
550
+ for (const bound of null != (_element_boundElements = element.boundElements) ? _element_boundElements : []){
551
+ var _updated_get;
552
+ if ('text' !== bound.type) continue;
553
+ const label = null != (_updated_get = updated.get(bound.id)) ? _updated_get : scene.get(bound.id);
554
+ if (label && !label.isDeleted) updated.set(bound.id, placeLabel(label, element));
555
+ }
556
+ }
557
+ const bumped = new Map([
558
+ ...updated
559
+ ].map(([id, element])=>[
560
+ id,
561
+ bump(scene.get(id), element)
562
+ ]));
563
+ return [
564
+ ...bumped.values(),
565
+ ...followers(scene, bumped, moving)
566
+ ];
567
+ }
568
+ function rotateElements(scene, ids, delta, pivot) {
569
+ const moving = new Set(movingSet(scene, ids));
570
+ const updated = new Map();
571
+ for (const id of moving){
572
+ const element = scene.get(id);
573
+ if (!element || element.locked || isFrame(element)) continue;
574
+ const [, , , , cx, cy] = absoluteCoords(element);
575
+ const [nx, ny] = rotate([
576
+ cx,
577
+ cy
578
+ ], pivot, delta);
579
+ let angle = ((element.angle || 0) + delta) % (2 * Math.PI);
580
+ if (angle < 0) angle += 2 * Math.PI;
581
+ updated.set(id, {
582
+ ...element,
583
+ x: element.x + (nx - cx),
584
+ y: element.y + (ny - cy),
585
+ angle
586
+ });
587
+ }
588
+ const bumped = new Map([
589
+ ...updated
590
+ ].map(([id, element])=>[
591
+ id,
592
+ bump(scene.get(id), element)
593
+ ]));
594
+ return [
595
+ ...bumped.values(),
596
+ ...followers(scene, bumped, moving)
597
+ ];
598
+ }
599
+ function movePoint(scene, id, index, x, y, zoom = 1) {
600
+ var _element_points;
601
+ const element = scene.get(id);
602
+ if (!element || !isLinear(element) || !(null == (_element_points = element.points) ? void 0 : _element_points[index])) return [];
603
+ const [, , , , cx, cy] = absoluteCoords(element);
604
+ const absolute = element.points.map(([px, py])=>rotate([
605
+ px + element.x,
606
+ py + element.y
607
+ ], [
608
+ cx,
609
+ cy
610
+ ], element.angle || 0));
611
+ absolute[index] = [
612
+ x,
613
+ y
614
+ ];
615
+ const [ox, oy] = absolute[0];
616
+ const points = absolute.map(([px, py])=>[
617
+ px - ox,
618
+ py - oy
619
+ ]);
620
+ const size = sizeOfPoints(points);
621
+ let next = {
622
+ ...element,
623
+ x: ox,
624
+ y: oy,
625
+ angle: 0,
626
+ points,
627
+ width: size.width,
628
+ height: size.height
629
+ };
630
+ const touched = [];
631
+ if ('arrow' === element.type && (0 === index || index === absolute.length - 1)) {
632
+ var _target_boundElements, _target_boundElements1;
633
+ const key = 0 === index ? 'startBinding' : 'endBinding';
634
+ const previous = element[key];
635
+ const target = bindableAt(scene, [
636
+ x,
637
+ y
638
+ ], 12 / zoom, id);
639
+ if (previous && previous.elementId !== (null == target ? void 0 : target.id)) {
640
+ var _shape_boundElements;
641
+ const shape = scene.get(previous.elementId);
642
+ const other = 0 === index ? element.endBinding : element.startBinding;
643
+ if (shape && (null == other ? void 0 : other.elementId) !== shape.id) touched.push(bump(shape, {
644
+ boundElements: (null != (_shape_boundElements = shape.boundElements) ? _shape_boundElements : []).filter((bound)=>bound.id !== id)
645
+ }));
646
+ }
647
+ next = {
648
+ ...next,
649
+ [key]: target ? {
650
+ elementId: target.id,
651
+ focus: 0,
652
+ gap: BINDING_GAP
653
+ } : null
654
+ };
655
+ if (target && (null == previous ? void 0 : previous.elementId) !== target.id && !(null != (_target_boundElements = target.boundElements) ? _target_boundElements : []).some((bound)=>bound.id === id)) touched.push(bump(target, {
656
+ boundElements: [
657
+ ...null != (_target_boundElements1 = target.boundElements) ? _target_boundElements1 : [],
658
+ {
659
+ id,
660
+ type: 'arrow'
661
+ }
662
+ ]
663
+ }));
664
+ if (target) next = followBindings(next, (other)=>{
665
+ var _touched_find;
666
+ return null != (_touched_find = touched.find((t)=>t.id === other)) ? _touched_find : scene.get(other);
667
+ });
668
+ }
669
+ return [
670
+ bump(element, next),
671
+ ...touched
672
+ ];
673
+ }
674
+ function deleteElements(scene, ids) {
675
+ const doomed = new Set();
676
+ for (const id of ids){
677
+ var _element_boundElements;
678
+ const element = scene.get(id);
679
+ if (element && !element.isDeleted) {
680
+ doomed.add(id);
681
+ for (const bound of null != (_element_boundElements = element.boundElements) ? _element_boundElements : [])if ('text' === bound.type) doomed.add(bound.id);
682
+ if (isFrame(element)) {
683
+ for (const child of scene.visible())if (child.frameId === id) doomed.add(child.id);
684
+ }
685
+ }
686
+ }
687
+ const result = new Map();
688
+ for (const id of doomed)result.set(id, bump(scene.get(id), {
689
+ isDeleted: true
690
+ }));
691
+ for (const element of scene.visible()){
692
+ var _element_boundElements1;
693
+ if (doomed.has(element.id)) continue;
694
+ let changes = {};
695
+ if (element.startBinding && doomed.has(element.startBinding.elementId)) changes = {
696
+ ...changes,
697
+ startBinding: null
698
+ };
699
+ if (element.endBinding && doomed.has(element.endBinding.elementId)) changes = {
700
+ ...changes,
701
+ endBinding: null
702
+ };
703
+ if (null == (_element_boundElements1 = element.boundElements) ? void 0 : _element_boundElements1.some((bound)=>doomed.has(bound.id))) changes = {
704
+ ...changes,
705
+ boundElements: element.boundElements.filter((bound)=>!doomed.has(bound.id))
706
+ };
707
+ if (element.containerId && doomed.has(element.containerId)) changes = {
708
+ ...changes,
709
+ containerId: null
710
+ };
711
+ if (Object.keys(changes).length) result.set(element.id, bump(element, changes));
712
+ }
713
+ return [
714
+ ...result.values()
715
+ ];
716
+ }
717
+ function styleElements(scene, ids, patch) {
718
+ var _ref;
719
+ var _scene_get;
720
+ const result = [];
721
+ const targets = new Set(ids);
722
+ for (const id of ids)for (const bound of null != (_ref = null == (_scene_get = scene.get(id)) ? void 0 : _scene_get.boundElements) ? _ref : [])if ('text' === bound.type) targets.add(bound.id);
723
+ for (const id of targets){
724
+ const element = scene.get(id);
725
+ if (!element || element.isDeleted || element.locked) continue;
726
+ const changes = {};
727
+ for (const [key, value] of Object.entries(patch)){
728
+ if (('fontSize' === key || 'fontFamily' === key || 'textAlign' === key) && 'text' !== element.type) continue;
729
+ if (('startArrowhead' === key || 'endArrowhead' === key) && 'arrow' !== element.type) continue;
730
+ if ('backgroundColor' !== key && 'fillStyle' !== key || 'text' !== element.type && 'image' !== element.type && 'arrow' !== element.type) {
731
+ if ('strokeColor' !== key || 'image' !== element.type) {
732
+ if ('roundness' !== key || 'rectangle' === element.type || 'diamond' === element.type || 'line' === element.type || 'arrow' === element.type || 'image' === element.type) changes[key] = value;
733
+ }
734
+ }
735
+ }
736
+ if (Object.keys(changes).length) {
737
+ if ('text' === element.type && (void 0 !== changes.fontSize || void 0 !== changes.fontFamily)) {
738
+ var _ref1, _changes_fontSize, _ref2, _changes_fontFamily, _fontFamily, _element_lineHeight, _element_text;
739
+ const fontSize = null != (_ref1 = null != (_changes_fontSize = changes.fontSize) ? _changes_fontSize : element.fontSize) ? _ref1 : 20;
740
+ const fontFamily = null != (_ref2 = null != (_changes_fontFamily = changes.fontFamily) ? _changes_fontFamily : element.fontFamily) ? _ref2 : 5;
741
+ const lineHeight = void 0 !== changes.fontFamily ? null != (_fontFamily = ({
742
+ 5: 1.25,
743
+ 6: 1.35,
744
+ 7: 1.15,
745
+ 8: 1.25,
746
+ 1: 1.25,
747
+ 2: 1.15,
748
+ 3: 1.2,
749
+ 9: 1.15
750
+ })[fontFamily]) ? _fontFamily : 1.25 : null != (_element_lineHeight = element.lineHeight) ? _element_lineHeight : 1.25;
751
+ const metrics = measureText(null != (_element_text = element.text) ? _element_text : '', fontSize, fontFamily, lineHeight);
752
+ Object.assign(changes, {
753
+ lineHeight,
754
+ width: metrics.width,
755
+ height: metrics.height
756
+ });
757
+ const container = element.containerId ? scene.get(element.containerId) : void 0;
758
+ if (container) Object.assign(changes, placeLabel({
759
+ ...element,
760
+ ...changes
761
+ }, container));
762
+ }
763
+ result.push(bump(element, changes));
764
+ }
765
+ }
766
+ return result;
767
+ }
768
+ function reorder(scene, ids, move) {
769
+ const all = scene.all().filter((element)=>!element.isDeleted);
770
+ const chosen = all.filter((element)=>ids.includes(element.id));
771
+ if (!chosen.length) return [];
772
+ const rest = all.filter((element)=>!ids.includes(element.id));
773
+ let lower = null;
774
+ let upper = null;
775
+ if ('front' === move) lower = rest.length ? rest[rest.length - 1].index : null;
776
+ else if ('back' === move) upper = rest.length ? rest[0].index : null;
777
+ else if ('forward' === move) {
778
+ var _ref;
779
+ var _rest_;
780
+ const top = all.indexOf(chosen[chosen.length - 1]);
781
+ const above = all.slice(top + 1).find((element)=>!ids.includes(element.id));
782
+ if (!above) return [];
783
+ const position = rest.indexOf(above);
784
+ lower = above.index;
785
+ upper = null != (_ref = null == (_rest_ = rest[position + 1]) ? void 0 : _rest_.index) ? _ref : null;
786
+ } else {
787
+ var _ref1;
788
+ var _rest_1;
789
+ const bottom = all.indexOf(chosen[0]);
790
+ const below = [
791
+ ...all.slice(0, bottom)
792
+ ].reverse().find((element)=>!ids.includes(element.id));
793
+ if (!below) return [];
794
+ const position = rest.indexOf(below);
795
+ upper = below.index;
796
+ lower = null != (_ref1 = null == (_rest_1 = rest[position - 1]) ? void 0 : _rest_1.index) ? _ref1 : null;
797
+ }
798
+ const keys = indicesBetween(lower, upper, chosen.length);
799
+ return chosen.sort(compareOrder).map((element, i)=>bump(element, {
800
+ index: keys[i]
801
+ }));
802
+ }
803
+ function duplicate(scene, ids, offset = 10) {
804
+ const moving = movingSet(scene, ids);
805
+ const originals = scene.all().filter((element)=>moving.includes(element.id) && !element.isDeleted);
806
+ const idMap = new Map(originals.map((element)=>[
807
+ element.id,
808
+ randomId()
809
+ ]));
810
+ const groupMap = new Map();
811
+ const keys = indicesBetween(scene.lastIndex(), null, originals.length);
812
+ const elements = originals.map((element, i)=>{
813
+ var _element_groupIds, _idMap_get, _idMap_get1;
814
+ const copy = {
815
+ ...element,
816
+ id: idMap.get(element.id),
817
+ x: element.x + offset,
818
+ y: element.y + offset,
819
+ index: keys[i],
820
+ seed: Math.floor(Math.random() * 2 ** 31),
821
+ version: 1,
822
+ versionNonce: Math.floor(Math.random() * 2 ** 31),
823
+ updated: Date.now(),
824
+ groupIds: (null != (_element_groupIds = element.groupIds) ? _element_groupIds : []).map((group)=>{
825
+ var _groupMap_get;
826
+ return null != (_groupMap_get = groupMap.get(group)) ? _groupMap_get : groupMap.set(group, randomId()).get(group);
827
+ }),
828
+ boundElements: element.boundElements ? element.boundElements.filter((bound)=>idMap.has(bound.id)).map((bound)=>({
829
+ ...bound,
830
+ id: idMap.get(bound.id)
831
+ })) : null,
832
+ containerId: element.containerId ? null != (_idMap_get = idMap.get(element.containerId)) ? _idMap_get : null : element.containerId,
833
+ frameId: element.frameId ? null != (_idMap_get1 = idMap.get(element.frameId)) ? _idMap_get1 : element.frameId : null
834
+ };
835
+ if (copy.startBinding) copy.startBinding = idMap.has(copy.startBinding.elementId) ? {
836
+ ...copy.startBinding,
837
+ elementId: idMap.get(copy.startBinding.elementId)
838
+ } : null;
839
+ if (copy.endBinding) copy.endBinding = idMap.has(copy.endBinding.elementId) ? {
840
+ ...copy.endBinding,
841
+ elementId: idMap.get(copy.endBinding.elementId)
842
+ } : null;
843
+ return copy;
844
+ });
845
+ return {
846
+ elements,
847
+ selected: ids.map((id)=>idMap.get(id)).filter((id)=>Boolean(id))
848
+ };
849
+ }
850
+ function erasedBy(scene, flat, threshold) {
851
+ const hit = new Set();
852
+ const visible = scene.visible();
853
+ for(let i = 0; i + 1 < flat.length; i += 2){
854
+ const point = [
855
+ flat[i],
856
+ flat[i + 1]
857
+ ];
858
+ for (const element of visible)if (!hit.has(element.id) && !element.locked) {
859
+ if (hitElement(element, point, threshold)) {
860
+ hit.add(element.id);
861
+ if (element.containerId) hit.add(element.containerId);
862
+ }
863
+ }
864
+ }
865
+ return [
866
+ ...hit
867
+ ];
868
+ }
869
+ function edit_group(scene, ids) {
870
+ if (ids.length < 2) return [];
871
+ const groupId = randomId();
872
+ return ids.map((id)=>scene.get(id)).filter((element)=>Boolean(element)).map((element)=>{
873
+ var _element_groupIds;
874
+ return bump(element, {
875
+ groupIds: [
876
+ ...null != (_element_groupIds = element.groupIds) ? _element_groupIds : [],
877
+ groupId
878
+ ]
879
+ });
880
+ });
881
+ }
882
+ function ungroup(scene, ids) {
883
+ return ids.map((id)=>scene.get(id)).filter((element)=>{
884
+ var _element_groupIds;
885
+ return Boolean(null == element ? void 0 : null == (_element_groupIds = element.groupIds) ? void 0 : _element_groupIds.length);
886
+ }).map((element)=>bump(element, {
887
+ groupIds: element.groupIds.slice(0, -1)
888
+ }));
889
+ }
890
+ function lockElements(scene, ids, locked) {
891
+ return ids.map((id)=>scene.get(id)).filter((element)=>Boolean(element)).map((element)=>bump(element, {
892
+ locked
893
+ }));
894
+ }
895
+ export { createFreeDraw, createImage, createLinear, createShape, createText, deleteElements, duplicate, editText, edit_group as group, erasedBy, expandSelection, followBindings, hitTest, inBox, lockElements, moveElements, movePoint, movingSet, placeLabel, reorder, resizeElements, rotateElements, styleElements, ungroup };
896
+
897
+ //# sourceMappingURL=edit.js.map