@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
@@ -5021,6 +5021,53 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5021
5021
  if (changes) this.updateShapes(changes);
5022
5022
  return this;
5023
5023
  }
5024
+ /**
5025
+ * Shared clustering logic for layout methods. Resolves shapes, optionally filters to
5026
+ * axis-aligned shapes, checks canBeLaidOut, and groups shapes into clusters via arrow bindings.
5027
+ *
5028
+ * @internal
5029
+ */
5030
+ getShapeClusters(shapes, type, opts) {
5031
+ const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5032
+ let freshShapes = compact(ids.map((id) => this.getShape(id)));
5033
+ if (opts?.filterAxisAligned) {
5034
+ freshShapes = freshShapes.filter(
5035
+ (s) => this.getShapePageTransform(s)?.rotation() % (PI / 2) === 0
5036
+ );
5037
+ }
5038
+ const clusters = [];
5039
+ const allBounds = [];
5040
+ const visited = /* @__PURE__ */ new Set();
5041
+ for (const shape of freshShapes) {
5042
+ if (visited.has(shape.id)) continue;
5043
+ visited.add(shape.id);
5044
+ const shapePageBounds = this.getShapePageBounds(shape);
5045
+ if (!shapePageBounds) continue;
5046
+ if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5047
+ type,
5048
+ shapes: freshShapes
5049
+ })) {
5050
+ continue;
5051
+ }
5052
+ const shapesMovingTogether = [shape];
5053
+ const boundsOfShapesMovingTogether = [shapePageBounds];
5054
+ this.collectShapesViaArrowBindings({
5055
+ bindings: this.getBindingsToShape(shape.id, "arrow"),
5056
+ initialShapes: freshShapes,
5057
+ resultShapes: shapesMovingTogether,
5058
+ resultBounds: boundsOfShapesMovingTogether,
5059
+ visited
5060
+ });
5061
+ const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5062
+ if (!commonPageBounds) continue;
5063
+ clusters.push({
5064
+ shapes: shapesMovingTogether,
5065
+ pageBounds: commonPageBounds
5066
+ });
5067
+ allBounds.push(commonPageBounds);
5068
+ }
5069
+ return { clusters, allBounds };
5070
+ }
5024
5071
  /**
5025
5072
  * @internal
5026
5073
  */
@@ -5131,40 +5178,8 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5131
5178
  */
5132
5179
  stackShapes(shapes, operation, gap) {
5133
5180
  const _gap = gap ?? this.options.adjacentShapeMargin;
5134
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5135
5181
  if (this.getIsReadonly()) return this;
5136
- const shapesToStackFirstPass = compact(ids.map((id) => this.getShape(id)));
5137
- const shapeClustersToStack = [];
5138
- const allBounds = [];
5139
- const visited = /* @__PURE__ */ new Set();
5140
- for (const shape of shapesToStackFirstPass) {
5141
- if (visited.has(shape.id)) continue;
5142
- visited.add(shape.id);
5143
- const shapePageBounds = this.getShapePageBounds(shape);
5144
- if (!shapePageBounds) continue;
5145
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5146
- type: "stack",
5147
- shapes: shapesToStackFirstPass
5148
- })) {
5149
- continue;
5150
- }
5151
- const shapesMovingTogether = [shape];
5152
- const boundsOfShapesMovingTogether = [shapePageBounds];
5153
- this.collectShapesViaArrowBindings({
5154
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5155
- initialShapes: shapesToStackFirstPass,
5156
- resultShapes: shapesMovingTogether,
5157
- resultBounds: boundsOfShapesMovingTogether,
5158
- visited
5159
- });
5160
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5161
- if (!commonPageBounds) continue;
5162
- shapeClustersToStack.push({
5163
- shapes: shapesMovingTogether,
5164
- pageBounds: commonPageBounds
5165
- });
5166
- allBounds.push(commonPageBounds);
5167
- }
5182
+ const { clusters: shapeClustersToStack } = this.getShapeClusters(shapes, "stack");
5168
5183
  const len = shapeClustersToStack.length;
5169
5184
  if (_gap === 0 && len < 3 || len < 2) return this;
