@tldraw/editor 4.5.0-canary.034ea85352da → 4.5.0-canary.0d748e7df5db

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 (38) hide show
  1. package/dist-cjs/index.d.ts +24 -1
  2. package/dist-cjs/index.js +1 -1
  3. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +13 -16
  4. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  5. package/dist-cjs/lib/editor/Editor.js +206 -254
  6. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  7. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js +15 -16
  8. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js.map +3 -3
  9. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +1 -1
  10. package/dist-cjs/lib/hooks/useCanvasEvents.js +1 -1
  11. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  12. package/dist-cjs/lib/primitives/geometry/Geometry2d.js +10 -0
  13. package/dist-cjs/lib/primitives/geometry/Geometry2d.js.map +2 -2
  14. package/dist-cjs/version.js +3 -3
  15. package/dist-cjs/version.js.map +1 -1
  16. package/dist-esm/index.d.mts +24 -1
  17. package/dist-esm/index.mjs +1 -1
  18. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +13 -16
  19. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  20. package/dist-esm/lib/editor/Editor.mjs +207 -255
  21. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  22. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs +15 -16
  23. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs.map +3 -3
  24. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +1 -1
  25. package/dist-esm/lib/hooks/useCanvasEvents.mjs +1 -1
  26. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  27. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs +10 -0
  28. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs.map +2 -2
  29. package/dist-esm/version.mjs +3 -3
  30. package/dist-esm/version.mjs.map +1 -1
  31. package/package.json +7 -7
  32. package/src/lib/components/default-components/DefaultCanvas.tsx +19 -26
  33. package/src/lib/editor/Editor.ts +283 -393
  34. package/src/lib/editor/managers/HistoryManager/HistoryManager.ts +6 -5
  35. package/src/lib/editor/shapes/ShapeUtil.ts +1 -1
  36. package/src/lib/hooks/useCanvasEvents.ts +1 -1
  37. package/src/lib/primitives/geometry/Geometry2d.ts +12 -0
  38. package/src/version.ts +3 -3
@@ -139,7 +139,7 @@ import {
139
139
  } from "../utils/deepLinks.mjs";
140
140
  import { getIncrementedName } from "../utils/getIncrementedName.mjs";
141
141
  import { getReorderingShapesChanges } from "../utils/reorderShapes.mjs";
142
- import { getDroppedShapesToNewParents } from "../utils/reparenting.mjs";
142
+ import { getDroppedShapesToNewParents, kickoutOccludedShapes } from "../utils/reparenting.mjs";
143
143
  import { applyRotationToSnapshotShapes, getRotationSnapshot } from "../utils/rotation.mjs";
144
144
  import { bindingsIndex } from "./derivations/bindingsIndex.mjs";
145
145
  import { notVisibleShapes } from "./derivations/notVisibleShapes.mjs";
