orcasvn-react-diagrams 0.2.5 → 0.2.6

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 (53) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +15 -34
  3. package/ai/api-contract.json +38 -1
  4. package/ai/manifest.json +1 -1
  5. package/dist/cjs/examples.js +1252 -113
  6. package/dist/cjs/index.js +876 -81
  7. package/dist/cjs/types/api/createDiagramEditor.d.ts +2 -1
  8. package/dist/cjs/types/api/types.d.ts +26 -1
  9. package/dist/cjs/types/displaybox/demos/OffsetAnchorAvoidanceDemoTab.d.ts +3 -0
  10. package/dist/cjs/types/displaybox/demos/ZoomToFitElementsDemoTab.d.ts +3 -0
  11. package/dist/cjs/types/displaybox/demos/offsetAnchorAvoidanceDemo.d.ts +4 -0
  12. package/dist/cjs/types/displaybox/demos/textDemo.d.ts +3 -0
  13. package/dist/cjs/types/displaybox/demos/zoomToFitElementsDemo.d.ts +4 -0
  14. package/dist/cjs/types/engine/AutoLayoutService.d.ts +44 -1
  15. package/dist/cjs/types/engine/DiagramEngine.d.ts +12 -0
  16. package/dist/cjs/types/engine/LinkRoutingService.d.ts +1 -0
  17. package/dist/cjs/types/models/TextModel.d.ts +1 -0
  18. package/dist/cjs/types/renderer/konva/KonvaInteraction.d.ts +2 -0
  19. package/dist/esm/examples.js +1252 -113
  20. package/dist/esm/examples.js.map +1 -1
  21. package/dist/esm/index.js +876 -81
  22. package/dist/esm/index.js.map +1 -1
  23. package/dist/esm/types/api/createDiagramEditor.d.ts +2 -1
  24. package/dist/esm/types/api/types.d.ts +26 -1
  25. package/dist/esm/types/displaybox/demos/OffsetAnchorAvoidanceDemoTab.d.ts +3 -0
  26. package/dist/esm/types/displaybox/demos/ZoomToFitElementsDemoTab.d.ts +3 -0
  27. package/dist/esm/types/displaybox/demos/offsetAnchorAvoidanceDemo.d.ts +4 -0
  28. package/dist/esm/types/displaybox/demos/textDemo.d.ts +3 -0
  29. package/dist/esm/types/displaybox/demos/zoomToFitElementsDemo.d.ts +4 -0
  30. package/dist/esm/types/engine/AutoLayoutService.d.ts +44 -1
  31. package/dist/esm/types/engine/DiagramEngine.d.ts +12 -0
  32. package/dist/esm/types/engine/LinkRoutingService.d.ts +1 -0
  33. package/dist/esm/types/models/TextModel.d.ts +1 -0
  34. package/dist/esm/types/renderer/konva/KonvaInteraction.d.ts +2 -0
  35. package/dist/examples.d.ts +27 -1
  36. package/dist/index.d.ts +29 -2
  37. package/docs/API_CONTRACT.md +38 -3
  38. package/docs/CAPABILITIES.md +3 -0
  39. package/docs/COMMANDS_EVENTS.md +8 -0
  40. package/docs/DOCUMENTATION_WORKFLOW.md +2 -1
  41. package/docs/INTEGRATION_PLAYBOOK.md +3 -0
  42. package/docs/STATE_INVARIANTS.md +4 -0
  43. package/package.json +1 -1
  44. package/src/displaybox/demos/AutoLayoutDemoTab.tsx +337 -316
  45. package/src/displaybox/demos/EngineEventsDemoTab.tsx +12 -11
  46. package/src/displaybox/demos/OffsetAnchorAvoidanceDemoTab.tsx +30 -0
  47. package/src/displaybox/demos/TextLayoutDemoTab.tsx +18 -1
  48. package/src/displaybox/demos/ZoomToFitElementsDemoTab.tsx +29 -0
  49. package/src/displaybox/demos/autoLayoutDemo.ts +74 -73
  50. package/src/displaybox/demos/index.tsx +80 -64
  51. package/src/displaybox/demos/offsetAnchorAvoidanceDemo.ts +211 -0
  52. package/src/displaybox/demos/textDemo.ts +6 -2
  53. package/src/displaybox/demos/zoomToFitElementsDemo.ts +83 -0