5170
5185
  let val;
@@ -5250,40 +5265,11 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5250
5265
  packShapes(shapes, _gap) {
5251
5266
  if (this.getIsReadonly()) return this;
5252
5267
  const gap = _gap ?? this.options.adjacentShapeMargin;
5253
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5254
- const shapesToPackFirstPass = compact(ids.map((id) => this.getShape(id)));
5255
- const shapeClustersToPack = [];
5256
- const allBounds = [];
5257
- const visited = /* @__PURE__ */ new Set();
5258
- for (const shape of shapesToPackFirstPass) {
5259
- if (visited.has(shape.id)) continue;
5260
- visited.add(shape.id);
5261
- const shapePageBounds = this.getShapePageBounds(shape);
5262
- if (!shapePageBounds) continue;
5263
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5264
- type: "pack",
5265
- shapes: shapesToPackFirstPass
5266
- })) {
5267
- continue;
5268
- }
5269
- const shapesMovingTogether = [shape];
5270
- const boundsOfShapesMovingTogether = [shapePageBounds];
5271
- this.collectShapesViaArrowBindings({
5272
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5273
- initialShapes: shapesToPackFirstPass,
5274
- resultShapes: shapesMovingTogether,
5275
- resultBounds: boundsOfShapesMovingTogether,
5276
- visited
5277
- });
5278
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5279
- if (!commonPageBounds) continue;
5280
- shapeClustersToPack.push({
5281
- shapes: shapesMovingTogether,
5282
- pageBounds: commonPageBounds,
5283
- nextPageBounds: commonPageBounds.clone()
5284
- });
5285
- allBounds.push(commonPageBounds);
5286
- }
5268
+ const { clusters, allBounds } = this.getShapeClusters(shapes, "pack");
5269
+ const shapeClustersToPack = clusters.map((cluster) => ({
5270
+ ...cluster,
5271
+ nextPageBounds: cluster.pageBounds.clone()
5272
+ }));
5287
5273
  if (shapeClustersToPack.length < 2) return this;
5288
5274
  let area = 0;
5289
5275
  for (const { pageBounds } of shapeClustersToPack) {
@@ -5367,39 +5353,7 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5367
5353
  */
5368
5354
  alignShapes(shapes, operation) {
5369
5355
  if (this.getIsReadonly()) return this;
5370
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5371
- const shapesToAlignFirstPass = compact(ids.map((id) => this.getShape(id)));
5372
- const shapeClustersToAlign = [];
5373
- const allBounds = [];
5374
- const visited = /* @__PURE__ */ new Set();
5375
- for (const shape of shapesToAlignFirstPass) {
5376
- if (visited.has(shape.id)) continue;
5377
- visited.add(shape.id);
5378
- const shapePageBounds = this.getShapePageBounds(shape);
5379
- if (!shapePageBounds) continue;
5380
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5381
- type: "align",
5382
- shapes: shapesToAlignFirstPass
5383
- })) {
5384
- continue;
5385
- }
5386
- const shapesMovingTogether = [shape];
5387
- const boundsOfShapesMovingTogether = [shapePageBounds];
5388
- this.collectShapesViaArrowBindings({
5389
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5390
- initialShapes: shapesToAlignFirstPass,
5391
- resultShapes: shapesMovingTogether,
5392
- resultBounds: boundsOfShapesMovingTogether,
5393
- visited
5394
- });
5395
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5396
- if (!commonPageBounds) continue;
5397
- shapeClustersToAlign.push({
5398
- shapes: shapesMovingTogether,
5399
- pageBounds: commonPageBounds
5400
- });
5401
- allBounds.push(commonPageBounds);
5402
- }
5356
+ const { clusters: shapeClustersToAlign, allBounds } = this.getShapeClusters(shapes, "align");
5403
5357
  if (shapeClustersToAlign.length < 2) return this;
5404
5358
  const commonBounds = Box.Common(allBounds);
5405
5359
  const changes = [];
@@ -5461,39 +5415,7 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5461
5415
  */