@@ -4023,6 +4023,9 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
4023
4023
  if (geometry.isClosed) {
4024
4024
  if (distance <= outerMargin || hitInside && distance <= 0 && distance > -innerMargin) {
4025
4025
  if (geometry.isFilled || isGroup && geometry.children[0].isFilled) {
4026
+ if (geometry.ignoreHit(pointInShapeSpace)) {
4027
+ continue;
4028
+ }
4026
4029
  return inMarginClosestToEdgeHit || shape;
4027
4030
  } else {
4028
4031
  if (this.getShapePageBounds(shape).contains(viewportPageBounds)) continue;
@@ -5018,6 +5021,53 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5018
5021
  if (changes) this.updateShapes(changes);
5019
5022
  return this;
5020
5023
  }
5024
+ /**
5025
+ * Shared clustering logic for layout methods. Resolves shapes, optionally filters to
5026
+ * axis-aligned shapes, checks canBeLaidOut, and groups shapes into clusters via arrow bindings.
5027
+ *
5028
+ * @internal
5029
+ */
5030
+ getShapeClusters(shapes, type, opts) {
5031
+ const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5032
+ let freshShapes = compact(ids.map((id) => this.getShape(id)));
5033
+ if (opts?.filterAxisAligned) {
5034
+ freshShapes = freshShapes.filter(
5035
+ (s) => this.getShapePageTransform(s)?.rotation() % (PI / 2) === 0
5036
+ );
5037
+ }
5038
+ const clusters = [];
5039
+ const allBounds = [];
5040
+ const visited = /* @__PURE__ */ new Set();
5041
+ for (const shape of freshShapes) {
5042
+ if (visited.has(shape.id)) continue;
5043
+ visited.add(shape.id);
5044
+ const shapePageBounds = this.getShapePageBounds(shape);
5045
+ if (!shapePageBounds) continue;
5046
+ if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5047
+ type,
5048
+ shapes: freshShapes
5049
+ })) {
5050
+ continue;
5051
+ }
5052
+ const shapesMovingTogether = [shape];
5053
+ const boundsOfShapesMovingTogether = [shapePageBounds];
5054
+ this.collectShapesViaArrowBindings({
5055
+ bindings: this.getBindingsToShape(shape.id, "arrow"),
5056
+ initialShapes: freshShapes,
5057
+ resultShapes: shapesMovingTogether,
5058
+ resultBounds: boundsOfShapesMovingTogether,
5059
+ visited
5060
+ });
5061
+ const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5062
+ if (!commonPageBounds) continue;
5063
+ clusters.push({
5064
+ shapes: shapesMovingTogether,
5065
+ pageBounds: commonPageBounds
5066
+ });
5067
+ allBounds.push(commonPageBounds);
5068
+ }
5069
+ return { clusters, allBounds };
5070
+ }
5021
5071
  /**
5022
5072
  * @internal
5023
5073
  */
@@ -5128,40 +5178,8 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5128
5178
  */
5129
5179
  stackShapes(shapes, operation, gap) {
5130
5180
  const _gap = gap ?? this.options.adjacentShapeMargin;
5131
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5132
5181
  if (this.getIsReadonly()) return this;
5133
- const shapesToStackFirstPass = compact(ids.map((id) => this.getShape(id)));
5134
- const shapeClustersToStack = [];
5135
- const allBounds = [];
5136
- const visited = /* @__PURE__ */ new Set();
5137
- for (const shape of shapesToStackFirstPass) {
5138
- if (visited.has(shape.id)) continue;
5139
- visited.add(shape.id);
5140
- const shapePageBounds = this.getShapePageBounds(shape);
5141
- if (!shapePageBounds) continue;
5142
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5143
- type: "stack",
5144
- shapes: shapesToStackFirstPass
5145
- })) {
5146
- continue;
5147
- }
5148
- const shapesMovingTogether = [shape];
5149
- const boundsOfShapesMovingTogether = [shapePageBounds];
5150
- this.collectShapesViaArrowBindings({
5151
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5152
- initialShapes: shapesToStackFirstPass,
5153
- resultShapes: shapesMovingTogether,
5154
- resultBounds: boundsOfShapesMovingTogether,
5155
- visited
5156
- });
5157
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5158
- if (!commonPageBounds) continue;
5159
- shapeClustersToStack.push({
5160
- shapes: shapesMovingTogether,
5161
- pageBounds: commonPageBounds
5162
- });
5163
- allBounds.push(commonPageBounds);
5164
- }
5182
+ const { clusters: shapeClustersToStack } = this.getShapeClusters(shapes, "stack");
5165
5183
  const len = shapeClustersToStack.length;
5166
5184
  if (_gap === 0 && len < 3 || len < 2) return this;
5167
5185
  let val;
