@tldraw/editor 3.14.0-canary.0f41576b87d4 → 3.14.0-canary.103612e26824

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 (44) hide show
  1. package/dist-cjs/index.d.ts +13 -92
  2. package/dist-cjs/index.js +1 -4
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/editor/Editor.js +23 -50
  5. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  6. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js +1 -3
  7. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js.map +2 -2
  8. package/dist-cjs/lib/editor/shapes/ShapeUtil.js +10 -0
  9. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  10. package/dist-cjs/lib/editor/tools/BaseBoxShapeTool/children/Pointing.js +6 -10
  11. package/dist-cjs/lib/editor/tools/BaseBoxShapeTool/children/Pointing.js.map +3 -3
  12. package/dist-cjs/lib/primitives/geometry/Geometry2d.js +2 -6
  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 +13 -92
  17. package/dist-esm/index.mjs +1 -4
  18. package/dist-esm/index.mjs.map +2 -2
  19. package/dist-esm/lib/editor/Editor.mjs +23 -50
  20. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  21. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs +1 -3
  22. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs.map +2 -2
  23. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +10 -0
  24. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  25. package/dist-esm/lib/editor/tools/BaseBoxShapeTool/children/Pointing.mjs +6 -10
  26. package/dist-esm/lib/editor/tools/BaseBoxShapeTool/children/Pointing.mjs.map +3 -3
  27. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs +2 -6
  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/editor.css +11 -10
  32. package/package.json +7 -7
  33. package/src/index.ts +0 -5
  34. package/src/lib/editor/Editor.ts +33 -67
  35. package/src/lib/editor/managers/HistoryManager/HistoryManager.ts +1 -3
  36. package/src/lib/editor/shapes/ShapeUtil.ts +15 -46
  37. package/src/lib/editor/tools/BaseBoxShapeTool/children/Pointing.ts +17 -22
  38. package/src/lib/primitives/geometry/Geometry2d.ts +2 -7
  39. package/src/version.ts +3 -3
  40. package/dist-cjs/lib/utils/reparenting.js +0 -232
  41. package/dist-cjs/lib/utils/reparenting.js.map +0 -7
  42. package/dist-esm/lib/utils/reparenting.mjs +0 -216
  43. package/dist-esm/lib/utils/reparenting.mjs.map +0 -7
  44. package/src/lib/utils/reparenting.ts +0 -383
package/src/index.ts CHANGED
@@ -182,10 +182,6 @@ 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,
189
185
  type TLGeometryOpts,
190
186
  type TLHandleDragInfo,
191
187
  type TLResizeInfo,
@@ -450,7 +446,6 @@ export { hardResetEditor } from './lib/utils/hardResetEditor'
450
446
  export { isAccelKey } from './lib/utils/keyboard'
451
447
  export { normalizeWheel } from './lib/utils/normalizeWheel'
452
448
  export { refreshPage } from './lib/utils/refreshPage'
453
- export { getDroppedShapesToNewParents, kickoutOccludedShapes } from './lib/utils/reparenting'
454
449
  export {
455
450
  getFontsFromRichText,
456
451
  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.getShape(freshShape.parentId)
5531
+ return this.store.get(freshShape.parentId)
5532
5532
  }
5533
5533
 
