@tldraw/editor 4.5.0-next.752e1005c7c7 → 4.5.0-next.c9344cc3688e

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 (51) hide show
  1. package/dist-cjs/index.d.ts +26 -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 +123 -169
  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/editor/tools/BaseBoxShapeTool/children/Pointing.js +2 -3
  12. package/dist-cjs/lib/editor/tools/BaseBoxShapeTool/children/Pointing.js.map +2 -2
  13. package/dist-cjs/lib/hooks/useCanvasEvents.js +3 -3
  14. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  15. package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js +1 -2
  16. package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js.map +2 -2
  17. package/dist-cjs/lib/utils/dom.js +8 -4
  18. package/dist-cjs/lib/utils/dom.js.map +2 -2
  19. package/dist-cjs/version.js +3 -3
  20. package/dist-cjs/version.js.map +1 -1
  21. package/dist-esm/index.d.mts +26 -1
  22. package/dist-esm/index.mjs +3 -1
  23. package/dist-esm/index.mjs.map +2 -2
  24. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +13 -16
  25. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  26. package/dist-esm/lib/editor/Editor.mjs +123 -169
  27. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  28. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs +15 -16
  29. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs.map +3 -3
  30. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +1 -1
  31. package/dist-esm/lib/editor/tools/BaseBoxShapeTool/children/Pointing.mjs +2 -3
  32. package/dist-esm/lib/editor/tools/BaseBoxShapeTool/children/Pointing.mjs.map +2 -2
  33. package/dist-esm/lib/hooks/useCanvasEvents.mjs +9 -4
  34. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  35. package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs +2 -3
  36. package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs.map +2 -2
  37. package/dist-esm/lib/utils/dom.mjs +8 -4
  38. package/dist-esm/lib/utils/dom.mjs.map +2 -2
  39. package/dist-esm/version.mjs +3 -3
  40. package/dist-esm/version.mjs.map +1 -1
  41. package/package.json +7 -7
  42. package/src/index.ts +1 -0
  43. package/src/lib/components/default-components/DefaultCanvas.tsx +19 -26
  44. package/src/lib/editor/Editor.ts +169 -269
  45. package/src/lib/editor/managers/HistoryManager/HistoryManager.ts +6 -5
  46. package/src/lib/editor/shapes/ShapeUtil.ts +1 -1
  47. package/src/lib/editor/tools/BaseBoxShapeTool/children/Pointing.ts +3 -3
  48. package/src/lib/hooks/useCanvasEvents.ts +9 -5
  49. package/src/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.ts +2 -5
  50. package/src/lib/utils/dom.ts +16 -8
  51. package/src/version.ts +3 -3
@@ -124,8 +124,8 @@ var import_TextManager = require("./managers/TextManager/TextManager");
124
124
  var import_TickManager = require("./managers/TickManager/TickManager");
125
125
  var import_UserPreferencesManager = require("./managers/UserPreferencesManager/UserPreferencesManager");
126
126
  var import_RootState = require("./tools/RootState");