@@ -5247,40 +5265,11 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5247
5265
  packShapes(shapes, _gap) {
5248
5266
  if (this.getIsReadonly()) return this;
5249
5267
  const gap = _gap ?? this.options.adjacentShapeMargin;
5250
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5251
- const shapesToPackFirstPass = compact(ids.map((id) => this.getShape(id)));
5252
- const shapeClustersToPack = [];
5253
- const allBounds = [];
5254
- const visited = /* @__PURE__ */ new Set();
5255
- for (const shape of shapesToPackFirstPass) {
5256
- if (visited.has(shape.id)) continue;
5257
- visited.add(shape.id);
5258
- const shapePageBounds = this.getShapePageBounds(shape);
5259
- if (!shapePageBounds) continue;
5260
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5261
- type: "pack",
5262
- shapes: shapesToPackFirstPass
5263
- })) {
5264
- continue;
5265
- }
5266
- const shapesMovingTogether = [shape];
5267
- const boundsOfShapesMovingTogether = [shapePageBounds];
5268
- this.collectShapesViaArrowBindings({
5269
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5270
- initialShapes: shapesToPackFirstPass,
5271
- resultShapes: shapesMovingTogether,
5272
- resultBounds: boundsOfShapesMovingTogether,
5273
- visited
5274
- });
5275
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5276
- if (!commonPageBounds) continue;
5277
- shapeClustersToPack.push({
5278
- shapes: shapesMovingTogether,
5279
- pageBounds: commonPageBounds,
5280
- nextPageBounds: commonPageBounds.clone()
5281
- });
5282
- allBounds.push(commonPageBounds);
5283
- }
5268
+ const { clusters, allBounds } = this.getShapeClusters(shapes, "pack");
5269
+ const shapeClustersToPack = clusters.map((cluster) => ({
5270
+ ...cluster,
5271
+ nextPageBounds: cluster.pageBounds.clone()
5272
+ }));
5284
5273
  if (shapeClustersToPack.length < 2) return this;
5285
5274
  let area = 0;
5286
5275
  for (const { pageBounds } of shapeClustersToPack) {
@@ -5364,39 +5353,7 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5364
5353
  */
5365
5354
  alignShapes(shapes, operation) {
5366
5355
  if (this.getIsReadonly()) return this;
5367
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5368
- const shapesToAlignFirstPass = compact(ids.map((id) => this.getShape(id)));
5369
- const shapeClustersToAlign = [];
5370
- const allBounds = [];
5371
- const visited = /* @__PURE__ */ new Set();
5372
- for (const shape of shapesToAlignFirstPass) {
5373
- if (visited.has(shape.id)) continue;
5374
- visited.add(shape.id);
5375
- const shapePageBounds = this.getShapePageBounds(shape);
5376
- if (!shapePageBounds) continue;
5377
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5378
- type: "align",
5379
- shapes: shapesToAlignFirstPass
5380
- })) {
5381
- continue;
5382
- }
5383
- const shapesMovingTogether = [shape];
5384
- const boundsOfShapesMovingTogether = [shapePageBounds];
5385
- this.collectShapesViaArrowBindings({
5386
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5387
- initialShapes: shapesToAlignFirstPass,
5388
- resultShapes: shapesMovingTogether,
5389
- resultBounds: boundsOfShapesMovingTogether,
5390
- visited
5391
- });
5392
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5393
- if (!commonPageBounds) continue;
5394
- shapeClustersToAlign.push({
5395
- shapes: shapesMovingTogether,
5396
- pageBounds: commonPageBounds
5397
- });
5398
- allBounds.push(commonPageBounds);
5399
- }
5356
+ const { clusters: shapeClustersToAlign, allBounds } = this.getShapeClusters(shapes, "align");
5400
5357
  if (shapeClustersToAlign.length < 2) return this;
5401
5358
  const commonBounds = Box.Common(allBounds);
5402
5359
  const changes = [];
@@ -5458,39 +5415,7 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5458
5415
  */
