@tldraw/editor 3.14.0-canary.6d58db7084e2 → 3.14.0-canary.6fbbca54ff57

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 (33) hide show
  1. package/dist-cjs/index.d.ts +76 -13
  2. package/dist-cjs/index.js +4 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/editor/Editor.js +29 -21
  5. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  6. package/dist-cjs/lib/editor/shapes/ShapeUtil.js +0 -10
  7. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  8. package/dist-cjs/lib/primitives/geometry/Geometry2d.js +6 -2
  9. package/dist-cjs/lib/primitives/geometry/Geometry2d.js.map +2 -2
  10. package/dist-cjs/lib/utils/reparenting.js +232 -0
  11. package/dist-cjs/lib/utils/reparenting.js.map +7 -0
  12. package/dist-cjs/version.js +3 -3
  13. package/dist-cjs/version.js.map +1 -1
  14. package/dist-esm/index.d.mts +76 -13
  15. package/dist-esm/index.mjs +4 -1
  16. package/dist-esm/index.mjs.map +2 -2
  17. package/dist-esm/lib/editor/Editor.mjs +29 -21
  18. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  19. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +0 -10
  20. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  21. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs +6 -2
  22. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs.map +2 -2
  23. package/dist-esm/lib/utils/reparenting.mjs +216 -0
  24. package/dist-esm/lib/utils/reparenting.mjs.map +7 -0
  25. package/dist-esm/version.mjs +3 -3
  26. package/dist-esm/version.mjs.map +1 -1
  27. package/package.json +7 -7
  28. package/src/index.ts +5 -0
  29. package/src/lib/editor/Editor.ts +39 -29
  30. package/src/lib/editor/shapes/ShapeUtil.ts +46 -15
  31. package/src/lib/primitives/geometry/Geometry2d.ts +7 -2
  32. package/src/lib/utils/reparenting.ts +383 -0
  33. package/src/version.ts +3 -3