127
- var __setMetaKeyTimeout_dec, __setCtrlKeyTimeout_dec, __setAltKeyTimeout_dec, __setShiftKeyTimeout_dec, _getIsReadonly_dec, _getIsFocused_dec, _getSharedOpacity_dec, _getSharedStyles_dec, __getSelectionSharedStyles_dec, __getBindingsIndexCache_dec, _getCurrentPageRenderingShapesSorted_dec, _getCurrentPageShapesSorted_dec, _getCurrentPageShapes_dec, _getCurrentPageBounds_dec, _getCulledShapes_dec, _getNotVisibleShapes_dec, __getShapeMaskedPageBoundsCache_dec, __getShapeMaskCache_dec, __getShapeClipPathCache_dec, __getShapePageBoundsCache_dec, __getShapePageTransformCache_dec, __getShapeHandlesCache_dec, __getAllAssetsQuery_dec, _getCurrentPageShapeIdsSorted_dec, _getCurrentPageId_dec, _getPages_dec, __getAllPagesQuery_dec, _getRenderingShapes_dec, _getCollaboratorsOnCurrentPage_dec, _getCollaborators_dec, __getCollaboratorsQuery_dec, _getViewportPageBounds_dec, _getViewportScreenCenter_dec, _getViewportScreenBounds_dec, _getEfficientZoomLevel_dec, __getAboveDebouncedZoomThreshold_dec, _getDebouncedZoomLevel_dec, _getZoomLevel_dec, _getCameraForFollowing_dec, _getViewportPageBoundsForFollowing_dec, _getCamera_dec, __unsafe_getCameraId_dec, _getErasingShapes_dec, _getErasingShapeIds_dec, _getHintingShape_dec, _getHintingShapeIds_dec, _getHoveredShape_dec, _getHoveredShapeId_dec, _getRichTextEditor_dec, _getEditingShape_dec, _getEditingShapeId_dec, _getFocusedGroup_dec, _getFocusedGroupId_dec, _getSelectionRotatedScreenBounds_dec, _getSelectionRotatedPageBounds_dec, _getSelectionRotation_dec, _getSelectionPageBounds_dec, _getOnlySelectedShape_dec, _getOnlySelectedShapeId_dec, _getCurrentPageShapesInReadingOrder_dec, _getSelectedShapes_dec, _getSelectedShapeIds_dec, __getCurrentPageStateId_dec, _getCurrentPageState_dec, __getPageStatesQuery_dec, _getPageStates_dec, _getInstanceState_dec, _getDocumentSettings_dec, _getCurrentToolId_dec, _getCurrentTool_dec, _getPath_dec, _canRedo_dec, _canUndo_dec, _getIsShapeHiddenCache_dec, _a, _init;
128
- class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_dec = [import_state.computed], _canUndo_dec = [import_state.computed], _canRedo_dec = [import_state.computed], _getPath_dec = [import_state.computed], _getCurrentTool_dec = [import_state.computed], _getCurrentToolId_dec = [import_state.computed], _getDocumentSettings_dec = [import_state.computed], _getInstanceState_dec = [import_state.computed], _getPageStates_dec = [import_state.computed], __getPageStatesQuery_dec = [import_state.computed], _getCurrentPageState_dec = [import_state.computed], __getCurrentPageStateId_dec = [import_state.computed], _getSelectedShapeIds_dec = [import_state.computed], _getSelectedShapes_dec = [import_state.computed], _getCurrentPageShapesInReadingOrder_dec = [import_state.computed], _getOnlySelectedShapeId_dec = [import_state.computed], _getOnlySelectedShape_dec = [import_state.computed], _getSelectionPageBounds_dec = [import_state.computed], _getSelectionRotation_dec = [import_state.computed], _getSelectionRotatedPageBounds_dec = [import_state.computed], _getSelectionRotatedScreenBounds_dec = [import_state.computed], _getFocusedGroupId_dec = [import_state.computed], _getFocusedGroup_dec = [import_state.computed], _getEditingShapeId_dec = [import_state.computed], _getEditingShape_dec = [import_state.computed], _getRichTextEditor_dec = [import_state.computed], _getHoveredShapeId_dec = [import_state.computed], _getHoveredShape_dec = [import_state.computed], _getHintingShapeIds_dec = [import_state.computed], _getHintingShape_dec = [import_state.computed], _getErasingShapeIds_dec = [import_state.computed], _getErasingShapes_dec = [import_state.computed], __unsafe_getCameraId_dec = [import_state.computed], _getCamera_dec = [import_state.computed], _getViewportPageBoundsForFollowing_dec = [import_state.computed], _getCameraForFollowing_dec = [import_state.computed], _getZoomLevel_dec = [import_state.computed], _getDebouncedZoomLevel_dec = [import_state.computed], __getAboveDebouncedZoomThreshold_dec = [import_state.computed], _getEfficientZoomLevel_dec = [import_state.computed], _getViewportScreenBounds_dec = [import_state.computed], _getViewportScreenCenter_dec = [import_state.computed], _getViewportPageBounds_dec = [import_state.computed], __getCollaboratorsQuery_dec = [import_state.computed], _getCollaborators_dec = [import_state.computed], _getCollaboratorsOnCurrentPage_dec = [import_state.computed], _getRenderingShapes_dec = [import_state.computed], __getAllPagesQuery_dec = [import_state.computed], _getPages_dec = [import_state.computed], _getCurrentPageId_dec = [import_state.computed], _getCurrentPageShapeIdsSorted_dec = [import_state.computed], __getAllAssetsQuery_dec = [import_state.computed], __getShapeHandlesCache_dec = [import_state.computed], __getShapePageTransformCache_dec = [import_state.computed], __getShapePageBoundsCache_dec = [import_state.computed], __getShapeClipPathCache_dec = [import_state.computed], __getShapeMaskCache_dec = [import_state.computed], __getShapeMaskedPageBoundsCache_dec = [import_state.computed], _getNotVisibleShapes_dec = [import_state.computed], _getCulledShapes_dec = [import_state.computed], _getCurrentPageBounds_dec = [import_state.computed], _getCurrentPageShapes_dec = [import_state.computed], _getCurrentPageShapesSorted_dec = [import_state.computed], _getCurrentPageRenderingShapesSorted_dec = [import_state.computed], __getBindingsIndexCache_dec = [import_state.computed], __getSelectionSharedStyles_dec = [import_state.computed], _getSharedStyles_dec = [(0, import_state.computed)({ isEqual: (a, b) => a.equals(b) })], _getSharedOpacity_dec = [import_state.computed], _getIsFocused_dec = [import_state.computed], _getIsReadonly_dec = [import_state.computed], __setShiftKeyTimeout_dec = [import_utils.bind], __setAltKeyTimeout_dec = [import_utils.bind], __setCtrlKeyTimeout_dec = [import_utils.bind], __setMetaKeyTimeout_dec = [import_utils.bind], _a) {
127
+ var __setMetaKeyTimeout_dec, __setCtrlKeyTimeout_dec, __setAltKeyTimeout_dec, __setShiftKeyTimeout_dec, _getIsReadonly_dec, _getIsFocused_dec, _getSharedOpacity_dec, _getSharedStyles_dec, __getSelectionSharedStyles_dec, __getBindingsIndexCache_dec, _getCurrentPageRenderingShapesSorted_dec, _getCurrentPageShapesSorted_dec, _getCurrentPageShapes_dec, _getCurrentPageBounds_dec, _getCulledShapes_dec, _getNotVisibleShapes_dec, __getShapeMaskedPageBoundsCache_dec, __getShapeMaskCache_dec, __getShapeClipPathCache_dec, __getShapePageBoundsCache_dec, __getShapePageTransformCache_dec, __getShapeHandlesCache_dec, __getAllAssetsQuery_dec, _getCurrentPageShapeIdsSorted_dec, _getCurrentPageId_dec, _getPages_dec, __getAllPagesQuery_dec, _getRenderingShapes_dec, _getCollaboratorsOnCurrentPage_dec, _getCollaborators_dec, __getCollaboratorsQuery_dec, _getViewportPageBounds_dec, _getViewportScreenCenter_dec, _getViewportScreenBounds_dec, _getEfficientZoomLevel_dec, __getAboveDebouncedZoomThreshold_dec, _getDebouncedZoomLevel_dec, _getResizeScaleFactor_dec, _getZoomLevel_dec, _getCameraForFollowing_dec, _getViewportPageBoundsForFollowing_dec, _getCamera_dec, __unsafe_getCameraId_dec, _getErasingShapes_dec, _getErasingShapeIds_dec, _getHintingShape_dec, _getHintingShapeIds_dec, _getHoveredShape_dec, _getHoveredShapeId_dec, _getRichTextEditor_dec, _getEditingShape_dec, _getEditingShapeId_dec, _getFocusedGroup_dec, _getFocusedGroupId_dec, _getSelectionRotatedScreenBounds_dec, _getSelectionRotatedPageBounds_dec, _getSelectionRotation_dec, _getSelectionPageBounds_dec, _getOnlySelectedShape_dec, _getOnlySelectedShapeId_dec, _getCurrentPageShapesInReadingOrder_dec, _getSelectedShapes_dec, _getSelectedShapeIds_dec, __getCurrentPageStateId_dec, _getCurrentPageState_dec, __getPageStatesQuery_dec, _getPageStates_dec, _getInstanceState_dec, _getDocumentSettings_dec, _getCurrentToolId_dec, _getCurrentTool_dec, _getPath_dec, _canRedo_dec, _canUndo_dec, _getIsShapeHiddenCache_dec, _a, _init;
128
+ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_dec = [import_state.computed], _canUndo_dec = [import_state.computed], _canRedo_dec = [import_state.computed], _getPath_dec = [import_state.computed], _getCurrentTool_dec = [import_state.computed], _getCurrentToolId_dec = [import_state.computed], _getDocumentSettings_dec = [import_state.computed], _getInstanceState_dec = [import_state.computed], _getPageStates_dec = [import_state.computed], __getPageStatesQuery_dec = [import_state.computed], _getCurrentPageState_dec = [import_state.computed], __getCurrentPageStateId_dec = [import_state.computed], _getSelectedShapeIds_dec = [import_state.computed], _getSelectedShapes_dec = [import_state.computed], _getCurrentPageShapesInReadingOrder_dec = [import_state.computed], _getOnlySelectedShapeId_dec = [import_state.computed], _getOnlySelectedShape_dec = [import_state.computed], _getSelectionPageBounds_dec = [import_state.computed], _getSelectionRotation_dec = [import_state.computed], _getSelectionRotatedPageBounds_dec = [import_state.computed], _getSelectionRotatedScreenBounds_dec = [import_state.computed], _getFocusedGroupId_dec = [import_state.computed], _getFocusedGroup_dec = [import_state.computed], _getEditingShapeId_dec = [import_state.computed], _getEditingShape_dec = [import_state.computed], _getRichTextEditor_dec = [import_state.computed], _getHoveredShapeId_dec = [import_state.computed], _getHoveredShape_dec = [import_state.computed], _getHintingShapeIds_dec = [import_state.computed], _getHintingShape_dec = [import_state.computed], _getErasingShapeIds_dec = [import_state.computed], _getErasingShapes_dec = [import_state.computed], __unsafe_getCameraId_dec = [import_state.computed], _getCamera_dec = [import_state.computed], _getViewportPageBoundsForFollowing_dec = [import_state.computed], _getCameraForFollowing_dec = [import_state.computed], _getZoomLevel_dec = [import_state.computed], _getResizeScaleFactor_dec = [import_state.computed], _getDebouncedZoomLevel_dec = [import_state.computed], __getAboveDebouncedZoomThreshold_dec = [import_state.computed], _getEfficientZoomLevel_dec = [import_state.computed], _getViewportScreenBounds_dec = [import_state.computed], _getViewportScreenCenter_dec = [import_state.computed], _getViewportPageBounds_dec = [import_state.computed], __getCollaboratorsQuery_dec = [import_state.computed], _getCollaborators_dec = [import_state.computed], _getCollaboratorsOnCurrentPage_dec = [import_state.computed], _getRenderingShapes_dec = [import_state.computed], __getAllPagesQuery_dec = [import_state.computed], _getPages_dec = [import_state.computed], _getCurrentPageId_dec = [import_state.computed], _getCurrentPageShapeIdsSorted_dec = [import_state.computed], __getAllAssetsQuery_dec = [import_state.computed], __getShapeHandlesCache_dec = [import_state.computed], __getShapePageTransformCache_dec = [import_state.computed], __getShapePageBoundsCache_dec = [import_state.computed], __getShapeClipPathCache_dec = [import_state.computed], __getShapeMaskCache_dec = [import_state.computed], __getShapeMaskedPageBoundsCache_dec = [import_state.computed], _getNotVisibleShapes_dec = [import_state.computed], _getCulledShapes_dec = [import_state.computed], _getCurrentPageBounds_dec = [import_state.computed], _getCurrentPageShapes_dec = [import_state.computed], _getCurrentPageShapesSorted_dec = [import_state.computed], _getCurrentPageRenderingShapesSorted_dec = [import_state.computed], __getBindingsIndexCache_dec = [import_state.computed], __getSelectionSharedStyles_dec = [import_state.computed], _getSharedStyles_dec = [(0, import_state.computed)({ isEqual: (a, b) => a.equals(b) })], _getSharedOpacity_dec = [import_state.computed], _getIsFocused_dec = [import_state.computed], _getIsReadonly_dec = [import_state.computed], __setShiftKeyTimeout_dec = [import_utils.bind], __setAltKeyTimeout_dec = [import_utils.bind], __setCtrlKeyTimeout_dec = [import_utils.bind], __setMetaKeyTimeout_dec = [import_utils.bind], _a) {
129
129
  constructor({
130
130
  store,
131
131
  user,
@@ -2048,6 +2048,9 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
2048
2048
  getZoomLevel() {
2049
2049
  return this.getCamera().z;
2050
2050
  }
2051
+ getResizeScaleFactor() {
2052
+ return this.user.getIsDynamicResizeMode() ? 1 / this.getZoomLevel() : 1;
2053
+ }
2051
2054
  getDebouncedZoomLevel() {
2052
2055
  if (this.options.debouncedZoom) {
2053
2056
  if (this.getCameraState() === "idle") {
@@ -4988,6 +4991,53 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
4988
4991
  if (changes) this.updateShapes(changes);
4989
4992
  return this;
4990
4993
  }
4994
+ /**
4995
+ * Shared clustering logic for layout methods. Resolves shapes, optionally filters to
4996
+ * axis-aligned shapes, checks canBeLaidOut, and groups shapes into clusters via arrow bindings.
4997
+ *
4998
+ * @internal
4999
+ */
5000
+ getShapeClusters(shapes, type, opts) {
5001
+ const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5002
+ let freshShapes = (0, import_utils.compact)(ids.map((id) => this.getShape(id)));
5003
+ if (opts?.filterAxisAligned) {
5004
+ freshShapes = freshShapes.filter(
5005
+ (s) => this.getShapePageTransform(s)?.rotation() % (import_utils2.PI / 2) === 0
5006
+ );
5007
+ }
5008
+ const clusters = [];
5009
+ const allBounds = [];
5010
+ const visited = /* @__PURE__ */ new Set();
5011
+ for (const shape of freshShapes) {
5012
+ if (visited.has(shape.id)) continue;
5013
+ visited.add(shape.id);
5014
+ const shapePageBounds = this.getShapePageBounds(shape);
5015
+ if (!shapePageBounds) continue;
5016
+ if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5017
+ type,
5018
+ shapes: freshShapes
5019
+ })) {
5020
+ continue;
5021
+ }
5022
+ const shapesMovingTogether = [shape];
5023
+ const boundsOfShapesMovingTogether = [shapePageBounds];
5024
+ this.collectShapesViaArrowBindings({
5025
+ bindings: this.getBindingsToShape(shape.id, "arrow"),
5026
+ initialShapes: freshShapes,
5027
+ resultShapes: shapesMovingTogether,
5028
+ resultBounds: boundsOfShapesMovingTogether,
5029
+ visited
5030
+ });
5031
+ const commonPageBounds = import_Box.Box.Common(boundsOfShapesMovingTogether);
5032
+ if (!commonPageBounds) continue;
5033
+ clusters.push({
5034
+ shapes: shapesMovingTogether,
5035
+ pageBounds: commonPageBounds
5036
+ });
5037
+ allBounds.push(commonPageBounds);
5038
+ }
5039
+ return { clusters, allBounds };
5040
+ }
4991
5041
  /**
4992
5042
  * @internal
4993
5043
  */
@@ -5098,40 +5148,8 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5098
5148
  */
5099
5149
  stackShapes(shapes, operation, gap) {
5100
5150
  const _gap = gap ?? this.options.adjacentShapeMargin;
5101
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5102
5151
  if (this.getIsReadonly()) return this;
5103
- const shapesToStackFirstPass = (0, import_utils.compact)(ids.map((id) => this.getShape(id)));
5104
- const shapeClustersToStack = [];
5105
- const allBounds = [];
5106
- const visited = /* @__PURE__ */ new Set();
5107
- for (const shape of shapesToStackFirstPass) {
5108
- if (visited.has(shape.id)) continue;
5109
- visited.add(shape.id);
5110
- const shapePageBounds = this.getShapePageBounds(shape);
5111
- if (!shapePageBounds) continue;
5112
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5113
- type: "stack",
5114
- shapes: shapesToStackFirstPass
5115
- })) {
5116
- continue;
5117
- }
5118
- const shapesMovingTogether = [shape];
5119
- const boundsOfShapesMovingTogether = [shapePageBounds];
5120
- this.collectShapesViaArrowBindings({
5121
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5122
- initialShapes: shapesToStackFirstPass,
5123
- resultShapes: shapesMovingTogether,
5124
- resultBounds: boundsOfShapesMovingTogether,
5125
- visited
5126
- });
5127
- const commonPageBounds = import_Box.Box.Common(boundsOfShapesMovingTogether);
5128
- if (!commonPageBounds) continue;
5129
- shapeClustersToStack.push({
5130
- shapes: shapesMovingTogether,
5131
- pageBounds: commonPageBounds
5132
- });
5133
- allBounds.push(commonPageBounds);
5134
- }
5152
+ const { clusters: shapeClustersToStack } = this.getShapeClusters(shapes, "stack");
5135
5153
  const len = shapeClustersToStack.length;