5534
5534
  /**
@@ -5711,10 +5711,6 @@ 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
-
5718
5714
  changes.push({
5719
5715
  id: shape.id,
5720
5716
  type: shape.type,
@@ -5818,11 +5814,6 @@ export class Editor extends EventEmitter<TLEventMap> {
5818
5814
  return shapeIds
5819
5815
  }
5820
5816
 
5821
- /** @deprecated Use {@link Editor.getDraggingOverShape} instead */
5822
- getDroppingOverShape(point: Vec, droppingShapes: TLShape[]): TLShape | undefined {
5823
- return this.getDraggingOverShape(point, droppingShapes)
5824
- }
5825
-
5826
5817
  /**
5827
5818
  * Get the shape that some shapes should be dropped on at a given point.
5828
5819
  *
@@ -5833,33 +5824,35 @@ export class Editor extends EventEmitter<TLEventMap> {
5833
5824
  *
5834
5825
  * @public
5835
5826
  */
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
- )
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]
5841
5832
 
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
- )
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
+ }
5845
+
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)
5852
5849
 
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
5856
5850
  if (
5857
- shapeUtil.onDragShapesOver ||
5858
- shapeUtil.onDragShapesIn ||
5859
- shapeUtil.onDragShapesOut ||
5860
- shapeUtil.onDropShapesOver
5851
+ maskedPageBounds &&
5852
+ maskedPageBounds.containsPoint(point) &&
5853
+ this.getShapeGeometry(shape).hitTestPoint(this.getPointInShapeSpace(shape, point), 0, true)
5861
5854
  ) {
5862
- return maybeDraggingOverShape
5855
+ return shape
5863
5856
  }
5864
5857
  }
5865
5858
  }
@@ -6303,7 +6296,10 @@ export class Editor extends EventEmitter<TLEventMap> {
6303
6296
  })
6304
6297
  const shapesToCreate = shapesToCreateWithOriginals.map(({ shape }) => shape)
6305
6298
 
