@tldraw/editor 4.5.0-canary.b245c8ea38ef → 4.5.0-canary.b83751dd9949

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 (46) hide show
  1. package/dist-cjs/index.d.ts +20 -1
  2. package/dist-cjs/index.js +2 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +13 -16
  5. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  6. package/dist-cjs/lib/editor/Editor.js +117 -167
  7. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  8. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js +15 -16
  9. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js.map +3 -3
  10. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +1 -1
  11. package/dist-cjs/lib/hooks/useCanvasEvents.js +3 -3
  12. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  13. package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js +1 -2
  14. package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js.map +2 -2
  15. package/dist-cjs/lib/utils/dom.js +8 -4
  16. package/dist-cjs/lib/utils/dom.js.map +2 -2
  17. package/dist-cjs/version.js +3 -3
  18. package/dist-cjs/version.js.map +1 -1
  19. package/dist-esm/index.d.mts +20 -1
  20. package/dist-esm/index.mjs +3 -1
  21. package/dist-esm/index.mjs.map +2 -2
  22. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +13 -16
  23. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  24. package/dist-esm/lib/editor/Editor.mjs +117 -167
  25. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  26. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs +15 -16
  27. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs.map +3 -3
  28. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +1 -1
  29. package/dist-esm/lib/hooks/useCanvasEvents.mjs +9 -4
  30. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  31. package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs +2 -3
  32. package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs.map +2 -2
  33. package/dist-esm/lib/utils/dom.mjs +8 -4
  34. package/dist-esm/lib/utils/dom.mjs.map +2 -2
  35. package/dist-esm/version.mjs +3 -3
  36. package/dist-esm/version.mjs.map +1 -1
  37. package/package.json +7 -7
  38. package/src/index.ts +1 -0
  39. package/src/lib/components/default-components/DefaultCanvas.tsx +19 -26
  40. package/src/lib/editor/Editor.ts +160 -269
  41. package/src/lib/editor/managers/HistoryManager/HistoryManager.ts +6 -5
  42. package/src/lib/editor/shapes/ShapeUtil.ts +1 -1
  43. package/src/lib/hooks/useCanvasEvents.ts +9 -5
  44. package/src/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.ts +2 -5
  45. package/src/lib/utils/dom.ts +16 -8
  46. package/src/version.ts +3 -3
@@ -115,7 +115,6 @@ export function DefaultCanvas({ className }: TLCanvasComponentProps) {
115
115
  )
116
116
 
117
117
  const hideShapes = useValue('debug_shapes', () => debugFlags.hideShapes.get(), [debugFlags])
118
- const debugSvg = useValue('debug_svg', () => debugFlags.debugSvg.get(), [debugFlags])
119
118
  const debugGeometry = useValue('debug_geometry', () => debugFlags.debugGeometry.get(), [
120
119
  debugFlags,
121
120
  ])
@@ -158,7 +157,7 @@ export function DefaultCanvas({ className }: TLCanvasComponentProps) {
158
157
  <div ref={rHtmlLayer} className="tl-html-layer tl-shapes" draggable={false}>
159
158
  <OnTheCanvasWrapper />
160
159
  {SelectionBackground && <SelectionBackgroundWrapper />}
161
- {hideShapes ? null : debugSvg ? <ShapesWithSVGs /> : <ShapesToDisplay />}
160
+ {hideShapes ? null : <ShapesLayer />}
162
161
  </div>
163
162
  <div className="tl-overlays">
164
163
  <CanvasShapeIndicators />
@@ -396,17 +395,27 @@ function OverlaysWrapper() {
396
395
  )
397
396
  }
398
397
 