5136
5154
  if (_gap === 0 && len < 3 || len < 2) return this;
5137
5155
  let val;
@@ -5217,40 +5235,11 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5217
5235
  packShapes(shapes, _gap) {
5218
5236
  if (this.getIsReadonly()) return this;
5219
5237
  const gap = _gap ?? this.options.adjacentShapeMargin;
5220
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5221
- const shapesToPackFirstPass = (0, import_utils.compact)(ids.map((id) => this.getShape(id)));
5222
- const shapeClustersToPack = [];
5223
- const allBounds = [];
5224
- const visited = /* @__PURE__ */ new Set();
5225
- for (const shape of shapesToPackFirstPass) {
5226
- if (visited.has(shape.id)) continue;
5227
- visited.add(shape.id);
5228
- const shapePageBounds = this.getShapePageBounds(shape);
5229
- if (!shapePageBounds) continue;
5230
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5231
- type: "pack",
5232
- shapes: shapesToPackFirstPass
5233
- })) {
5234
- continue;
5235
- }
5236
- const shapesMovingTogether = [shape];
5237
- const boundsOfShapesMovingTogether = [shapePageBounds];
5238
- this.collectShapesViaArrowBindings({
5239
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5240
- initialShapes: shapesToPackFirstPass,
5241
- resultShapes: shapesMovingTogether,
5242
- resultBounds: boundsOfShapesMovingTogether,
5243
- visited
5244
- });
5245
- const commonPageBounds = import_Box.Box.Common(boundsOfShapesMovingTogether);
5246
- if (!commonPageBounds) continue;
5247
- shapeClustersToPack.push({
5248
- shapes: shapesMovingTogether,
5249
- pageBounds: commonPageBounds,
5250
- nextPageBounds: commonPageBounds.clone()
5251
- });
5252
- allBounds.push(commonPageBounds);
5253
- }
5238
+ const { clusters, allBounds } = this.getShapeClusters(shapes, "pack");
5239
+ const shapeClustersToPack = clusters.map((cluster) => ({
5240
+ ...cluster,
5241
+ nextPageBounds: cluster.pageBounds.clone()
5242
+ }));
5254
5243
  if (shapeClustersToPack.length < 2) return this;