6306
- if (!this.canCreateShapes(shapesToCreate)) {
6299
+ const maxShapesReached =
6300
+ shapesToCreate.length + this.getCurrentPageShapeIds().size > this.options.maxShapesPerPage
6301
+
6302
+ if (maxShapesReached) {
6307
6303
  alertMaxShapes(this)
6308
6304
  return
6309
6305
  }
@@ -7732,32 +7728,6 @@ export class Editor extends EventEmitter<TLEventMap> {
7732
7728
  return {}
7733
7729
  }
7734
7730
 
7735
- /**
7736
- * Get whether the provided shape can be created.
7737
- *
7738
- * @param shape - The shape or shape IDs to check.
7739
- *
7740
- * @public
7741
- */
7742
- canCreateShape<T extends TLUnknownShape>(
7743
- shape: OptionalKeys<TLShapePartial<T>, 'id'> | T['id']
7744
- ): boolean {
7745
- return this.canCreateShapes([shape])
7746
- }
7747
-
7748
- /**
7749
- * Get whether the provided shapes can be created.
7750
- *
7751
- * @param shapes - The shapes or shape IDs to create.
7752
- *
7753
- * @public
7754
- */
7755
- canCreateShapes<T extends TLUnknownShape>(
7756
- shapes: (T['id'] | OptionalKeys<TLShapePartial<T>, 'id'>)[]
7757
- ): boolean {
7758
- return shapes.length + this.getCurrentPageShapeIds().size <= this.options.maxShapesPerPage
7759
- }
7760
-
7761
7731
  /**
7762
7732
  * Create a single shape.
7763
7733
  *
@@ -7804,7 +7774,6 @@ export class Editor extends EventEmitter<TLEventMap> {
7804
7774
  if (maxShapesReached) {
7805
7775
  // can't create more shapes than fit on the page
7806
7776
  alertMaxShapes(this)
7807
- // todo: throw an error here? Otherwise we'll need to check every time whether the shapes were actually created
7808
7777
  return this
7809
7778
  }
7810
7779
 
@@ -7837,10 +7806,9 @@ export class Editor extends EventEmitter<TLEventMap> {
7837
7806
 
7838
7807
  for (let i = currentPageShapesSorted.length - 1; i >= 0; i--) {
7839
7808
  const parent = currentPageShapesSorted[i]
7840
- const util = this.getShapeUtil(parent)
7841
7809
  if (
7842
- util.canReceiveNewChildrenOfType(parent, partial.type) &&
7843
7810
  !this.isShapeHidden(parent) &&
7811
+ this.getShapeUtil(parent).canReceiveNewChildrenOfType(parent, partial.type) &&
7844
7812
  this.isPointInShape(
7845
7813
  parent,
7846
7814
  // If no parent is provided, then we can treat the
@@ -9544,8 +9512,6 @@ export class Editor extends EventEmitter<TLEventMap> {
9544
9512
  previousPagePoint,
9545
9513
  currentScreenPoint,
9546
9514
  currentPagePoint,
9547
- originScreenPoint,
9548
- originPagePoint,
9549
9515
  } = this.inputs
9550
9516
 
9551
9517
  const { screenBounds } = this.store.unsafeGetWithoutCapture(TLINSTANCE_ID)!
@@ -9574,8 +9540,8 @@ export class Editor extends EventEmitter<TLEventMap> {
9574
9540
  // Reset velocity on pointer down, or when a pinch starts or ends
9575
9541
  if (info.name === 'pointer_down' || this.inputs.isPinching) {
9576
9542
  pointerVelocity.set(0, 0)
9577
- originScreenPoint.setTo(currentScreenPoint)
9578
- originPagePoint.setTo(currentPagePoint)
9543
+ this.inputs.originScreenPoint.setTo(currentScreenPoint)
9544
+ this.inputs.originPagePoint.setTo(currentPagePoint)
9579
9545
  }
9580
9546
 
9581
9547
  // todo: We only have to do this if there are multiple users in the document
@@ -241,9 +241,7 @@ export class HistoryManager<R extends UnknownRecord> {
241
241
  }
242
242
 
243
243
  bailToMark(id: string) {
244
- if (id) {
245
- this._undo({ pushToRedoStack: false, toMark: id })
246
- }
244
+ this._undo({ pushToRedoStack: false, toMark: id })
247
245
 
248
246
  return this
249
247
  }
@@ -4,15 +4,12 @@ import { LegacyMigrations, MigrationSequence } from '@tldraw/store'
4
4
  import {
5
5
  RecordProps,
6
6
  TLHandle,
7
- TLParentId,
8
7
  TLPropsMigrations,
9
8
  TLShape,
10
9
  TLShapeCrop,
11
- TLShapeId,
12
10
  TLShapePartial,
13
11
  TLUnknownShape,
14
12
  } from '@tldraw/tlschema'
15
- import { IndexKey } from '@tldraw/utils'
16
13
  import { ReactElement } from 'react'
17
14
  import { Box, SelectionHandle } from '../../primitives/Box'
18
15
  import { Vec } from '../../primitives/Vec'
@@ -390,6 +387,17 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
390
387
  return false
391
388
  }
392
389
 
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
+
393
401
  /**
394
402
  * Get the shape as an SVG object.
395
403
  *
@@ -509,16 +517,7 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
509
517
  ): Omit<TLShapePartial<Shape>, 'id' | 'type'> | undefined | void
510
518
 
511
519
  /**
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.
520
+ * A callback called when some other shapes are dragged over this one.
522
521
  *
523
522
  * @example
524
523
  *
@@ -532,7 +531,7 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
532
531
  * @param shapes - The shapes that are being dragged over this one.
533
532
  * @public
534
533
  */
535
- onDragShapesOver?(shape: Shape, shapes: TLShape[], info: TLDragShapesOverInfo): void
534
+ onDragShapesOver?(shape: Shape, shapes: TLShape[]): void
536
535
 
537
536
  /**
538
537
  * A callback called when some other shapes are dragged out of this one.
@@ -541,7 +540,7 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
541
540
  * @param shapes - The shapes that are being dragged out.
542
541
  * @public
543
542
  */
544
- onDragShapesOut?(shape: Shape, shapes: TLShape[], info: TLDragShapesOutInfo): void
543
+ onDragShapesOut?(shape: Shape, shapes: TLShape[]): void
545
544
 
546
545
  /**
547
546
  * A callback called when some other shapes are dropped over this one.
@@ -550,7 +549,7 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
550
549
  * @param shapes - The shapes that are being dropped over this one.
551
550
  * @public
552
551
  */
553
- onDropShapesOver?(shape: Shape, shapes: TLShape[], info: TLDropShapesOverInfo): void
552
+ onDropShapesOver?(shape: Shape, shapes: TLShape[]): void
554
553
 
555
554
  /**
556
555
  * A callback called when a shape starts being resized.
@@ -749,36 +748,6 @@ export interface TLCropInfo<T extends TLShape> {
749
748
  aspectRatioLocked?: boolean
750
749
  }
751
750
 
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
-
782
751
  /**
783
752
  * The type of resize.
784
753
  *
@@ -11,33 +11,29 @@ export class Pointing extends StateNode {
11
11
  static override id = 'pointing'
12
12
 
13
13
  override onPointerMove(info: TLPointerEventInfo) {
14
- const { editor } = this
15
- if (editor.inputs.isDragging) {
16
- const { originPagePoint } = editor.inputs
14
+ if (this.editor.inputs.isDragging) {
15
+ const { originPagePoint } = this.editor.inputs
17
16
 
18
17
  const shapeType = (this.parent as BaseBoxShapeTool)!.shapeType
19
18
 
20
19
  const id = createShapeId()
21
20
 
22
- const creatingMarkId = editor.markHistoryStoppingPoint(`creating_box:${id}`)
23
- const newPoint = maybeSnapToGrid(originPagePoint, editor)
24
-
25
- // Allow this to trigger the max shapes reached alert
26
- this.editor.createShapes<TLBaseBoxShape>([
27
- {
28
- id,
29
- type: shapeType,
30
- x: newPoint.x,
31
- y: newPoint.y,
32
- props: {
33
- w: 1,
34
- h: 1,
21
+ const creatingMarkId = this.editor.markHistoryStoppingPoint(`creating_box:${id}`)
22
+ const newPoint = maybeSnapToGrid(originPagePoint, this.editor)
23
+ this.editor
24
+ .createShapes<TLBaseBoxShape>([
25
+ {
26
+ id,
27
+ type: shapeType,
28
+ x: newPoint.x,
29
+ y: newPoint.y,
30
+ props: {
31
+ w: 1,
32
+ h: 1,
33
+ },
35
34
  },
36
- },
37
- ])
38
- const shape = editor.getShape(id)
39
- if (!shape) this.cancel()
40
- editor.select(id)
35
+ ])
36
+ .select(id)
41
37
 
42
38
  const parent = this.parent as BaseBoxShapeTool
43
39
  this.editor.setCurrentTool(
@@ -83,7 +79,6 @@ export class Pointing extends StateNode {
83
79
 
84
80
  this.editor.markHistoryStoppingPoint(`creating_box:${id}`)
85
81
 
86
- // Allow this to trigger the max shapes reached alert
87
82
  // todo: add scale here when dynamic size is enabled (is this still needed?)
88
83
  this.editor.createShapes<TLBaseBoxShape>([
89
84
  {
@@ -44,7 +44,6 @@ export const Geometry2dFilters: {
44
44
  /** @public */
45
45
  export interface TransformedGeometry2dOptions {
46
46
  isLabel?: boolean
47
- isEmptyLabel?: boolean
48
47
  isInternal?: boolean
49
48
  debugColor?: string
50
49
  ignore?: boolean
@@ -58,24 +57,20 @@ export interface Geometry2dOptions extends TransformedGeometry2dOptions {
58
57
 
59
58
  /** @public */
60
59
  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
62
60
  isFilled = false
63
61
  isClosed = true
64
62
  isLabel = false
65
- isEmptyLabel = false
66
63
  isInternal = false
67
64
  debugColor?: string
68
65
  ignore?: boolean
69
66
 
70
67
  constructor(opts: Geometry2dOptions) {
71
- const { isLabel = false, isEmptyLabel = false, isInternal = false } = opts
72
68
  this.isFilled = opts.isFilled
73
69
  this.isClosed = opts.isClosed
70
+ this.isLabel = opts.isLabel ?? false
71
+ this.isInternal = opts.isInternal ?? false
74
72
  this.debugColor = opts.debugColor
75
73
  this.ignore = opts.ignore
76
- this.isLabel = isLabel
77
- this.isEmptyLabel = isEmptyLabel
78
- this.isInternal = isInternal
79
74
  }
80
75
 
81
76
  isExcludedByFilter(filters?: Geometry2dFilters) {
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.0f41576b87d4'
4
+ export const version = '3.14.0-canary.103612e26824'
5
5
  export const publishDates = {
6
6
  major: '2024-09-13T14:36:29.063Z',
7
- minor: '2025-06-26T16:20:43.145Z',
8
- patch: '2025-06-26T16:20:43.145Z',
7
+ minor: '2025-06-24T10:50:50.049Z',
8
+ patch: '2025-06-24T10:50:50.049Z',
9
9
  }
@@ -1,232 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var reparenting_exports = {};
20
- __export(reparenting_exports, {
21
- doesGeometryOverlapPolygon: () => doesGeometryOverlapPolygon,
22
- getDroppedShapesToNewParents: () => getDroppedShapesToNewParents,
23
- kickoutOccludedShapes: () => kickoutOccludedShapes
24
- });
25
- module.exports = __toCommonJS(reparenting_exports);
26
- var import_state = require("@tldraw/state");
27
- var import_utils = require("@tldraw/utils");
28
- var import_Group2d = require("../primitives/geometry/Group2d");
29
- var import_intersect = require("../primitives/intersect");
30
- var import_utils2 = require("../primitives/utils");
31
- function kickoutOccludedShapes(editor, shapeIds, opts) {
32
- const parentsToCheck = /* @__PURE__ */ new Set();
33
- for (const id of shapeIds) {
34
- const shape = editor.getShape(id);
35
- if (!shape) continue;
36
- parentsToCheck.add(shape);
37
- const parent = editor.getShape(shape.parentId);
38
- if (!parent) continue;
39
- parentsToCheck.add(parent);
40
- }
41
- const parentsToLostChildren = /* @__PURE__ */ new Map();
42
- for (const parent of parentsToCheck) {
43
- const childIds = editor.getSortedChildIdsForParent(parent);
44
- if (opts?.filter && !opts.filter(parent)) {
45
- parentsToLostChildren.set(parent, childIds);
46
- } else {
47
- const overlappingChildren = getOverlappingShapes(editor, parent.id, childIds);
48
- if (overlappingChildren.length < childIds.length) {
49
- parentsToLostChildren.set(
50
- parent,
51
- childIds.filter((id) => !overlappingChildren.includes(id))
52
- );
53
- }
54
- }
55
- }
56
- const sortedShapeIds = editor.getCurrentPageShapesSorted().map((s) => s.id);
57
- const parentsToNewChildren = {};
58
- for (const [prevParent, lostChildrenIds] of parentsToLostChildren) {
59
- const lostChildren = (0, import_utils.compact)(lostChildrenIds.map((id) => editor.getShape(id)));
60
- const { reparenting, remainingShapesToReparent } = getDroppedShapesToNewParents(
61
- editor,
62
- lostChildren,
63
- (shape, maybeNewParent) => {
64
- if (opts?.filter && !opts.filter(maybeNewParent)) return false;
65
- return maybeNewParent.id !== prevParent.id && sortedShapeIds.indexOf(maybeNewParent.id) < sortedShapeIds.indexOf(shape.id);
66
- }
67
- );
68
- reparenting.forEach((childrenToReparent, newParentId) => {
69
- if (childrenToReparent.length === 0) return;
70
- if (!parentsToNewChildren[newParentId]) {
71
- parentsToNewChildren[newParentId] = {
72
- parentId: newParentId,
73
- shapeIds: []
74
- };
75
- }
76
- parentsToNewChildren[newParentId].shapeIds.push(...childrenToReparent.map((s) => s.id));
77
- });
78
- if (remainingShapesToReparent.size > 0) {
79
- const newParentId = editor.findShapeAncestor(prevParent, (s) => editor.isShapeOfType(s, "group"))?.id ?? editor.getCurrentPageId();
80
- remainingShapesToReparent.forEach((shape) => {
81
- if (!parentsToNewChildren[newParentId]) {
82
- let insertIndexKey;
83
- const oldParentSiblingIds = editor.getSortedChildIdsForParent(newParentId);
84
- const oldParentIndex = oldParentSiblingIds.indexOf(prevParent.id);
85
- if (oldParentIndex > -1) {
86
- const siblingsIndexAbove = oldParentSiblingIds[oldParentIndex + 1];
87
- const indexKeyAbove = siblingsIndexAbove ? editor.getShape(siblingsIndexAbove).index : (0, import_utils.getIndexAbove)(prevParent.index);
88
- insertIndexKey = (0, import_utils.getIndexBetween)(prevParent.index, indexKeyAbove);
89
- } else {
90
- }
91
- parentsToNewChildren[newParentId] = {
92
- parentId: newParentId,
93
- shapeIds: [],
94
- index: insertIndexKey
95
- };
96
- }
97
- parentsToNewChildren[newParentId].shapeIds.push(shape.id);
98
- });
99
- }
100
- }
101
- editor.run(() => {
102
- Object.values(parentsToNewChildren).forEach(({ parentId, shapeIds: shapeIds2, index }) => {
103
- if (shapeIds2.length === 0) return;
104
- shapeIds2.sort((a, b) => sortedShapeIds.indexOf(a) < sortedShapeIds.indexOf(b) ? -1 : 1);
105
- editor.reparentShapes(shapeIds2, parentId, index);
106
- });
107
- });
108
- }
109
- function getOverlappingShapes(editor, shape, otherShapes) {
110
- if (otherShapes.length === 0) {
111
- return import_state.EMPTY_ARRAY;
112
- }
113
- const parentPageBounds = editor.getShapePageBounds(shape);
114
- if (!parentPageBounds) return import_state.EMPTY_ARRAY;
115
- const parentGeometry = editor.getShapeGeometry(shape);
116
- const parentPageTransform = editor.getShapePageTransform(shape);
117
- const parentPageCorners = parentPageTransform.applyToPoints(parentGeometry.vertices);
118
- const parentPageMaskVertices = editor.getShapeMask(shape);
119
- const parentPagePolygon = parentPageMaskVertices ? (0, import_intersect.intersectPolygonPolygon)(parentPageMaskVertices, parentPageCorners) : parentPageCorners;
120
- if (!parentPagePolygon) return import_state.EMPTY_ARRAY;
121
- return otherShapes.filter((childId) => {
122
- const shapePageBounds = editor.getShapePageBounds(childId);
123
- if (!shapePageBounds || !parentPageBounds.includes(shapePageBounds)) return false;
124
- const parentPolygonInShapeShape = editor.getShapePageTransform(childId).clone().invert().applyToPoints(parentPagePolygon);
125
- const geometry = editor.getShapeGeometry(childId);
126
- return doesGeometryOverlapPolygon(geometry, parentPolygonInShapeShape);
127
- });
128
- }
129
- function doesGeometryOverlapPolygon(geometry, parentCornersInShapeSpace) {
130
- if (geometry instanceof import_Group2d.Group2d) {
131
- return geometry.children.some(
132
- (childGeometry) => doesGeometryOverlapPolygon(childGeometry, parentCornersInShapeSpace)
133
- );
134
- }
135
- const { vertices, center, isFilled, isEmptyLabel, isClosed } = geometry;
136
- if (isEmptyLabel) return false;
137
- if (vertices.some((v) => (0, import_utils2.pointInPolygon)(v, parentCornersInShapeSpace))) {
138
- return true;
139
- }
140
- if (isClosed) {
141
- if (isFilled) {
142
- if ((0, import_utils2.pointInPolygon)(center, parentCornersInShapeSpace)) {
143
- return true;
144
- }
145
- if (parentCornersInShapeSpace.every((v) => (0, import_utils2.pointInPolygon)(v, vertices))) {
146
- return true;
147
- }
148
- }
149
- if ((0, import_intersect.polygonsIntersect)(parentCornersInShapeSpace, vertices)) {
150
- return true;
151
- }
152
- } else {
153
- if ((0, import_intersect.polygonIntersectsPolyline)(parentCornersInShapeSpace, vertices)) {
154
- return true;
155
- }
156
- }
157
- return false;
158
- }
159
- function getDroppedShapesToNewParents(editor, shapes, cb) {
160
- const shapesToActuallyCheck = new Set(shapes);
161
- const movingGroups = /* @__PURE__ */ new Set();
162
- for (const shape of shapes) {
163
- const parent = editor.getShapeParent(shape);
164
- if (parent && editor.isShapeOfType(parent, "group")) {
165
- if (!movingGroups.has(parent)) {
166
- movingGroups.add(parent);
167
- }
168
- }
169
- }
170
- for (const movingGroup of movingGroups) {
171
- const children = (0, import_utils.compact)(
172
- editor.getSortedChildIdsForParent(movingGroup).map((id) => editor.getShape(id))
173
- );
174
- for (const child of children) {
175
- shapesToActuallyCheck.delete(child);
176
- }
177
- shapesToActuallyCheck.add(movingGroup);
178
- }
179
- const shapeGroupIds = /* @__PURE__ */ new Map();
180
- const reparenting = /* @__PURE__ */ new Map();
181
- const remainingShapesToReparent = new Set(shapesToActuallyCheck);
182
- const potentialParentShapes = editor.getCurrentPageShapesSorted().filter(
183
- (s) => editor.getShapeUtil(s).canReceiveNewChildrenOfType?.(s, s.type) && !remainingShapesToReparent.has(s)
184
- );
185
- parentCheck: for (let i = potentialParentShapes.length - 1; i >= 0; i--) {
186
- const parentShape = potentialParentShapes[i];
187
- const parentShapeContainingGroupId = editor.findShapeAncestor(
188
- parentShape,
189
- (s) => editor.isShapeOfType(s, "group")
190
- )?.id;
191
- const parentGeometry = editor.getShapeGeometry(parentShape);
192
- const parentPageTransform = editor.getShapePageTransform(parentShape);
193
- const parentPageMaskVertices = editor.getShapeMask(parentShape);
194
- const parentPageCorners = parentPageTransform.applyToPoints(parentGeometry.vertices);
195
- const parentPagePolygon = parentPageMaskVertices ? (0, import_intersect.intersectPolygonPolygon)(parentPageMaskVertices, parentPageCorners) : parentPageCorners;
196
- if (!parentPagePolygon) continue parentCheck;
197
- const childrenToReparent = [];
198
- shapeCheck: for (const shape of remainingShapesToReparent) {
199
- if (parentShape.id === shape.id) continue shapeCheck;
200
- if (cb && !cb(shape, parentShape)) continue shapeCheck;
201
- if (!shapeGroupIds.has(shape.id)) {
202
- shapeGroupIds.set(
203
- shape.id,
204
- editor.findShapeAncestor(shape, (s) => editor.isShapeOfType(s, "group"))?.id
205
- );
206
- }
207
- const shapeGroupId = shapeGroupIds.get(shape.id);
208
- if (shapeGroupId !== parentShapeContainingGroupId) continue shapeCheck;
209
- if (editor.findShapeAncestor(parentShape, (s) => shape.id === s.id)) continue shapeCheck;
210
- const parentPolygonInShapeSpace = editor.getShapePageTransform(shape).clone().invert().applyToPoints(parentPagePolygon);
211
- if (doesGeometryOverlapPolygon(editor.getShapeGeometry(shape), parentPolygonInShapeSpace)) {
212
- if (!editor.getShapeUtil(parentShape).canReceiveNewChildrenOfType?.(parentShape, shape.type))
213
- continue shapeCheck;
214
- if (shape.parentId !== parentShape.id) {
215
- childrenToReparent.push(shape);
216
- }
217
- remainingShapesToReparent.delete(shape);
218
- continue shapeCheck;
219
- }
220
- }
221
- if (childrenToReparent.length) {
222
- reparenting.set(parentShape.id, childrenToReparent);
223
- }
224
- }
225
- return {
226
- // these are the shapes that will be reparented to new parents
227
- reparenting,
228
- // these are the shapes that will be reparented to the page or their ancestral group
229
- remainingShapesToReparent
230
- };
231
- }
232
- //# sourceMappingURL=reparenting.js.map