5459
5416
  distributeShapes(shapes, operation) {
5460
5417
  if (this.getIsReadonly()) return this;
5461
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5462
- const shapesToDistributeFirstPass = compact(ids.map((id) => this.getShape(id)));
5463
- const shapeClustersToDistribute = [];
5464
- const allBounds = [];
5465
- const visited = /* @__PURE__ */ new Set();
5466
- for (const shape of shapesToDistributeFirstPass) {
5467
- if (visited.has(shape.id)) continue;
5468
- visited.add(shape.id);
5469
- const shapePageBounds = this.getShapePageBounds(shape);
5470
- if (!shapePageBounds) continue;
5471
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5472
- type: "distribute",
5473
- shapes: shapesToDistributeFirstPass
5474
- })) {
5475
- continue;
5476
- }
5477
- const shapesMovingTogether = [shape];
5478
- const boundsOfShapesMovingTogether = [shapePageBounds];
5479
- this.collectShapesViaArrowBindings({
5480
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5481
- initialShapes: shapesToDistributeFirstPass,
5482
- resultShapes: shapesMovingTogether,
5483
- resultBounds: boundsOfShapesMovingTogether,
5484
- visited
5485
- });
5486
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5487
- if (!commonPageBounds) continue;
5488
- shapeClustersToDistribute.push({
5489
- shapes: shapesMovingTogether,
5490
- pageBounds: commonPageBounds
5491
- });
5492
- allBounds.push(commonPageBounds);
5493
- }
5418
+ const { clusters: shapeClustersToDistribute } = this.getShapeClusters(shapes, "distribute");
5494
5419
  if (shapeClustersToDistribute.length < 3) return this;
5495
5420
  let val;
5496
5421
  let min;
@@ -5512,6 +5437,7 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5512
5437
  const last2 = shapeClustersToDistribute.sort((a, b) => b.pageBounds[max] - a.pageBounds[max])[0];
5513
5438
  if (first === last2) {
5514
5439
  const excludedShapeIds = new Set(first.shapes.map((s) => s.id));
5440
+ const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5515
5441
  return this.distributeShapes(
5516
5442
  ids.filter((id) => !excludedShapeIds.has(id)),
5517
5443
  operation
@@ -5564,41 +5490,12 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5564
5490
  * @public
5565
5491
  */
5566
5492
  stretchShapes(shapes, operation) {
5567
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5568
5493
  if (this.getIsReadonly()) return this;
5569
- const shapesToStretchFirstPass = compact(ids.map((id) => this.getShape(id))).filter(
5570
- (s) => this.getShapePageTransform(s)?.rotation() % (PI / 2) === 0
5494
+ const { clusters: shapeClustersToStretch, allBounds } = this.getShapeClusters(
5495
+ shapes,
5496
+ "stretch",
5497
+ { filterAxisAligned: true }
5571
5498
  );
5572
- const shapeClustersToStretch = [];
5573
- const allBounds = [];
5574
- const visited = /* @__PURE__ */ new Set();
5575
- for (const shape of shapesToStretchFirstPass) {
5576
- if (visited.has(shape.id)) continue;
5577
- visited.add(shape.id);
5578
- const shapePageBounds = this.getShapePageBounds(shape);
5579
- if (!shapePageBounds) continue;
5580
- const shapesMovingTogether = [shape];
5581
- const boundsOfShapesMovingTogether = [shapePageBounds];
5582
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5583
- type: "stretch"
5584
- })) {
5585
- continue;
5586
- }
5587
- this.collectShapesViaArrowBindings({
5588
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5589
- initialShapes: shapesToStretchFirstPass,
5590
- resultShapes: shapesMovingTogether,
5591
- resultBounds: boundsOfShapesMovingTogether,
5592
- visited
5593
- });
5594
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5595
- if (!commonPageBounds) continue;
5596
- shapeClustersToStretch.push({
5597
- shapes: shapesMovingTogether,
5598
- pageBounds: commonPageBounds
5599
- });
5600
- allBounds.push(commonPageBounds);
5601
- }
5602
5499
  if (shapeClustersToStretch.length < 2) return this;
5603
5500
  const commonBounds = Box.Common(allBounds);
5604
5501
  let val;
@@ -5624,7 +5521,7 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5624
5521
  for (const shape of shapes2) {
5625
5522
  const shapeLocalOffset = localOffset.clone();
5626
5523
  const parentTransform = this.getShapeParentTransform(shape);
5627
- if (parentTransform) localOffset.rot(-parentTransform.rotation());
5524
+ if (parentTransform) shapeLocalOffset.rot(-parentTransform.rotation());
5628
5525
  shapeLocalOffset.add(shape);
5629
5526
  const changes = this.getChangesToTranslateShape(shape, shapeLocalOffset);
5630
5527
  this.updateShape(changes);
@@ -5639,6 +5536,62 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5639
5536
  });