package/src/index.ts CHANGED
@@ -182,6 +182,10 @@ export { BaseBoxShapeUtil, type TLBaseBoxShape } from './lib/editor/shapes/BaseB
182
182
  export {
183
183
  ShapeUtil,
184
184
  type TLCropInfo,
185
+ type TLDragShapesInInfo,
186
+ type TLDragShapesOutInfo,
187
+ type TLDragShapesOverInfo,
188
+ type TLDropShapesOverInfo,
185
189
  type TLGeometryOpts,
186
190
  type TLHandleDragInfo,
187
191
  type TLResizeInfo,
@@ -446,6 +450,7 @@ export { hardResetEditor } from './lib/utils/hardResetEditor'
446
450
  export { isAccelKey } from './lib/utils/keyboard'
447
451
  export { normalizeWheel } from './lib/utils/normalizeWheel'
448
452
  export { refreshPage } from './lib/utils/refreshPage'
453
+ export { getDroppedShapesToNewParents, kickoutOccludedShapes } from './lib/utils/reparenting'
449
454
  export {
450
455
  getFontsFromRichText,
451
456
  type RichTextFontVisitor,
@@ -5528,7 +5528,7 @@ export class Editor extends EventEmitter<TLEventMap> {
5528
5528
  if (!id) return undefined
5529
5529
  const freshShape = this.getShape(id)
5530
5530
  if (freshShape === undefined || !isShapeId(freshShape.parentId)) return undefined
5531
- return this.store.get(freshShape.parentId)
5531
+ return this.getShape(freshShape.parentId)
5532
5532
  }
5533
5533
 
5534
5534
  /**
@@ -5711,6 +5711,10 @@ export class Editor extends EventEmitter<TLEventMap> {
5711
5711
  const newPoint = invertedParentTransform.applyToPoint(pagePoint)
5712
5712
  const newRotation = pageTransform.rotation() - parentPageRotation
5713
5713
 
5714
+ if (shape.id === parentId) {
5715
+ throw Error('Attempted to reparent a shape to itself!')
5716
+ }
5717
+
5714
5718
  changes.push({
5715
5719
  id: shape.id,
5716
5720
  type: shape.type,
@@ -5814,6 +5818,11 @@ export class Editor extends EventEmitter<TLEventMap> {
5814
5818
  return shapeIds
5815
5819
  }
5816
5820
 
5821
+ /** @deprecated Use {@link Editor.getDraggingOverShape} instead */
5822
+ getDroppingOverShape(point: Vec, droppingShapes: TLShape[]): TLShape | undefined {
5823
+ return this.getDraggingOverShape(point, droppingShapes)
5824
+ }
5825
+
5817
5826
  /**
5818
5827
  * Get the shape that some shapes should be dropped on at a given point.
5819
5828
  *
@@ -5824,35 +5833,33 @@ export class Editor extends EventEmitter<TLEventMap> {
5824
5833
  *
5825
5834
  * @public
5826
5835
  */
5827
- getDroppingOverShape(point: VecLike, droppingShapes: TLShape[] = []) {
5828
- // starting from the top...
5829
- const currentPageShapesSorted = this.getCurrentPageShapesSorted()
5830
- for (let i = currentPageShapesSorted.length - 1; i >= 0; i--) {
5831
- const shape = currentPageShapesSorted[i]
5832
-
5833
- if (
5834
- // ignore hidden shapes
5835
- this.isShapeHidden(shape) ||
5836
- // don't allow dropping on selected shapes
5837
- this.getSelectedShapeIds().includes(shape.id) ||
5838
- // only allow shapes that can receive children
5839
- !this.getShapeUtil(shape).canDropShapes(shape, droppingShapes) ||
5840
- // don't allow dropping a shape on itself or one of it's children
5841
- droppingShapes.find((s) => s.id === shape.id || this.hasAncestor(shape, s.id))
5842
- ) {
5843
- continue
5844
- }
5836
+ getDraggingOverShape(point: Vec, droppingShapes: TLShape[]): TLShape | undefined {
5837
+ // get fresh moving shapes
5838
+ const draggingShapes = compact(droppingShapes.map((s) => this.getShape(s))).filter(
5839
+ (s) => !s.isLocked && !this.isShapeHidden(s)
5840
+ )
5845
5841
 
5846
- // Only allow dropping into the masked page bounds of the shape, e.g. when a frame is
5847
- // partially clipped by its own parent frame
5848
- const maskedPageBounds = this.getShapeMaskedPageBounds(shape.id)
5842
+ const maybeDraggingOverShapes = this.getShapesAtPoint(point, {
5843
+ hitInside: true,
5844
+ margin: 0,
5845
+ }).filter(
5846
+ (s) =>
5847
+ !droppingShapes.includes(s) &&
5848
+ !s.isLocked &&
5849
+ !this.isShapeHidden(s) &&
5850
+ !draggingShapes.includes(s)
5851
+ )
5849
5852
 
5853
+ for (const maybeDraggingOverShape of maybeDraggingOverShapes) {
5854
+ const shapeUtil = this.getShapeUtil(maybeDraggingOverShape)
5855
+ // Any shape that can handle any dragging interactions is a valid target
5850
5856
  if (
5851
- maskedPageBounds &&
5852
- maskedPageBounds.containsPoint(point) &&
5853
- this.getShapeGeometry(shape).hitTestPoint(this.getPointInShapeSpace(shape, point), 0, true)
5857
+ shapeUtil.onDragShapesOver ||
5858
+ shapeUtil.onDragShapesIn ||
5859
+ shapeUtil.onDragShapesOut ||
5860
+ shapeUtil.onDropShapesOver
5854
5861
  ) {
5855
- return shape
5862
+ return maybeDraggingOverShape
5856
5863
  }
5857
5864
  }
5858
5865
  }
@@ -7806,9 +7813,10 @@ export class Editor extends EventEmitter<TLEventMap> {
7806
7813
 
7807
7814
  for (let i = currentPageShapesSorted.length - 1; i >= 0; i--) {
7808
7815
  const parent = currentPageShapesSorted[i]
7816
+ const util = this.getShapeUtil(parent)
7809
7817
  if (
7818
+ util.canReceiveNewChildrenOfType(parent, partial.type) &&
7810
7819
  !this.isShapeHidden(parent) &&
7811
- this.getShapeUtil(parent).canReceiveNewChildrenOfType(parent, partial.type) &&
7812
7820
  this.isPointInShape(
7813
7821
  parent,
7814
7822
  // If no parent is provided, then we can treat the
@@ -9512,6 +9520,8 @@ export class Editor extends EventEmitter<TLEventMap> {
9512
9520
  previousPagePoint,
9513
9521
  currentScreenPoint,
9514
9522
  currentPagePoint,
9523
+ originScreenPoint,
9524
+ originPagePoint,
9515
9525
  } = this.inputs
9516
9526
 
9517
9527
  const { screenBounds } = this.store.unsafeGetWithoutCapture(TLINSTANCE_ID)!
@@ -9540,8 +9550,8 @@ export class Editor extends EventEmitter<TLEventMap> {
9540
9550
  // Reset velocity on pointer down, or when a pinch starts or ends
9541
9551
  if (info.name === 'pointer_down' || this.inputs.isPinching) {
9542
9552
  pointerVelocity.set(0, 0)
9543
- this.inputs.originScreenPoint.setTo(currentScreenPoint)
9544
- this.inputs.originPagePoint.setTo(currentPagePoint)
9553
+ originScreenPoint.setTo(currentScreenPoint)
9554
+ originPagePoint.setTo(currentPagePoint)
9545
9555
  }
9546
9556
 
9547
9557
  // todo: We only have to do this if there are multiple users in the document
@@ -4,12 +4,15 @@ import { LegacyMigrations, MigrationSequence } from '@tldraw/store'
4
4
  import {
5
5
  RecordProps,
6
6
  TLHandle,
7
+ TLParentId,
7
8
  TLPropsMigrations,
8
9
  TLShape,
9
10
  TLShapeCrop,
11
+ TLShapeId,
10
12
  TLShapePartial,
11
13
  TLUnknownShape,
12
14
  } from '@tldraw/tlschema'
15
+ import { IndexKey } from '@tldraw/utils'
13
16
  import { ReactElement } from 'react'
14
17
  import { Box, SelectionHandle } from '../../primitives/Box'
15
18
  import { Vec } from '../../primitives/Vec'
@@ -387,17 +390,6 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
387
390
  return false
388
391
  }
389
392
 
390
- /**
391
- * Get whether the shape can receive children of a given type.
392
- *
393
- * @param shape - The shape type.
394
- * @param shapes - The shapes that are being dropped.
395
- * @public
396
- */
397
- canDropShapes(_shape: Shape, _shapes: TLShape[]) {
398
- return false
399
- }
400
-
401
393
  /**
402
394
  * Get the shape as an SVG object.
403
395
  *
@@ -517,7 +509,16 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
517
509
  ): Omit<TLShapePartial<Shape>, 'id' | 'type'> | undefined | void
518
510
 
519
511
  /**
520
- * A callback called when some other shapes are dragged over this one.
512
+ * A callback called when some other shapes are dragged into this one. This fires when the shapes are dragged over the shape for the first time.
513
+ *
514
+ * @param shape - The shape.
515
+ * @param shapes - The shapes that are being dragged in.
516
+ * @public
517
+ */
518
+ onDragShapesIn?(shape: Shape, shapes: TLShape[], info: TLDragShapesInInfo): void
519
+
520
+ /**
521
+ * A callback called when some other shapes are dragged over this one. This fires when the shapes are dragged over the shape for the first time (after the onDragShapesIn callback), and again on every update while the shapes are being dragged.
521
522
  *
522
523
  * @example
523
524
  *
@@ -531,7 +532,7 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
531
532
  * @param shapes - The shapes that are being dragged over this one.
532
533
  * @public
533
534
  */
534
- onDragShapesOver?(shape: Shape, shapes: TLShape[]): void
535
+ onDragShapesOver?(shape: Shape, shapes: TLShape[], info: TLDragShapesOverInfo): void
535
536
 
536
537
  /**
537
538
  * A callback called when some other shapes are dragged out of this one.
@@ -540,7 +541,7 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
540
541
  * @param shapes - The shapes that are being dragged out.
541
542
  * @public
542
543
  */
543
- onDragShapesOut?(shape: Shape, shapes: TLShape[]): void
544
+ onDragShapesOut?(shape: Shape, shapes: TLShape[], info: TLDragShapesOutInfo): void
544
545
 
545
546
  /**
546
547
  * A callback called when some other shapes are dropped over this one.
@@ -549,7 +550,7 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
549
550
  * @param shapes - The shapes that are being dropped over this one.
550
551
  * @public
551
552
  */
552
- onDropShapesOver?(shape: Shape, shapes: TLShape[]): void
553
+ onDropShapesOver?(shape: Shape, shapes: TLShape[], info: TLDropShapesOverInfo): void
553
554
 
554
555
  /**
555
556
  * A callback called when a shape starts being resized.
@@ -748,6 +749,36 @@ export interface TLCropInfo<T extends TLShape> {
748
749
  aspectRatioLocked?: boolean
749
750
  }
750
751
 
752
+ /** @public */
753
+ export interface TLDragShapesInInfo {
754
+ initialDraggingOverShapeId: TLShapeId | null
755
+ prevDraggingOverShapeId: TLShapeId | null
756
+ initialParentIds: Map<TLShapeId, TLParentId>
757
+ initialIndices: Map<TLShapeId, IndexKey>
758
+ }
759
+
760
+ /** @public */
761
+ export interface TLDragShapesOverInfo {
762
+ initialDraggingOverShapeId: TLShapeId | null
763
+ initialParentIds: Map<TLShapeId, TLParentId>
764
+ initialIndices: Map<TLShapeId, IndexKey>
765
+ }
766
+
767
+ /** @public */
768
+ export interface TLDragShapesOutInfo {
769
+ nextDraggingOverShapeId: TLShapeId | null
770
+ initialDraggingOverShapeId: TLShapeId | null
771
+ initialParentIds: Map<TLShapeId, TLParentId>
772
+ initialIndices: Map<TLShapeId, IndexKey>
773
+ }
774
+
775
+ /** @public */
776
+ export interface TLDropShapesOverInfo {
777
+ initialDraggingOverShapeId: TLShapeId | null
778
+ initialParentIds: Map<TLShapeId, TLParentId>
779
+ initialIndices: Map<TLShapeId, IndexKey>
780
+ }
781
+
751
782
  /**
752
783
  * The type of resize.
753
784
  *
@@ -44,6 +44,7 @@ export const Geometry2dFilters: {
44
44
  /** @public */
45
45
  export interface TransformedGeometry2dOptions {
46
46
  isLabel?: boolean
47
+ isEmptyLabel?: boolean
47
48
  isInternal?: boolean
48
49
  debugColor?: string
49
50
  ignore?: boolean
@@ -57,20 +58,24 @@ export interface Geometry2dOptions extends TransformedGeometry2dOptions {
57
58
 
58
59
  /** @public */
59
60
  export abstract class Geometry2d {
61
+ // todo: consider making accessors for these too, so that they can be overridden in subclasses by geometries with more complex logic
60
62
  isFilled = false
61
63
  isClosed = true
62
64
  isLabel = false
65
+ isEmptyLabel = false
63
66
  isInternal = false
64
67
  debugColor?: string
65
68
  ignore?: boolean
66
69
 
67
70
  constructor(opts: Geometry2dOptions) {
71
+ const { isLabel = false, isEmptyLabel = false, isInternal = false } = opts
68
72
  this.isFilled = opts.isFilled
69
73
  this.isClosed = opts.isClosed
70
- this.isLabel = opts.isLabel ?? false
71
- this.isInternal = opts.isInternal ?? false
72
74
  this.debugColor = opts.debugColor
73
75
  this.ignore = opts.ignore
76
+ this.isLabel = isLabel
77
+ this.isEmptyLabel = isEmptyLabel
78
+ this.isInternal = isInternal
74
79
  }
75
80
 
76
81
  isExcludedByFilter(filters?: Geometry2dFilters) {
@@ -0,0 +1,383 @@
1
+ import { EMPTY_ARRAY } from '@tldraw/state'
2
+ import { TLGroupShape, TLParentId, TLShape, TLShapeId } from '@tldraw/tlschema'
3
+ import { IndexKey, compact, getIndexAbove, getIndexBetween } from '@tldraw/utils'
4
+ import { Editor } from '../editor/Editor'
5
+ import { Vec } from '../primitives/Vec'
6
+ import { Geometry2d } from '../primitives/geometry/Geometry2d'
7
+ import { Group2d } from '../primitives/geometry/Group2d'
8
+ import {
9
+ intersectPolygonPolygon,
10
+ polygonIntersectsPolyline,
11
+ polygonsIntersect,
12
+ } from '../primitives/intersect'
13
+ import { pointInPolygon } from '../primitives/utils'
14
+
15
+ /**
16
+ * Reparents shapes that are no longer contained within their parent shapes.
17
+ * todo: rename me to something more descriptive, like `reparentOccludedShapes` or `reparentAutoDroppedShapes`
18
+ *
19
+ * @param editor - The editor instance.
20
+ * @param shapeIds - The IDs of the shapes to reparent.
21
+ * @param opts - Optional options, including a callback to filter out certain parents, such as when removing a frame.
22
+ *
23
+ * @public
24
+ */
25
+ export function kickoutOccludedShapes(
26
+ editor: Editor,
27
+ shapeIds: TLShapeId[],
28
+ opts?: { filter?(parent: TLShape): boolean }
29
+ ) {
30
+ const parentsToCheck = new Set<TLShape>()
31
+
32
+ for (const id of shapeIds) {
33
+ const shape = editor.getShape(id)
34
+
35
+ if (!shape) continue
36
+ parentsToCheck.add(shape)
37
+
38
+ const parent = editor.getShape(shape.parentId)
39
+ if (!parent) continue
40
+ parentsToCheck.add(parent)
41
+ }
42
+
43
+ // Check all of the parents and gather up parents who have lost children
44
+ const parentsToLostChildren = new Map<TLShape, TLShapeId[]>()
45
+
46
+ for (const parent of parentsToCheck) {
47
+ const childIds = editor.getSortedChildIdsForParent(parent)
48
+ if (opts?.filter && !opts.filter(parent)) {
49
+ // If the shape is filtered out, we kick out all of its children
50
+ parentsToLostChildren.set(parent, childIds)
51
+ } else {
52
+ const overlappingChildren = getOverlappingShapes(editor, parent.id, childIds)
53
+ if (overlappingChildren.length < childIds.length) {
54
+ parentsToLostChildren.set(
55
+ parent,
56
+ childIds.filter((id) => !overlappingChildren.includes(id))
57
+ )
58
+ }
59
+ }
60
+ }
61
+
62
+ // Get all of the shapes on the current page, sorted by their index
63
+ const sortedShapeIds = editor.getCurrentPageShapesSorted().map((s) => s.id)
64
+
65
+ const parentsToNewChildren: Record<
66
+ TLParentId,
67
+ { parentId: TLParentId; shapeIds: TLShapeId[]; index?: IndexKey }
68
+ > = {}
69
+
70
+ for (const [prevParent, lostChildrenIds] of parentsToLostChildren) {
71
+ const lostChildren = compact(lostChildrenIds.map((id) => editor.getShape(id)))
72
+
73
+ // Don't fall "up" into frames in front of the shape
74
+ // if (pageShapes.indexOf(shape) < frameSortPosition) continue shapeCheck
75
+
76
+ // Otherwise, we have no next dropping shape under the cursor, so go find
77
+ // all the frames on the page where the moving shapes will fall into
78
+ const { reparenting, remainingShapesToReparent } = getDroppedShapesToNewParents(
79
+ editor,
80
+ lostChildren,
81
+ (shape, maybeNewParent) => {
82
+ // If we're filtering out a potential parent, don't reparent shapes to the filtered out shape
83
+ if (opts?.filter && !opts.filter(maybeNewParent)) return false
84
+ return (
85
+ maybeNewParent.id !== prevParent.id &&
86
+ sortedShapeIds.indexOf(maybeNewParent.id) < sortedShapeIds.indexOf(shape.id)
87
+ )
88
+ }
89
+ )
90
+
91
+ reparenting.forEach((childrenToReparent, newParentId) => {
92
+ if (childrenToReparent.length === 0) return
93
+ if (!parentsToNewChildren[newParentId]) {
94
+ parentsToNewChildren[newParentId] = {
95
+ parentId: newParentId,
96
+ shapeIds: [],
97
+ }
98
+ }
99
+ parentsToNewChildren[newParentId].shapeIds.push(...childrenToReparent.map((s) => s.id))
100
+ })
101
+
102
+ // Reparent the rest to the page (or containing group)
103
+ if (remainingShapesToReparent.size > 0) {
104
+ // The remaining shapes are going to be reparented to the old parent's containing group, if there was one, or else to the page
105
+ const newParentId =
106
+ editor.findShapeAncestor(prevParent, (s) => editor.isShapeOfType<TLGroupShape>(s, 'group'))
107
+ ?.id ?? editor.getCurrentPageId()
108
+
109
+ remainingShapesToReparent.forEach((shape) => {
110
+ if (!parentsToNewChildren[newParentId]) {
111
+ let insertIndexKey: IndexKey | undefined
112
+
113
+ const oldParentSiblingIds = editor.getSortedChildIdsForParent(newParentId)
114
+ const oldParentIndex = oldParentSiblingIds.indexOf(prevParent.id)
115
+ if (oldParentIndex > -1) {
116
+ // If the old parent is a direct child of the new parent, then we'll add them above the old parent but below the next sibling.
117
+ const siblingsIndexAbove = oldParentSiblingIds[oldParentIndex + 1]
118
+ const indexKeyAbove = siblingsIndexAbove
119
+ ? editor.getShape(siblingsIndexAbove)!.index
120
+ : getIndexAbove(prevParent.index)
121
+ insertIndexKey = getIndexBetween(prevParent.index, indexKeyAbove)
122
+ } else {
123
+ // If the old parent is not a direct child of the new parent, then we'll add them to the "top" of the new parent's children.
124
+ // This is done automatically if we leave the index undefined, so let's do that.
125
+ }
126
+
127
+ parentsToNewChildren[newParentId] = {
128
+ parentId: newParentId,
129
+ shapeIds: [],
130
+ index: insertIndexKey,
131
+ }
132
+ }
133
+
134
+ parentsToNewChildren[newParentId].shapeIds.push(shape.id)
135
+ })
136
+ }
137
+ }
138
+
139
+ editor.run(() => {
140
+ Object.values(parentsToNewChildren).forEach(({ parentId, shapeIds, index }) => {
141
+ if (shapeIds.length === 0) return
142
+ // Before we reparent, sort the new shape ids by their place in the original absolute order on the page
143
+ shapeIds.sort((a, b) => (sortedShapeIds.indexOf(a) < sortedShapeIds.indexOf(b) ? -1 : 1))
144
+ editor.reparentShapes(shapeIds, parentId, index)
145
+ })
146
+ })
147
+ }
148
+
149
+ /**
150
+ * Get the shapes that overlap with a given shape.
151
+ *
152
+ * @param editor - The editor instance.
153
+ * @param shape - The shapes or shape IDs to check against.
154
+ * @param otherShapes - The shapes or shape IDs to check for overlap.
155
+ * @returns An array of shapes or shape IDs that overlap with the given shape.
156
+ */
157
+ function getOverlappingShapes<T extends TLShape[] | TLShapeId[]>(
158
+ editor: Editor,
159
+ shape: T[number],
160
+ otherShapes: T
161
+ ) {
162
+ if (otherShapes.length === 0) {
163
+ return EMPTY_ARRAY
164
+ }
165
+
166
+ const parentPageBounds = editor.getShapePageBounds(shape)
167
+ if (!parentPageBounds) return EMPTY_ARRAY
168
+
169
+ const parentGeometry = editor.getShapeGeometry(shape)
170
+ const parentPageTransform = editor.getShapePageTransform(shape)
171
+ const parentPageCorners = parentPageTransform.applyToPoints(parentGeometry.vertices)
172
+
173
+ const parentPageMaskVertices = editor.getShapeMask(shape)
174
+ const parentPagePolygon = parentPageMaskVertices
175
+ ? intersectPolygonPolygon(parentPageMaskVertices, parentPageCorners)
176
+ : parentPageCorners
177
+
178
+ if (!parentPagePolygon) return EMPTY_ARRAY
179
+
180
+ return otherShapes.filter((childId) => {
181
+ const shapePageBounds = editor.getShapePageBounds(childId)
182
+ if (!shapePageBounds || !parentPageBounds.includes(shapePageBounds)) return false
183
+
184
+ const parentPolygonInShapeShape = editor
185
+ .getShapePageTransform(childId)
186
+ .clone()
187
+ .invert()
188
+ .applyToPoints(parentPagePolygon)
189
+
190
+ const geometry = editor.getShapeGeometry(childId)
191
+
192
+ return doesGeometryOverlapPolygon(geometry, parentPolygonInShapeShape)
193
+ })
194
+ }
195
+
196
+ /**
197
+ * @public
198
+ */
199
+ export function doesGeometryOverlapPolygon(
200
+ geometry: Geometry2d,
201
+ parentCornersInShapeSpace: Vec[]
202
+ ): boolean {
203
+ // If the child is a group, check if any of its children overlap the box
204
+ if (geometry instanceof Group2d) {
205
+ return geometry.children.some((childGeometry) =>
206
+ doesGeometryOverlapPolygon(childGeometry, parentCornersInShapeSpace)
207
+ )
208
+ }
209
+
210
+ // Otherwise, check if the geometry overlaps the box
211
+ const { vertices, center, isFilled, isEmptyLabel, isClosed } = geometry
212
+
213
+ // We'll do things in order of cheapest to most expensive checks
214
+
215
+ // Skip empty labels
216
+ if (isEmptyLabel) return false
217
+
218
+ // If any of the shape's vertices are inside the occluder, it's inside
219
+ if (vertices.some((v) => pointInPolygon(v, parentCornersInShapeSpace))) {
220
+ return true
221
+ }
222
+
223
+ // If the shape is filled and closed and its center is inside the parent, it's inside
224
+ if (isClosed) {
225
+ if (isFilled) {
226
+ // If closed and filled, check if the center is inside the parent
227
+ if (pointInPolygon(center, parentCornersInShapeSpace)) {
228
+ return true
229
+ }
230
+
231
+ // ..then, slightly more expensive check, see the shape covers the entire parent but not its center
232
+ if (parentCornersInShapeSpace.every((v) => pointInPolygon(v, vertices))) {
233
+ return true
234
+ }
235
+ }
236
+
237
+ // If any the shape's vertices intersect the edge of the occluder, it's inside.
238
+ // for example when a rotated rectangle is moved over the corner of a parent rectangle
239
+ // If the child shape is closed, intersect as a polygon
240
+ if (polygonsIntersect(parentCornersInShapeSpace, vertices)) {
241
+ return true
242
+ }
243
+ } else {
244
+ // if the child shape is not closed, intersect as a polyline
245
+ if (polygonIntersectsPolyline(parentCornersInShapeSpace, vertices)) {
246
+ return true
247
+ }
248
+ }
249
+
250
+ // If none of the above checks passed, the shape is outside the parent
251
+ return false
252
+ }
253
+
254
+ /**
255
+ * Get the shapes that will be reparented to new parents when the shapes are dropped.
256
+ *
257
+ * @param editor - The editor instance.
258
+ * @param shapes - The shapes to check.
259
+ * @param cb - A callback to filter out certain shapes.
260
+ * @returns An object with the shapes that will be reparented to new parents and the shapes that will be reparented to the page or their ancestral group.
261
+ *
262
+ * @public
263
+ */
264
+ export function getDroppedShapesToNewParents(
265
+ editor: Editor,
266
+ shapes: Set<TLShape> | TLShape[],
267
+ cb?: (shape: TLShape, parent: TLShape) => boolean
268
+ ) {
269
+ const shapesToActuallyCheck = new Set<TLShape>(shapes)
270
+ const movingGroups = new Set<TLGroupShape>()
271
+
272
+ for (const shape of shapes) {
273
+ const parent = editor.getShapeParent(shape)
274
+ if (parent && editor.isShapeOfType<TLGroupShape>(parent, 'group')) {
275
+ if (!movingGroups.has(parent)) {
276
+ movingGroups.add(parent)
277
+ }
278
+ }
279
+ }
280
+
281
+ // If all of a group's children are moving, then move the group instead
282
+ for (const movingGroup of movingGroups) {
283
+ const children = compact(
284
+ editor.getSortedChildIdsForParent(movingGroup).map((id) => editor.getShape(id))
285
+ )
286
+ for (const child of children) {
287
+ shapesToActuallyCheck.delete(child)
288
+ }
289
+ shapesToActuallyCheck.add(movingGroup)
290
+ }
291
+
292
+ // this could be cached and passed in
293
+ const shapeGroupIds = new Map<TLShapeId, TLShapeId | undefined>()
294
+
295
+ const reparenting = new Map<TLShapeId, TLShape[]>()
296
+
297
+ const remainingShapesToReparent = new Set(shapesToActuallyCheck)
298
+
299
+ const potentialParentShapes = editor
300
+ .getCurrentPageShapesSorted()
301
+ // filter out any shapes that aren't frames or that are included among the provided shapes
302
+ .filter(
303
+ (s) =>
304
+ editor.getShapeUtil(s).canReceiveNewChildrenOfType?.(s, s.type) &&
305
+ !remainingShapesToReparent.has(s)
306
+ )
307
+
308
+ parentCheck: for (let i = potentialParentShapes.length - 1; i >= 0; i--) {
309
+ const parentShape = potentialParentShapes[i]
310
+ const parentShapeContainingGroupId = editor.findShapeAncestor(parentShape, (s) =>
311
+ editor.isShapeOfType<TLGroupShape>(s, 'group')
312
+ )?.id
313
+
314
+ const parentGeometry = editor.getShapeGeometry(parentShape)
315
+ const parentPageTransform = editor.getShapePageTransform(parentShape)
316
+ const parentPageMaskVertices = editor.getShapeMask(parentShape)
317
+ const parentPageCorners = parentPageTransform.applyToPoints(parentGeometry.vertices)
318
+ const parentPagePolygon = parentPageMaskVertices
319
+ ? intersectPolygonPolygon(parentPageMaskVertices, parentPageCorners)
320
+ : parentPageCorners
321
+
322
+ if (!parentPagePolygon) continue parentCheck
323
+
324
+ const childrenToReparent = []
325
+
326
+ // For each of the dropping shapes...
327
+ shapeCheck: for (const shape of remainingShapesToReparent) {
328
+ // Don't reparent a frame to itself
329
+ if (parentShape.id === shape.id) continue shapeCheck
330
+
331
+ // Use the callback to filter out certain shapes
332
+ if (cb && !cb(shape, parentShape)) continue shapeCheck
333
+
334
+ if (!shapeGroupIds.has(shape.id)) {
335
+ shapeGroupIds.set(
336
+ shape.id,
337
+ editor.findShapeAncestor(shape, (s) => editor.isShapeOfType<TLGroupShape>(s, 'group'))?.id
338
+ )
339
+ }
340
+
341
+ const shapeGroupId = shapeGroupIds.get(shape.id)
342
+
343
+ // Are the shape and the parent part of different groups?
344
+ if (shapeGroupId !== parentShapeContainingGroupId) continue shapeCheck
345
+
346
+ // Is the shape is actually the ancestor of the parent?
347
+ if (editor.findShapeAncestor(parentShape, (s) => shape.id === s.id)) continue shapeCheck
348
+
349
+ // Convert the parent polygon to the shape's space
350
+ const parentPolygonInShapeSpace = editor
351
+ .getShapePageTransform(shape)
352
+ .clone()
353
+ .invert()
354
+ .applyToPoints(parentPagePolygon)
355
+
356
+ // If the shape overlaps the parent polygon, reparent it to that parent
357
+ if (doesGeometryOverlapPolygon(editor.getShapeGeometry(shape), parentPolygonInShapeSpace)) {
358
+ // Use the util to check if the shape can be reparented to the parent
359
+ if (
360
+ !editor.getShapeUtil(parentShape).canReceiveNewChildrenOfType?.(parentShape, shape.type)
361
+ )
362
+ continue shapeCheck
363
+
364
+ if (shape.parentId !== parentShape.id) {
365
+ childrenToReparent.push(shape)
366
+ }
367
+ remainingShapesToReparent.delete(shape)
368
+ continue shapeCheck
369
+ }
370
+ }
371
+
372
+ if (childrenToReparent.length) {
373
+ reparenting.set(parentShape.id, childrenToReparent)
374
+ }
375
+ }
376
+
377
+ return {
378
+ // these are the shapes that will be reparented to new parents
379
+ reparenting,
380
+ // these are the shapes that will be reparented to the page or their ancestral group
381
+ remainingShapesToReparent,
382
+ }
383
+ }
package/src/version.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  // This file is automatically generated by internal/scripts/refresh-assets.ts.
2
2
  // Do not edit manually. Or do, I'm a comment, not a cop.
3
3
 
4
- export const version = '3.14.0-canary.6d58db7084e2'
4
+ export const version = '3.14.0-canary.6fbbca54ff57'
5
5
  export const publishDates = {
6
6
  major: '2024-09-13T14:36:29.063Z',
7
- minor: '2025-06-24T11:25:37.563Z',
8
- patch: '2025-06-24T11:25:37.563Z',
7
+ minor: '2025-06-24T16:09:26.541Z',
8
+ patch: '2025-06-24T16:09:26.541Z',
9
9
  }