5255
5244
  let area = 0;
5256
5245
  for (const { pageBounds } of shapeClustersToPack) {
@@ -5334,39 +5323,7 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5334
5323
  */
5335
5324
  alignShapes(shapes, operation) {
5336
5325
  if (this.getIsReadonly()) return this;
5337
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5338
- const shapesToAlignFirstPass = (0, import_utils.compact)(ids.map((id) => this.getShape(id)));
5339
- const shapeClustersToAlign = [];
5340
- const allBounds = [];
5341
- const visited = /* @__PURE__ */ new Set();
5342
- for (const shape of shapesToAlignFirstPass) {
5343
- if (visited.has(shape.id)) continue;
5344
- visited.add(shape.id);
5345
- const shapePageBounds = this.getShapePageBounds(shape);
5346
- if (!shapePageBounds) continue;
5347
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5348
- type: "align",
5349
- shapes: shapesToAlignFirstPass
5350
- })) {
5351
- continue;
5352
- }
5353
- const shapesMovingTogether = [shape];
5354
- const boundsOfShapesMovingTogether = [shapePageBounds];
5355
- this.collectShapesViaArrowBindings({
5356
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5357
- initialShapes: shapesToAlignFirstPass,
5358
- resultShapes: shapesMovingTogether,
5359
- resultBounds: boundsOfShapesMovingTogether,
5360
- visited
5361
- });
5362
- const commonPageBounds = import_Box.Box.Common(boundsOfShapesMovingTogether);
5363
- if (!commonPageBounds) continue;
5364
- shapeClustersToAlign.push({
5365
- shapes: shapesMovingTogether,
5366
- pageBounds: commonPageBounds
5367
- });
5368
- allBounds.push(commonPageBounds);
5369
- }
5326
+ const { clusters: shapeClustersToAlign, allBounds } = this.getShapeClusters(shapes, "align");
5370
5327
  if (shapeClustersToAlign.length < 2) return this;