5640
5537
  return this;
5641
5538
  }
5539
+ /**
5540
+ * Resize and reposition a set of shapes so that their combined page bounds matches the given
5541
+ * target bounds.
5542
+ *
5543
+ * @example
5544
+ * ```ts
5545
+ * editor.resizeToBounds([box1, box2], { x: 0, y: 0, w: 500, h: 500 })
5546
+ * editor.resizeToBounds(editor.getSelectedShapeIds(), new Box(0, 0, 500, 500))
5547
+ * ```
5548
+ *
5549
+ * @param shapes - The shapes (or shape ids) to resize.
5550
+ * @param bounds - The target bounding box.
5551
+ *
5552
+ * @public
5553
+ */
5554
+ resizeToBounds(shapes, bounds) {
5555
+ if (this.getIsReadonly()) return this;
5556
+ const targetBounds = Box.From(bounds);
5557
+ const { clusters: shapeClusters, allBounds } = this.getShapeClusters(
5558
+ shapes,
5559
+ "resize_to_bounds",
5560
+ { filterAxisAligned: true }
5561
+ );
5562
+ if (shapeClusters.length === 0) return this;
5563
+ const commonBounds = Box.Common(allBounds);
5564
+ if (!commonBounds) return this;
5565
+ if (commonBounds.width === 0 || commonBounds.height === 0) return this;
5566
+ const scaleX = targetBounds.width / commonBounds.width;
5567
+ const scaleY = targetBounds.height / commonBounds.height;
5568
+ const scale = new Vec(scaleX, scaleY);
5569
+ shapeClusters.forEach(({ shapes: shapes2, pageBounds }) => {
5570
+ const localOffset = new Vec(
5571
+ targetBounds.minX - commonBounds.minX + (pageBounds.minX - commonBounds.minX) * (scaleX - 1),
5572
+ targetBounds.minY - commonBounds.minY + (pageBounds.minY - commonBounds.minY) * (scaleY - 1)
5573
+ );
5574
+ const scaleOrigin = new Vec(
5575
+ targetBounds.minX + (pageBounds.minX - commonBounds.minX) * scaleX,
5576
+ targetBounds.minY + (pageBounds.minY - commonBounds.minY) * scaleY
5577
+ );
5578
+ for (const shape of shapes2) {
5579
+ const shapeLocalOffset = localOffset.clone();
5580
+ const parentTransform = this.getShapeParentTransform(shape);
5581
+ if (parentTransform) shapeLocalOffset.rot(-parentTransform.rotation());
5582
+ shapeLocalOffset.add(shape);
5583
+ const changes = this.getChangesToTranslateShape(shape, shapeLocalOffset);
5584
+ this.updateShape(changes);
5585
+ this.resizeShape(shape.id, scale, {
5586
+ initialBounds: this.getShapeGeometry(shape).bounds,
5587
+ scaleOrigin,
5588
+ isAspectRatioLocked: this.getShapeUtil(shape).isAspectRatioLocked(shape),
5589
+ scaleAxisRotation: 0
5590
+ });
5591
+ }
5592
+ });
5593
+ return this;
5594
+ }
5642
5595
  /**
5643
5596
  * Resize a shape.
5644
5597
  *
@@ -6758,38 +6711,10 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
6758
6711
  const bindingIdMap = new Map(
6759
6712
  preserveIds ? bindings.map((binding) => [binding.id, binding.id]) : bindings.map((binding) => [binding.id, createBindingId()])
6760
6713
  );
6761
- let pasteParentId = this.getCurrentPageId();
6762
- let lowestDepth = Infinity;
6763
- let lowestAncestors = [];
6764
- for (const shape of this.getSelectedShapes()) {
6765
- if (lowestDepth === 0) break;
6766
- const isFrame = this.isShapeOfType(shape, "frame");
6767
- const ancestors = this.getShapeAncestors(shape);
6768
- if (isFrame) ancestors.push(shape);
6769
- const depth = isFrame ? ancestors.length + 1 : ancestors.length;
6770
- if (depth < lowestDepth) {
6771
- lowestDepth = depth;
6772
- lowestAncestors = ancestors;
6773
- pasteParentId = isFrame ? shape.id : shape.parentId;
6774
- } else if (depth === lowestDepth) {
6775
- if (lowestAncestors.length !== ancestors.length) {
6776
- throw Error(`Ancestors: ${lowestAncestors.length} !== ${ancestors.length}`);
6777
- }
6778
- if (lowestAncestors.length === 0) {
6779
- pasteParentId = currentPageId;
6780
- break;
6781
- } else {
6782
- pasteParentId = currentPageId;
6783
- for (let i = 0; i < lowestAncestors.length; i++) {
6784
- if (ancestors[i] !== lowestAncestors[i]) break;
6785
- pasteParentId = ancestors[i].id;
6786
- }
6787
- }
6788
- }
6789
- }
6714
+ let pasteParentId = currentPageId;
6715
+ const shapesById = new Map(shapes.map((s) => [s.id, s]));
6716
+ const rootShapesFromContent = compact(rootShapeIds.map((id) => shapesById.get(id)));
6790
6717
  if (point) {
6791
- const shapesById = new Map(shapes.map((shape) => [shape.id, shape]));
6792
- const rootShapesFromContent = compact(rootShapeIds.map((id) => shapesById.get(id)));
6793
6718
  if (rootShapesFromContent.length > 0) {
6794
6719
  const targetParent = this.getShapeAtPoint(point, {
6795
6720
  hitInside: true,
@@ -6797,38 +6722,55 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
6797
6722
  hitLocked: true,
6798
6723
  filter: (shape) => {
6799
6724
  const util = this.getShapeUtil(shape);
6800
- if (!util.canReceiveNewChildrenOfType) return false;
6801
6725
  return rootShapesFromContent.every(
6802
- (rootShape) => util.canReceiveNewChildrenOfType(shape, rootShape.type)
6726
+ (rootShape) => util.canReceiveNewChildrenOfType?.(shape, rootShape.type)
6803
6727
  );
6804
6728
  }
6805
6729
  });
6806
- pasteParentId = targetParent ? targetParent.id : currentPageId;
6807
- }
6808
- }
6809
- let isDuplicating = false;
6810
- if (!isPageId(pasteParentId)) {
6811
- const parent = this.getShape(pasteParentId);
6812
- if (parent) {
6813
- if (!this.getViewportPageBounds().includes(this.getShapePageBounds(parent))) {
6814
- pasteParentId = currentPageId;
6815
- } else {
6816
- if (rootShapeIds.length === 1) {
6817
- const rootShape = shapes.find((s) => s.id === rootShapeIds[0]);
6818
- if (this.isShapeOfType(parent, "frame") && this.isShapeOfType(rootShape, "frame") && rootShape.props.w === parent?.props.w && rootShape.props.h === parent?.props.h) {
6819
- isDuplicating = true;
6730
+ pasteParentId = targetParent?.id ?? currentPageId;
6731
+ }
6732
+ } else if (!preservePosition) {
6733
+ const selectedShapes = this.getSelectedShapes();
6734
+ let selectedParent = null;
6735
+ const canAcceptAll = (candidate) => {
6736
+ const util = this.getShapeUtil(candidate);
6737
+ return rootShapesFromContent.every(
6738
+ (rs) => util.canReceiveNewChildrenOfType?.(candidate, rs.type)
6739
+ );
6740
+ };
6741
+ for (const shape of selectedShapes) {
6742
+ const candidate = canAcceptAll(shape) ? shape : this.findShapeAncestor(shape, canAcceptAll) ?? (isShapeId(shape.parentId) ? this.getShape(shape.parentId) : null);
6743
+ if (!candidate) {
6744
+ selectedParent = null;
6745
+ break;
6746
+ }
6747
+ if (!selectedParent) {
6748
+ selectedParent = candidate;
6749
+ } else if (selectedParent.id !== candidate.id) {
6750
+ const spAncestors = this.getShapeAncestors(selectedParent);
6751
+ if (canAcceptAll(selectedParent)) spAncestors.push(selectedParent);
6752
+ const acceptingAncestors = spAncestors.filter(canAcceptAll);
6753
+ const candidateAncestorIds = /* @__PURE__ */ new Set([
6754
+ candidate.id,
6755
+ ...this.getShapeAncestors(candidate).map((a) => a.id)
6756
+ ]);
6757
+ let common = null;
6758
+ for (let i = acceptingAncestors.length - 1; i >= 0; i--) {
6759
+ if (candidateAncestorIds.has(acceptingAncestors[i].id)) {
6760
+ common = acceptingAncestors[i];
6761
+ break;
6820
6762
  }
6821
6763
  }