package/dist/cjs/index.js CHANGED
@@ -270,6 +270,7 @@ var TextModel = /** @class */ (function () {
270
270
  this.size = data.size ? __assign({}, data.size) : undefined;
271
271
  this.style = data.style;
272
272
  this.ownerId = (_a = data.ownerId) !== null && _a !== void 0 ? _a : null;
273
+ this.interaction = data.interaction ? __assign({}, data.interaction) : undefined;
273
274
  this.layout = data.layout ? __assign({}, data.layout) : undefined;
274
275
  this.displayContent = (_b = data.displayContent) !== null && _b !== void 0 ? _b : data.content;
275
276
  this.displayOffset = data.displayOffset ? __assign({}, data.displayOffset) : undefined;
@@ -306,6 +307,7 @@ var TextModel = /** @class */ (function () {
306
307
  size: this.size ? __assign({}, this.size) : undefined,
307
308
  style: this.style,
308
309
  ownerId: this.ownerId,
310
+ interaction: this.interaction ? __assign({}, this.interaction) : undefined,
309
311
  layout: this.layout ? __assign({}, this.layout) : undefined,
310
312
  displayContent: (_a = this.displayContent) !== null && _a !== void 0 ? _a : this.content,
311
313
  displayOffset: this.displayOffset ? __assign({}, this.displayOffset) : undefined,
@@ -320,7 +322,7 @@ var distance = function (a, b) {
320
322
  var dy = a.y - b.y;
321
323
  return Math.sqrt(dx * dx + dy * dy);
322
324
  };
323
- var clamp$1 = function (value, min, max) {
325
+ var clamp$2 = function (value, min, max) {
324
326
  if (value < min)
325
327
  return min;
326
328
  if (value > max)
@@ -1167,7 +1169,7 @@ var pathLength = function (points) {
1167
1169
  }
1168
1170
  return total;
1169
1171
  };
1170
- var clamp = function (value, min, max) { return Math.max(min, Math.min(max, value)); };
1172
+ var clamp$1 = function (value, min, max) { return Math.max(min, Math.min(max, value)); };
1171
1173
  var ObstacleRouter = /** @class */ (function () {
1172
1174
  function ObstacleRouter(options) {
1173
1175
  var _a, _b, _c;
@@ -1447,7 +1449,7 @@ var ObstacleRouter = /** @class */ (function () {
1447
1449
  return null;
1448
1450
  if (corridor.axis === 'horizontal') {
1449
1451
  var direction_1 = source.x <= target.x ? 1 : -1;
1450
- var corridorY = clamp((source.y + target.y) / 2, corridor.bounds.y + this.padding, corridor.bounds.y + corridor.bounds.height - this.padding);
1452
+ var corridorY = clamp$1((source.y + target.y) / 2, corridor.bounds.y + this.padding, corridor.bounds.y + corridor.bounds.height - this.padding);
1451
1453
  var sourceExitX = direction_1 > 0
1452
1454
  ? corridor.sourceBranchRect.x + corridor.sourceBranchRect.width + this.padding
1453
1455
  : corridor.sourceBranchRect.x - this.padding;
@@ -1464,7 +1466,7 @@ var ObstacleRouter = /** @class */ (function () {
1464
1466
  ]);
1465
1467
  }
1466
1468
  var direction = source.y <= target.y ? 1 : -1;
1467
- var corridorX = clamp((source.x + target.x) / 2, corridor.bounds.x + this.padding, corridor.bounds.x + corridor.bounds.width - this.padding);
1469
+ var corridorX = clamp$1((source.x + target.x) / 2, corridor.bounds.x + this.padding, corridor.bounds.x + corridor.bounds.width - this.padding);
1468
1470
  var sourceExitY = direction > 0
1469
1471
  ? corridor.sourceBranchRect.y + corridor.sourceBranchRect.height + this.padding
1470
1472
  : corridor.sourceBranchRect.y - this.padding;
@@ -2016,6 +2018,7 @@ var TextLayoutService = /** @class */ (function () {
2016
2018
  return TextLayoutService;
2017
2019
  }());
2018
2020
 
2021
+ var GRID_ROW_UNITS = 12;
2019
2022
  var AutoLayoutService = /** @class */ (function () {
2020
2023
  function AutoLayoutService(config) {
2021
2024
  this.model = config.model;
@@ -2030,11 +2033,24 @@ var AutoLayoutService = /** @class */ (function () {
2030
2033
  if (!layout || layout.mode === 'manual')
2031
2034
  return { patches: [], movedPortIds: [] };
2032
2035
  var preserveParentSize = Boolean(options === null || options === void 0 ? void 0 : options.preserveParentSize);
2036
+ var preserveParentWidth = preserveParentSize || Boolean(options === null || options === void 0 ? void 0 : options.preserveParentWidth);
2037
+ var preserveParentHeight = preserveParentSize || Boolean(options === null || options === void 0 ? void 0 : options.preserveParentHeight);
2033
2038
  var padding = this.resolveLayoutPadding(layout);
2034
2039
  var labelReservedTopLane = this.resolveLabelReservedTopLane(parentId, layout);
2035
2040
  var autoResize = this.resolveAutoResizeMode(layout);
2036
2041
  var children = this.model.getChildren(parentId);
2037
2042
  if (children.length === 0) {
2043
+ if (preserveParentWidth || preserveParentHeight) {
2044
+ var targetWidth_1 = preserveParentWidth ? parent.size.width : Math.max(0, Math.round(padding.x * 2));
2045
+ var targetHeight_1 = preserveParentHeight
2046
+ ? parent.size.height
2047
+ : Math.max(0, Math.round(padding.y * 2 + labelReservedTopLane));
2048
+ if (parent.size.width === targetWidth_1 && parent.size.height === targetHeight_1) {
2049
+ return { patches: [], movedPortIds: [] };
2050
+ }
2051
+ var resizePatches_1 = this.commandQueue.run(createResizeElementCommand(parentId, { width: targetWidth_1, height: targetHeight_1 }), this.model);
2052
+ return { patches: resizePatches_1, movedPortIds: __spreadArray([], parent.portIds, true) };
2053
+ }
2038
2054
  if (preserveParentSize) {
2039
2055
  return { patches: [], movedPortIds: [] };
2040
2056
  }
@@ -2049,9 +2065,17 @@ var AutoLayoutService = /** @class */ (function () {
2049
2065
  return { patches: resizePatches, movedPortIds: __spreadArray([], parent.portIds, true) };
2050
2066
  }
2051
2067
  if (layout.mode === 'grid') {
2052
- return this.applyGridLayoutForParent(parentId, parent, children, preserveParentSize);
2068
+ return this.applyGridLayoutForParent(parentId, parent, children, {
2069
+ preserveParentSize: preserveParentSize,
2070
+ preserveParentWidth: preserveParentWidth,
2071
+ preserveParentHeight: preserveParentHeight,
2072
+ });
2053
2073
  }
2054
- return this.applyAxisLayoutForParent(parentId, parent, children, layout.mode, preserveParentSize);
2074
+ return this.applyAxisLayoutForParent(parentId, parent, children, layout.mode, {
2075
+ preserveParentSize: preserveParentSize,
2076
+ preserveParentWidth: preserveParentWidth,
2077
+ preserveParentHeight: preserveParentHeight,
2078
+ });
2055
2079
  };
2056
2080
  AutoLayoutService.prototype.applyLayoutCascade = function (startParentId) {
2057
2081
  var _a;
@@ -2094,11 +2118,102 @@ var AutoLayoutService = /** @class */ (function () {
2094
2118
  });
2095
2119
  return { patches: patches, movedPortIds: Array.from(new Set(movedPortIds)) };
2096
2120
  };
2097
- AutoLayoutService.prototype.applyAxisLayoutForParent = function (parentId, parent, children, axis, preserveParentSize) {
2121
+ AutoLayoutService.prototype.describeGridRows = function (parentId) {
2122
+ var _a;
2123
+ var parent = this.model.getElement(parentId);
2124
+ if (!parent || ((_a = parent.layout) === null || _a === void 0 ? void 0 : _a.mode) !== 'grid')
2125
+ return [];
2126
+ return this.resolveAssignedGridRows(parent).map(function (row) { return ({ childIds: __spreadArray([], row.childIds, true) }); });
2127
+ };
2128
+ AutoLayoutService.prototype.describeGridSlotState = function (parentId) {
2129
+ var _this = this;
2130
+ var _a;
2131
+ var parent = this.model.getElement(parentId);
2132
+ if (!parent || ((_a = parent.layout) === null || _a === void 0 ? void 0 : _a.mode) !== 'grid')
2133
+ return null;
2134
+ var assignedRows = this.resolveAssignedGridRows(parent);
2135
+ return {
2136
+ orderedChildIds: assignedRows.flatMap(function (row) { return row.childIds; }),
2137
+ slotWeights: assignedRows.flatMap(function (row) { return row.weights.slice(0, row.childIds.length).map(function (weight) { return _this.clampGridUnits(weight); }); }),
2138
+ };
2139
+ };
2140
+ AutoLayoutService.prototype.resolveGridReorderedLayout = function (parentId, previousState) {
2141
+ var _this = this;
2142
+ var _a;
2143
+ var parent = this.model.getElement(parentId);
2144
+ if (!parent || ((_a = parent.layout) === null || _a === void 0 ? void 0 : _a.mode) !== 'grid' || !parent.layout)
2145
+ return null;
2146
+ var sortedChildren = this.sortGridChildrenForLayout(this.model.getChildren(parentId));
2147
+ var orderedChildIds = sortedChildren.map(function (child) { return child.id; });
2148
+ if (orderedChildIds.length === previousState.orderedChildIds.length &&
2149
+ orderedChildIds.every(function (childId, index) { return childId === previousState.orderedChildIds[index]; })) {
2150
+ return null;
2151
+ }
2152
+ var weightByChildId = new Map();
2153
+ previousState.orderedChildIds.forEach(function (childId, index) {
2154
+ var _a;
2155
+ weightByChildId.set(childId, _this.clampGridUnits((_a = previousState.slotWeights[index]) !== null && _a !== void 0 ? _a : GRID_ROW_UNITS));
2156
+ });
2157
+ var reorderedTemplate = orderedChildIds.map(function (childId) { var _a; return (_a = weightByChildId.get(childId)) !== null && _a !== void 0 ? _a : GRID_ROW_UNITS; });
2158
+ return __assign(__assign({}, parent.layout), { gridTemplate: reorderedTemplate });
2159
+ };
2160
+ AutoLayoutService.prototype.resolveGridChildResizeTopologyChange = function (parentId, childId, requestedWidth) {
2161
+ var _a;
2162
+ var parent = this.model.getElement(parentId);
2163
+ if (!parent || ((_a = parent.layout) === null || _a === void 0 ? void 0 : _a.mode) !== 'grid' || !parent.layout)
2164
+ return null;
2165
+ var slotState = this.resolveGridChildSlotState(parentId, childId);
2166
+ if (!slotState)
2167
+ return null;
2168
+ var assignedRows = slotState.assignedRows, orderedChildIds = slotState.orderedChildIds, slotWeights = slotState.slotWeights, childFlatIndex = slotState.childFlatIndex, rowIndex = slotState.rowIndex;
2169
+ var currentUnits = slotWeights[childFlatIndex];
2170
+ var requestedUnits = this.resolveRequestedGridUnits(parent, assignedRows[rowIndex], requestedWidth);
2171
+ if (requestedUnits === currentUnits)
2172
+ return null;
2173
+ var nextSlotWeights = __spreadArray([], slotWeights, true);
2174
+ nextSlotWeights[childFlatIndex] = requestedUnits;
2175
+ var nextRows = this.packGridRowsFromSlots(orderedChildIds, nextSlotWeights);
2176
+ var beforeRows = assignedRows.map(function (row) { return ({ childIds: __spreadArray([], row.childIds, true) }); });
2177
+ var afterRows = nextRows.map(function (row) { return ({ childIds: __spreadArray([], row.childIds, true) }); });
2178
+ var rowsChanged = !this.gridRowSnapshotsEqual(beforeRows, afterRows);
2179
+ var reason = requestedUnits < currentUnits && rowsChanged ? 'child-resize-split' : 'child-resize-redistribute';
2180
+ return {
2181
+ parentId: parentId,
2182
+ triggerChildId: childId,
2183
+ reason: reason,
2184
+ beforeRows: beforeRows,
2185
+ afterRows: afterRows,
2186
+ nextLayout: __assign(__assign({}, parent.layout), { gridTemplate: nextSlotWeights }),
2187
+ };
2188
+ };
2189
+ AutoLayoutService.prototype.snapGridChildRequestedWidth = function (parentId, childId, requestedWidth) {
2190
+ var _a, _b;
2191
+ var parent = this.model.getElement(parentId);
2192
+ if (!parent || ((_a = parent.layout) === null || _a === void 0 ? void 0 : _a.mode) !== 'grid' || !parent.layout)
2193
+ return requestedWidth;
2194
+ var slotState = this.resolveGridChildSlotState(parentId, childId);
2195
+ if (!slotState)
2196
+ return requestedWidth;
2197
+ var requestedUnits = this.resolveRequestedGridUnits(parent, slotState.assignedRows[slotState.rowIndex], requestedWidth);
2198
+ var rowUnitWidth = this.resolveGridRowUnitWidth(Math.max(0, parent.size.width - this.resolveLayoutPadding(parent.layout).x * 2), slotState.assignedRows[slotState.rowIndex].childIds.length, (_b = parent.layout.gap) !== null && _b !== void 0 ? _b : 12);
2199
+ return Math.max(1, Math.round(requestedUnits * rowUnitWidth));
2200
+ };
2201
+ AutoLayoutService.prototype.canGridChildGrowRightward = function (parentId, childId) {
2202
+ var _a;
2203
+ var parent = this.model.getElement(parentId);
2204
+ if (!parent || ((_a = parent.layout) === null || _a === void 0 ? void 0 : _a.mode) !== 'grid' || !parent.layout)
2205
+ return false;
2206
+ var orderedIds = this.resolveAssignedGridRows(parent).flatMap(function (row) { return row.childIds; });
2207
+ var childIndex = orderedIds.indexOf(childId);
2208
+ return childIndex >= 0 && childIndex < orderedIds.length - 1;
2209
+ };
2210
+ AutoLayoutService.prototype.applyAxisLayoutForParent = function (parentId, parent, children, axis, options) {
2098
2211
  var _a, _b;
2099
2212
  var _this = this;
2100
2213
  var _c, _d, _e, _f;
2101
- if (preserveParentSize === void 0) { preserveParentSize = false; }
2214
+ var preserveParentSize = Boolean(options === null || options === void 0 ? void 0 : options.preserveParentSize);
2215
+ var preserveParentWidth = preserveParentSize || Boolean(options === null || options === void 0 ? void 0 : options.preserveParentWidth);
2216
+ var preserveParentHeight = preserveParentSize || Boolean(options === null || options === void 0 ? void 0 : options.preserveParentHeight);
2102
2217
  var layout = parent.layout;
2103
2218
  var padding = this.resolveLayoutPadding(layout);
2104
2219
  var gap = (_c = layout === null || layout === void 0 ? void 0 : layout.gap) !== null && _c !== void 0 ? _c : 12;
@@ -2181,8 +2296,16 @@ var AutoLayoutService = /** @class */ (function () {
2181
2296
  }
2182
2297
  else {
2183
2298
  (_a = resolveParentSize(fittedSizes), newParentWidth = _a.width, newParentHeight = _a.height);
2299
+ if (preserveParentWidth)
2300
+ newParentWidth = preservedParentWidth;
2301
+ if (preserveParentHeight)
2302
+ newParentHeight = preservedParentHeight;
2184
2303
  fittedSizes = calculateFittedSizes(newParentWidth, newParentHeight);
2185
2304
  (_b = resolveParentSize(fittedSizes), newParentWidth = _b.width, newParentHeight = _b.height);
2305
+ if (preserveParentWidth)
2306
+ newParentWidth = preservedParentWidth;
2307
+ if (preserveParentHeight)
2308
+ newParentHeight = preservedParentHeight;
2186
2309
  }
2187
2310
  var availableWidth = Math.max(0, newParentWidth - padding.x * 2);
2188
2311
  var availableHeight = Math.max(0, newParentHeight - padding.y * 2 - labelReservedTopLane);
@@ -2217,10 +2340,12 @@ var AutoLayoutService = /** @class */ (function () {
2217
2340
  height: newParentHeight,
2218
2341
  });
2219
2342
  };
2220
- AutoLayoutService.prototype.applyGridLayoutForParent = function (parentId, parent, children, preserveParentSize) {
2343
+ AutoLayoutService.prototype.applyGridLayoutForParent = function (parentId, parent, children, options) {
2221
2344
  var _this = this;
2222
2345
  var _a, _b;
2223
- if (preserveParentSize === void 0) { preserveParentSize = false; }
2346
+ var preserveParentSize = Boolean(options === null || options === void 0 ? void 0 : options.preserveParentSize);
2347
+ var preserveParentWidth = preserveParentSize || Boolean(options === null || options === void 0 ? void 0 : options.preserveParentWidth);
2348
+ var preserveParentHeight = preserveParentSize || Boolean(options === null || options === void 0 ? void 0 : options.preserveParentHeight);
2224
2349
  var layout = parent.layout;
2225
2350
  var padding = this.resolveLayoutPadding(layout);
2226
2351
  var gap = (_a = layout === null || layout === void 0 ? void 0 : layout.gap) !== null && _a !== void 0 ? _a : 12;
@@ -2228,9 +2353,7 @@ var AutoLayoutService = /** @class */ (function () {
2228
2353
  var labelReservedTopLane = this.resolveLabelReservedTopLane(parentId, layout);
2229
2354
  var constraints = this.resolveChildSizeConstraints(layout);
2230
2355
  var autoResize = this.resolveAutoResizeMode(layout);
2231
- // Keep grid child ordering stable across resize/layout cycles.
2232
- // Grid slot assignment should not depend on transient child positions produced during drag.
2233
- var sorted = __spreadArray([], children, true);
2356
+ var sorted = this.sortGridChildrenForLayout(children);
2234
2357
  var templateRows = this.normalizeGridTemplate(layout === null || layout === void 0 ? void 0 : layout.gridTemplate);
2235
2358
  var assignedRows = this.assignChildrenToGridRows(sorted.map(function (child) { return child.id; }), templateRows);
2236
2359
  var rowMetrics = assignedRows.map(function (row) {
@@ -2245,20 +2368,33 @@ var AutoLayoutService = /** @class */ (function () {
2245
2368
  };
2246
2369
  });
2247
2370
  var rowHeight = clampedSizes.reduce(function (max, size) { return Math.max(max, size.height); }, 0);
2248
- var rowWidth = clampedSizes.reduce(function (sum, size) { return sum + size.width; }, 0) + Math.max(0, clampedSizes.length - 1) * gap;
2371
+ var totalGap = Math.max(0, clampedSizes.length - 1) * gap;
2372
+ var usedUnits = effectiveWeights.reduce(function (sum, weight) { return sum + _this.clampGridUnits(weight); }, 0);
2373
+ var requiredUnitWidth = effectiveWeights.reduce(function (max, weight, index) {
2374
+ var _a, _b;
2375
+ var slotUnits = Math.max(1, weight !== null && weight !== void 0 ? weight : 1);
2376
+ var width = (_b = (_a = clampedSizes[index]) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
2377
+ return Math.max(max, width / slotUnits);
2378
+ }, 0);
2379
+ var rowWidth = usedUnits >= GRID_ROW_UNITS
2380
+ ? autoResize === 'grow-shrink'
2381
+ ? Math.round(requiredUnitWidth * GRID_ROW_UNITS) + totalGap
2382
+ : clampedSizes.reduce(function (sum, size) { return sum + size.width; }, 0) + totalGap
2383
+ : Math.round(requiredUnitWidth * Math.max(1, usedUnits)) + totalGap;
2249
2384
  return {
2250
2385
  row: row,
2251
2386
  rowHeight: rowHeight,
2252
2387
  rowWidth: rowWidth,
2388
+ usedUnits: usedUnits,
2253
2389
  effectiveWeights: effectiveWeights,
2254
2390
  };
2255
2391
  });
2256
2392
  var requiredInnerWidth = rowMetrics.reduce(function (max, metric) { return Math.max(max, metric.rowWidth); }, 0);
2257
2393
  var requiredInnerHeight = rowMetrics.reduce(function (sum, metric) { return sum + metric.rowHeight; }, 0) + Math.max(0, rowMetrics.length - 1) * gap;
2258
- var newParentWidth = preserveParentSize
2394
+ var newParentWidth = preserveParentWidth
2259
2395
  ? Math.max(0, Math.round(parent.size.width))
2260
2396
  : this.resolveAutoResizedDimension(parent.size.width, padding.x * 2 + requiredInnerWidth, autoResize);
2261
- var newParentHeight = preserveParentSize
2397
+ var newParentHeight = preserveParentHeight
2262
2398
  ? Math.max(0, Math.round(parent.size.height))
2263
2399
  : this.resolveAutoResizedDimension(parent.size.height, padding.y * 2 + labelReservedTopLane + requiredInnerHeight, autoResize);
2264
2400
  var availableWidth = Math.max(0, newParentWidth - padding.x * 2);
@@ -2277,8 +2413,11 @@ var AutoLayoutService = /** @class */ (function () {
2277
2413
  if (childCount <= 0)
2278
2414
  return;
2279
2415
  var totalGap = Math.max(0, childCount - 1) * gap;
2280
- var rowInnerWidth = Math.max(0, availableWidth - totalGap);
2281
- var cellWidths = _this.distributeWeightedSizes(rowInnerWidth, metric.effectiveWeights);
2416
+ var rowOuterWidth = metric.usedUnits >= GRID_ROW_UNITS
2417
+ ? availableWidth
2418
+ : Math.min(availableWidth, metric.rowWidth);
2419
+ var rowInnerWidth = Math.max(0, rowOuterWidth - totalGap);
2420
+ var cellWidths = _this.resolveGridCellWidths(rowInnerWidth, metric.effectiveWeights);
2282
2421
  var rowHeight = metric.rowHeight;
2283
2422
  var cursorX = padding.x;
2284
2423
  metric.row.childIds.forEach(function (childId, index) {
@@ -2302,6 +2441,13 @@ var AutoLayoutService = /** @class */ (function () {
2302
2441
  height: newParentHeight,
2303
2442
  });
2304
2443
  };
2444
+ AutoLayoutService.prototype.resolveGridCellWidths = function (rowInnerWidth, weights) {
2445
+ var _this = this;
2446
+ if (weights.length === 0)
2447
+ return [];
2448
+ var safeWeights = weights.map(function (weight) { return _this.clampGridUnits(weight); });
2449
+ return this.distributeWeightedSizes(Math.max(0, rowInnerWidth), safeWeights);
2450
+ };
2305
2451
  AutoLayoutService.prototype.applyResolvedLayout = function (parentId, parent, desiredPositions, desiredSizes, parentSize) {
2306
2452
  var _this = this;
2307
2453
  var _a;
@@ -2364,43 +2510,96 @@ var AutoLayoutService = /** @class */ (function () {
2364
2510
  : Number.POSITIVE_INFINITY;
2365
2511
  return { minWidth: minWidth, maxWidth: maxWidth, minHeight: minHeight, maxHeight: maxHeight };
2366
2512
  };
2513
+ AutoLayoutService.prototype.resolveAssignedGridRows = function (parent) {
2514
+ var _a;
2515
+ var children = this.model.getChildren(parent.id);
2516
+ var sorted = this.sortGridChildrenForLayout(children);
2517
+ var templateRows = this.normalizeGridTemplate((_a = parent.layout) === null || _a === void 0 ? void 0 : _a.gridTemplate);
2518
+ return this.assignChildrenToGridRows(sorted.map(function (child) { return child.id; }), templateRows);
2519
+ };
2520
+ AutoLayoutService.prototype.repartitionAssignedGridRows = function (assignedRows, rowIndex, nextCurrentRowCount) {
2521
+ var _a, _b;
2522
+ var allChildIds = assignedRows.flatMap(function (row) { return row.childIds; });
2523
+ var prefixRows = assignedRows.slice(0, rowIndex).map(function (row) { return ({
2524
+ weights: __spreadArray([], row.weights.slice(0, row.childIds.length), true),
2525
+ childIds: __spreadArray([], row.childIds, true),
2526
+ }); });
2527
+ var prefixCount = prefixRows.reduce(function (sum, row) { return sum + row.childIds.length; }, 0);
2528
+ var nextRows = __spreadArray([], prefixRows, true);
2529
+ var currentChildIds = allChildIds.slice(prefixCount, prefixCount + nextCurrentRowCount);
2530
+ if (currentChildIds.length > 0) {
2531
+ nextRows.push({
2532
+ weights: this.fitGridRowWeights((_a = assignedRows[rowIndex]) === null || _a === void 0 ? void 0 : _a.weights, currentChildIds.length),
2533
+ childIds: currentChildIds,
2534
+ });
2535
+ }
2536
+ var cursor = prefixCount + currentChildIds.length;
2537
+ var trailingRowIndex = rowIndex + 1;
2538
+ while (cursor < allChildIds.length) {
2539
+ var sourceRow = assignedRows[Math.min(trailingRowIndex, assignedRows.length - 1)];
2540
+ var sourceCapacity = Math.max(1, (_b = sourceRow === null || sourceRow === void 0 ? void 0 : sourceRow.childIds.length) !== null && _b !== void 0 ? _b : 1);
2541
+ var rowChildIds = allChildIds.slice(cursor, cursor + sourceCapacity);
2542
+ nextRows.push({
2543
+ weights: this.fitGridRowWeights(sourceRow === null || sourceRow === void 0 ? void 0 : sourceRow.weights, rowChildIds.length),
2544
+ childIds: rowChildIds,
2545
+ });
2546
+ cursor += rowChildIds.length;
2547
+ trailingRowIndex += 1;
2548
+ }
2549
+ return nextRows;
2550
+ };
2551
+ AutoLayoutService.prototype.fitGridRowWeights = function (sourceWeights, childCount) {
2552
+ if (childCount <= 0)
2553
+ return [];
2554
+ var base = Array.isArray(sourceWeights) && sourceWeights.length > 0 ? sourceWeights : [1];
2555
+ var normalizedBase = this.normalizeRowWeightsToTwelveUnits(base);
2556
+ if (normalizedBase.length === childCount) {
2557
+ return normalizedBase;
2558
+ }
2559
+ if (normalizedBase.length > childCount) {
2560
+ return this.normalizeRowWeightsToTwelveUnits(normalizedBase.slice(0, childCount));
2561
+ }
2562
+ return this.buildEqualGridUnits(childCount);
2563
+ };
2564
+ AutoLayoutService.prototype.gridRowSnapshotsEqual = function (a, b) {
2565
+ var _a, _b, _c, _d;
2566
+ if (a.length !== b.length)
2567
+ return false;
2568
+ for (var index = 0; index < a.length; index += 1) {
2569
+ var left = (_b = (_a = a[index]) === null || _a === void 0 ? void 0 : _a.childIds) !== null && _b !== void 0 ? _b : [];
2570
+ var right = (_d = (_c = b[index]) === null || _c === void 0 ? void 0 : _c.childIds) !== null && _d !== void 0 ? _d : [];
2571
+ if (left.length !== right.length)
2572
+ return false;
2573
+ for (var childIndex = 0; childIndex < left.length; childIndex += 1) {
2574
+ if (left[childIndex] !== right[childIndex])
2575
+ return false;
2576
+ }
2577
+ }
2578
+ return true;
2579
+ };
2367
2580
  AutoLayoutService.prototype.normalizeGridTemplate = function (template) {
2581
+ var slots = this.normalizeGridSlotUnits(template);
2368
2582
  var rows = [];
2369
- (template !== null && template !== void 0 ? template : []).forEach(function (entry) {
2370
- if (typeof entry === 'number') {
2371
- var count = Math.max(0, Math.floor(entry));
2372
- if (count > 0) {
2373
- rows.push({ weights: Array.from({ length: count }, function () { return 1; }) });
2374
- }
2375
- return;
2376
- }
2377
- if (!Array.isArray(entry))
2378
- return;
2379
- if (entry.length === 1 && Number.isFinite(entry[0]) && entry[0] > 1) {
2380
- var count = Math.max(0, Math.floor(entry[0]));
2381
- if (count > 0) {
2382
- rows.push({ weights: Array.from({ length: count }, function () { return 1; }) });
2383
- }
2384
- return;
2385
- }
2386
- var weights = entry
2387
- .map(function (value) {
2388
- if (!Number.isFinite(value) || value <= 0)
2389
- return 1;
2390
- return value;
2391
- })
2392
- .filter(function (value) { return value > 0; });
2393
- if (weights.length > 0) {
2394
- rows.push({ weights: weights });
2583
+ var currentRow = [];
2584
+ var currentUnits = 0;
2585
+ slots.forEach(function (slotUnits) {
2586
+ if (currentRow.length > 0 && currentUnits + slotUnits > GRID_ROW_UNITS) {
2587
+ rows.push({ weights: __spreadArray([], currentRow, true) });
2588
+ currentRow = [];
2589
+ currentUnits = 0;
2395
2590
  }
2591
+ currentRow.push(slotUnits);
2592
+ currentUnits += slotUnits;
2396
2593
  });
2594
+ if (currentRow.length > 0) {
2595
+ rows.push({ weights: __spreadArray([], currentRow, true) });
2596
+ }
2397
2597
  if (rows.length === 0) {
2398
- rows.push({ weights: [1] });
2598
+ rows.push({ weights: [GRID_ROW_UNITS] });
2399
2599
  }
2400
2600
  return rows;
2401
2601
  };
2402
2602
  AutoLayoutService.prototype.assignChildrenToGridRows = function (childIds, templateRows) {
2403
- var _a;
2404
2603
  var assigned = [];
2405
2604
  var cursor = 0;
2406
2605
  templateRows.forEach(function (rowTemplate) {
@@ -2416,19 +2615,348 @@ var AutoLayoutService = /** @class */ (function () {
2416
2615
  cursor += rowChildren.length;
2417
2616
  });
2418
2617
  while (cursor < childIds.length) {
2419
- var remaining = childIds.length - cursor;
2420
- var lastRow = templateRows[templateRows.length - 1];
2421
- var fallbackColumns = (_a = lastRow === null || lastRow === void 0 ? void 0 : lastRow.weights.length) !== null && _a !== void 0 ? _a : 1;
2422
- var fallbackCount = Math.max(1, Math.min(remaining, fallbackColumns));
2423
- var rowChildren = childIds.slice(cursor, cursor + fallbackCount);
2618
+ var rowChildren = childIds.slice(cursor, cursor + 1);
2424
2619
  assigned.push({
2425
- weights: Array.from({ length: rowChildren.length }, function () { return 1; }),
2620
+ weights: [GRID_ROW_UNITS],
2426
2621
  childIds: rowChildren,
2427
2622
  });
2428
2623
  cursor += rowChildren.length;
2429
2624
  }
2430
2625
  return assigned;
2431
2626
  };
2627
+ AutoLayoutService.prototype.resolveGridChildSlotState = function (parentId, childId) {
2628
+ var _this = this;
2629
+ var _a;
2630
+ var parent = this.model.getElement(parentId);
2631
+ if (!parent || ((_a = parent.layout) === null || _a === void 0 ? void 0 : _a.mode) !== 'grid')
2632
+ return null;
2633
+ var assignedRows = this.resolveAssignedGridRows(parent);
2634
+ if (assignedRows.length === 0)
2635
+ return null;
2636
+ var orderedChildIds = assignedRows.flatMap(function (row) { return row.childIds; });
2637
+ var slotWeights = assignedRows.flatMap(function (row) { return row.weights.slice(0, row.childIds.length).map(function (weight) { return _this.clampGridUnits(weight); }); });
2638
+ var childFlatIndex = orderedChildIds.indexOf(childId);
2639
+ if (childFlatIndex < 0)
2640
+ return null;
2641
+ var rowIndex = assignedRows.findIndex(function (row) { return row.childIds.includes(childId); });
2642
+ if (rowIndex < 0)
2643
+ return null;
2644
+ return { assignedRows: assignedRows, orderedChildIds: orderedChildIds, slotWeights: slotWeights, childFlatIndex: childFlatIndex, rowIndex: rowIndex };
2645
+ };
2646
+ AutoLayoutService.prototype.packGridRowsFromSlots = function (childIds, slotWeights) {
2647
+ var _this = this;
2648
+ var rows = [];
2649
+ var currentChildIds = [];
2650
+ var currentWeights = [];
2651
+ var currentUnits = 0;
2652
+ childIds.forEach(function (childId, index) {
2653
+ var _a;
2654
+ var slotUnits = _this.clampGridUnits((_a = slotWeights[index]) !== null && _a !== void 0 ? _a : GRID_ROW_UNITS);
2655
+ if (currentChildIds.length > 0 && currentUnits + slotUnits > GRID_ROW_UNITS) {
2656
+ rows.push({ childIds: __spreadArray([], currentChildIds, true), weights: __spreadArray([], currentWeights, true) });
2657
+ currentChildIds = [];
2658
+ currentWeights = [];
2659
+ currentUnits = 0;
2660
+ }
2661
+ currentChildIds.push(childId);
2662
+ currentWeights.push(slotUnits);
2663
+ currentUnits += slotUnits;
2664
+ });
2665
+ if (currentChildIds.length > 0) {
2666
+ rows.push({ childIds: __spreadArray([], currentChildIds, true), weights: __spreadArray([], currentWeights, true) });
2667
+ }
2668
+ return rows;
2669
+ };
2670
+ AutoLayoutService.prototype.resolveRequestedGridUnits = function (parent, currentRow, requestedWidth) {
2671
+ var _a;
2672
+ if (!parent.layout || parent.layout.mode !== 'grid' || !currentRow)
2673
+ return GRID_ROW_UNITS;
2674
+ var rowUnitWidth = this.resolveGridRowUnitWidth(Math.max(0, parent.size.width - this.resolveLayoutPadding(parent.layout).x * 2), currentRow.childIds.length, (_a = parent.layout.gap) !== null && _a !== void 0 ? _a : 12);
2675
+ if (rowUnitWidth <= 0)
2676
+ return GRID_ROW_UNITS;
2677
+ return this.clampGridUnits(Math.round(requestedWidth / rowUnitWidth));
2678
+ };
2679
+ AutoLayoutService.prototype.sortGridChildrenForLayout = function (children) {
2680
+ return __spreadArray([], children, true).sort(function (a, b) {
2681
+ if (a.position.y === b.position.y) {
2682
+ if (a.position.x === b.position.x)
2683
+ return a.id.localeCompare(b.id);
2684
+ return a.position.x - b.position.x;
2685
+ }
2686
+ return a.position.y - b.position.y;
2687
+ });
2688
+ };
2689
+ AutoLayoutService.prototype.resolveRightwardGridOverflowChange = function (params) {
2690
+ var _a, _b;
2691
+ var assignedRows = params.assignedRows, rowIndex = params.rowIndex, childIndex = params.childIndex, requestedWidth = params.requestedWidth, availableWidth = params.availableWidth, gap = params.gap, parentLayout = params.parentLayout, parentId = params.parentId, childId = params.childId;
2692
+ var currentRow = assignedRows[rowIndex];
2693
+ if (!currentRow || childIndex < 0 || childIndex >= currentRow.childIds.length - 1)
2694
+ return null;
2695
+ var rowUnitWidth = this.resolveGridRowUnitWidth(availableWidth, currentRow.childIds.length, gap);
2696
+ if (rowUnitWidth <= 0)
2697
+ return null;
2698
+ var currentUnits = (_b = (_a = currentRow.weights[childIndex]) !== null && _a !== void 0 ? _a : this.normalizeRowWeightsToTwelveUnits(currentRow.weights)[childIndex]) !== null && _b !== void 0 ? _b : 1;
2699
+ var requestedUnits = this.clampLayoutSize(Math.round(requestedWidth / rowUnitWidth), currentUnits + 1, GRID_ROW_UNITS - 1);
2700
+ var leftWeights = currentRow.weights.slice(0, childIndex);
2701
+ var leftSum = leftWeights.reduce(function (sum, value) { return sum + value; }, 0);
2702
+ var rightWeights = currentRow.weights.slice(childIndex + 1);
2703
+ var rightIds = currentRow.childIds.slice(childIndex + 1);
2704
+ var followingRows = assignedRows.slice(rowIndex + 1);
2705
+ for (var retainCount = rightIds.length - 1; retainCount >= 0; retainCount -= 1) {
2706
+ var maxTargetUnits = GRID_ROW_UNITS - leftSum - retainCount;
2707
+ if (requestedUnits > maxTargetUnits)
2708
+ continue;
2709
+ var retainedIds = rightIds.slice(0, retainCount);
2710
+ var retainedWeights = rightWeights.slice(0, retainCount);
2711
+ var overflowIds = __spreadArray(__spreadArray([], rightIds.slice(retainCount), true), followingRows.flatMap(function (row) { return row.childIds; }), true);
2712
+ var remainingUnits = GRID_ROW_UNITS - leftSum - requestedUnits;
2713
+ var nextCurrentWeights = __spreadArray(__spreadArray(__spreadArray([], leftWeights, true), [
2714
+ requestedUnits
2715
+ ], false), this.distributeUnitsWithMinimums(remainingUnits, retainedWeights), true);
2716
+ var nextRows = assignedRows.slice(0, rowIndex).map(function (row) { return ({
2717
+ weights: __spreadArray([], row.weights, true),
2718
+ childIds: __spreadArray([], row.childIds, true),
2719
+ }); });
2720
+ nextRows.push({
2721
+ weights: nextCurrentWeights,
2722
+ childIds: __spreadArray(__spreadArray([], currentRow.childIds.slice(0, childIndex + 1), true), retainedIds, true),
2723
+ });
2724
+ var trailingRows = this.reflowTrailingGridRows(overflowIds, followingRows, currentRow.weights.length);
2725
+ var allRows = __spreadArray(__spreadArray([], nextRows, true), trailingRows, true);
2726
+ var beforeRows = assignedRows.map(function (row) { return ({ childIds: __spreadArray([], row.childIds, true) }); });
2727
+ var afterRows = allRows.map(function (row) { return ({ childIds: __spreadArray([], row.childIds, true) }); });
2728
+ if (this.gridRowSnapshotsEqual(beforeRows, afterRows) && this.gridWeightsEqual(assignedRows, allRows)) {
2729
+ return null;
2730
+ }
2731
+ return {
2732
+ parentId: parentId,
2733
+ triggerChildId: childId,
2734
+ reason: 'child-resize-redistribute',
2735
+ beforeRows: beforeRows,
2736
+ afterRows: afterRows,
2737
+ nextLayout: __assign(__assign({}, parentLayout), { gridTemplate: this.flattenAssignedRowsToGridTemplate(allRows) }),
2738
+ };
2739
+ }
2740
+ return null;
2741
+ };
2742
+ AutoLayoutService.prototype.resolveSameRowGridUnitResizeChange = function (params) {
2743
+ var _this = this;
2744
+ var _a, _b, _c, _d;
2745
+ var assignedRows = params.assignedRows, rowIndex = params.rowIndex, childIndex = params.childIndex, requestedWidth = params.requestedWidth, availableWidth = params.availableWidth, gap = params.gap, parentLayout = params.parentLayout, parentId = params.parentId, childId = params.childId;
2746
+ var currentRow = assignedRows[rowIndex];
2747
+ if (!currentRow || currentRow.childIds.length <= 1)
2748
+ return null;
2749
+ var donorIndices = Array.from({ length: Math.max(0, currentRow.childIds.length - childIndex - 1) }, function (_, index) { return childIndex + 1 + index; });
2750
+ if (donorIndices.length === 0)
2751
+ return null;
2752
+ var rowUnitWidth = this.resolveGridRowUnitWidth(availableWidth, currentRow.childIds.length, gap);
2753
+ if (rowUnitWidth <= 0)
2754
+ return null;
2755
+ var currentUnits = (_b = (_a = currentRow.weights[childIndex]) !== null && _a !== void 0 ? _a : this.normalizeRowWeightsToTwelveUnits(currentRow.weights)[childIndex]) !== null && _b !== void 0 ? _b : 1;
2756
+ var requestedUnits = this.clampLayoutSize(Math.round(requestedWidth / rowUnitWidth), 1, GRID_ROW_UNITS - 1);
2757
+ if (requestedUnits === currentUnits)
2758
+ return null;
2759
+ var nextWeights = __spreadArray([], currentRow.weights, true);
2760
+ var delta = requestedUnits - currentUnits;
2761
+ if (delta > 0) {
2762
+ for (var _i = 0, donorIndices_1 = donorIndices; _i < donorIndices_1.length; _i++) {
2763
+ var donorIndex = donorIndices_1[_i];
2764
+ if (delta <= 0)
2765
+ break;
2766
+ var donorCapacity = Math.max(0, ((_c = nextWeights[donorIndex]) !== null && _c !== void 0 ? _c : 1) - 1);
2767
+ var transfer = Math.min(delta, donorCapacity);
2768
+ nextWeights[donorIndex] -= transfer;
2769
+ nextWeights[childIndex] += transfer;
2770
+ delta -= transfer;
2771
+ }
2772
+ }
2773
+ else {
2774
+ var release = Math.abs(delta);
2775
+ for (var _e = 0, donorIndices_2 = donorIndices; _e < donorIndices_2.length; _e++) {
2776
+ var donorIndex = donorIndices_2[_e];
2777
+ if (release <= 0)
2778
+ break;
2779
+ var transfer = release;
2780
+ nextWeights[donorIndex] += transfer;
2781
+ nextWeights[childIndex] -= transfer;
2782
+ release -= transfer;
2783
+ }
2784
+ }
2785
+ if (delta > 0)
2786
+ return null;
2787
+ if (((_d = nextWeights[childIndex]) !== null && _d !== void 0 ? _d : currentUnits) === currentUnits)
2788
+ return null;
2789
+ var nextRows = assignedRows.map(function (row, index) {
2790
+ return index === rowIndex
2791
+ ? {
2792
+ weights: _this.normalizeRowWeightsToTwelveUnits(nextWeights),
2793
+ childIds: __spreadArray([], row.childIds, true),
2794
+ }
2795
+ : { weights: __spreadArray([], row.weights, true), childIds: __spreadArray([], row.childIds, true) };
2796
+ });
2797
+ return {
2798
+ parentId: parentId,
2799
+ triggerChildId: childId,
2800
+ reason: 'child-resize-redistribute',
2801
+ beforeRows: assignedRows.map(function (row) { return ({ childIds: __spreadArray([], row.childIds, true) }); }),
2802
+ afterRows: nextRows.map(function (row) { return ({ childIds: __spreadArray([], row.childIds, true) }); }),
2803
+ nextLayout: __assign(__assign({}, parentLayout), { gridTemplate: this.flattenAssignedRowsToGridTemplate(nextRows) }),
2804
+ };
2805
+ };
2806
+ AutoLayoutService.prototype.splitGridRowIntoTwelveUnits = function (assignedRows, rowIndex, childId, requestedWidth, availableWidth, gap) {
2807
+ var _a;
2808
+ var currentRow = assignedRows[rowIndex];
2809
+ var nextRow = assignedRows[rowIndex + 1];
2810
+ if (!currentRow || !nextRow || currentRow.childIds.length !== 1 || nextRow.childIds.length === 0) {
2811
+ return [];
2812
+ }
2813
+ var rowUnitWidth = this.resolveGridRowUnitWidth(availableWidth, 2, gap);
2814
+ var requestedUnits = this.clampLayoutSize(Math.round(requestedWidth / rowUnitWidth), 1, GRID_ROW_UNITS - 1);
2815
+ var nextRows = assignedRows.slice(0, rowIndex).map(function (row) { return ({
2816
+ weights: __spreadArray([], row.weights, true),
2817
+ childIds: __spreadArray([], row.childIds, true),
2818
+ }); });
2819
+ nextRows.push({
2820
+ weights: [requestedUnits, GRID_ROW_UNITS - requestedUnits],
2821
+ childIds: [childId, nextRow.childIds[0]],
2822
+ });
2823
+ var trailingRows = [];
2824
+ var remainingChildIds = __spreadArray(__spreadArray([], nextRow.childIds.slice(1), true), assignedRows.slice(rowIndex + 2).flatMap(function (row) { return row.childIds; }), true);
2825
+ var cursor = 0;
2826
+ var seedRows = assignedRows.slice(rowIndex + 1);
2827
+ var seedIndex = 0;
2828
+ while (cursor < remainingChildIds.length) {
2829
+ var seed = seedRows[Math.min(seedIndex, seedRows.length - 1)];
2830
+ var capacity = Math.max(1, (_a = seed === null || seed === void 0 ? void 0 : seed.childIds.length) !== null && _a !== void 0 ? _a : 1);
2831
+ var rowChildIds = remainingChildIds.slice(cursor, cursor + capacity);
2832
+ trailingRows.push({
2833
+ weights: this.fitGridRowWeights(seed === null || seed === void 0 ? void 0 : seed.weights, rowChildIds.length),
2834
+ childIds: rowChildIds,
2835
+ });
2836
+ cursor += rowChildIds.length;
2837
+ seedIndex += 1;
2838
+ }
2839
+ return __spreadArray(__spreadArray([], nextRows, true), trailingRows, true);
2840
+ };
2841
+ AutoLayoutService.prototype.reflowTrailingGridRows = function (childIds, seedRows, fallbackColumnCount) {
2842
+ var _a;
2843
+ if (childIds.length === 0)
2844
+ return [];
2845
+ var trailingRows = [];
2846
+ var cursor = 0;
2847
+ var seedIndex = 0;
2848
+ var normalizedSeedRows = seedRows.length > 0 ? seedRows : [{ weights: this.buildEqualGridUnits(Math.max(1, fallbackColumnCount)), childIds: [] }];
2849
+ while (cursor < childIds.length) {
2850
+ var seed = normalizedSeedRows[Math.min(seedIndex, normalizedSeedRows.length - 1)];
2851
+ var capacity = Math.max(1, (_a = seed === null || seed === void 0 ? void 0 : seed.weights.length) !== null && _a !== void 0 ? _a : fallbackColumnCount);
2852
+ var rowChildIds = childIds.slice(cursor, cursor + capacity);
2853
+ trailingRows.push({
2854
+ weights: this.fitGridRowWeights(seed === null || seed === void 0 ? void 0 : seed.weights, rowChildIds.length),
2855
+ childIds: rowChildIds,
2856
+ });
2857
+ cursor += rowChildIds.length;
2858
+ seedIndex += 1;
2859
+ }
2860
+ return trailingRows;
2861
+ };
2862
+ AutoLayoutService.prototype.flattenAssignedRowsToGridTemplate = function (rows) {
2863
+ var _this = this;
2864
+ return rows.flatMap(function (row) { return _this.normalizeRowWeightsToTwelveUnits(row.weights.slice(0, row.childIds.length)); });
2865
+ };
2866
+ AutoLayoutService.prototype.buildEqualGridUnits = function (count) {
2867
+ if (count <= 0)
2868
+ return [];
2869
+ var base = Math.floor(GRID_ROW_UNITS / count);
2870
+ var remainder = GRID_ROW_UNITS - base * count;
2871
+ return Array.from({ length: count }, function (_, index) { return base + (index < remainder ? 1 : 0); });
2872
+ };
2873
+ AutoLayoutService.prototype.normalizeRowWeightsToTwelveUnits = function (weights) {
2874
+ var safeWeights = weights.map(function (weight) { return (Number.isFinite(weight) && weight > 0 ? weight : 1); });
2875
+ if (safeWeights.length === 0)
2876
+ return [GRID_ROW_UNITS];
2877
+ if (safeWeights.length === 1)
2878
+ return [GRID_ROW_UNITS];
2879
+ var distributed = this.distributeWeightedSizes(GRID_ROW_UNITS, safeWeights);
2880
+ var normalized = distributed.map(function (value) { return Math.max(1, value); });
2881
+ var total = normalized.reduce(function (sum, value) { return sum + value; }, 0);
2882
+ if (total === GRID_ROW_UNITS)
2883
+ return normalized;
2884
+ if (total < GRID_ROW_UNITS) {
2885
+ var cursor_1 = 0;
2886
+ while (total < GRID_ROW_UNITS) {
2887
+ normalized[cursor_1 % normalized.length] += 1;
2888
+ total += 1;
2889
+ cursor_1 += 1;
2890
+ }
2891
+ return normalized;
2892
+ }
2893
+ var cursor = normalized.length - 1;
2894
+ while (total > GRID_ROW_UNITS && cursor >= 0) {
2895
+ if (normalized[cursor] > 1) {
2896
+ normalized[cursor] -= 1;
2897
+ total -= 1;
2898
+ }
2899
+ else {
2900
+ cursor -= 1;
2901
+ }
2902
+ if (cursor < 0 && total > GRID_ROW_UNITS) {
2903
+ cursor = normalized.length - 1;
2904
+ }
2905
+ }
2906
+ return normalized;
2907
+ };
2908
+ AutoLayoutService.prototype.normalizeGridSlotUnits = function (template) {
2909
+ var _this = this;
2910
+ var slots = [];
2911
+ (template !== null && template !== void 0 ? template : []).forEach(function (entry) {
2912
+ if (typeof entry === 'number') {
2913
+ if (Number.isFinite(entry) && entry > 0) {
2914
+ slots.push(_this.clampGridUnits(entry));
2915
+ }
2916
+ return;
2917
+ }
2918
+ if (!Array.isArray(entry))
2919
+ return;
2920
+ entry.forEach(function (value) {
2921
+ if (Number.isFinite(value) && value > 0) {
2922
+ slots.push(_this.clampGridUnits(value));
2923
+ }
2924
+ });
2925
+ });
2926
+ return slots;
2927
+ };
2928
+ AutoLayoutService.prototype.clampGridUnits = function (value) {
2929
+ return Math.max(1, Math.min(GRID_ROW_UNITS, Math.round(value)));
2930
+ };
2931
+ AutoLayoutService.prototype.distributeUnitsWithMinimums = function (totalUnits, seedWeights) {
2932
+ if (seedWeights.length === 0)
2933
+ return [];
2934
+ var minimumTotal = seedWeights.length;
2935
+ var clampedTotal = Math.max(minimumTotal, totalUnits);
2936
+ var distributed = this.distributeWeightedSizes(clampedTotal - minimumTotal, seedWeights).map(function (value) { return value + 1; });
2937
+ return distributed;
2938
+ };
2939
+ AutoLayoutService.prototype.gridWeightsEqual = function (leftRows, rightRows) {
2940
+ var _a, _b, _c, _d;
2941
+ if (leftRows.length !== rightRows.length)
2942
+ return false;
2943
+ for (var rowIndex = 0; rowIndex < leftRows.length; rowIndex += 1) {
2944
+ var left = (_b = (_a = leftRows[rowIndex]) === null || _a === void 0 ? void 0 : _a.weights) !== null && _b !== void 0 ? _b : [];
2945
+ var right = (_d = (_c = rightRows[rowIndex]) === null || _c === void 0 ? void 0 : _c.weights) !== null && _d !== void 0 ? _d : [];
2946
+ if (left.length !== right.length)
2947
+ return false;
2948
+ for (var index = 0; index < left.length; index += 1) {
2949
+ if (left[index] !== right[index])
2950
+ return false;
2951
+ }
2952
+ }
2953
+ return true;
2954
+ };
2955
+ AutoLayoutService.prototype.resolveGridRowUnitWidth = function (availableWidth, childCount, gap) {
2956
+ var totalGap = Math.max(0, childCount - 1) * gap;
2957
+ var rowInnerWidth = Math.max(0, availableWidth - totalGap);
2958
+ return rowInnerWidth / GRID_ROW_UNITS;
2959
+ };
2432
2960
  AutoLayoutService.prototype.resolveLabelReservedTopLane = function (parentId, layout) {
2433
2961
  var _a, _b, _c, _d;
2434
2962
  var policy = layout === null || layout === void 0 ? void 0 : layout.labelReservedSpace;
@@ -2963,11 +3491,18 @@ var LinkRoutingService = /** @class */ (function () {
2963
3491
  var onEdge = (shape === null || shape === void 0 ? void 0 : shape.projectToBorder)
2964
3492
  ? this.isPointOnProjectedBorder(point, rect, shape.projectToBorder, EDGE_TOLERANCE)
2965
3493
  : this.isPointOnRectEdge(point, rect, EDGE_TOLERANCE);
2966
- var onEdgeSide = onEdge ? (_b = (_a = shape === null || shape === void 0 ? void 0 : shape.resolveBorderSide) === null || _a === void 0 ? void 0 : _a.call(shape, point, rect)) !== null && _b !== void 0 ? _b : this.resolveEdgeSide(point, rect) : undefined;
3494
+ var projectedBorderPoint = (shape === null || shape === void 0 ? void 0 : shape.projectToBorder)
3495
+ ? shape.projectToBorder(point, rect)
3496
+ : projectPointToBoundary(point, rect, 'rect');
3497
+ var projectedBoundaryEquivalent = !onEdge && !this.isPointInsideRect(point, rect, EDGE_TOLERANCE);
3498
+ var sidePoint = projectedBoundaryEquivalent ? projectedBorderPoint : point;
3499
+ var onEdgeSide = onEdge || projectedBoundaryEquivalent
3500
+ ? (_b = (_a = shape === null || shape === void 0 ? void 0 : shape.resolveBorderSide) === null || _a === void 0 ? void 0 : _a.call(shape, sidePoint, rect)) !== null && _b !== void 0 ? _b : this.resolveEdgeSide(sidePoint, rect)
3501
+ : undefined;
2967
3502
  if (onEdgeSide && oppositePoint) {
2968
3503
  onEdgeSide = this.resolveCornerTieBreakSide(point, rect, oppositePoint, onEdgeSide);
2969
3504
  }
2970
- return { onEdge: onEdge, onEdgeSide: onEdgeSide };
3505
+ return { onEdge: onEdge || projectedBoundaryEquivalent, onEdgeSide: onEdgeSide };
2971
3506
  };
2972
3507
  LinkRoutingService.prototype.resolveCornerTieBreakSide = function (point, rect, oppositePoint, currentSide) {
2973
3508
  var onLeft = Math.abs(point.x - rect.x) <= EDGE_TOLERANCE;
@@ -3129,6 +3664,12 @@ var LinkRoutingService = /** @class */ (function () {
3129
3664
  var onBottom = Math.abs(point.y - (rect.y + rect.height)) <= tolerance;
3130
3665
  return onLeft || onRight || onTop || onBottom;
3131
3666
  };
3667
+ LinkRoutingService.prototype.isPointInsideRect = function (point, rect, tolerance) {
3668
+ return (point.x >= rect.x - tolerance &&
3669
+ point.x <= rect.x + rect.width + tolerance &&
3670
+ point.y >= rect.y - tolerance &&
3671
+ point.y <= rect.y + rect.height + tolerance);
3672
+ };
3132
3673
  LinkRoutingService.prototype.resolveEdgeSide = function (point, rect) {
3133
3674
  var distances = [
3134
3675
  { side: 'left', value: Math.abs(point.x - rect.x) },
@@ -3173,8 +3714,8 @@ var MutationPipeline = /** @class */ (function () {
3173
3714
  }());
3174
3715
 
3175
3716
  var clampToRect = function (point, rect) { return ({
3176
- x: clamp$1(point.x, rect.x, rect.x + rect.width),
3177
- y: clamp$1(point.y, rect.y, rect.y + rect.height),
3717
+ x: clamp$2(point.x, rect.x, rect.x + rect.width),
3718
+ y: clamp$2(point.y, rect.y, rect.y + rect.height),
3178
3719
  }); };
3179
3720
  var snapToRectBorder = function (point, rect) {
3180
3721
  var clamped = clampToRect(point, rect);
@@ -3311,9 +3852,7 @@ var DiagramEngine = /** @class */ (function () {
3311
3852
  });
3312
3853
  this.emitChange(allPatches);
3313
3854
  };
3314
- DiagramEngine.prototype.moveElementTo = function (id, x, y) {
3315
- var _this = this;
3316
- var oldWorld = this.model.getElementWorldPosition(id);
3855
+ DiagramEngine.prototype.resolveElementMovePosition = function (id, x, y) {
3317
3856
  var snapped = this.snapper.snap({ x: x, y: y });
3318
3857
  var element = this.model.getElement(id);
3319
3858
  var position = snapped;
@@ -3353,14 +3892,46 @@ var DiagramEngine = /** @class */ (function () {
3353
3892
  }
3354
3893
  }
3355
3894
  }
3895
+ return { element: element === null || element === void 0 ? void 0 : element.toData(), position: position };
3896
+ };
3897
+ DiagramEngine.prototype.buildChildMutationLayoutSteps = function (parentId, options) {
3898
+ var _this = this;
3899
+ var _a;
3900
+ if (!parentId) {
3901
+ return [function () { return _this.applyLayoutCascade(null); }];
3902
+ }
3903
+ var parent = this.model.getElement(parentId);
3904
+ if ((options === null || options === void 0 ? void 0 : options.preserveGridParentWidth) && ((_a = parent === null || parent === void 0 ? void 0 : parent.layout) === null || _a === void 0 ? void 0 : _a.mode) === 'grid') {
3905
+ return [
3906
+ function () { return _this.applyLayoutForParent(parentId, { preserveParentWidth: true }); },
3907
+ function () { var _a; return _this.applyLayoutCascade((_a = parent.parentId) !== null && _a !== void 0 ? _a : null); },
3908
+ ];
3909
+ }
3910
+ return [function () { return _this.applyLayoutCascade(parentId); }];
3911
+ };
3912
+ DiagramEngine.prototype.moveElementTo = function (id, x, y) {
3913
+ var _a;
3914
+ var oldWorld = this.model.getElementWorldPosition(id);
3915
+ var _b = this.resolveElementMovePosition(id, x, y), element = _b.element, position = _b.position;
3916
+ var beforeGridSlotState = (element === null || element === void 0 ? void 0 : element.parentId)
3917
+ ? this.autoLayoutService.describeGridSlotState(element.parentId)
3918
+ : null;
3356
3919
  var patches = this.commandQueue.run(createMoveElementCommand(id, position), this.model);
3920
+ if ((element === null || element === void 0 ? void 0 : element.parentId) && beforeGridSlotState) {
3921
+ var reorderedLayout = this.autoLayoutService.resolveGridReorderedLayout(element.parentId, beforeGridSlotState);
3922
+ if (reorderedLayout) {
3923
+ patches.push.apply(patches, this.commandQueue.run(createSetElementLayoutCommand(element.parentId, reorderedLayout), this.model));
3924
+ }
3925
+ }
3357
3926
  var movedPortIds = [];
3358
3927
  if (element) {
3359
3928
  movedPortIds.push.apply(movedPortIds, this.collectElementPortIds(element.id));
3360
3929
  }
3361
3930
  var allPatches = this.mutationPipeline.run({
3362
3931
  basePatches: patches,
3363
- layoutSteps: [function () { var _a; return _this.applyLayoutCascade((_a = element === null || element === void 0 ? void 0 : element.parentId) !== null && _a !== void 0 ? _a : null); }],
3932
+ layoutSteps: this.buildChildMutationLayoutSteps((_a = element === null || element === void 0 ? void 0 : element.parentId) !== null && _a !== void 0 ? _a : null, {
3933
+ preserveGridParentWidth: true,
3934
+ }),
3364
3935
  movedPortIds: movedPortIds,
3365
3936
  });
3366
3937
  this.emitChange(allPatches);
@@ -3377,24 +3948,106 @@ var DiagramEngine = /** @class */ (function () {
3377
3948
  }
3378
3949
  }
3379
3950
  };
3951
+ DiagramEngine.prototype.resizeElementBounds = function (id, bounds) {
3952
+ var _this = this;
3953
+ var _a, _b, _c, _d;
3954
+ var _e = this.resolveElementMovePosition(id, bounds.x, bounds.y), element = _e.element, position = _e.position;
3955
+ if (!element)
3956
+ return;
3957
+ var oldSize = __assign({}, element.size);
3958
+ var preserveParentSize = Boolean(element.layout && element.layout.mode !== 'manual');
3959
+ var gridLayoutChange = this.resolveGridChildWidthResizeTopologyChange(element, bounds.width);
3960
+ var patches = [];
3961
+ if (gridLayoutChange) {
3962
+ patches.push.apply(patches, this.commandQueue.run(createSetElementLayoutCommand(gridLayoutChange.parentId, gridLayoutChange.afterLayout), this.model));
3963
+ }
3964
+ var positionChanged = element.position.x !== position.x || element.position.y !== position.y;
3965
+ if (positionChanged) {
3966
+ patches.push.apply(patches, this.commandQueue.run(createMoveElementCommand(id, position), this.model));
3967
+ }
3968
+ patches.push.apply(patches, this.commandQueue.run(createResizeElementCommand(id, { width: bounds.width, height: bounds.height }), this.model));
3969
+ var movedPortIds = this.collectElementPortIds(element.id);
3970
+ var layoutSteps = [function () { return _this.applyLayoutForParent(id, { preserveParentSize: preserveParentSize }); }];
3971
+ if (gridLayoutChange && element.parentId) {
3972
+ var grandParentId_1 = (_b = (_a = this.model.getElement(element.parentId)) === null || _a === void 0 ? void 0 : _a.parentId) !== null && _b !== void 0 ? _b : null;
3973
+ layoutSteps.push(function () { var _a; return _this.applyLayoutForParent((_a = element.parentId) !== null && _a !== void 0 ? _a : '', { preserveParentWidth: true }); });
3974
+ if (grandParentId_1) {
3975
+ layoutSteps.push(function () { return _this.applyLayoutCascade(grandParentId_1); });
3976
+ }
3977
+ }
3978
+ else {
3979
+ layoutSteps.push.apply(layoutSteps, this.buildChildMutationLayoutSteps((_c = element.parentId) !== null && _c !== void 0 ? _c : null, {
3980
+ preserveGridParentWidth: true,
3981
+ }));
3982
+ }
3983
+ var allPatches = this.mutationPipeline.run({
3984
+ basePatches: patches,
3985
+ layoutSteps: layoutSteps,
3986
+ movedPortIds: movedPortIds,
3987
+ });
3988
+ if (gridLayoutChange) {
3989
+ this.events.emit('gridLayoutChanged', {
3990
+ parentId: gridLayoutChange.parentId,
3991
+ triggerChildId: gridLayoutChange.triggerChildId,
3992
+ reason: gridLayoutChange.reason,
3993
+ beforeRows: gridLayoutChange.beforeRows,
3994
+ afterRows: gridLayoutChange.afterRows,
3995
+ beforeGridTemplate: gridLayoutChange.beforeGridTemplate,
3996
+ afterGridTemplate: gridLayoutChange.afterGridTemplate,
3997
+ });
3998
+ }
3999
+ this.emitChange(allPatches);
4000
+ var newSize = (_d = this.model.getElement(id)) === null || _d === void 0 ? void 0 : _d.size;
4001
+ if (newSize && (oldSize.width !== newSize.width || oldSize.height !== newSize.height)) {
4002
+ this.events.emit('elementResized', { elementId: id, oldSize: oldSize, newSize: __assign({}, newSize) });
4003
+ }
4004
+ };
3380
4005
  DiagramEngine.prototype.resizeElement = function (id, width, height) {
3381
4006
  var _this = this;
3382
- var _a;
4007
+ var _a, _b, _c, _d;
3383
4008
  var element = this.model.getElement(id);
3384
4009
  var oldSize = element === null || element === void 0 ? void 0 : element.size;
3385
4010
  var preserveParentSize = Boolean((element === null || element === void 0 ? void 0 : element.layout) && element.layout.mode !== 'manual');
3386
- var patches = this.commandQueue.run(createResizeElementCommand(id, { width: width, height: height }), this.model);
4011
+ var gridLayoutChange = element
4012
+ ? this.resolveGridChildWidthResizeTopologyChange(element, width)
4013
+ : null;
4014
+ var patches = [];
4015
+ if (gridLayoutChange) {
4016
+ patches.push.apply(patches, this.commandQueue.run(createSetElementLayoutCommand(gridLayoutChange.parentId, gridLayoutChange.afterLayout), this.model));
4017
+ }
4018
+ patches.push.apply(patches, this.commandQueue.run(createResizeElementCommand(id, { width: width, height: height }), this.model));
3387
4019
  var movedPortIds = element ? this.collectElementPortIds(element.id) : [];
4020
+ var layoutSteps = [function () { return _this.applyLayoutForParent(id, { preserveParentSize: preserveParentSize }); }];
4021
+ if (gridLayoutChange && (element === null || element === void 0 ? void 0 : element.parentId)) {
4022
+ var grandParentId_2 = (_b = (_a = this.model.getElement(element.parentId)) === null || _a === void 0 ? void 0 : _a.parentId) !== null && _b !== void 0 ? _b : null;
4023
+ layoutSteps.push(function () { var _a; return _this.applyLayoutForParent((_a = element.parentId) !== null && _a !== void 0 ? _a : '', { preserveParentWidth: true }); });
4024
+ if (grandParentId_2) {
4025
+ layoutSteps.push(function () { return _this.applyLayoutCascade(grandParentId_2); });
4026
+ }
4027
+ }
4028
+ else {
4029
+ layoutSteps.push.apply(layoutSteps, this.buildChildMutationLayoutSteps((_c = element === null || element === void 0 ? void 0 : element.parentId) !== null && _c !== void 0 ? _c : null, {
4030
+ preserveGridParentWidth: true,
4031
+ }));
4032
+ }
3388
4033
  var allPatches = this.mutationPipeline.run({
3389
4034
  basePatches: patches,
3390
- layoutSteps: [
3391
- function () { return _this.applyLayoutForParent(id, { preserveParentSize: preserveParentSize }); },
3392
- function () { var _a; return _this.applyLayoutCascade((_a = element === null || element === void 0 ? void 0 : element.parentId) !== null && _a !== void 0 ? _a : null); },
3393
- ],
4035
+ layoutSteps: layoutSteps,
3394
4036
  movedPortIds: movedPortIds,
3395
4037
  });
4038
+ if (gridLayoutChange) {
4039
+ this.events.emit('gridLayoutChanged', {
4040
+ parentId: gridLayoutChange.parentId,
4041
+ triggerChildId: gridLayoutChange.triggerChildId,
4042
+ reason: gridLayoutChange.reason,
4043
+ beforeRows: gridLayoutChange.beforeRows,
4044
+ afterRows: gridLayoutChange.afterRows,
4045
+ beforeGridTemplate: gridLayoutChange.beforeGridTemplate,
4046
+ afterGridTemplate: gridLayoutChange.afterGridTemplate,
4047
+ });
4048
+ }
3396
4049
  this.emitChange(allPatches);
3397
- var newSize = (_a = this.model.getElement(id)) === null || _a === void 0 ? void 0 : _a.size;
4050
+ var newSize = (_d = this.model.getElement(id)) === null || _d === void 0 ? void 0 : _d.size;
3398
4051
  if (oldSize && newSize && (oldSize.width !== newSize.width || oldSize.height !== newSize.height)) {
3399
4052
  this.events.emit('elementResized', { elementId: id, oldSize: __assign({}, oldSize), newSize: __assign({}, newSize) });
3400
4053
  }
@@ -3699,6 +4352,7 @@ var DiagramEngine = /** @class */ (function () {
3699
4352
  return baseRotation + instanceRotation;
3700
4353
  };
3701
4354
  DiagramEngine.prototype.normalizeElementResize = function (id, proposal, handle, minSize) {
4355
+ var _a, _b;
3702
4356
  if (minSize === void 0) { minSize = 20; }
3703
4357
  var clamped = {
3704
4358
  x: proposal.x,
@@ -3710,13 +4364,19 @@ var DiagramEngine = /** @class */ (function () {
3710
4364
  if (!element)
3711
4365
  return clamped;
3712
4366
  var lockHorizontalResize = this.shouldLockAutoLayoutChildHorizontalResize(element);
4367
+ var currentWorldPosition = this.model.getElementWorldPosition(id);
4368
+ var anchorGridWidthToLeft = this.shouldAnchorAutoLayoutGridChildWidthToLeft(element);
4369
+ var freezeGridLeftEdge = anchorGridWidthToLeft || lockHorizontalResize;
4370
+ var snappedGridWidth = this.resolveSnappedGridChildWidth(element, clamped.width);
3713
4371
  var shape = this.shapeRegistry.get(element.shapeId);
3714
4372
  if (!(shape === null || shape === void 0 ? void 0 : shape.normalizeResize)) {
3715
- if (!lockHorizontalResize)
4373
+ if (!lockHorizontalResize && !anchorGridWidthToLeft)
3716
4374
  return clamped;
3717
- var width_1 = Math.max(minSize, element.size.width);
3718
- var x_1 = clamped.x;
3719
- if (handle === 'sw' || handle === 'nw') {
4375
+ var width_1 = lockHorizontalResize
4376
+ ? Math.max(minSize, element.size.width)
4377
+ : Math.max(minSize, snappedGridWidth);
4378
+ var x_1 = freezeGridLeftEdge ? ((_a = currentWorldPosition === null || currentWorldPosition === void 0 ? void 0 : currentWorldPosition.x) !== null && _a !== void 0 ? _a : clamped.x) : clamped.x;
4379
+ if (!freezeGridLeftEdge && (handle === 'sw' || handle === 'nw')) {
3720
4380
  x_1 += clamped.width - width_1;
3721
4381
  }
3722
4382
  return { x: x_1, y: clamped.y, width: width_1, height: clamped.height };
@@ -3727,11 +4387,15 @@ var DiagramEngine = /** @class */ (function () {
3727
4387
  minSize: minSize,
3728
4388
  proposal: { width: clamped.width, height: clamped.height },
3729
4389
  });
3730
- var width = lockHorizontalResize ? Math.max(minSize, element.size.width) : Math.max(minSize, normalized.width);
4390
+ var width = lockHorizontalResize
4391
+ ? Math.max(minSize, element.size.width)
4392
+ : anchorGridWidthToLeft
4393
+ ? Math.max(minSize, snappedGridWidth)
4394
+ : Math.max(minSize, normalized.width);
3731
4395
  var height = Math.max(minSize, normalized.height);
3732
- var x = clamped.x;
4396
+ var x = freezeGridLeftEdge ? ((_b = currentWorldPosition === null || currentWorldPosition === void 0 ? void 0 : currentWorldPosition.x) !== null && _b !== void 0 ? _b : clamped.x) : clamped.x;
3733
4397
  var y = clamped.y;
3734
- if (handle === 'sw' || handle === 'nw') {
4398
+ if (!freezeGridLeftEdge && (handle === 'sw' || handle === 'nw')) {
3735
4399
  x += clamped.width - width;
3736
4400
  }
3737
4401
  if (handle === 'ne' || handle === 'nw') {
@@ -3749,7 +4413,64 @@ var DiagramEngine = /** @class */ (function () {
3749
4413
  if (element.layout && element.layout.mode !== 'manual')
3750
4414
  return false;
3751
4415
  var parent = this.model.getElement(parentId);
3752
- return ((_b = parent === null || parent === void 0 ? void 0 : parent.layout) === null || _b === void 0 ? void 0 : _b.mode) === 'grid';
4416
+ if (((_b = parent === null || parent === void 0 ? void 0 : parent.layout) === null || _b === void 0 ? void 0 : _b.mode) !== 'grid')
4417
+ return false;
4418
+ return !Boolean(parent.layout.gridChildWidthResizeEnabled);
4419
+ };
4420
+ DiagramEngine.prototype.shouldAnchorAutoLayoutGridChildWidthToLeft = function (element) {
4421
+ var _a, _b;
4422
+ var parentId = (_a = element.parentId) !== null && _a !== void 0 ? _a : null;
4423
+ if (!parentId)
4424
+ return false;
4425
+ if (this.model.getChildren(element.id).length > 0)
4426
+ return false;
4427
+ if (element.layout && element.layout.mode !== 'manual')
4428
+ return false;
4429
+ var parent = this.model.getElement(parentId);
4430
+ return Boolean(((_b = parent === null || parent === void 0 ? void 0 : parent.layout) === null || _b === void 0 ? void 0 : _b.mode) === 'grid' && parent.layout.gridChildWidthResizeEnabled);
4431
+ };
4432
+ DiagramEngine.prototype.shouldRestrictAutoLayoutChildRightGrowth = function (element, handle, requestedWidth) {
4433
+ return false;
4434
+ };
4435
+ DiagramEngine.prototype.resolveSnappedGridChildWidth = function (element, requestedWidth) {
4436
+ var _a;
4437
+ var parentId = (_a = element.parentId) !== null && _a !== void 0 ? _a : null;
4438
+ if (!parentId)
4439
+ return requestedWidth;
4440
+ if (this.model.getChildren(element.id).length > 0)
4441
+ return requestedWidth;
4442
+ if (element.layout && element.layout.mode !== 'manual')
4443
+ return requestedWidth;
4444
+ var parent = this.model.getElement(parentId);
4445
+ if (!(parent === null || parent === void 0 ? void 0 : parent.layout) || parent.layout.mode !== 'grid' || !parent.layout.gridChildWidthResizeEnabled)
4446
+ return requestedWidth;
4447
+ return this.autoLayoutService.snapGridChildRequestedWidth(parentId, element.id, requestedWidth);
4448
+ };
4449
+ DiagramEngine.prototype.resolveGridChildWidthResizeTopologyChange = function (element, requestedWidth) {
4450
+ var _a, _b, _c;
4451
+ var parentId = (_a = element.parentId) !== null && _a !== void 0 ? _a : null;
4452
+ if (!parentId)
4453
+ return null;
4454
+ if (this.model.getChildren(element.id).length > 0)
4455
+ return null;
4456
+ if (element.layout && element.layout.mode !== 'manual')
4457
+ return null;
4458
+ var parent = this.model.getElement(parentId);
4459
+ if (!parent || ((_b = parent.layout) === null || _b === void 0 ? void 0 : _b.mode) !== 'grid' || !parent.layout.gridChildWidthResizeEnabled)
4460
+ return null;
4461
+ var topologyChange = this.autoLayoutService.resolveGridChildResizeTopologyChange(parentId, element.id, requestedWidth);
4462
+ if (!topologyChange)
4463
+ return null;
4464
+ return {
4465
+ parentId: topologyChange.parentId,
4466
+ triggerChildId: topologyChange.triggerChildId,
4467
+ reason: topologyChange.reason,
4468
+ beforeRows: topologyChange.beforeRows,
4469
+ afterRows: topologyChange.afterRows,
4470
+ beforeGridTemplate: parent.layout.gridTemplate ? __spreadArray([], parent.layout.gridTemplate, true) : undefined,
4471
+ afterGridTemplate: __spreadArray([], ((_c = topologyChange.nextLayout.gridTemplate) !== null && _c !== void 0 ? _c : []), true),
4472
+ afterLayout: topologyChange.nextLayout,
4473
+ };
3753
4474
  };
3754
4475
  DiagramEngine.prototype.getPortWorldPosition = function (id) {
3755
4476
  return this.model.getPortWorldPosition(id);
@@ -4032,8 +4753,8 @@ var DiagramEngine = /** @class */ (function () {
4032
4753
  }
4033
4754
  if (style === null || style === void 0 ? void 0 : style.moveBounds) {
4034
4755
  var bounds = style.moveBounds;
4035
- position.x = clamp$1(position.x, bounds.x, bounds.x + bounds.width);
4036
- position.y = clamp$1(position.y, bounds.y, bounds.y + bounds.height);
4756
+ position.x = clamp$2(position.x, bounds.x, bounds.x + bounds.width);
4757
+ position.y = clamp$2(position.y, bounds.y, bounds.y + bounds.height);
4037
4758
  }
4038
4759
  if (effectiveMoveMode === 'border') {
4039
4760
  var resolvedBorder = this.resolveBorderPositionWithLimits(position, element, (_e = element.portMovement) === null || _e === void 0 ? void 0 : _e.positionLimits);
@@ -5954,6 +6675,10 @@ var KonvaInteraction = /** @class */ (function () {
5954
6675
  return;
5955
6676
  }
5956
6677
  if (hit.type === 'text') {
6678
+ if (!_this.isTextMovable(hit.id)) {
6679
+ _this.setCursor('default');
6680
+ return;
6681
+ }
5957
6682
  var base = _this.engine.getTextWorldPosition(hit.id);
5958
6683
  if (base) {
5959
6684
  _this.dragState = {
@@ -6343,6 +7068,8 @@ var KonvaInteraction = /** @class */ (function () {
6343
7068
  var hit = _this.resolveHit(point);
6344
7069
  if (!hit || hit.type !== 'text')
6345
7070
  return;
7071
+ if (!_this.isTextEditable(hit.id))
7072
+ return;
6346
7073
  _this.startTextEdit(hit.id);
6347
7074
  };
6348
7075
  this.stage.on('dblclick dbltap', doubleClick);
@@ -7495,8 +8222,12 @@ var KonvaInteraction = /** @class */ (function () {
7495
8222
  y: normalized.y + normalized.height / 2,
7496
8223
  }
7497
8224
  : { x: normalized.x, y: normalized.y };
7498
- this.engine.moveElementTo(state.elementId, moveTarget.x, moveTarget.y);
7499
- this.engine.resizeElement(state.elementId, normalized.width, normalized.height);
8225
+ this.engine.resizeElementBounds(state.elementId, {
8226
+ x: moveTarget.x,
8227
+ y: moveTarget.y,
8228
+ width: normalized.width,
8229
+ height: normalized.height,
8230
+ });
7500
8231
  this.updateSelectionOverlays();
7501
8232
  };
7502
8233
  KonvaInteraction.prototype.parseResizeHandle = function (id) {
@@ -7832,6 +8563,14 @@ var KonvaInteraction = /** @class */ (function () {
7832
8563
  KonvaInteraction.prototype.getTextById = function (id) {
7833
8564
  return this.engine.getState().texts.find(function (text) { return text.id === id; });
7834
8565
  };
8566
+ KonvaInteraction.prototype.isTextMovable = function (id) {
8567
+ var _a, _b;
8568
+ return ((_b = (_a = this.getTextById(id)) === null || _a === void 0 ? void 0 : _a.interaction) === null || _b === void 0 ? void 0 : _b.movable) !== false;
8569
+ };
8570
+ KonvaInteraction.prototype.isTextEditable = function (id) {
8571
+ var _a, _b;
8572
+ return ((_b = (_a = this.getTextById(id)) === null || _a === void 0 ? void 0 : _a.interaction) === null || _b === void 0 ? void 0 : _b.editable) !== false;
8573
+ };
7835
8574
  KonvaInteraction.prototype.getLinkById = function (id) {
7836
8575
  return this.engine.getState().links.find(function (link) { return link.id === id; });
7837
8576
  };
@@ -8514,6 +9253,24 @@ var hasBounds = function (bounds) {
8514
9253
  bounds.maxX >= bounds.minX &&
8515
9254
  bounds.maxY >= bounds.minY;
8516
9255
  };
9256
+ var DEFAULT_VIEWPORT_FIT_PADDING = 24;
9257
+ var DEFAULT_VIEWPORT_FIT_MIN_ZOOM = 0.2;
9258
+ var DEFAULT_VIEWPORT_FIT_MAX_ZOOM = 3;
9259
+ var resolveViewportFitOptions = function (options) {
9260
+ var padding = typeof (options === null || options === void 0 ? void 0 : options.padding) === 'number' && Number.isFinite(options.padding)
9261
+ ? Math.max(0, options.padding)
9262
+ : DEFAULT_VIEWPORT_FIT_PADDING;
9263
+ var requestedMinZoom = typeof (options === null || options === void 0 ? void 0 : options.minZoom) === 'number' && Number.isFinite(options.minZoom)
9264
+ ? Math.max(0.000001, options.minZoom)
9265
+ : DEFAULT_VIEWPORT_FIT_MIN_ZOOM;
9266
+ var requestedMaxZoom = typeof (options === null || options === void 0 ? void 0 : options.maxZoom) === 'number' && Number.isFinite(options.maxZoom)
9267
+ ? Math.max(0.000001, options.maxZoom)
9268
+ : DEFAULT_VIEWPORT_FIT_MAX_ZOOM;
9269
+ var minZoom = Math.min(requestedMinZoom, requestedMaxZoom);
9270
+ var maxZoom = Math.max(requestedMinZoom, requestedMaxZoom);
9271
+ return { padding: padding, minZoom: minZoom, maxZoom: maxZoom };
9272
+ };
9273
+ var clamp = function (value, min, max) { return Math.max(min, Math.min(max, value)); };
8517
9274
  var resolveStateWorldBounds = function (state) {
8518
9275
  var bounds = createBounds();
8519
9276
  state.elements.forEach(function (element) {
@@ -8550,6 +9307,34 @@ var resolveStateWorldBounds = function (state) {
8550
9307
  });
8551
9308
  return hasBounds(bounds) ? bounds : null;
8552
9309
  };
9310
+ var resolveElementWorldBounds = function (state) {
9311
+ var bounds = createBounds();
9312
+ state.elements.forEach(function (element) {
9313
+ includeRect(bounds, element.position.x, element.position.y, element.size.width, element.size.height);
9314
+ });
9315
+ return hasBounds(bounds) ? bounds : null;
9316
+ };
9317
+ var resolveViewportFitTransform = function (state, viewportSize, options) {
9318
+ var bounds = resolveElementWorldBounds(state);
9319
+ if (!bounds)
9320
+ return null;
9321
+ var safeWidth = Math.max(1, viewportSize.width);
9322
+ var safeHeight = Math.max(1, viewportSize.height);
9323
+ var _a = resolveViewportFitOptions(options), padding = _a.padding, minZoom = _a.minZoom, maxZoom = _a.maxZoom;
9324
+ var availableWidth = Math.max(1, safeWidth - padding * 2);
9325
+ var availableHeight = Math.max(1, safeHeight - padding * 2);
9326
+ var contentWidth = Math.max(1, bounds.maxX - bounds.minX);
9327
+ var contentHeight = Math.max(1, bounds.maxY - bounds.minY);
9328
+ var rawZoom = Math.min(availableWidth / contentWidth, availableHeight / contentHeight);
9329
+ var zoom = clamp(rawZoom, minZoom, maxZoom);
9330
+ var centerX = (bounds.minX + bounds.maxX) / 2;
9331
+ var centerY = (bounds.minY + bounds.maxY) / 2;
9332
+ var pan = {
9333
+ x: safeWidth / 2 - centerX * zoom,
9334
+ y: safeHeight / 2 - centerY * zoom,
9335
+ };
9336
+ return { pan: pan, zoom: zoom };
9337
+ };
8553
9338
  var resolveFitToContentPadding = function (fitToContent) {
8554
9339
  if (typeof fitToContent === 'object' && typeof fitToContent.padding === 'number') {
8555
9340
  return Math.max(0, fitToContent.padding);
@@ -8625,10 +9410,12 @@ var registerSimpleShapes = function (registry, shapes, isPort) {
8625
9410
  var createDiagramEditor = function (config) {
8626
9411
  var _a, _b;
8627
9412
  var Konva = getKonva();
9413
+ var stageWidth = (_a = config.width) !== null && _a !== void 0 ? _a : 900;
9414
+ var stageHeight = (_b = config.height) !== null && _b !== void 0 ? _b : 560;
8628
9415
  var stage = new Konva.Stage({
8629
9416
  container: config.container,
8630
- width: (_a = config.width) !== null && _a !== void 0 ? _a : 900,
8631
- height: (_b = config.height) !== null && _b !== void 0 ? _b : 560,
9417
+ width: stageWidth,
9418
+ height: stageHeight,
8632
9419
  });
8633
9420
  var stageContainer = typeof stage.container === 'function' ? stage.container() : stage.container;
8634
9421
  if (stageContainer && stageContainer.tabIndex < 0) {
@@ -8775,6 +9562,12 @@ var createDiagramEditor = function (config) {
8775
9562
  completeLinkToPort: function (targetPortId) { return interaction.completeLinkToPort(targetPortId); },
8776
9563
  completeLinkToElement: function (targetElementId, pointer) { return interaction.completeLinkToElement(targetElementId, pointer); },
8777
9564
  cancelLink: function () { return interaction.cancelLink(); },
9565
+ zoomToFitElements: function (options) {
9566
+ var transform = resolveViewportFitTransform(engine.getState(), { width: stageWidth, height: stageHeight }, options);
9567
+ if (!transform)
9568
+ return;
9569
+ engine.setViewport(transform.pan, transform.zoom);
9570
+ },
8778
9571
  exportImage: function (options) {
8779
9572
  engine.render();
8780
9573
  if (typeof stage.toDataURL !== 'function') {
@@ -8801,6 +9594,8 @@ var createDiagramEditor = function (config) {
8801
9594
  return stage.toDataURL(__assign(__assign({}, baseOptions), { x: cropX, y: cropY, width: cropWidth, height: cropHeight }));
8802
9595
  },
8803
9596
  resize: function (width, height) {
9597
+ stageWidth = width;
9598
+ stageHeight = height;
8804
9599
  stage.width(width);
8805
9600
  stage.height(height);
8806
9601
  engine.render();