5371
5328
  const commonBounds = import_Box.Box.Common(allBounds);
5372
5329
  const changes = [];
@@ -5428,39 +5385,7 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5428
5385
  */
5429
5386
  distributeShapes(shapes, operation) {
5430
5387
  if (this.getIsReadonly()) return this;
5431
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5432
- const shapesToDistributeFirstPass = (0, import_utils.compact)(ids.map((id) => this.getShape(id)));
5433
- const shapeClustersToDistribute = [];
5434
- const allBounds = [];
5435
- const visited = /* @__PURE__ */ new Set();
5436
- for (const shape of shapesToDistributeFirstPass) {
5437
- if (visited.has(shape.id)) continue;
5438
- visited.add(shape.id);
5439
- const shapePageBounds = this.getShapePageBounds(shape);
5440
- if (!shapePageBounds) continue;
5441
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5442
- type: "distribute",
5443
- shapes: shapesToDistributeFirstPass
5444
- })) {
5445
- continue;
5446
- }
5447
- const shapesMovingTogether = [shape];
5448
- const boundsOfShapesMovingTogether = [shapePageBounds];
5449
- this.collectShapesViaArrowBindings({
5450
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5451
- initialShapes: shapesToDistributeFirstPass,
5452
- resultShapes: shapesMovingTogether,
5453
- resultBounds: boundsOfShapesMovingTogether,
5454
- visited
5455
- });
5456
- const commonPageBounds = import_Box.Box.Common(boundsOfShapesMovingTogether);
5457
- if (!commonPageBounds) continue;
5458
- shapeClustersToDistribute.push({
5459
- shapes: shapesMovingTogether,
5460
- pageBounds: commonPageBounds
5461
- });
5462
- allBounds.push(commonPageBounds);
5463
- }
5388
+ const { clusters: shapeClustersToDistribute } = this.getShapeClusters(shapes, "distribute");
5464
5389
  if (shapeClustersToDistribute.length < 3) return this;