5462
5416
  distributeShapes(shapes, operation) {
5463
5417
  if (this.getIsReadonly()) return this;
5464
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5465
- const shapesToDistributeFirstPass = compact(ids.map((id) => this.getShape(id)));
5466
- const shapeClustersToDistribute = [];
5467
- const allBounds = [];
5468
- const visited = /* @__PURE__ */ new Set();
5469
- for (const shape of shapesToDistributeFirstPass) {
5470
- if (visited.has(shape.id)) continue;
5471
- visited.add(shape.id);
5472
- const shapePageBounds = this.getShapePageBounds(shape);
5473
- if (!shapePageBounds) continue;
5474
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5475
- type: "distribute",
5476
- shapes: shapesToDistributeFirstPass
5477
- })) {
5478
- continue;
5479
- }
5480
- const shapesMovingTogether = [shape];
5481
- const boundsOfShapesMovingTogether = [shapePageBounds];
5482
- this.collectShapesViaArrowBindings({
5483
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5484
- initialShapes: shapesToDistributeFirstPass,
5485
- resultShapes: shapesMovingTogether,
5486
- resultBounds: boundsOfShapesMovingTogether,
5487
- visited
5488
- });
5489
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5490
- if (!commonPageBounds) continue;
5491
- shapeClustersToDistribute.push({
5492
- shapes: shapesMovingTogether,
5493
- pageBounds: commonPageBounds
5494
- });
5495
- allBounds.push(commonPageBounds);
5496
- }
5418
+ const { clusters: shapeClustersToDistribute } = this.getShapeClusters(shapes, "distribute");
5497
5419
  if (shapeClustersToDistribute.length < 3) return this;
5498
5420
  let val;
5499
5421
  let min;
@@ -5515,6 +5437,7 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5515
5437
  const last2 = shapeClustersToDistribute.sort((a, b) => b.pageBounds[max] - a.pageBounds[max])[0];
5516
5438
  if (first === last2) {
5517
5439
  const excludedShapeIds = new Set(first.shapes.map((s) => s.id));
5440
+ const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5518
5441
  return this.distributeShapes(
5519
5442
  ids.filter((id) => !excludedShapeIds.has(id)),
5520
5443
  operation
@@ -5567,41 +5490,12 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5567
5490
  * @public
5568
5491
  */
5569
5492
  stretchShapes(shapes, operation) {
5570
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5571
5493
  if (this.getIsReadonly()) return this;
5572
- const shapesToStretchFirstPass = compact(ids.map((id) => this.getShape(id))).filter(
5573
- (s) => this.getShapePageTransform(s)?.rotation() % (PI / 2) === 0
5494
+ const { clusters: shapeClustersToStretch, allBounds } = this.getShapeClusters(
5495
+ shapes,
5496
+ "stretch",
5497
+ { filterAxisAligned: true }
5574
5498
  );
5575
- const shapeClustersToStretch = [];
5576
- const allBounds = [];
5577
- const visited = /* @__PURE__ */ new Set();
5578
- for (const shape of shapesToStretchFirstPass) {
5579
- if (visited.has(shape.id)) continue;
5580
- visited.add(shape.id);
5581
- const shapePageBounds = this.getShapePageBounds(shape);
5582
- if (!shapePageBounds) continue;
5583
- const shapesMovingTogether = [shape];
5584
- const boundsOfShapesMovingTogether = [shapePageBounds];
5585
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5586
- type: "stretch"
5587
- })) {
5588
- continue;
5589
- }
5590
- this.collectShapesViaArrowBindings({
5591
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5592
- initialShapes: shapesToStretchFirstPass,
5593
- resultShapes: shapesMovingTogether,
5594
- resultBounds: boundsOfShapesMovingTogether,
5595
- visited
5596
- });
5597
- const commonPageBounds = Box.Common(boundsOfShapesMovingTogether);
5598
- if (!commonPageBounds) continue;
5599
- shapeClustersToStretch.push({
5600
- shapes: shapesMovingTogether,
5601
- pageBounds: commonPageBounds
5602
- });
5603
- allBounds.push(commonPageBounds);
5604
- }
5605
5499
  if (shapeClustersToStretch.length < 2) return this;
5606
5500
  const commonBounds = Box.Common(allBounds);
5607
5501
  let val;
@@ -5627,7 +5521,7 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5627
5521
  for (const shape of shapes2) {
5628
5522
  const shapeLocalOffset = localOffset.clone();
5629
5523
  const parentTransform = this.getShapeParentTransform(shape);
5630
- if (parentTransform) localOffset.rot(-parentTransform.rotation());
5524
+ if (parentTransform) shapeLocalOffset.rot(-parentTransform.rotation());
5631
5525
  shapeLocalOffset.add(shape);
5632
5526
  const changes = this.getChangesToTranslateShape(shape, shapeLocalOffset);
5633
5527
  this.updateShape(changes);
@@ -5642,6 +5536,62 @@ class Editor extends (_a = EventEmitter, _getIsShapeHiddenCache_dec = [computed]
5642
5536
  });
5643
5537
  return this;
5644
5538
  }