6764
+ selectedParent = common;
6765
+ if (!selectedParent) break;
6822
6766
  }
6823
- } else {
6824
- pasteParentId = currentPageId;
6825
6767
  }
6826
- }
6827
- if (!isDuplicating) {
6828
- isDuplicating = shapeIdMap.has(pasteParentId);
6829
- }
6830
- if (isDuplicating) {
6831
- pasteParentId = this.getShape(pasteParentId).parentId;
6768
+ if (selectedParent && shapeIdMap.has(selectedParent.id)) {
6769
+ selectedParent = null;
6770
+ }
6771
+ if (selectedParent) {
6772
+ pasteParentId = selectedParent.id;
6773
+ }
6832
6774
  }
6833
6775
  let index = this.getHighestIndexForParent(pasteParentId);
6834
6776
  const rootShapes = [];
@@ -6892,22 +6834,17 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
6892
6834
  })
6893
6835
  );
6894
6836
  this.run(() => {
6895
- if (assetsToCreate.length > 0) {
6896
- this.createAssets(assetsToCreate);
6897
- }
6837
+ if (assetsToCreate.length > 0) this.createAssets(assetsToCreate);
6898
6838
  this.createShapes(newShapes);
6899
6839
  this.createBindings(newBindings);
6900
- if (select) {
6901
- this.select(...rootShapes.map((s) => s.id));
6902
- }
6840
+ if (select) this.select(...rootShapes.map((s) => s.id));
6903
6841
  if (pasteParentId !== currentPageId) {
6904
6842
  this.reparentShapes(
6905
6843
  rootShapes.map((s) => s.id),
6906
6844
  pasteParentId
6907
6845
  );
6908
6846
  }
6909
- const newCreatedShapes = newShapes.map((s) => this.getShape(s.id));
6910
- const bounds = Box.Common(newCreatedShapes.map((s) => this.getShapePageBounds(s)));
6847
+ const rootBounds = Box.Common(compact(rootShapes.map((s) => this.getShapePageBounds(s.id))));
6911
6848
  if (point === void 0) {
6912
6849
  if (!isPageId(pasteParentId)) {
6913
6850
  const shape = this.getShape(pasteParentId);
@@ -6915,40 +6852,44 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
6915
6852
  this.getShapePageTransform(shape),
6916
6853
  this.getShapeGeometry(shape).bounds.center
6917
6854
  );
6855
+ } else if (preservePosition) {
6856
+ point = rootBounds.center;
6918
6857
  } else {
6919
6858
  const viewportPageBounds = this.getViewportPageBounds();
6920
- if (preservePosition || viewportPageBounds.includes(Box.From(bounds))) {
6921
- point = bounds.center;
6922
- } else {
6923
- point = viewportPageBounds.center;
6924
- }
6925
- }
6926
- }
6927
- if (rootShapes.length === 1) {
6928
- const onlyRoot = rootShapes[0];
6929
- if (this.isShapeOfType(onlyRoot, "frame")) {
6930
- while (this.getShapesAtPoint(point).some(
6931
- (shape) => this.isShapeOfType(shape, "frame") && shape.props.w === onlyRoot.props.w && shape.props.h === onlyRoot.props.h
6932
- )) {
6933
- point.x += bounds.w + 16;
6934
- }
6859
+ const anyOverlap = rootShapes.some((s) => {
6860
+ const b = this.getShapePageBounds(s.id);
6861
+ return b && viewportPageBounds.collides(b);
6862
+ });
6863
+ point = anyOverlap ? rootBounds.center : viewportPageBounds.center;
6935
6864
  }
6936
6865
  }
6937
6866
  const pageCenter = Box.Common(
6938
6867
  compact(rootShapes.map(({ id }) => this.getShapePageBounds(id)))
6939
6868
  ).center;
6940
6869
  const offset = Vec.Sub(point, pageCenter);
6941
- this.updateShapes(
6942
- rootShapes.map(({ id }) => {
6943
- const s = this.getShape(id);
6944
- const localRotation = this.getShapeParentTransform(id).decompose().rotation;
6945
- const localDelta = Vec.Rot(offset, -localRotation);
6946
- return { id: s.id, type: s.type, x: s.x + localDelta.x, y: s.y + localDelta.y };
6947
- })
6948
- );
6870
+ if (offset.x !== 0 || offset.y !== 0) {
6871
+ this.updateShapes(
6872
+ rootShapes.map(({ id }) => {
6873
+ const s = this.getShape(id);
6874
+ const localRotation = this.getShapeParentTransform(id).decompose().rotation;
6875
+ const localDelta = Vec.Rot(offset, -localRotation);
6876
+ return { id: s.id, type: s.type, x: s.x + localDelta.x, y: s.y + localDelta.y };
6877
+ })
6878
+ );
6879
+ }
6949
6880
  if (isPageId(pasteParentId)) {
6950
6881
  const currentRootShapes = compact(rootShapes.map((s) => this.getShape(s.id)));
6951
- const { reparenting } = getDroppedShapesToNewParents(this, currentRootShapes);
6882
+ const { reparenting } = getDroppedShapesToNewParents(
6883
+ this,
6884
+ currentRootShapes,
6885
+ (shape, parent) => {
6886
+ if (shapeIdMap.has(parent.id)) return false;
6887
+ const shapeBounds = this.getShapePageBounds(shape);
6888
+ const parentBounds = this.getShapePageBounds(parent);
6889
+ if (!shapeBounds || !parentBounds) return false;
6890
+ return parentBounds.containsPoint(shapeBounds.center);
6891
+ }
6892
+ );
6952
6893
  reparenting.forEach((childrenToReparent, newParentId) => {
6953
6894
  if (childrenToReparent.length === 0) return;
6954
6895
  this.reparentShapes(
@@ -6957,6 +6898,17 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
6957
6898
  );
6958
6899
  });
6959
6900
  }
6901
+ const newShapeIdSet = new Set(newShapes.map((s) => s.id));
6902
+ const shapesToKickout = rootShapes.map((s) => s.id).filter((id) => {
6903
+ const shape = this.getShape(id);
6904
+ if (!shape) return false;
6905
+ if (isPageId(shape.parentId)) return false;
6906
+ const children = this.getSortedChildIdsForParent(id);
6907
+ return !children.some((childId) => newShapeIdSet.has(childId));
6908
+ });
6909
+ if (shapesToKickout.length > 0) {
6910
+ kickoutOccludedShapes(this, shapesToKickout);
6911
+ }
6960
6912
  });
6961
6913
  return this;
6962
6914
  }