399
- function ShapesWithSVGs() {
398
+ function ShapesLayer() {
400
399
  const editor = useEditor()
401
-
400
+ const debugSvg = useValue('debug svg', () => debugFlags.debugSvg.get(), [debugFlags])
402
401
  const renderingShapes = useValue('rendering shapes', () => editor.getRenderingShapes(), [editor])
403
402
 
404
- return renderingShapes.map((result) => (
405
- <Fragment key={result.id + '_fragment'}>
406
- <Shape {...result} />
407
- <DebugSvgCopy id={result.id} mode="iframe" />
408
- </Fragment>
409
- ))
403
+ return (
404
+ <ShapeCullingProvider>
405
+ {renderingShapes.map((result) =>
406
+ debugSvg ? (
407
+ <Fragment key={result.id + '_fragment'}>
408
+ <Shape {...result} />
409
+ <DebugSvgCopy id={result.id} mode="iframe" />
410
+ </Fragment>
411
+ ) : (
412
+ <Shape key={result.id + '_shape'} {...result} />
413
+ )
414
+ )}
415
+ <CullingController />
416
+ {tlenv.isSafari && <ReflowIfNeeded />}
417
+ </ShapeCullingProvider>
418
+ )
410
419
  }
411
420
  function ReflowIfNeeded() {
412
421
  const editor = useEditor()
@@ -449,22 +458,6 @@ function CullingController() {
449
458
  return null
450
459
  }
451
460
 
452
- function ShapesToDisplay() {
453
- const editor = useEditor()
454
-
455
- const renderingShapes = useValue('rendering shapes', () => editor.getRenderingShapes(), [editor])
456
-
457
- return (
458
- <ShapeCullingProvider>
459
- {renderingShapes.map((result) => (
460
- <Shape key={result.id + '_shape'} {...result} />
461
- ))}
462
- <CullingController />
463
- {tlenv.isSafari && <ReflowIfNeeded />}
464
- </ShapeCullingProvider>
465
- )
466
- }
467
-
468
461
  function HintedShapeIndicator() {
469
462
  const editor = useEditor()
470
463
  const { ShapeIndicator } = useEditorComponents()
@@ -158,6 +158,7 @@ import {
158
158
  TLEditStartInfo,
159
159
  TLGeometryOpts,
160
160
  TLResizeMode,
161
+ TLShapeUtilCanBeLaidOutOpts,
161
162
  TLShapeUtilCanBindOpts,
162
163
  } from './shapes/ShapeUtil'
163
164
  import { RootState } from './tools/RootState'
@@ -6771,6 +6772,77 @@ export class Editor extends EventEmitter<TLEventMap> {
6771
6772
  return this
6772
6773
  }
6773
6774
 
6775
+ /**
6776
+ * Shared clustering logic for layout methods. Resolves shapes, optionally filters to
6777
+ * axis-aligned shapes, checks canBeLaidOut, and groups shapes into clusters via arrow bindings.
6778
+ *
6779
+ * @internal
6780
+ */
6781
+ private getShapeClusters(
6782
+ shapes: TLShapeId[] | TLShape[],
6783
+ type: TLShapeUtilCanBeLaidOutOpts['type'],
6784
+ opts?: { filterAxisAligned?: boolean }
6785
+ ): { clusters: { shapes: TLShape[]; pageBounds: Box }[]; allBounds: Box[] } {
6786
+ const ids =
6787
+ typeof shapes[0] === 'string'
6788
+ ? (shapes as TLShapeId[])
6789
+ : (shapes as TLShape[]).map((s) => s.id)
6790
+
6791
+ // always fresh shapes
6792
+ let freshShapes = compact(ids.map((id) => this.getShape(id)))
6793
+
6794
+ // optionally filter to axis-aligned shapes (rotation is a multiple of 90 degrees)
6795
+ if (opts?.filterAxisAligned) {
6796
+ freshShapes = freshShapes.filter(
6797
+ (s) => this.getShapePageTransform(s)?.rotation() % (PI / 2) === 0
6798
+ )
6799
+ }
6800
+
6801
+ const clusters: { shapes: TLShape[]; pageBounds: Box }[] = []
6802
+ const allBounds: Box[] = []
6803
+ const visited = new Set<TLShapeId>()
6804
+
6805
+ for (const shape of freshShapes) {
6806
+ if (visited.has(shape.id)) continue
6807
+ visited.add(shape.id)
6808
+
6809
+ const shapePageBounds = this.getShapePageBounds(shape)
6810
+ if (!shapePageBounds) continue
6811
+
6812
+ if (
6813
+ !this.getShapeUtil(shape).canBeLaidOut?.(shape, {
6814
+ type,
6815
+ shapes: freshShapes,
6816
+ })
6817
+ ) {
6818
+ continue
6819
+ }
6820
+
6821
+ const shapesMovingTogether = [shape]
6822
+ const boundsOfShapesMovingTogether: Box[] = [shapePageBounds]
6823
+
6824
+ this.collectShapesViaArrowBindings({
6825
+ bindings: this.getBindingsToShape(shape.id, 'arrow'),
6826
+ initialShapes: freshShapes,
6827
+ resultShapes: shapesMovingTogether,
6828
+ resultBounds: boundsOfShapesMovingTogether,
6829
+ visited,
6830
+ })
6831
+
6832
+ const commonPageBounds = Box.Common(boundsOfShapesMovingTogether)
6833
+ if (!commonPageBounds) continue
6834
+
6835
+ clusters.push({
6836
+ shapes: shapesMovingTogether,
6837
+ pageBounds: commonPageBounds,
6838
+ })
6839
+
6840
+ allBounds.push(commonPageBounds)
6841
+ }
6842
+
6843
+ return { clusters, allBounds }
6844
+ }
6845
+
6774
6846
  /**
6775
6847
  * @internal
6776
6848
  */
@@ -6916,61 +6988,11 @@ export class Editor extends EventEmitter<TLEventMap> {
6916
6988
  gap?: number
6917
6989
  ): this {
6918
6990
  const _gap = gap ?? this.options.adjacentShapeMargin
6919
- const ids =
6920
- typeof shapes[0] === 'string'
6921
- ? (shapes as TLShapeId[])
6922
- : (shapes as TLShape[]).map((s) => s.id)
6923
6991
  if (this.getIsReadonly()) return this
6924
6992
 
6925
6993
  // todo: this has a lot of extra code to handle stacking with custom gaps or auto gaps or other things like that. I don't think anyone has ever used this stuff.
6926
6994
 
6927
- // always fresh shapes
6928
- const shapesToStackFirstPass = compact(ids.map((id) => this.getShape(id)))
6929
-
6930
- const shapeClustersToStack: {
6931
- shapes: TLShape[]
6932
- pageBounds: Box
6933
- }[] = []
6934
- const allBounds: Box[] = []
6935
- const visited = new Set<TLShapeId>()
6936
-
6937
- for (const shape of shapesToStackFirstPass) {
6938
- if (visited.has(shape.id)) continue
6939
- visited.add(shape.id)
6940
-
6941
- const shapePageBounds = this.getShapePageBounds(shape)
6942
- if (!shapePageBounds) continue
6943
-
6944
- if (
6945
- !this.getShapeUtil(shape).canBeLaidOut?.(shape, {
6946
- type: 'stack',
6947
- shapes: shapesToStackFirstPass,
6948
- })
6949
- ) {
6950
- continue
6951
- }
6952
-
6953
- const shapesMovingTogether = [shape]
6954
- const boundsOfShapesMovingTogether: Box[] = [shapePageBounds]
6955
-
6956
- this.collectShapesViaArrowBindings({
6957
- bindings: this.getBindingsToShape(shape.id, 'arrow'),
6958
- initialShapes: shapesToStackFirstPass,
6959
- resultShapes: shapesMovingTogether,
6960
- resultBounds: boundsOfShapesMovingTogether,
6961
- visited,
6962
- })
6963
-
6964
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether)
6965
- if (!commonPageBounds) continue
6966
-
6967
- shapeClustersToStack.push({
6968
- shapes: shapesMovingTogether,
6969
- pageBounds: commonPageBounds,
6970
- })
6971
-
6972
- allBounds.push(commonPageBounds)
6973
- }
6995
+ const { clusters: shapeClustersToStack } = this.getShapeClusters(shapes, 'stack')
6974
6996
 
6975
6997
  const len = shapeClustersToStack.length
6976
6998
  if ((_gap === 0 && len < 3) || len < 2) return this
@@ -7086,61 +7108,12 @@ export class Editor extends EventEmitter<TLEventMap> {
7086
7108
 
7087
7109
  const gap = _gap ?? this.options.adjacentShapeMargin
7088
7110
 
7089
- const ids =
7090
- typeof shapes[0] === 'string'
7091
- ? (shapes as TLShapeId[])
7092
- : (shapes as TLShape[]).map((s) => s.id)
7093
-
7094
- // Always fresh shapes
7095
- const shapesToPackFirstPass = compact(ids.map((id) => this.getShape(id)))
7096
-
7097
- const shapeClustersToPack: {
7098
- shapes: TLShape[]
7099
- pageBounds: Box
7100
- nextPageBounds: Box
7101
- }[] = []
7111
+ const { clusters, allBounds } = this.getShapeClusters(shapes, 'pack')
7102
7112
 
7103
- const allBounds: Box[] = []
7104
- const visited = new Set<TLShapeId>()
7105
-
7106
- for (const shape of shapesToPackFirstPass) {
7107
- if (visited.has(shape.id)) continue
7108
- visited.add(shape.id)
7109
-
7110
- const shapePageBounds = this.getShapePageBounds(shape)
7111
- if (!shapePageBounds) continue
7112
-
7113
- if (
7114
- !this.getShapeUtil(shape).canBeLaidOut?.(shape, {
7115
- type: 'pack',
7116
- shapes: shapesToPackFirstPass,
7117
- })
7118
- ) {
7119
- continue
7120
- }
7121
-
7122
- const shapesMovingTogether = [shape]
7123
- const boundsOfShapesMovingTogether: Box[] = [shapePageBounds]
7124
-
7125
- this.collectShapesViaArrowBindings({
7126
- bindings: this.getBindingsToShape(shape.id, 'arrow'),
7127
- initialShapes: shapesToPackFirstPass,
7128
- resultShapes: shapesMovingTogether,
7129
- resultBounds: boundsOfShapesMovingTogether,
7130
- visited,
7131
- })
7132
-
7133
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether)
7134
- if (!commonPageBounds) continue
7135
-
7136
- shapeClustersToPack.push({
7137
- shapes: shapesMovingTogether,
7138
- pageBounds: commonPageBounds,
7139
- nextPageBounds: commonPageBounds.clone(),
7140
- })
7141
-
7142
- allBounds.push(commonPageBounds)
7143
- }
7113
+ const shapeClustersToPack = clusters.map((cluster) => ({
7114
+ ...cluster,
7115
+ nextPageBounds: cluster.pageBounds.clone(),
7116
+ }))
7144
7117
 
7145
7118
  if (shapeClustersToPack.length < 2) return this
7146
7119
 
@@ -7262,63 +7235,7 @@ export class Editor extends EventEmitter<TLEventMap> {
7262
7235
  ): this {
7263
7236
  if (this.getIsReadonly()) return this
7264
7237
 
7265
- const ids =
7266
- typeof shapes[0] === 'string'
7267
- ? (shapes as TLShapeId[])
7268
- : (shapes as TLShape[]).map((s) => s.id)
7269
-
7270
- // Always get fresh shapes
7271
- const shapesToAlignFirstPass = compact(ids.map((id) => this.getShape(id)))
7272
-
7273
- const shapeClustersToAlign: {
7274
- shapes: TLShape[]
7275
- pageBounds: Box
7276
- }[] = []
7277
- const allBounds: Box[] = []
7278
- const visited = new Set<TLShapeId>()
7279
-
7280
- for (const shape of shapesToAlignFirstPass) {
7281
- if (visited.has(shape.id)) continue
7282
- visited.add(shape.id)
7283
-
7284
- const shapePageBounds = this.getShapePageBounds(shape)
7285
- if (!shapePageBounds) continue
7286
-
7287
- if (
7288
- !this.getShapeUtil(shape).canBeLaidOut?.(shape, {
7289
- type: 'align',
7290
- shapes: shapesToAlignFirstPass,
7291
- })
7292
- ) {
7293
- continue
7294
- }
7295
-
7296
- // In this implementation, we want to create psuedo-groups out of shapes that
7297
- // are moving together. At the moment shapes only move together if they're connected
7298
- // by arrows. So let's say A -> B -> C -> D and A, B, and C are selected. If we're
7299
- // aligning A, B, and C, then we want these to move together as one unit.
7300
-
7301
- const shapesMovingTogether = [shape]
7302
- const boundsOfShapesMovingTogether: Box[] = [shapePageBounds]
7303
-
7304
- this.collectShapesViaArrowBindings({
7305
- bindings: this.getBindingsToShape(shape.id, 'arrow'),
7306
- initialShapes: shapesToAlignFirstPass,
7307
- resultShapes: shapesMovingTogether,
7308
- resultBounds: boundsOfShapesMovingTogether,
7309
- visited,
7310
- })
7311
-
7312
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether)
7313
- if (!commonPageBounds) continue
7314
-
7315
- shapeClustersToAlign.push({
7316
- shapes: shapesMovingTogether,
7317
- pageBounds: commonPageBounds,
7318
- })
7319
-
7320
- allBounds.push(commonPageBounds)
7321
- }
7238
+ const { clusters: shapeClustersToAlign, allBounds } = this.getShapeClusters(shapes, 'align')
7322
7239
 
7323
7240
  if (shapeClustersToAlign.length < 2) return this
7324
7241
 
@@ -7393,59 +7310,7 @@ export class Editor extends EventEmitter<TLEventMap> {
7393
7310
  distributeShapes(shapes: TLShapeId[] | TLShape[], operation: 'horizontal' | 'vertical'): this {
7394
7311
  if (this.getIsReadonly()) return this
7395
7312
 
7396
- const ids =
7397
- typeof shapes[0] === 'string'
7398
- ? (shapes as TLShapeId[])
7399
- : (shapes as TLShape[]).map((s) => s.id)
7400
-
7401
- // always fresh shapes
7402
- const shapesToDistributeFirstPass = compact(ids.map((id) => this.getShape(id)))
7403
-
7404
- const shapeClustersToDistribute: {
7405
- shapes: TLShape[]
7406
- pageBounds: Box
7407
- }[] = []
7408
-
7409
- const allBounds: Box[] = []
7410
- const visited = new Set<TLShapeId>()
7411
-
7412
- for (const shape of shapesToDistributeFirstPass) {
7413
- if (visited.has(shape.id)) continue
7414
- visited.add(shape.id)
7415
-
7416
- const shapePageBounds = this.getShapePageBounds(shape)
7417
- if (!shapePageBounds) continue
7418
-
7419
- if (
7420
- !this.getShapeUtil(shape).canBeLaidOut?.(shape, {
7421
- type: 'distribute',
7422
- shapes: shapesToDistributeFirstPass,
7423
- })
7424
- ) {
7425
- continue
7426
- }
7427
-
7428
- const shapesMovingTogether = [shape]
7429
- const boundsOfShapesMovingTogether: Box[] = [shapePageBounds]
7430
-
7431
- this.collectShapesViaArrowBindings({
7432
- bindings: this.getBindingsToShape(shape.id, 'arrow'),
7433
- initialShapes: shapesToDistributeFirstPass,
7434
- resultShapes: shapesMovingTogether,
7435
- resultBounds: boundsOfShapesMovingTogether,
7436
- visited,
7437
- })
7438
-
7439
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether)
7440
- if (!commonPageBounds) continue
7441
-
7442
- shapeClustersToDistribute.push({
7443
- shapes: shapesMovingTogether,
7444
- pageBounds: commonPageBounds,
7445
- })
7446
-
7447
- allBounds.push(commonPageBounds)
7448
- }
7313
+ const { clusters: shapeClustersToDistribute } = this.getShapeClusters(shapes, 'distribute')
7449
7314
 
7450
7315
  if (shapeClustersToDistribute.length < 3) return this
7451
7316
 
@@ -7473,6 +7338,10 @@ export class Editor extends EventEmitter<TLEventMap> {
7473
7338
  // If the first shape group is also the last shape group, distribute without it
7474
7339
  if (first === last) {
7475
7340
  const excludedShapeIds = new Set(first.shapes.map((s) => s.id))
7341
+ const ids =
7342
+ typeof shapes[0] === 'string'
7343
+ ? (shapes as TLShapeId[])
7344
+ : (shapes as TLShape[]).map((s) => s.id)
7476
7345
  return this.distributeShapes(
7477
7346
  ids.filter((id) => !excludedShapeIds.has(id)),
7478
7347
  operation
@@ -7542,63 +7411,14 @@ export class Editor extends EventEmitter<TLEventMap> {
7542
7411
  * @public
7543
7412
  */
7544
7413
  stretchShapes(shapes: TLShapeId[] | TLShape[], operation: 'horizontal' | 'vertical'): this {
7545
- const ids =
7546
- typeof shapes[0] === 'string'
7547
- ? (shapes as TLShapeId[])
7548
- : (shapes as TLShape[]).map((s) => s.id)
7549
-
7550
7414
  if (this.getIsReadonly()) return this
7551
7415
 
7552
- // always fresh shapes, skip anything that isn't rotated 90 deg
7553
- const shapesToStretchFirstPass = compact(ids.map((id) => this.getShape(id))).filter(
7554
- (s) => this.getShapePageTransform(s)?.rotation() % (PI / 2) === 0
7416
+ const { clusters: shapeClustersToStretch, allBounds } = this.getShapeClusters(
7417
+ shapes,
7418
+ 'stretch',
7419
+ { filterAxisAligned: true }
7555
7420
  )
7556
7421
 
7557
- const shapeClustersToStretch: {
7558
- shapes: TLShape[]
7559
- pageBounds: Box
7560
- }[] = []
7561
-
7562
- const allBounds: Box[] = []
7563
- const visited = new Set<TLShapeId>()
7564
-
7565
- for (const shape of shapesToStretchFirstPass) {
7566
- if (visited.has(shape.id)) continue
7567
- visited.add(shape.id)
7568
-
7569
- const shapePageBounds = this.getShapePageBounds(shape)
7570
- if (!shapePageBounds) continue
7571
-
7572
- const shapesMovingTogether = [shape]
7573
- const boundsOfShapesMovingTogether: Box[] = [shapePageBounds]
7574
-
7575
- if (
7576
- !this.getShapeUtil(shape).canBeLaidOut?.(shape, {
7577
- type: 'stretch',
7578
- })
7579
- ) {
7580
- continue
7581
- }
7582
-
7583
- this.collectShapesViaArrowBindings({
7584
- bindings: this.getBindingsToShape(shape.id, 'arrow'),
7585
- initialShapes: shapesToStretchFirstPass,
7586
- resultShapes: shapesMovingTogether,
7587
- resultBounds: boundsOfShapesMovingTogether,
7588
- visited,
7589
- })
7590
-
7591
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether)
7592
- if (!commonPageBounds) continue
7593
-
7594
- shapeClustersToStretch.push({
7595
- shapes: shapesMovingTogether,
7596
- pageBounds: commonPageBounds,
7597
- })
7598
-
7599
- allBounds.push(commonPageBounds)
7600
- }
7601
-
7602
7422
  if (shapeClustersToStretch.length < 2) return this
7603
7423
 
7604
7424
  const commonBounds = Box.Common(allBounds)
@@ -7631,7 +7451,7 @@ export class Editor extends EventEmitter<TLEventMap> {
7631
7451
  // First translate
7632
7452
  const shapeLocalOffset = localOffset.clone()
7633
7453
  const parentTransform = this.getShapeParentTransform(shape)
7634
- if (parentTransform) localOffset.rot(-parentTransform.rotation())
7454
+ if (parentTransform) shapeLocalOffset.rot(-parentTransform.rotation())
7635
7455
  shapeLocalOffset.add(shape)
7636
7456
  const changes = this.getChangesToTranslateShape(shape, shapeLocalOffset)
7637
7457
  this.updateShape(changes)
@@ -7650,6 +7470,77 @@ export class Editor extends EventEmitter<TLEventMap> {
7650
7470
  return this
7651
7471
  }
7652
7472
 
7473
+ /**
7474
+ * Resize and reposition a set of shapes so that their combined page bounds matches the given
7475
+ * target bounds.
7476
+ *
7477
+ * @example
7478
+ * ```ts
7479
+ * editor.resizeToBounds([box1, box2], { x: 0, y: 0, w: 500, h: 500 })
7480
+ * editor.resizeToBounds(editor.getSelectedShapeIds(), new Box(0, 0, 500, 500))
7481
+ * ```
7482
+ *
7483
+ * @param shapes - The shapes (or shape ids) to resize.
7484
+ * @param bounds - The target bounding box.
7485
+ *
7486
+ * @public
7487
+ */
7488
+ resizeToBounds(shapes: TLShapeId[] | TLShape[], bounds: BoxLike): this {
7489
+ if (this.getIsReadonly()) return this
7490
+
7491
+ const targetBounds = Box.From(bounds)
7492
+
7493
+ const { clusters: shapeClusters, allBounds } = this.getShapeClusters(
7494
+ shapes,
7495
+ 'resize_to_bounds',
7496
+ { filterAxisAligned: true }
7497
+ )
7498
+
7499
+ if (shapeClusters.length === 0) return this
7500
+
7501
+ const commonBounds = Box.Common(allBounds)
7502
+ if (!commonBounds) return this
7503
+ if (commonBounds.width === 0 || commonBounds.height === 0) return this
7504
+
7505
+ const scaleX = targetBounds.width / commonBounds.width
7506
+ const scaleY = targetBounds.height / commonBounds.height
7507
+ const scale = new Vec(scaleX, scaleY)
7508
+
7509
+ shapeClusters.forEach(({ shapes, pageBounds }) => {
7510
+ const localOffset = new Vec(
7511
+ targetBounds.minX -
7512
+ commonBounds.minX +
7513
+ (pageBounds.minX - commonBounds.minX) * (scaleX - 1),
7514
+ targetBounds.minY - commonBounds.minY + (pageBounds.minY - commonBounds.minY) * (scaleY - 1)
7515
+ )
7516
+
7517
+ const scaleOrigin = new Vec(
7518
+ targetBounds.minX + (pageBounds.minX - commonBounds.minX) * scaleX,
7519
+ targetBounds.minY + (pageBounds.minY - commonBounds.minY) * scaleY
7520
+ )
7521
+
7522
+ for (const shape of shapes) {
7523
+ // First translate
7524
+ const shapeLocalOffset = localOffset.clone()
7525
+ const parentTransform = this.getShapeParentTransform(shape)
7526
+ if (parentTransform) shapeLocalOffset.rot(-parentTransform.rotation())
7527
+ shapeLocalOffset.add(shape)
7528
+ const changes = this.getChangesToTranslateShape(shape, shapeLocalOffset)
7529
+ this.updateShape(changes)
7530
+
7531
+ // Then resize
7532
+ this.resizeShape(shape.id, scale, {
7533
+ initialBounds: this.getShapeGeometry(shape).bounds,
7534
+ scaleOrigin,
7535
+ isAspectRatioLocked: this.getShapeUtil(shape).isAspectRatioLocked(shape),
7536
+ scaleAxisRotation: 0,
7537
+ })
7538
+ }
7539
+ })
7540
+
7541
+ return this
7542
+ }
7543
+
7653
7544
  /**
7654
7545
  * Resize a shape.
7655
7546
  *
@@ -11,11 +11,12 @@ import {
11
11
  import { exhaustiveSwitchError, noop } from '@tldraw/utils'
12
12
  import { TLHistoryBatchOptions, TLHistoryEntry } from '../../types/history-types'
13
13
 
14
- enum HistoryRecorderState {
15
- Recording = 'recording',
16
- RecordingPreserveRedoStack = 'recordingPreserveRedoStack',
17
- Paused = 'paused',
18
- }
14
+ const HistoryRecorderState = {
15
+ Recording: 'recording',
16
+ RecordingPreserveRedoStack: 'recordingPreserveRedoStack',
17
+ Paused: 'paused',
18
+ } as const
19
+ type HistoryRecorderState = (typeof HistoryRecorderState)[keyof typeof HistoryRecorderState]
19
20
 
20
21
  /** @public */
21
22
  export class HistoryManager<R extends UnknownRecord> {
@@ -69,7 +69,7 @@ export interface TLShapeUtilCanBindOpts<Shape extends TLShape = TLShape> {
69
69
  */
70
70
  export interface TLShapeUtilCanBeLaidOutOpts {
71
71
  /** The type of action causing the layout. */
72
- type?: 'align' | 'distribute' | 'pack' | 'stack' | 'flip' | 'stretch'
72
+ type?: 'align' | 'distribute' | 'pack' | 'stack' | 'flip' | 'stretch' | 'resize_to_bounds'
73
73
  /** The other shapes being laid out */
74
74
  shapes?: TLShape[]
75
75
  }
@@ -2,7 +2,12 @@ import { useValue } from '@tldraw/state-react'
2
2
  import React, { useEffect, useMemo } from 'react'
3
3
  import { RIGHT_MOUSE_BUTTON } from '../constants'
4
4
  import { tlenv } from '../globals/environment'
5
- import { preventDefault, releasePointerCapture, setPointerCapture } from '../utils/dom'
5
+ import {
6
+ elementShouldCaptureKeys,
7
+ preventDefault,
8
+ releasePointerCapture,
9
+ setPointerCapture,
10
+ } from '../utils/dom'
6
11
  import { getPointerInfo } from '../utils/getPointerInfo'
7
12
  import { useEditor } from './useEditor'
8
13
 
@@ -78,15 +83,14 @@ export function useCanvasEvents() {
78
83
  // check that e.target is an HTMLElement
79
84
  if (!(e.target instanceof HTMLElement)) return
80
85
 
81
- const editingShapeId = editor.getEditingShape()?.id
86
+ const editingShapeId = editor.getEditingShapeId()
82
87
  if (
83
88
  // if the target is not inside the editing shape
84
89
  !(editingShapeId && e.target.closest(`[data-shape-id="${editingShapeId}"]`)) &&
85
90
  // and the target is not an clickable element
86
91
  e.target.tagName !== 'A' &&
87
- // or a TextArea.tsx ?
88
- e.target.tagName !== 'TEXTAREA' &&
89
- !e.target.isContentEditable
92
+ // and the target is not an editable element
93
+ !elementShouldCaptureKeys(e.target, false)
90
94
  ) {
91
95
  preventDefault(e)
92
96
  }
@@ -1,9 +1,7 @@
1
1
  import { useEffect } from 'react'
2
- import { preventDefault } from '../utils/dom'
2
+ import { elementShouldCaptureKeys, preventDefault } from '../utils/dom'
3
3
  import { useEditor } from './useEditor'
4
4
 
5
- const IGNORED_TAGS = ['textarea', 'input']
6
-
7
5
  /**
8
6
  * When double tapping with the pencil in iOS, it enables a little zoom window in the UI. We don't
9
7
  * want this for drawing operations and can disable it by setting 'disableDoubleTapZoom' in the main
@@ -24,8 +22,7 @@ export function useFixSafariDoubleTapZoomPencilEvents(ref: React.RefObject<HTMLE
24
22
 
25
23
  // Allow events to propagate if the app is editing a shape, or if the event is occurring in a text area or input
26
24
  if (
27
- IGNORED_TAGS.includes((target as Element).tagName?.toLocaleLowerCase()) ||
28
- (target as HTMLElement).isContentEditable ||
25
+ elementShouldCaptureKeys(target instanceof Element ? target : null, false) ||
29
26
  editor.isIn('select.editing_shape')
30
27
  ) {
31
28
  return
@@ -98,13 +98,21 @@ export const setStyleProperty = (
98
98
  }
99
99
 
100
100
  /** @internal */
101
- export function activeElementShouldCaptureKeys(ignoreButtons = false) {
102
- const { activeElement } = document
103
- const elements = ignoreButtons ? ['input', 'textarea'] : ['input', 'select', 'button', 'textarea']
104
- return !!(
105
- activeElement &&
106
- ((activeElement as HTMLElement).isContentEditable ||
107
- elements.indexOf(activeElement.tagName.toLowerCase()) > -1 ||
108
- activeElement.classList.contains('tlui-slider__thumb'))
101
+ export function elementShouldCaptureKeys(el: Element | null, includeButtonsAndMenus = true) {
102
+ if (!el) return false
103
+
104
+ const tagName = el.tagName.toLowerCase()
105
+ return (
106
+ (el as HTMLElement).isContentEditable ||
107
+ tagName === 'input' ||
108
+ tagName === 'textarea' ||
109
+ (includeButtonsAndMenus && tagName === 'select') ||
110
+ (includeButtonsAndMenus && tagName === 'button') ||
111
+ el.classList.contains('tlui-slider__thumb')
109
112
  )
110
113
  }
114
+
115
+ /** @internal */
116
+ export function activeElementShouldCaptureKeys(includeButtonsAndMenus = true) {
117
+ return elementShouldCaptureKeys(document.activeElement, includeButtonsAndMenus)
118
+ }