@tldraw/editor 4.5.0-canary.7c873e27b89b → 4.5.0-canary.85626c5d8aed

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-cjs/index.js CHANGED
@@ -369,7 +369,7 @@ var import_LocalIndexedDb = require("./lib/utils/sync/LocalIndexedDb");
369
369
  var import_uniq = require("./lib/utils/uniq");
370
370
  (0, import_utils.registerTldrawLibraryVersion)(
371
371
  "@tldraw/editor",
372
- "4.5.0-canary.7c873e27b89b",
372
+ "4.5.0-canary.85626c5d8aed",
373
373
  "cjs"
374
374
  );
375
375
  //# sourceMappingURL=index.js.map
@@ -6728,38 +6728,10 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
6728
6728
  const bindingIdMap = new Map(
6729
6729
  preserveIds ? bindings.map((binding) => [binding.id, binding.id]) : bindings.map((binding) => [binding.id, (0, import_tlschema.createBindingId)()])
6730
6730
  );
6731
- let pasteParentId = this.getCurrentPageId();
6732
- let lowestDepth = Infinity;
6733
- let lowestAncestors = [];
6734
- for (const shape of this.getSelectedShapes()) {
6735
- if (lowestDepth === 0) break;
6736
- const isFrame = this.isShapeOfType(shape, "frame");
6737
- const ancestors = this.getShapeAncestors(shape);
6738
- if (isFrame) ancestors.push(shape);
6739
- const depth = isFrame ? ancestors.length + 1 : ancestors.length;
6740
- if (depth < lowestDepth) {
6741
- lowestDepth = depth;
6742
- lowestAncestors = ancestors;
6743
- pasteParentId = isFrame ? shape.id : shape.parentId;
6744
- } else if (depth === lowestDepth) {
6745
- if (lowestAncestors.length !== ancestors.length) {
6746
- throw Error(`Ancestors: ${lowestAncestors.length} !== ${ancestors.length}`);
6747
- }
6748
- if (lowestAncestors.length === 0) {
6749
- pasteParentId = currentPageId;
6750
- break;
6751
- } else {
6752
- pasteParentId = currentPageId;
6753
- for (let i = 0; i < lowestAncestors.length; i++) {
6754
- if (ancestors[i] !== lowestAncestors[i]) break;
6755
- pasteParentId = ancestors[i].id;
6756
- }
6757
- }
6758
- }
6759
- }
6731
+ let pasteParentId = currentPageId;
6732
+ const shapesById = new Map(shapes.map((s) => [s.id, s]));
6733
+ const rootShapesFromContent = (0, import_utils.compact)(rootShapeIds.map((id) => shapesById.get(id)));
6760
6734
  if (point) {
6761
- const shapesById = new Map(shapes.map((shape) => [shape.id, shape]));
6762
- const rootShapesFromContent = (0, import_utils.compact)(rootShapeIds.map((id) => shapesById.get(id)));
6763
6735
  if (rootShapesFromContent.length > 0) {
6764
6736
  const targetParent = this.getShapeAtPoint(point, {
6765
6737
  hitInside: true,
@@ -6767,38 +6739,55 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
6767
6739
  hitLocked: true,
6768
6740
  filter: (shape) => {
6769
6741
  const util = this.getShapeUtil(shape);
6770
- if (!util.canReceiveNewChildrenOfType) return false;
6771
6742
  return rootShapesFromContent.every(
6772
- (rootShape) => util.canReceiveNewChildrenOfType(shape, rootShape.type)
6743
+ (rootShape) => util.canReceiveNewChildrenOfType?.(shape, rootShape.type)
6773
6744
  );
6774
6745
  }
6775
6746
  });
6776
- pasteParentId = targetParent ? targetParent.id : currentPageId;
6777
- }
6778
- }
6779
- let isDuplicating = false;
6780
- if (!(0, import_tlschema.isPageId)(pasteParentId)) {
6781
- const parent = this.getShape(pasteParentId);
6782
- if (parent) {
6783
- if (!this.getViewportPageBounds().includes(this.getShapePageBounds(parent))) {
6784
- pasteParentId = currentPageId;
6785
- } else {
6786
- if (rootShapeIds.length === 1) {
6787
- const rootShape = shapes.find((s) => s.id === rootShapeIds[0]);
6788
- if (this.isShapeOfType(parent, "frame") && this.isShapeOfType(rootShape, "frame") && rootShape.props.w === parent?.props.w && rootShape.props.h === parent?.props.h) {
6789
- isDuplicating = true;
6747
+ pasteParentId = targetParent?.id ?? currentPageId;
6748
+ }
6749
+ } else if (!preservePosition) {
6750
+ const selectedShapes = this.getSelectedShapes();
6751
+ let selectedParent = null;
6752
+ const canAcceptAll = (candidate) => {
6753
+ const util = this.getShapeUtil(candidate);
6754
+ return rootShapesFromContent.every(
6755
+ (rs) => util.canReceiveNewChildrenOfType?.(candidate, rs.type)
6756
+ );
6757
+ };
6758
+ for (const shape of selectedShapes) {
6759
+ const candidate = canAcceptAll(shape) ? shape : this.findShapeAncestor(shape, canAcceptAll) ?? ((0, import_tlschema.isShapeId)(shape.parentId) ? this.getShape(shape.parentId) : null);
6760
+ if (!candidate) {
6761
+ selectedParent = null;
6762
+ break;
6763
+ }
6764
+ if (!selectedParent) {
6765
+ selectedParent = candidate;
6766
+ } else if (selectedParent.id !== candidate.id) {
6767
+ const spAncestors = this.getShapeAncestors(selectedParent);
6768
+ if (canAcceptAll(selectedParent)) spAncestors.push(selectedParent);
6769
+ const acceptingAncestors = spAncestors.filter(canAcceptAll);
6770
+ const candidateAncestorIds = /* @__PURE__ */ new Set([
6771
+ candidate.id,
6772
+ ...this.getShapeAncestors(candidate).map((a) => a.id)
6773
+ ]);
6774
+ let common = null;
6775
+ for (let i = acceptingAncestors.length - 1; i >= 0; i--) {
6776
+ if (candidateAncestorIds.has(acceptingAncestors[i].id)) {
6777
+ common = acceptingAncestors[i];
6778
+ break;
6790
6779
  }
6791
6780
  }
6781
+ selectedParent = common;
6782
+ if (!selectedParent) break;
6792
6783
  }
6793
- } else {
6794
- pasteParentId = currentPageId;
6795
6784
  }
6796
- }
6797
- if (!isDuplicating) {
6798
- isDuplicating = shapeIdMap.has(pasteParentId);
6799
- }
6800
- if (isDuplicating) {
6801
- pasteParentId = this.getShape(pasteParentId).parentId;
6785
+ if (selectedParent && shapeIdMap.has(selectedParent.id)) {
6786
+ selectedParent = null;
6787
+ }
6788
+ if (selectedParent) {
6789
+ pasteParentId = selectedParent.id;
6790
+ }
6802
6791
  }
6803
6792
  let index = this.getHighestIndexForParent(pasteParentId);
6804
6793
  const rootShapes = [];
@@ -6862,22 +6851,17 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
6862
6851
  })
6863
6852
  );