5539
+ /**
5540
+ * Resize and reposition a set of shapes so that their combined page bounds matches the given
5541
+ * target bounds.
5542
+ *
5543
+ * @example
5544
+ * ```ts
5545
+ * editor.resizeToBounds([box1, box2], { x: 0, y: 0, w: 500, h: 500 })
5546
+ * editor.resizeToBounds(editor.getSelectedShapeIds(), new Box(0, 0, 500, 500))
5547
+ * ```
5548
+ *
5549
+ * @param shapes - The shapes (or shape ids) to resize.
5550
+ * @param bounds - The target bounding box.
5551
+ *
5552
+ * @public
5553
+ */
5554
+ resizeToBounds(shapes, bounds) {
5555
+ if (this.getIsReadonly()) return this;
5556
+ const targetBounds = Box.From(bounds);
5557
+ const { clusters: shapeClusters, allBounds } = this.getShapeClusters(
5558
+ shapes,
5559
+ "resize_to_bounds",
5560
+ { filterAxisAligned: true }
5561
+ );
5562
+ if (shapeClusters.length === 0) return this;
5563
+ const commonBounds = Box.Common(allBounds);
5564
+ if (!commonBounds) return this;
5565
+ if (commonBounds.width === 0 || commonBounds.height === 0) return this;
5566
+ const scaleX = targetBounds.width / commonBounds.width;
5567
+ const scaleY = targetBounds.height / commonBounds.height;
5568
+ const scale = new Vec(scaleX, scaleY);
5569
+ shapeClusters.forEach(({ shapes: shapes2, pageBounds }) => {
5570
+ const localOffset = new Vec(
5571
+ targetBounds.minX - commonBounds.minX + (pageBounds.minX - commonBounds.minX) * (scaleX - 1),
5572
+ targetBounds.minY - commonBounds.minY + (pageBounds.minY - commonBounds.minY) * (scaleY - 1)
5573
+ );
5574
+ const scaleOrigin = new Vec(
5575
+ targetBounds.minX + (pageBounds.minX - commonBounds.minX) * scaleX,
5576
+ targetBounds.minY + (pageBounds.minY - commonBounds.minY) * scaleY
5577
+ );
5578
+ for (const shape of shapes2) {
5579
+ const shapeLocalOffset = localOffset.clone();
5580
+ const parentTransform = this.getShapeParentTransform(shape);
5581
+ if (parentTransform) shapeLocalOffset.rot(-parentTransform.rotation());
5582
+ shapeLocalOffset.add(shape);
5583
+ const changes = this.getChangesToTranslateShape(shape, shapeLocalOffset);
5584
+ this.updateShape(changes);
5585
+ this.resizeShape(shape.id, scale, {
5586
+ initialBounds: this.getShapeGeometry(shape).bounds,
5587
+ scaleOrigin,
5588
+ isAspectRatioLocked: this.getShapeUtil(shape).isAspectRatioLocked(shape),
5589
+ scaleAxisRotation: 0
5590
+ });
5591
+ }
5592
+ });
5593
+ return this;
5594
+ }
5645
5595
  /**
5646
5596
  * Resize a shape.
5647
5597
  *