5465
5390
  let val;
5466
5391
  let min;
@@ -5482,6 +5407,7 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5482
5407
  const last2 = shapeClustersToDistribute.sort((a, b) => b.pageBounds[max] - a.pageBounds[max])[0];
5483
5408
  if (first === last2) {
5484
5409
  const excludedShapeIds = new Set(first.shapes.map((s) => s.id));
5410
+ const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5485
5411
  return this.distributeShapes(
5486
5412
  ids.filter((id) => !excludedShapeIds.has(id)),
5487
5413
  operation
@@ -5534,41 +5460,12 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5534
5460
  * @public
5535
5461
  */
5536
5462
  stretchShapes(shapes, operation) {
5537
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
5538
5463
  if (this.getIsReadonly()) return this;
5539
- const shapesToStretchFirstPass = (0, import_utils.compact)(ids.map((id) => this.getShape(id))).filter(
5540
- (s) => this.getShapePageTransform(s)?.rotation() % (import_utils2.PI / 2) === 0
5464
+ const { clusters: shapeClustersToStretch, allBounds } = this.getShapeClusters(
5465
+ shapes,
5466
+ "stretch",
5467
+ { filterAxisAligned: true }
5541
5468
  );
5542
- const shapeClustersToStretch = [];
5543
- const allBounds = [];
5544
- const visited = /* @__PURE__ */ new Set();
5545
- for (const shape of shapesToStretchFirstPass) {
5546
- if (visited.has(shape.id)) continue;
5547
- visited.add(shape.id);
5548
- const shapePageBounds = this.getShapePageBounds(shape);
5549
- if (!shapePageBounds) continue;
5550
- const shapesMovingTogether = [shape];
5551
- const boundsOfShapesMovingTogether = [shapePageBounds];
5552
- if (!this.getShapeUtil(shape).canBeLaidOut?.(shape, {
5553
- type: "stretch"
5554
- })) {
5555
- continue;
5556
- }
5557
- this.collectShapesViaArrowBindings({
5558
- bindings: this.getBindingsToShape(shape.id, "arrow"),
5559
- initialShapes: shapesToStretchFirstPass,
5560
- resultShapes: shapesMovingTogether,
5561
- resultBounds: boundsOfShapesMovingTogether,
5562
- visited
5563
- });
5564
- const commonPageBounds = import_Box.Box.Common(boundsOfShapesMovingTogether);
5565
- if (!commonPageBounds) continue;
5566
- shapeClustersToStretch.push({
5567
- shapes: shapesMovingTogether,
5568
- pageBounds: commonPageBounds
5569
- });
5570
- allBounds.push(commonPageBounds);
5571
- }
5572
5469
  if (shapeClustersToStretch.length < 2) return this;
5573
5470
  const commonBounds = import_Box.Box.Common(allBounds);
5574
5471
  let val;
@@ -5594,7 +5491,7 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5594
5491
  for (const shape of shapes2) {
5595
5492
  const shapeLocalOffset = localOffset.clone();
5596
5493
  const parentTransform = this.getShapeParentTransform(shape);
5597
- if (parentTransform) localOffset.rot(-parentTransform.rotation());
5494
+ if (parentTransform) shapeLocalOffset.rot(-parentTransform.rotation());
5598
5495
  shapeLocalOffset.add(shape);
5599
5496
  const changes = this.getChangesToTranslateShape(shape, shapeLocalOffset);
5600
5497
  this.updateShape(changes);
@@ -5609,6 +5506,62 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5609
5506
  });
5610
5507
  return this;
5611
5508
  }
5509
+ /**
5510
+ * Resize and reposition a set of shapes so that their combined page bounds matches the given
5511
+ * target bounds.
5512
+ *
5513
+ * @example
5514
+ * ```ts
5515
+ * editor.resizeToBounds([box1, box2], { x: 0, y: 0, w: 500, h: 500 })
5516
+ * editor.resizeToBounds(editor.getSelectedShapeIds(), new Box(0, 0, 500, 500))
5517
+ * ```
5518
+ *
5519
+ * @param shapes - The shapes (or shape ids) to resize.
5520
+ * @param bounds - The target bounding box.
5521
+ *
5522
+ * @public
5523
+ */
5524
+ resizeToBounds(shapes, bounds) {
5525
+ if (this.getIsReadonly()) return this;
5526
+ const targetBounds = import_Box.Box.From(bounds);
5527
+ const { clusters: shapeClusters, allBounds } = this.getShapeClusters(
5528
+ shapes,
5529
+ "resize_to_bounds",
5530
+ { filterAxisAligned: true }
5531
+ );
5532
+ if (shapeClusters.length === 0) return this;
5533
+ const commonBounds = import_Box.Box.Common(allBounds);
5534
+ if (!commonBounds) return this;
5535
+ if (commonBounds.width === 0 || commonBounds.height === 0) return this;
5536
+ const scaleX = targetBounds.width / commonBounds.width;
5537
+ const scaleY = targetBounds.height / commonBounds.height;
5538
+ const scale = new import_Vec.Vec(scaleX, scaleY);
5539
+ shapeClusters.forEach(({ shapes: shapes2, pageBounds }) => {
5540
+ const localOffset = new import_Vec.Vec(
5541
+ targetBounds.minX - commonBounds.minX + (pageBounds.minX - commonBounds.minX) * (scaleX - 1),
5542
+ targetBounds.minY - commonBounds.minY + (pageBounds.minY - commonBounds.minY) * (scaleY - 1)
5543
+ );
5544
+ const scaleOrigin = new import_Vec.Vec(
5545
+ targetBounds.minX + (pageBounds.minX - commonBounds.minX) * scaleX,
5546
+ targetBounds.minY + (pageBounds.minY - commonBounds.minY) * scaleY
5547
+ );
5548
+ for (const shape of shapes2) {
5549
+ const shapeLocalOffset = localOffset.clone();
5550
+ const parentTransform = this.getShapeParentTransform(shape);
5551
+ if (parentTransform) shapeLocalOffset.rot(-parentTransform.rotation());
5552
+ shapeLocalOffset.add(shape);
5553
+ const changes = this.getChangesToTranslateShape(shape, shapeLocalOffset);
5554
+ this.updateShape(changes);
5555
+ this.resizeShape(shape.id, scale, {
5556
+ initialBounds: this.getShapeGeometry(shape).bounds,
5557
+ scaleOrigin,
5558
+ isAspectRatioLocked: this.getShapeUtil(shape).isAspectRatioLocked(shape),
5559
+ scaleAxisRotation: 0
5560
+ });
5561
+ }
5562
+ });
5563
+ return this;
5564
+ }
5612
5565
  /**
5613
5566
  * Resize a shape.
5614
5567
  *
@@ -7968,6 +7921,7 @@ __decorateElement(_init, 1, "getCamera", _getCamera_dec, Editor);
7968
7921
  __decorateElement(_init, 1, "getViewportPageBoundsForFollowing", _getViewportPageBoundsForFollowing_dec, Editor);
7969
7922
  __decorateElement(_init, 1, "getCameraForFollowing", _getCameraForFollowing_dec, Editor);
7970
7923
  __decorateElement(_init, 1, "getZoomLevel", _getZoomLevel_dec, Editor);
7924
+ __decorateElement(_init, 1, "getResizeScaleFactor", _getResizeScaleFactor_dec, Editor);
7971
7925
  __decorateElement(_init, 1, "getDebouncedZoomLevel", _getDebouncedZoomLevel_dec, Editor);
7972
7926
  __decorateElement(_init, 1, "_getAboveDebouncedZoomThreshold", __getAboveDebouncedZoomThreshold_dec, Editor);
7973
7927
  __decorateElement(_init, 1, "getEfficientZoomLevel", _getEfficientZoomLevel_dec, Editor);