6864
6853
  this.run(() => {
6865
- if (assetsToCreate.length > 0) {
6866
- this.createAssets(assetsToCreate);
6867
- }
6854
+ if (assetsToCreate.length > 0) this.createAssets(assetsToCreate);
6868
6855
  this.createShapes(newShapes);
6869
6856
  this.createBindings(newBindings);
6870
- if (select) {
6871
- this.select(...rootShapes.map((s) => s.id));
6872
- }
6857
+ if (select) this.select(...rootShapes.map((s) => s.id));
6873
6858
  if (pasteParentId !== currentPageId) {
6874
6859
  this.reparentShapes(
6875
6860
  rootShapes.map((s) => s.id),
6876
6861
  pasteParentId
6877
6862
  );
6878
6863
  }
6879
- const newCreatedShapes = newShapes.map((s) => this.getShape(s.id));
6880
- const bounds = import_Box.Box.Common(newCreatedShapes.map((s) => this.getShapePageBounds(s)));
6864
+ const rootBounds = import_Box.Box.Common((0, import_utils.compact)(rootShapes.map((s) => this.getShapePageBounds(s.id))));
6881
6865
  if (point === void 0) {
6882
6866
  if (!(0, import_tlschema.isPageId)(pasteParentId)) {
6883
6867
  const shape = this.getShape(pasteParentId);
@@ -6885,40 +6869,44 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
6885
6869
  this.getShapePageTransform(shape),
6886
6870
  this.getShapeGeometry(shape).bounds.center
6887
6871
  );
6872
+ } else if (preservePosition) {
6873
+ point = rootBounds.center;
6888
6874
  } else {
6889
6875
  const viewportPageBounds = this.getViewportPageBounds();
6890
- if (preservePosition || viewportPageBounds.includes(import_Box.Box.From(bounds))) {
6891
- point = bounds.center;
6892
- } else {
6893
- point = viewportPageBounds.center;
6894
- }
6895
- }
6896
- }
6897
- if (rootShapes.length === 1) {
6898
- const onlyRoot = rootShapes[0];
6899
- if (this.isShapeOfType(onlyRoot, "frame")) {
6900
- while (this.getShapesAtPoint(point).some(
6901
- (shape) => this.isShapeOfType(shape, "frame") && shape.props.w === onlyRoot.props.w && shape.props.h === onlyRoot.props.h
6902
- )) {
6903
- point.x += bounds.w + 16;
6904
- }
6876
+ const anyOverlap = rootShapes.some((s) => {
6877
+ const b = this.getShapePageBounds(s.id);
6878
+ return b && viewportPageBounds.collides(b);
6879
+ });
6880
+ point = anyOverlap ? rootBounds.center : viewportPageBounds.center;
6905
6881
  }
6906
6882
  }
6907
6883
  const pageCenter = import_Box.Box.Common(
6908
6884
  (0, import_utils.compact)(rootShapes.map(({ id }) => this.getShapePageBounds(id)))
6909
6885
  ).center;
6910
6886
  const offset = import_Vec.Vec.Sub(point, pageCenter);
6911
- this.updateShapes(
6912
- rootShapes.map(({ id }) => {
6913
- const s = this.getShape(id);
6914
- const localRotation = this.getShapeParentTransform(id).decompose().rotation;
6915
- const localDelta = import_Vec.Vec.Rot(offset, -localRotation);
6916
- return { id: s.id, type: s.type, x: s.x + localDelta.x, y: s.y + localDelta.y };
6917
- })
6918
- );
6887
+ if (offset.x !== 0 || offset.y !== 0) {
6888
+ this.updateShapes(
6889
+ rootShapes.map(({ id }) => {
6890
+ const s = this.getShape(id);
6891
+ const localRotation = this.getShapeParentTransform(id).decompose().rotation;
6892
+ const localDelta = import_Vec.Vec.Rot(offset, -localRotation);
6893
+ return { id: s.id, type: s.type, x: s.x + localDelta.x, y: s.y + localDelta.y };
6894
+ })
6895
+ );
6896
+ }
6919
6897
  if ((0, import_tlschema.isPageId)(pasteParentId)) {
6920
6898
  const currentRootShapes = (0, import_utils.compact)(rootShapes.map((s) => this.getShape(s.id)));
6921
- const { reparenting } = (0, import_reparenting.getDroppedShapesToNewParents)(this, currentRootShapes);
6899
+ const { reparenting } = (0, import_reparenting.getDroppedShapesToNewParents)(
6900
+ this,
6901
+ currentRootShapes,
6902
+ (shape, parent) => {
6903
+ if (shapeIdMap.has(parent.id)) return false;
6904
+ const shapeBounds = this.getShapePageBounds(shape);
6905
+ const parentBounds = this.getShapePageBounds(parent);
6906
+ if (!shapeBounds || !parentBounds) return false;
6907
+ return parentBounds.containsPoint(shapeBounds.center);
6908
+ }
6909
+ );
6922
6910
  reparenting.forEach((childrenToReparent, newParentId) => {
6923
6911
  if (childrenToReparent.length === 0) return;
6924
6912
  this.reparentShapes(
@@ -6927,6 +6915,17 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
6927
6915
  );
6928
6916
  });
6929
6917
  }
6918
+ const newShapeIdSet = new Set(newShapes.map((s) => s.id));
6919
+ const shapesToKickout = rootShapes.map((s) => s.id).filter((id) => {
6920
+ const shape = this.getShape(id);
6921
+ if (!shape) return false;
6922
+ if ((0, import_tlschema.isPageId)(shape.parentId)) return false;
6923
+ const children = this.getSortedChildIdsForParent(id);
6924
+ return !children.some((childId) => newShapeIdSet.has(childId));
6925
+ });
6926
+ if (shapesToKickout.length > 0) {
6927
+ (0, import_reparenting.kickoutOccludedShapes)(this, shapesToKickout);
6928
+ }
6930
6929
  });
6931
6930
  return this;
6932
6931
  }