@affino/diagram-core 0.10.2 → 0.11.1

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.
@@ -147,7 +147,7 @@ export class DiagramEngine {
147
147
  value: new Map()
148
148
  });
149
149
  this.history = new DiagramHistory(options.history);
150
- this.store = new DiagramSceneStore(initialScene);
150
+ this.store = new DiagramSceneStore(validateSceneInput(initialScene));
151
151
  this.state = this.store.state;
152
152
  this.ensureIndexes();
153
153
  }
@@ -240,7 +240,7 @@ export class DiagramEngine {
240
240
  return { ok: true, changed: changed.size > 0, revision: this.state.revision, documentRevision: this.state.documentRevision, failedCommandIndex: null, reason: null, affectedIds: [...affected] };
241
241
  }
242
242
  transact(mutator) {
243
- const next = mutator(this.store.getSnapshot());
243
+ const next = validateSceneInput(mutator(this.store.getSnapshot()));
244
244
  const patch = createReplaceScenePatch(next, serializeScene(this.store.getSnapshot()));
245
245
  this.history.clear();
246
246
  const result = this.commitPatch(patch, false, null);
@@ -482,25 +482,36 @@ export class DiagramEngine {
482
482
  return this.history.canRedo();
483
483
  }
484
484
  canDelete(ids = this.state.selection.ids) {
485
- return ids.some((id) => this.hasEntity(id) && !this.isEntityNonDeletable(id) && !this.isEntityLockedOrReadOnly(id));
485
+ return ids.some((id) => this.hasEntity(id) && !this.isEntityNonDeletable(id) && !this.isEntityLockedOrReadOnly(id))
486
+ && ids.every((id) => {
487
+ var _a;
488
+ const group = (_a = this.state.entities.groupsById.get(id)) !== null && _a !== void 0 ? _a : [...this.state.entities.groupsById.values()].find((candidate) => candidate.childIds.includes(id));
489
+ return !group || groupMembers(this.state, group.id).every((childId) => !this.isEntityNonDeletable(childId) && !this.isEntityLockedOrReadOnly(childId));
490
+ });
486
491
  }
487
492
  canMove(ids = this.state.selection.ids) {
488
493
  return ids.some((id) => this.isEntityTranslatable(id) && !this.isEntityLockedOrReadOnly(id));
489
494
  }
490
495
  canResize(ids = this.state.selection.ids) {
491
- return ids.some((id) => !this.isEntityLockedOrReadOnly(id) && (this.state.entities.nodesById.has(id) || this.state.entities.shapesById.has(id) || this.state.entities.textsById.has(id)));
496
+ return ids.some((id) => {
497
+ const group = this.state.entities.groupsById.get(id);
498
+ if (group)
499
+ return group.childIds.length > 0 && group.childIds.every((childId) => this.isEntityResizable(childId) && !this.isEntityLockedOrReadOnly(childId));
500
+ return this.isEntityResizable(id) && !this.isEntityLockedOrReadOnly(id);
501
+ });
492
502
  }
493
503
  canRotate(ids = this.state.selection.ids) {
494
- return this.canResize(ids);
504
+ return ids.some((id) => !this.isEntityLockedOrReadOnly(id) && (this.state.entities.nodesById.has(id) || this.state.entities.shapesById.has(id) || this.state.entities.textsById.has(id)));
495
505
  }
496
506
  canAlign(ids = this.state.selection.ids) {
497
- return ids.filter((id) => !this.isEntityLockedOrReadOnly(id) && (this.state.entities.nodesById.has(id) || this.state.entities.shapesById.has(id) || this.state.entities.textsById.has(id))).length >= 2;
507
+ return ids.filter((id) => !this.isEntityLockedOrReadOnly(id) && (this.state.entities.nodesById.has(id) || this.state.entities.shapesById.has(id) || this.state.entities.textsById.has(id) || this.state.entities.tablesById.has(id))).length >= 2;
498
508
  }
499
509
  canEditText(id = this.state.selection.primaryId) {
500
510
  return Boolean(id && this.state.entities.textsById.has(id) && !this.isEntityLockedOrReadOnly(id));
501
511
  }
502
512
  canPaste(clipboard) {
503
- return Boolean(clipboard && (clipboard.nodes.length || clipboard.edges.length || clipboard.texts.length || clipboard.shapes.length || clipboard.ports.length));
513
+ var _a, _b;
514
+ return Boolean(clipboard && (clipboard.nodes.length || clipboard.edges.length || clipboard.texts.length || clipboard.shapes.length || clipboard.ports.length || ((_a = clipboard.groups) === null || _a === void 0 ? void 0 : _a.length) || ((_b = clipboard.tables) === null || _b === void 0 ? void 0 : _b.length)));
504
515
  }
505
516
  getDiagnostics() {
506
517
  return {
@@ -551,11 +562,12 @@ export class DiagramEngine {
551
562
  return serializeScene(this.store.getSnapshot());
552
563
  }
553
564
  serializeDocument(options = {}) {
565
+ var _a, _b;
554
566
  const scene = this.serialize();
555
567
  return {
556
568
  format: "affino-diagram",
557
569
  version: 1,
558
- content: { nodes: scene.nodes, edges: scene.edges, texts: scene.texts, shapes: scene.shapes, ports: scene.ports },
570
+ content: { nodes: scene.nodes, edges: scene.edges, texts: scene.texts, shapes: scene.shapes, ports: scene.ports, groups: (_a = scene.groups) !== null && _a !== void 0 ? _a : [], tables: (_b = scene.tables) !== null && _b !== void 0 ? _b : [] },
559
571
  ...(options.includeView === false ? {} : { view: { viewport: scene.viewport, selection: scene.selection } }),
560
572
  };
561
573
  }
@@ -617,6 +629,10 @@ export class DiagramEngine {
617
629
  for (const edgeId of (_c = this.edgeIdsByPort.get(id)) !== null && _c !== void 0 ? _c : []) {
618
630
  invalidated.add(edgeId);
619
631
  }
632
+ for (const group of this.state.entities.groupsById.values()) {
633
+ if (group.childIds.includes(id))
634
+ invalidated.add(group.id);
635
+ }
620
636
  }
621
637
  return invalidated;
622
638
  }
@@ -679,6 +695,8 @@ export class DiagramEngine {
679
695
  ...this.state.entities.textsById.keys(),
680
696
  ...this.state.entities.shapesById.keys(),
681
697
  ...this.state.entities.portsById.keys(),
698
+ ...this.state.order.groupIds,
699
+ ...this.state.order.tableIds,
682
700
  ]);
683
701
  this.portIdsByNode = new Map();
684
702
  this.edgeIdsByNode = new Map();
@@ -733,7 +751,7 @@ export class DiagramEngine {
733
751
  this.spatialIndex.ensure(() => this.rebuildIndexGeometries());
734
752
  }
735
753
  createPatch(command) {
736
- var _a, _b, _c, _d;
754
+ var _a, _b, _c, _d, _e;
737
755
  switch (command.type) {
738
756
  case "moveEntities":
739
757
  return createMovePatch(this.state, command.ids, command.delta);
@@ -741,6 +759,28 @@ export class DiagramEngine {
741
759
  return createMovePatch(this.state, [command.id], command.delta);
742
760
  case "createNode":
743
761
  return createCreateEntityPatch(this.state, command.node);
762
+ case "createTable":
763
+ return createCreateTablePatch(this.state, command.table);
764
+ case "insertTableRow":
765
+ return createTableStructurePatch(this.state, command.id, "row", command.index, "insert", command.cells);
766
+ case "deleteTableRow":
767
+ return createTableStructurePatch(this.state, command.id, "row", command.index, "delete");
768
+ case "insertTableColumn":
769
+ return createTableStructurePatch(this.state, command.id, "column", command.index, "insert", command.cells);
770
+ case "deleteTableColumn":
771
+ return createTableStructurePatch(this.state, command.id, "column", command.index, "delete");
772
+ case "resizeTableRow":
773
+ return createResizeTableAxisPatch(this.state, command.id, "row", command.index, command.height);
774
+ case "resizeTableColumn":
775
+ return createResizeTableAxisPatch(this.state, command.id, "column", command.index, command.width);
776
+ case "updateTableCell":
777
+ return createUpdateTableCellPatch(this.state, command.id, command.row, command.column, command.cell);
778
+ case "updateTableStyle":
779
+ return createUpdateTableStylePatch(this.state, command.id, command.style);
780
+ case "groupEntities":
781
+ return createGroupEntitiesPatch(this.state, command.id, (_a = command.ids) !== null && _a !== void 0 ? _a : this.state.selection.ids);
782
+ case "ungroupEntities":
783
+ return createUngroupEntitiesPatch(this.state, command.ids);
744
784
  case "createPort":
745
785
  return createCreatePortPatch(this.state, command.port);
746
786
  case "deleteEntities":
@@ -760,7 +800,7 @@ export class DiagramEngine {
760
800
  case "deleteSelection":
761
801
  return createDeleteSelectionPatch(this.state);
762
802
  case "duplicateSelection":
763
- return this.clipboardService.createPastePatch(this.state, this.clipboardService.exportSubgraph(this.state, this.state.selection.ids, normalizeSelection), (_a = command.offset) !== null && _a !== void 0 ? _a : { x: 24, y: 24 }, createDeleteEntitiesPatch, normalizeSelection);
803
+ return this.clipboardService.createPastePatch(this.state, this.clipboardService.exportSubgraph(this.state, this.state.selection.ids, normalizeSelection), (_b = command.offset) !== null && _b !== void 0 ? _b : { x: 24, y: 24 }, createDeleteEntitiesPatch, normalizeSelection);
764
804
  case "pasteClipboard":
765
805
  try {
766
806
  validateSceneInput(command.clipboard);
@@ -768,7 +808,7 @@ export class DiagramEngine {
768
808
  catch {
769
809
  return null;
770
810
  }
771
- return this.clipboardService.createPastePatch(this.state, command.clipboard, (_b = command.offset) !== null && _b !== void 0 ? _b : { x: 24, y: 24 }, createDeleteEntitiesPatch, normalizeSelection);
811
+ return this.clipboardService.createPastePatch(this.state, command.clipboard, (_c = command.offset) !== null && _c !== void 0 ? _c : { x: 24, y: 24 }, createDeleteEntitiesPatch, normalizeSelection);
772
812
  case "resizeEntities":
773
813
  return createResizePatch(this.state, command.entries);
774
814
  case "rotateEntities":
@@ -790,9 +830,9 @@ export class DiagramEngine {
790
830
  case "updateEntities":
791
831
  return createEntityUpdatesPatch(this.state, command.updates);
792
832
  case "keyboard":
793
- return createKeyboardPatch(this.state, command.command, (_c = command.options) !== null && _c !== void 0 ? _c : {});
833
+ return createKeyboardPatch(this.state, command.command, (_d = command.options) !== null && _d !== void 0 ? _d : {});
794
834
  case "setSelection":
795
- return createSelectionPatch(this.state.selection, command.selection, (_d = command.mode) !== null && _d !== void 0 ? _d : "replace");
835
+ return createSelectionPatch(this.state.selection, command.selection, (_e = command.mode) !== null && _e !== void 0 ? _e : "replace");
796
836
  case "editText":
797
837
  return this.canEditText(command.id) ? createEditTextPatch(this.state, command.id, command.text) : null;
798
838
  case "setViewport":
@@ -947,6 +987,8 @@ export class DiagramEngine {
947
987
  ...this.state.order.nodeIds,
948
988
  ...this.state.order.textIds,
949
989
  ...this.state.entities.portsById.keys(),
990
+ ...this.state.order.groupIds,
991
+ ...this.state.order.tableIds,
950
992
  ];
951
993
  const baseOrder = new Map(ids.map((id, index) => [id, index]));
952
994
  this.orderedIdsCache = ids.sort((a, b) => {
@@ -982,11 +1024,15 @@ export class DiagramEngine {
982
1024
  return "shape";
983
1025
  if (this.state.entities.portsById.has(id))
984
1026
  return "port";
1027
+ if (this.state.entities.groupsById.has(id))
1028
+ return "group";
1029
+ if (this.state.entities.tablesById.has(id))
1030
+ return "table";
985
1031
  return null;
986
1032
  }
987
1033
  getEntityMetadata(id) {
988
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
989
- return (_h = (_f = (_d = (_b = (_a = this.state.entities.nodesById.get(id)) === null || _a === void 0 ? void 0 : _a.metadata) !== null && _b !== void 0 ? _b : (_c = this.state.entities.edgesById.get(id)) === null || _c === void 0 ? void 0 : _c.metadata) !== null && _d !== void 0 ? _d : (_e = this.state.entities.textsById.get(id)) === null || _e === void 0 ? void 0 : _e.metadata) !== null && _f !== void 0 ? _f : (_g = this.state.entities.shapesById.get(id)) === null || _g === void 0 ? void 0 : _g.metadata) !== null && _h !== void 0 ? _h : (_j = this.state.entities.portsById.get(id)) === null || _j === void 0 ? void 0 : _j.metadata;
1034
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
1035
+ return (_m = (_k = (_h = (_f = (_d = (_b = (_a = this.state.entities.nodesById.get(id)) === null || _a === void 0 ? void 0 : _a.metadata) !== null && _b !== void 0 ? _b : (_c = this.state.entities.edgesById.get(id)) === null || _c === void 0 ? void 0 : _c.metadata) !== null && _d !== void 0 ? _d : (_e = this.state.entities.textsById.get(id)) === null || _e === void 0 ? void 0 : _e.metadata) !== null && _f !== void 0 ? _f : (_g = this.state.entities.shapesById.get(id)) === null || _g === void 0 ? void 0 : _g.metadata) !== null && _h !== void 0 ? _h : (_j = this.state.entities.portsById.get(id)) === null || _j === void 0 ? void 0 : _j.metadata) !== null && _k !== void 0 ? _k : (_l = this.state.entities.groupsById.get(id)) === null || _l === void 0 ? void 0 : _l.metadata) !== null && _m !== void 0 ? _m : (_o = this.state.entities.tablesById.get(id)) === null || _o === void 0 ? void 0 : _o.metadata;
990
1036
  }
991
1037
  isEntityLockedOrReadOnly(id) {
992
1038
  const metadata = this.getEntityMetadata(id);
@@ -1000,10 +1046,19 @@ export class DiagramEngine {
1000
1046
  var _a;
1001
1047
  if (this.state.entities.portsById.has(id))
1002
1048
  return false;
1049
+ const group = this.state.entities.groupsById.get(id);
1050
+ if (group) {
1051
+ return group.childIds.length > 0
1052
+ && group.childIds.every((childId) => !this.isEntityLockedOrReadOnly(childId) && (this.isEntityTranslatable(childId) || this.state.entities.edgesById.has(childId)))
1053
+ && group.childIds.some((childId) => this.isEntityTranslatable(childId));
1054
+ }
1003
1055
  const edge = this.state.entities.edgesById.get(id);
1004
1056
  if (edge)
1005
1057
  return edge.source.kind === "point" || edge.target.kind === "point" || Boolean((_a = edge.points) === null || _a === void 0 ? void 0 : _a.length);
1006
- return this.state.entities.nodesById.has(id) || this.state.entities.textsById.has(id) || this.state.entities.shapesById.has(id);
1058
+ return this.state.entities.nodesById.has(id) || this.state.entities.textsById.has(id) || this.state.entities.shapesById.has(id) || this.state.entities.tablesById.has(id);
1059
+ }
1060
+ isEntityResizable(id) {
1061
+ return this.state.entities.nodesById.has(id) || this.state.entities.shapesById.has(id) || this.state.entities.textsById.has(id) || this.state.entities.edgesById.has(id) || this.state.entities.tablesById.has(id);
1007
1062
  }
1008
1063
  }
1009
1064
  function isBatchNoOp(state, command) {
@@ -1037,7 +1092,7 @@ function normalizeSearchText(value) {
1037
1092
  return (_a = value === null || value === void 0 ? void 0 : value.trim().toLocaleLowerCase()) !== null && _a !== void 0 ? _a : "";
1038
1093
  }
1039
1094
  function entityMatchesText(id, state, normalizedText) {
1040
- var _a;
1095
+ var _a, _b, _c;
1041
1096
  if (id.toLocaleLowerCase().includes(normalizedText)) {
1042
1097
  return true;
1043
1098
  }
@@ -1045,6 +1100,9 @@ function entityMatchesText(id, state, normalizedText) {
1045
1100
  if (text === null || text === void 0 ? void 0 : text.toLocaleLowerCase().includes(normalizedText)) {
1046
1101
  return true;
1047
1102
  }
1103
+ const tableText = (_c = (_b = state.entities.tablesById.get(id)) === null || _b === void 0 ? void 0 : _b.cells) === null || _c === void 0 ? void 0 : _c.flatMap((row) => row.map((cell) => cell.text)).join(" ");
1104
+ if (tableText === null || tableText === void 0 ? void 0 : tableText.toLocaleLowerCase().includes(normalizedText))
1105
+ return true;
1048
1106
  return searchableMetadataValues(getEntityMetadata(state, id)).some((value) => value.toLocaleLowerCase().includes(normalizedText));
1049
1107
  }
1050
1108
  function searchableMetadataValues(metadata) {
@@ -1095,19 +1153,21 @@ export function serializeScene(scene) {
1095
1153
  texts: scene.order.textIds.map((id) => scene.entities.textsById.get(id)).filter(isDefined),
1096
1154
  shapes: scene.order.shapeIds.map((id) => scene.entities.shapesById.get(id)).filter(isDefined),
1097
1155
  ports: [...scene.entities.portsById.values()],
1156
+ groups: scene.order.groupIds.map((id) => scene.entities.groupsById.get(id)).filter(isDefined),
1157
+ tables: scene.order.tableIds.map((id) => scene.entities.tablesById.get(id)).filter(isDefined),
1098
1158
  selection: scene.selection,
1099
1159
  viewport: scene.viewport,
1100
1160
  };
1101
1161
  }
1102
1162
  export function deserializeScene(scene) {
1103
- return scene;
1163
+ return validateSceneInput(scene);
1104
1164
  }
1105
1165
  export function deserializeDiagramDocument(input) {
1106
1166
  var _a;
1107
1167
  if (!input || typeof input !== "object")
1108
1168
  throw new TypeError("Invalid diagram document");
1109
1169
  if ("format" in input) {
1110
- if (input.format !== "affino-diagram" || input.version !== 1 || !input.content || ![input.content.nodes, input.content.edges, input.content.texts, input.content.shapes, input.content.ports].every(Array.isArray)) {
1170
+ if (input.format !== "affino-diagram" || input.version !== 1 || !input.content || ![input.content.nodes, input.content.edges, input.content.texts, input.content.shapes, input.content.ports].every(Array.isArray) || (input.content.groups !== undefined && !Array.isArray(input.content.groups)) || (input.content.tables !== undefined && !Array.isArray(input.content.tables))) {
1111
1171
  throw new TypeError("Unsupported or invalid diagram document");
1112
1172
  }
1113
1173
  const view = (_a = input.view) !== null && _a !== void 0 ? _a : {};
@@ -1115,17 +1175,85 @@ export function deserializeDiagramDocument(input) {
1115
1175
  }
1116
1176
  return validateSceneInput(input);
1117
1177
  }
1178
+ const DEFAULT_TABLE_STYLE = {
1179
+ grid: { visible: true, stroke: { color: "#D1D5DB", width: 1, style: "solid" } },
1180
+ border: { visible: true, stroke: { color: "#9CA3AF", width: 1, style: "solid" }, radius: 8 },
1181
+ cellPadding: 8,
1182
+ };
1183
+ function normalizeTable(table) {
1184
+ var _a;
1185
+ const defaultCell = normalizeTableCell((_a = table.defaultCell) !== null && _a !== void 0 ? _a : { text: "" });
1186
+ const cells = Array.from({ length: table.rowCount }, (_, row) => Array.from({ length: table.columnCount }, (_, column) => { var _a, _b, _c; return normalizeTableCell((_c = (_b = (_a = table.cells) === null || _a === void 0 ? void 0 : _a[row]) === null || _b === void 0 ? void 0 : _b[column]) !== null && _c !== void 0 ? _c : defaultCell); }));
1187
+ const rowHeights = table.rowHeights ? [...table.rowHeights] : Array(table.rowCount).fill(table.height / table.rowCount);
1188
+ const columnWidths = table.columnWidths ? [...table.columnWidths] : Array(table.columnCount).fill(table.width / table.columnCount);
1189
+ return { ...table, cells, rowHeights, columnWidths, defaultCell, style: normalizeTableStyle(table.style) };
1190
+ }
1191
+ function normalizeTableCell(cell) { return { text: cell.text, ...(cell.horizontalAlign ? { horizontalAlign: cell.horizontalAlign } : {}), ...(cell.verticalAlign ? { verticalAlign: cell.verticalAlign } : {}) }; }
1192
+ function normalizeTableStyle(style) {
1193
+ var _a, _b, _c;
1194
+ return {
1195
+ grid: { ...DEFAULT_TABLE_STYLE.grid, ...style === null || style === void 0 ? void 0 : style.grid, stroke: { ...DEFAULT_TABLE_STYLE.grid.stroke, ...(_a = style === null || style === void 0 ? void 0 : style.grid) === null || _a === void 0 ? void 0 : _a.stroke } },
1196
+ border: { ...DEFAULT_TABLE_STYLE.border, ...style === null || style === void 0 ? void 0 : style.border, stroke: { ...DEFAULT_TABLE_STYLE.border.stroke, ...(_b = style === null || style === void 0 ? void 0 : style.border) === null || _b === void 0 ? void 0 : _b.stroke } },
1197
+ cellPadding: (_c = style === null || style === void 0 ? void 0 : style.cellPadding) !== null && _c !== void 0 ? _c : DEFAULT_TABLE_STYLE.cellPadding,
1198
+ };
1199
+ }
1200
+ function validateDiagramTable(source) {
1201
+ var _a, _b;
1202
+ if (!source || source.kind !== "table" || ![source.x, source.y, source.width, source.height].every(Number.isFinite) || source.width <= 0 || source.height <= 0)
1203
+ throw new TypeError(`Invalid geometry for table ${(_a = source === null || source === void 0 ? void 0 : source.id) !== null && _a !== void 0 ? _a : ""}`);
1204
+ if (!Number.isInteger(source.rowCount) || source.rowCount < 1 || !Number.isInteger(source.columnCount) || source.columnCount < 1)
1205
+ throw new TypeError(`Invalid dimensions for table ${source.id}: at least one row and column are required`);
1206
+ if (source.cells !== undefined && (!Array.isArray(source.cells) || source.cells.length !== source.rowCount || source.cells.some((row) => !Array.isArray(row) || row.length !== source.columnCount)))
1207
+ throw new TypeError(`Invalid cell matrix for table ${source.id}`);
1208
+ for (const row of (_b = source.cells) !== null && _b !== void 0 ? _b : [])
1209
+ for (const cell of row)
1210
+ validateTableCell(cell, source.id);
1211
+ if (source.defaultCell)
1212
+ validateTableCell(source.defaultCell, source.id);
1213
+ if (source.rowHeights && (!validSizes(source.rowHeights, source.rowCount) || !sizesMatch(source.rowHeights, source.height)))
1214
+ throw new TypeError(`Invalid row heights for table ${source.id}`);
1215
+ if (source.columnWidths && (!validSizes(source.columnWidths, source.columnCount) || !sizesMatch(source.columnWidths, source.width)))
1216
+ throw new TypeError(`Invalid column widths for table ${source.id}`);
1217
+ if (source.style && !validTableStyleShape(source.style))
1218
+ throw new TypeError(`Invalid style for table ${source.id}`);
1219
+ validateDiagramTableStyle(normalizeTableStyle(source.style), source.id);
1220
+ }
1221
+ function validateTableCell(cell, id) {
1222
+ if (!cell || Object.keys(cell).some((key) => !["text", "horizontalAlign", "verticalAlign"].includes(key)) || typeof cell.text !== "string" || (cell.horizontalAlign !== undefined && !["left", "center", "right"].includes(cell.horizontalAlign)) || (cell.verticalAlign !== undefined && !["top", "middle", "bottom"].includes(cell.verticalAlign)))
1223
+ throw new TypeError(`Invalid cell in table ${id}`);
1224
+ }
1225
+ function validTableStyleShape(style) {
1226
+ if (!style || typeof style !== "object")
1227
+ return false;
1228
+ const value = style;
1229
+ const grid = value.grid;
1230
+ const border = value.border;
1231
+ const validStrokeShape = (stroke) => Boolean(stroke && typeof stroke === "object" && Object.keys(stroke).every((key) => ["color", "width", "style"].includes(key)));
1232
+ return Object.keys(value).every((key) => ["grid", "border", "cellPadding"].includes(key))
1233
+ && Boolean(grid && Object.keys(grid).every((key) => ["visible", "stroke"].includes(key)) && (grid.stroke === undefined || validStrokeShape(grid.stroke)))
1234
+ && Boolean(border && Object.keys(border).every((key) => ["visible", "stroke", "radius"].includes(key)) && (border.stroke === undefined || validStrokeShape(border.stroke)));
1235
+ }
1236
+ function validateDiagramTableStyle(style, id) {
1237
+ var _a, _b;
1238
+ const validStroke = (stroke) => typeof stroke.color === "string" && stroke.color.trim().length > 0 && Number.isFinite(stroke.width) && stroke.width >= 0 && ["solid", "dashed", "dotted"].includes(stroke.style);
1239
+ if (typeof style.grid.visible !== "boolean" || typeof style.border.visible !== "boolean" || !validStroke(style.grid.stroke) || !validStroke(style.border.stroke) || !Number.isFinite(style.border.radius) || style.border.radius < 0 || !Number.isFinite((_a = style.cellPadding) !== null && _a !== void 0 ? _a : NaN) || ((_b = style.cellPadding) !== null && _b !== void 0 ? _b : -1) < 0)
1240
+ throw new TypeError(`Invalid style for table ${id}`);
1241
+ }
1242
+ function validSizes(values, expectedLength) { return values.length === expectedLength && values.every((value) => Number.isFinite(value) && value > 0); }
1243
+ function sizesMatch(values, total) { return Math.abs(values.reduce((sum, value) => sum + value, 0) - total) <= Math.max(0.01, total * 1e-6); }
1118
1244
  function validateSceneInput(scene) {
1119
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
1245
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
1120
1246
  const nodesInput = (_a = scene.nodes) !== null && _a !== void 0 ? _a : [];
1121
1247
  const edgesInput = (_b = scene.edges) !== null && _b !== void 0 ? _b : [];
1122
1248
  const textsInput = (_c = scene.texts) !== null && _c !== void 0 ? _c : [];
1123
1249
  const shapesInput = (_d = scene.shapes) !== null && _d !== void 0 ? _d : [];
1124
1250
  const portsInput = (_e = scene.ports) !== null && _e !== void 0 ? _e : [];
1125
- if (![nodesInput, edgesInput, textsInput, shapesInput, portsInput].every(Array.isArray))
1251
+ const groupsInput = (_f = scene.groups) !== null && _f !== void 0 ? _f : [];
1252
+ const tablesInput = (_g = scene.tables) !== null && _g !== void 0 ? _g : [];
1253
+ if (![nodesInput, edgesInput, textsInput, shapesInput, portsInput, groupsInput, tablesInput].every(Array.isArray))
1126
1254
  throw new TypeError("Invalid diagram scene collections");
1127
1255
  const ids = new Set();
1128
- for (const entity of [...nodesInput, ...edgesInput, ...textsInput, ...shapesInput, ...portsInput]) {
1256
+ for (const entity of [...nodesInput, ...edgesInput, ...textsInput, ...shapesInput, ...portsInput, ...groupsInput, ...tablesInput]) {
1129
1257
  if (!entity || typeof entity.id !== "string" || !entity.id || ids.has(entity.id))
1130
1258
  throw new TypeError("Diagram entity IDs must be non-empty and globally unique");
1131
1259
  ids.add(entity.id);
@@ -1133,40 +1261,59 @@ function validateSceneInput(scene) {
1133
1261
  const nodes = new Set(nodesInput.map((node) => node.id));
1134
1262
  const ports = new Map(portsInput.map((port) => [port.id, port]));
1135
1263
  for (const node of nodesInput)
1136
- if (![node.x, node.y, node.width, node.height, (_f = node.rotation) !== null && _f !== void 0 ? _f : 0].every(Number.isFinite) || node.width < 0 || node.height < 0)
1264
+ if (![node.x, node.y, node.width, node.height, (_h = node.rotation) !== null && _h !== void 0 ? _h : 0].every(Number.isFinite) || node.width < 0 || node.height < 0)
1137
1265
  throw new TypeError(`Invalid geometry for node ${node.id}`);
1138
1266
  for (const port of portsInput)
1139
- if (!nodes.has(port.nodeId) || ![port.x, port.y, (_g = port.radius) !== null && _g !== void 0 ? _g : 0].every(Number.isFinite))
1267
+ if (!nodes.has(port.nodeId) || ![port.x, port.y, (_j = port.radius) !== null && _j !== void 0 ? _j : 0].every(Number.isFinite))
1140
1268
  throw new TypeError(`Invalid port ${port.id}`);
1141
1269
  for (const node of nodesInput)
1142
- if (((_h = node.portIds) !== null && _h !== void 0 ? _h : []).some((id) => { var _a; return ((_a = ports.get(id)) === null || _a === void 0 ? void 0 : _a.nodeId) !== node.id; }))
1270
+ if (((_k = node.portIds) !== null && _k !== void 0 ? _k : []).some((id) => { var _a; return ((_a = ports.get(id)) === null || _a === void 0 ? void 0 : _a.nodeId) !== node.id; }))
1143
1271
  throw new TypeError(`Invalid port reference on node ${node.id}`);
1144
1272
  for (const text of textsInput)
1145
- if (![text.x, text.y, (_j = text.width) !== null && _j !== void 0 ? _j : 0, (_k = text.height) !== null && _k !== void 0 ? _k : 0, (_l = text.fontSize) !== null && _l !== void 0 ? _l : 0, (_m = text.rotation) !== null && _m !== void 0 ? _m : 0].every(Number.isFinite))
1273
+ if (![text.x, text.y, (_l = text.width) !== null && _l !== void 0 ? _l : 0, (_m = text.height) !== null && _m !== void 0 ? _m : 0, (_o = text.fontSize) !== null && _o !== void 0 ? _o : 0, (_p = text.rotation) !== null && _p !== void 0 ? _p : 0].every(Number.isFinite))
1146
1274
  throw new TypeError(`Invalid geometry for text ${text.id}`);
1147
1275
  for (const shape of shapesInput)
1148
- if (![shape.x, shape.y, shape.width, shape.height, (_o = shape.rotation) !== null && _o !== void 0 ? _o : 0].every(Number.isFinite) || shape.width < 0 || shape.height < 0)
1276
+ if (![shape.x, shape.y, shape.width, shape.height, (_q = shape.rotation) !== null && _q !== void 0 ? _q : 0].every(Number.isFinite) || shape.width < 0 || shape.height < 0)
1149
1277
  throw new TypeError(`Invalid geometry for shape ${shape.id}`);
1278
+ for (const table of tablesInput)
1279
+ validateDiagramTable(table);
1150
1280
  for (const edge of edgesInput) {
1151
1281
  const validEndpoint = (endpoint) => endpoint.kind === "point" ? Number.isFinite(endpoint.point.x) && Number.isFinite(endpoint.point.y) : endpoint.kind === "node" ? nodes.has(endpoint.nodeId) : endpoint.kind === "port" && ports.has(endpoint.portId);
1152
- if (!validEndpoint(edge.source) || !validEndpoint(edge.target) || !((_p = edge.points) !== null && _p !== void 0 ? _p : []).every((point) => Number.isFinite(point.x) && Number.isFinite(point.y)))
1282
+ if (!validEndpoint(edge.source) || !validEndpoint(edge.target) || !((_r = edge.points) !== null && _r !== void 0 ? _r : []).every((point) => Number.isFinite(point.x) && Number.isFinite(point.y)))
1153
1283
  throw new TypeError(`Invalid edge ${edge.id}`);
1154
1284
  }
1285
+ const groupedChildren = new Set();
1286
+ const groupable = new Set([...nodesInput, ...edgesInput, ...textsInput, ...shapesInput, ...tablesInput].map((entity) => entity.id));
1287
+ for (const group of groupsInput) {
1288
+ if (group.kind !== "group" || !Array.isArray(group.childIds) || group.childIds.length < 2 || new Set(group.childIds).size !== group.childIds.length)
1289
+ throw new TypeError(`Invalid group ${group.id}: groups require at least two unique, non-nested children`);
1290
+ for (const childId of group.childIds) {
1291
+ if (!groupable.has(childId) || groupedChildren.has(childId))
1292
+ throw new TypeError(`Invalid group ${group.id}: nested, port, unknown, or already grouped child ${childId}`);
1293
+ groupedChildren.add(childId);
1294
+ }
1295
+ }
1155
1296
  if (scene.viewport && (![scene.viewport.x, scene.viewport.y, scene.viewport.width, scene.viewport.height, scene.viewport.zoom].filter((value) => value !== undefined).every(Number.isFinite) || (scene.viewport.zoom !== undefined && scene.viewport.zoom <= 0)))
1156
1297
  throw new TypeError("Invalid diagram viewport");
1157
- const selectionIds = (_r = (_q = scene.selection) === null || _q === void 0 ? void 0 : _q.ids) !== null && _r !== void 0 ? _r : [];
1298
+ const selectionIds = (_t = (_s = scene.selection) === null || _s === void 0 ? void 0 : _s.ids) !== null && _t !== void 0 ? _t : [];
1158
1299
  if (selectionIds.some((id) => !ids.has(id)))
1159
1300
  throw new TypeError("Diagram selection contains an unknown entity");
1160
- return { ...scene, nodes: nodesInput, edges: edgesInput, texts: textsInput, shapes: shapesInput, ports: portsInput };
1301
+ return { ...scene, nodes: nodesInput, edges: edgesInput, texts: textsInput, shapes: shapesInput, ports: portsInput, groups: groupsInput, tables: tablesInput.map(normalizeTable) };
1161
1302
  }
1162
1303
  function createMovePatch(state, ids, delta) {
1163
1304
  if (delta.x === 0 && delta.y === 0) {
1164
1305
  return null;
1165
1306
  }
1166
- const movableIds = ids.filter((id) => !isEntityLockedOrReadOnly(state, id) && (state.entities.nodesById.has(id)
1307
+ const groupIds = ids.filter((id) => state.entities.groupsById.has(id));
1308
+ const expandedIds = [
1309
+ ...ids.filter((id) => !state.entities.groupsById.has(id)),
1310
+ ...groupIds.filter((id) => !isEntityLockedOrReadOnly(state, id) && isGroupMovable(state, id)).flatMap((id) => groupMembers(state, id)),
1311
+ ];
1312
+ const movableIds = [...new Set(expandedIds)].filter((id) => !isEntityLockedOrReadOnly(state, id) && (state.entities.nodesById.has(id)
1167
1313
  || isEdgeTranslatable(state.entities.edgesById.get(id))
1168
1314
  || state.entities.textsById.has(id)
1169
- || state.entities.shapesById.has(id)));
1315
+ || state.entities.shapesById.has(id)
1316
+ || state.entities.tablesById.has(id)));
1170
1317
  if (!movableIds.length) {
1171
1318
  return null;
1172
1319
  }
@@ -1217,6 +1364,12 @@ function applyMoveDelta(state, ids, delta) {
1217
1364
  if (shape) {
1218
1365
  state.entities.shapesById.set(id, { ...shape, x: shape.x + delta.x, y: shape.y + delta.y });
1219
1366
  changed.add(id);
1367
+ continue;
1368
+ }
1369
+ const table = state.entities.tablesById.get(id);
1370
+ if (table) {
1371
+ state.entities.tablesById.set(id, { ...table, x: table.x + delta.x, y: table.y + delta.y });
1372
+ changed.add(id);
1220
1373
  }
1221
1374
  }
1222
1375
  return changed;
@@ -1295,6 +1448,87 @@ function createEdgePointsPatch(id, previousPoints, nextPoints) {
1295
1448
  };
1296
1449
  }
1297
1450
  function createResizePatch(state, entries) {
1451
+ var _a, _b, _c, _d, _e, _f;
1452
+ const groupEntries = entries.filter((entry) => state.entities.groupsById.has(entry.id));
1453
+ if (!groupEntries.length)
1454
+ return createResizePatchForEntities(state, entries);
1455
+ if (groupEntries.some((entry) => {
1456
+ const group = state.entities.groupsById.get(entry.id);
1457
+ return isEntityLockedOrReadOnly(state, entry.id)
1458
+ || group.childIds.some((id) => isEntityLockedOrReadOnly(state, id) || !isGroupResizableEntity(state, id));
1459
+ }))
1460
+ return null;
1461
+ const groupedChildren = new Set(groupEntries.flatMap((entry) => groupMembers(state, entry.id)));
1462
+ const regularPatch = createResizePatchForEntities(state, entries.filter((entry) => !state.entities.groupsById.has(entry.id) && !groupedChildren.has(entry.id)));
1463
+ const rectEntries = [];
1464
+ const previousEdges = new Map();
1465
+ const nextEdges = new Map();
1466
+ const entities = snapshotEntities(state);
1467
+ for (const entry of groupEntries) {
1468
+ const geometry = createEntityGeometry(entry.id, entities);
1469
+ if (!geometry)
1470
+ return null;
1471
+ const base = geometry.bounds;
1472
+ const nextBounds = { x: (_a = entry.x) !== null && _a !== void 0 ? _a : base.x, y: (_b = entry.y) !== null && _b !== void 0 ? _b : base.y, width: (_c = entry.width) !== null && _c !== void 0 ? _c : base.width, height: (_d = entry.height) !== null && _d !== void 0 ? _d : base.height };
1473
+ if (![nextBounds.x, nextBounds.y, nextBounds.width, nextBounds.height].every(Number.isFinite) || nextBounds.width < 0 || nextBounds.height < 0)
1474
+ return null;
1475
+ const scaleX = base.width === 0 ? 1 : nextBounds.width / base.width;
1476
+ const scaleY = base.height === 0 ? 1 : nextBounds.height / base.height;
1477
+ const transform = (point) => ({
1478
+ x: nextBounds.x + (point.x - base.x) * scaleX,
1479
+ y: nextBounds.y + (point.y - base.y) * scaleY,
1480
+ });
1481
+ for (const id of groupMembers(state, entry.id)) {
1482
+ const edge = state.entities.edgesById.get(id);
1483
+ if (edge) {
1484
+ const nextEdge = {
1485
+ ...edge,
1486
+ source: edge.source.kind === "point" ? { kind: "point", point: transform(edge.source.point) } : edge.source,
1487
+ target: edge.target.kind === "point" ? { kind: "point", point: transform(edge.target.point) } : edge.target,
1488
+ points: (_e = edge.points) === null || _e === void 0 ? void 0 : _e.map(transform),
1489
+ };
1490
+ previousEdges.set(id, edge);
1491
+ nextEdges.set(id, nextEdge);
1492
+ continue;
1493
+ }
1494
+ const childGeometry = createEntityGeometry(id, entities);
1495
+ const childBounds = (_f = childGeometry === null || childGeometry === void 0 ? void 0 : childGeometry.unrotatedBounds) !== null && _f !== void 0 ? _f : childGeometry === null || childGeometry === void 0 ? void 0 : childGeometry.bounds;
1496
+ if (!childBounds)
1497
+ return null;
1498
+ const nextChildBounds = { ...transform({ x: childBounds.x, y: childBounds.y }), width: childBounds.width * scaleX, height: childBounds.height * scaleY };
1499
+ rectEntries.push({ id, ...nextChildBounds });
1500
+ }
1501
+ }
1502
+ const childRectPatch = createResizePatchForEntities(state, rectEntries);
1503
+ const changedEdges = [...nextEdges].filter(([id, next]) => !diagramEdgesEqual(previousEdges.get(id), next));
1504
+ const edgePatch = changedEdges.length ? createEntitySnapshotPatch(changedEdges.map(([id]) => id), previousEdges, new Map(changedEdges), (next, id, value) => next.entities.edgesById.set(id, value)) : null;
1505
+ const groupPatch = [childRectPatch, edgePatch].filter((patch) => patch !== null).reduce((current, patch) => current ? composePatches(current, patch) : patch, null);
1506
+ return [regularPatch, groupPatch].filter((patch) => patch !== null).reduce((current, patch) => current ? composePatches(current, patch) : patch, null);
1507
+ }
1508
+ function createResizePatchForEntities(state, entries) {
1509
+ const tableEntries = entries.filter((entry) => state.entities.tablesById.has(entry.id));
1510
+ const otherEntries = entries.filter((entry) => !state.entities.tablesById.has(entry.id));
1511
+ const patches = [];
1512
+ const otherPatch = createResizePatchForNonTableEntities(state, otherEntries);
1513
+ if (otherPatch)
1514
+ patches.push(otherPatch);
1515
+ const previous = new Map();
1516
+ const next = new Map();
1517
+ for (const entry of tableEntries) {
1518
+ const table = state.entities.tablesById.get(entry.id);
1519
+ if (isEntityLockedOrReadOnly(state, entry.id))
1520
+ continue;
1521
+ const resized = resizeTable(table, entry);
1522
+ if (![resized.x, resized.y, resized.width, resized.height].every(Number.isFinite) || resized.width <= 0 || resized.height <= 0)
1523
+ return null;
1524
+ previous.set(entry.id, table);
1525
+ next.set(entry.id, resized);
1526
+ }
1527
+ if (next.size)
1528
+ patches.push(createEntitySnapshotPatch([...next.keys()], previous, next, (draft, id, value) => draft.entities.tablesById.set(id, value)));
1529
+ return patches.reduce((current, patch) => current ? composePatches(current, patch) : patch, null);
1530
+ }
1531
+ function createResizePatchForNonTableEntities(state, entries) {
1298
1532
  var _a, _b, _c;
1299
1533
  const previous = new Map();
1300
1534
  const nextEntries = [];
@@ -1464,6 +1698,8 @@ function snapshotEntities(state) {
1464
1698
  textsById: state.entities.textsById,
1465
1699
  shapesById: state.entities.shapesById,
1466
1700
  portsById: state.entities.portsById,
1701
+ groupsById: state.entities.groupsById,
1702
+ tablesById: state.entities.tablesById,
1467
1703
  };
1468
1704
  }
1469
1705
  function normalizeRotation(rotation) {
@@ -1471,7 +1707,8 @@ function normalizeRotation(rotation) {
1471
1707
  return normalized < 0 ? normalized + 360 : normalized;
1472
1708
  }
1473
1709
  function createOrderPatch(state, ids, mode) {
1474
- const existingIds = ids.filter((id) => hasEntity(state, id));
1710
+ const expandedIds = expandGroupEntityIds(state, ids, true);
1711
+ const existingIds = expandedIds.filter((id) => hasEntity(state, id));
1475
1712
  if (!existingIds.length)
1476
1713
  return null;
1477
1714
  if (mode === "front" || mode === "back") {
@@ -1479,10 +1716,12 @@ function createOrderPatch(state, ids, mode) {
1479
1716
  }
1480
1717
  const previous = cloneOrder(state.order);
1481
1718
  const next = cloneOrder(state.order);
1482
- reorderIds(next.nodeIds, ids, mode);
1483
- reorderIds(next.edgeIds, ids, mode);
1484
- reorderIds(next.textIds, ids, mode);
1485
- reorderIds(next.shapeIds, ids, mode);
1719
+ reorderIds(next.nodeIds, expandedIds, mode);
1720
+ reorderIds(next.edgeIds, expandedIds, mode);
1721
+ reorderIds(next.textIds, expandedIds, mode);
1722
+ reorderIds(next.shapeIds, expandedIds, mode);
1723
+ reorderIds(next.groupIds, expandedIds, mode);
1724
+ reorderIds(next.tableIds, expandedIds, mode);
1486
1725
  if (ordersEqual(previous, next))
1487
1726
  return null;
1488
1727
  return createOrderPatchUnchecked(previous, next);
@@ -1563,20 +1802,20 @@ function createOrderPatchUnchecked(previous, nextOrder) {
1563
1802
  orderChanged: true,
1564
1803
  apply: (state) => {
1565
1804
  state.order = cloneOrder(nextOrder);
1566
- return new Set([...nextOrder.nodeIds, ...nextOrder.edgeIds, ...nextOrder.textIds, ...nextOrder.shapeIds]);
1805
+ return new Set([...nextOrder.nodeIds, ...nextOrder.edgeIds, ...nextOrder.textIds, ...nextOrder.shapeIds, ...nextOrder.groupIds, ...nextOrder.tableIds]);
1567
1806
  },
1568
1807
  inverse: {
1569
1808
  orderChanged: true,
1570
1809
  apply: (state) => {
1571
1810
  state.order = cloneOrder(previous);
1572
- return new Set([...previous.nodeIds, ...previous.edgeIds, ...previous.textIds, ...previous.shapeIds]);
1811
+ return new Set([...previous.nodeIds, ...previous.edgeIds, ...previous.textIds, ...previous.shapeIds, ...previous.groupIds, ...previous.tableIds]);
1573
1812
  },
1574
1813
  inverse: undefined,
1575
1814
  },
1576
1815
  };
1577
1816
  }
1578
1817
  function createLayerPatch(state, ids, layer, layerRole) {
1579
- const entries = ids.filter((id) => !isEntityLockedOrReadOnly(state, id) && hasEntity(state, id));
1818
+ const entries = expandGroupEntityIds(state, ids, true).filter((id) => !isEntityLockedOrReadOnly(state, id) && hasEntity(state, id));
1580
1819
  if (!entries.length)
1581
1820
  return null;
1582
1821
  const previous = new Map(entries.map((id) => [id, getEntityMetadata(state, id)]));
@@ -1637,6 +1876,230 @@ function createCreateEntityPatch(state, node) {
1637
1876
  inverse: createDeleteEntitiesPatch([node.id]),
1638
1877
  };
1639
1878
  }
1879
+ function createTableSnapshotPatch(id, previous, next, orderChanged = false) {
1880
+ if (JSON.stringify(previous) === JSON.stringify(next))
1881
+ return null;
1882
+ const apply = (value) => (state) => { state.entities.tablesById.set(id, value); return new Set([id]); };
1883
+ return { orderChanged, apply: apply(next), inverse: { orderChanged, apply: apply(previous), inverse: undefined } };
1884
+ }
1885
+ function createCreateTablePatch(state, input) {
1886
+ let table;
1887
+ try {
1888
+ validateDiagramTable(input);
1889
+ table = normalizeTable(input);
1890
+ }
1891
+ catch {
1892
+ return null;
1893
+ }
1894
+ if (!isUniqueId(state, table.id))
1895
+ return null;
1896
+ return {
1897
+ orderChanged: true,
1898
+ apply: (next) => { next.entities.tablesById.set(table.id, table); next.order.tableIds.push(table.id); next.versions.set(table.id, 0); return new Set([table.id]); },
1899
+ inverse: createDeleteEntitiesPatch([table.id]),
1900
+ };
1901
+ }
1902
+ function createTableStructurePatch(state, id, axis, index, operation, suppliedCells) {
1903
+ const table = state.entities.tablesById.get(id);
1904
+ if (!table || isEntityLockedOrReadOnly(state, id))
1905
+ return null;
1906
+ const count = axis === "row" ? table.rowCount : table.columnCount;
1907
+ if (!Number.isInteger(index) || index < 0 || index > (operation === "insert" ? count : count - 1) || (operation === "delete" && count <= 1))
1908
+ return null;
1909
+ const rows = table.cells.map((row) => [...row]);
1910
+ let rowHeights = [...table.rowHeights];
1911
+ let columnWidths = [...table.columnWidths];
1912
+ let rowCount = table.rowCount;
1913
+ let columnCount = table.columnCount;
1914
+ if (axis === "row") {
1915
+ if (operation === "insert") {
1916
+ const cells = suppliedCells !== null && suppliedCells !== void 0 ? suppliedCells : Array.from({ length: columnCount }, () => ({ text: "" }));
1917
+ if (cells.length !== columnCount || cells.some((cell) => !isValidCell(cell)))
1918
+ return null;
1919
+ rows.splice(index, 0, cells.map(normalizeTableCell));
1920
+ const splitAt = Math.min(index, rowHeights.length - 1);
1921
+ const size = rowHeights[splitAt] / 2;
1922
+ rowHeights[splitAt] = size;
1923
+ rowHeights.splice(index, 0, size);
1924
+ rowCount += 1;
1925
+ }
1926
+ else {
1927
+ rows.splice(index, 1);
1928
+ const removed = rowHeights.splice(index, 1)[0];
1929
+ const receiver = index < rowHeights.length ? index : Math.max(0, index - 1);
1930
+ rowHeights[receiver] = rowHeights[receiver] + removed;
1931
+ rowCount -= 1;
1932
+ }
1933
+ }
1934
+ else {
1935
+ if (operation === "insert") {
1936
+ const cells = suppliedCells !== null && suppliedCells !== void 0 ? suppliedCells : Array.from({ length: rowCount }, () => ({ text: "" }));
1937
+ if (cells.length !== rowCount || cells.some((cell) => !isValidCell(cell)))
1938
+ return null;
1939
+ rows.forEach((row, rowIndex) => row.splice(index, 0, normalizeTableCell(cells[rowIndex])));
1940
+ const splitAt = Math.min(index, columnWidths.length - 1);
1941
+ const size = columnWidths[splitAt] / 2;
1942
+ columnWidths[splitAt] = size;
1943
+ columnWidths.splice(index, 0, size);
1944
+ columnCount += 1;
1945
+ }
1946
+ else {
1947
+ rows.forEach((row) => row.splice(index, 1));
1948
+ const removed = columnWidths.splice(index, 1)[0];
1949
+ const receiver = index < columnWidths.length ? index : Math.max(0, index - 1);
1950
+ columnWidths[receiver] = columnWidths[receiver] + removed;
1951
+ columnCount -= 1;
1952
+ }
1953
+ }
1954
+ return createTableSnapshotPatch(id, table, { ...table, rowCount, columnCount, cells: rows, rowHeights, columnWidths });
1955
+ }
1956
+ function isValidCell(cell) {
1957
+ return Boolean(cell && Object.keys(cell).every((key) => ["text", "horizontalAlign", "verticalAlign"].includes(key)) && typeof cell.text === "string" && (cell.horizontalAlign === undefined || ["left", "center", "right"].includes(cell.horizontalAlign)) && (cell.verticalAlign === undefined || ["top", "middle", "bottom"].includes(cell.verticalAlign)));
1958
+ }
1959
+ function createUpdateTableCellPatch(state, id, row, column, update) {
1960
+ const table = state.entities.tablesById.get(id);
1961
+ if (!table || isEntityLockedOrReadOnly(state, id) || !Number.isInteger(row) || !Number.isInteger(column) || row < 0 || row >= table.rowCount || column < 0 || column >= table.columnCount)
1962
+ return null;
1963
+ if (Object.keys(update).some((key) => !["text", "horizontalAlign", "verticalAlign"].includes(key)))
1964
+ return null;
1965
+ const cells = table.cells.map((line) => [...line]);
1966
+ const previous = cells[row][column];
1967
+ const nextCell = { ...previous, ...update };
1968
+ if (!isValidCell(nextCell))
1969
+ return null;
1970
+ cells[row][column] = nextCell;
1971
+ return createTableSnapshotPatch(id, table, { ...table, cells });
1972
+ }
1973
+ function createResizeTableAxisPatch(state, id, axis, index, size) {
1974
+ const table = state.entities.tablesById.get(id);
1975
+ const count = axis === "row" ? table === null || table === void 0 ? void 0 : table.rowCount : table === null || table === void 0 ? void 0 : table.columnCount;
1976
+ if (!table || isEntityLockedOrReadOnly(state, id) || !Number.isInteger(index) || index < 0 || index >= count || !Number.isFinite(size) || size <= 0)
1977
+ return null;
1978
+ if (axis === "row") {
1979
+ const rowHeights = [...table.rowHeights];
1980
+ rowHeights[index] = size;
1981
+ return createTableSnapshotPatch(id, table, { ...table, rowHeights, height: rowHeights.reduce((sum, value) => sum + value, 0) });
1982
+ }
1983
+ const columnWidths = [...table.columnWidths];
1984
+ columnWidths[index] = size;
1985
+ return createTableSnapshotPatch(id, table, { ...table, columnWidths, width: columnWidths.reduce((sum, value) => sum + value, 0) });
1986
+ }
1987
+ function createUpdateTableStylePatch(state, id, update) {
1988
+ var _a, _b;
1989
+ const table = state.entities.tablesById.get(id);
1990
+ if (!table || isEntityLockedOrReadOnly(state, id) || !validTableStyleUpdateShape(update))
1991
+ return null;
1992
+ const baseStyle = normalizeTableStyle(table.style);
1993
+ const style = normalizeTableStyle({
1994
+ ...baseStyle,
1995
+ ...update,
1996
+ grid: { ...baseStyle.grid, ...update.grid, stroke: { ...baseStyle.grid.stroke, ...(_a = update.grid) === null || _a === void 0 ? void 0 : _a.stroke } },
1997
+ border: { ...baseStyle.border, ...update.border, stroke: { ...baseStyle.border.stroke, ...(_b = update.border) === null || _b === void 0 ? void 0 : _b.stroke } },
1998
+ });
1999
+ try {
2000
+ validateDiagramTableStyle(style, id);
2001
+ }
2002
+ catch {
2003
+ return null;
2004
+ }
2005
+ return createTableSnapshotPatch(id, table, { ...table, style });
2006
+ }
2007
+ function validTableStyleUpdateShape(update) {
2008
+ if (!update || typeof update !== "object")
2009
+ return false;
2010
+ const value = update;
2011
+ const grid = value.grid;
2012
+ const border = value.border;
2013
+ const validStroke = (stroke) => Boolean(stroke && typeof stroke === "object" && Object.keys(stroke).every((key) => ["color", "width", "style"].includes(key)));
2014
+ return Object.keys(value).every((key) => ["grid", "border", "cellPadding"].includes(key))
2015
+ && (value.grid === undefined || Boolean(grid && Object.keys(grid).every((key) => ["visible", "stroke"].includes(key)) && (grid.stroke === undefined || validStroke(grid.stroke))))
2016
+ && (value.border === undefined || Boolean(border && Object.keys(border).every((key) => ["visible", "stroke", "radius"].includes(key)) && (border.stroke === undefined || validStroke(border.stroke))));
2017
+ }
2018
+ function resizeTable(table, entry) {
2019
+ var _a, _b, _c, _d;
2020
+ const width = (_a = entry.width) !== null && _a !== void 0 ? _a : table.width;
2021
+ const height = (_b = entry.height) !== null && _b !== void 0 ? _b : table.height;
2022
+ const sx = width / table.width;
2023
+ const sy = height / table.height;
2024
+ return { ...table, x: (_c = entry.x) !== null && _c !== void 0 ? _c : table.x, y: (_d = entry.y) !== null && _d !== void 0 ? _d : table.y, width, height, columnWidths: table.columnWidths.map((size) => size * sx), rowHeights: table.rowHeights.map((size) => size * sy) };
2025
+ }
2026
+ function createGroupEntitiesPatch(state, id, ids) {
2027
+ const childIds = [...ids];
2028
+ if (!isUniqueId(state, id) || childIds.length < 2 || new Set(childIds).size !== childIds.length)
2029
+ return null;
2030
+ const groupableIds = new Set([
2031
+ ...state.entities.nodesById.keys(),
2032
+ ...state.entities.edgesById.keys(),
2033
+ ...state.entities.textsById.keys(),
2034
+ ...state.entities.shapesById.keys(),
2035
+ ...state.entities.tablesById.keys(),
2036
+ ]);
2037
+ const alreadyGrouped = new Set([...state.entities.groupsById.values()].flatMap((group) => group.childIds));
2038
+ if (childIds.some((childId) => !groupableIds.has(childId) || alreadyGrouped.has(childId)))
2039
+ return null;
2040
+ const group = { id, kind: "group", childIds };
2041
+ const previousSelection = state.selection;
2042
+ const nextSelection = { ids: [id], primaryId: id };
2043
+ return {
2044
+ orderChanged: true,
2045
+ apply: (next) => {
2046
+ next.entities.groupsById.set(id, group);
2047
+ next.order.groupIds.push(id);
2048
+ next.versions.set(id, 0);
2049
+ next.selection = nextSelection;
2050
+ return new Set([id, "selection"]);
2051
+ },
2052
+ inverse: {
2053
+ orderChanged: true,
2054
+ apply: (next) => {
2055
+ next.entities.groupsById.delete(id);
2056
+ next.order.groupIds = next.order.groupIds.filter((groupId) => groupId !== id);
2057
+ next.versions.delete(id);
2058
+ next.selection = previousSelection;
2059
+ return new Set([id, "selection"]);
2060
+ },
2061
+ inverse: undefined,
2062
+ },
2063
+ };
2064
+ }
2065
+ function createUngroupEntitiesPatch(state, ids) {
2066
+ var _a;
2067
+ const groupIds = [...new Set(ids)];
2068
+ if (!groupIds.length || groupIds.some((id) => !state.entities.groupsById.has(id) || isEntityLockedOrReadOnly(state, id)))
2069
+ return null;
2070
+ const groups = groupIds.map((id) => state.entities.groupsById.get(id));
2071
+ const previousSelection = state.selection;
2072
+ const childIds = [...new Set(groups.flatMap((group) => group.childIds))];
2073
+ const nextSelection = { ids: childIds, primaryId: (_a = childIds[0]) !== null && _a !== void 0 ? _a : null };
2074
+ const positions = new Map(groupIds.map((id) => [id, state.order.groupIds.indexOf(id)]));
2075
+ return {
2076
+ orderChanged: true,
2077
+ apply: (next) => {
2078
+ for (const id of groupIds)
2079
+ next.entities.groupsById.delete(id);
2080
+ next.order.groupIds = next.order.groupIds.filter((id) => !groupIds.includes(id));
2081
+ for (const id of groupIds)
2082
+ next.versions.delete(id);
2083
+ next.selection = nextSelection;
2084
+ return new Set([...groupIds, "selection"]);
2085
+ },
2086
+ inverse: {
2087
+ orderChanged: true,
2088
+ apply: (next) => {
2089
+ var _a;
2090
+ for (const group of groups) {
2091
+ next.entities.groupsById.set(group.id, group);
2092
+ const position = (_a = positions.get(group.id)) !== null && _a !== void 0 ? _a : next.order.groupIds.length;
2093
+ next.order.groupIds.splice(Math.max(0, Math.min(position, next.order.groupIds.length)), 0, group.id);
2094
+ next.versions.set(group.id, 0);
2095
+ }
2096
+ next.selection = previousSelection;
2097
+ return new Set([...groupIds, "selection"]);
2098
+ },
2099
+ inverse: undefined,
2100
+ },
2101
+ };
2102
+ }
1640
2103
  function createCreatePortPatch(state, port) {
1641
2104
  var _a;
1642
2105
  const owner = state.entities.nodesById.get(port.nodeId);
@@ -1695,6 +2158,58 @@ function createReconnectEdgeEndpointPatch(state, id, endpoint, connection) {
1695
2158
  };
1696
2159
  }
1697
2160
  function isUniqueId(state, id) { return typeof id === "string" && id.length > 0 && !hasEntity(state, id); }
2161
+ function groupMembers(state, id) { var _a, _b; return (_b = (_a = state.entities.groupsById.get(id)) === null || _a === void 0 ? void 0 : _a.childIds) !== null && _b !== void 0 ? _b : []; }
2162
+ function expandGroupEntityIds(state, ids, includeGroups) {
2163
+ const expanded = new Set();
2164
+ for (const id of ids) {
2165
+ const group = state.entities.groupsById.get(id);
2166
+ if (!group) {
2167
+ expanded.add(id);
2168
+ continue;
2169
+ }
2170
+ if (includeGroups)
2171
+ expanded.add(id);
2172
+ for (const childId of group.childIds)
2173
+ expanded.add(childId);
2174
+ }
2175
+ return [...expanded];
2176
+ }
2177
+ function isGroupMovable(state, id) {
2178
+ const children = groupMembers(state, id);
2179
+ return children.length > 0
2180
+ && children.every((childId) => !isEntityLockedOrReadOnly(state, childId) && (isEntityTranslatable(state.entities, childId) || state.entities.edgesById.has(childId)))
2181
+ && children.some((childId) => isEntityTranslatable(state.entities, childId));
2182
+ }
2183
+ function isGroupResizableEntity(state, id) {
2184
+ return state.entities.nodesById.has(id) || state.entities.shapesById.has(id) || state.entities.textsById.has(id) || state.entities.edgesById.has(id) || state.entities.tablesById.has(id);
2185
+ }
2186
+ function isEntityTranslatable(entities, id) {
2187
+ var _a;
2188
+ if (entities.nodesById.has(id) || entities.textsById.has(id) || entities.shapesById.has(id) || entities.tablesById.has(id))
2189
+ return true;
2190
+ const edge = entities.edgesById.get(id);
2191
+ return Boolean(edge && (edge.source.kind === "point" || edge.target.kind === "point" || ((_a = edge.points) === null || _a === void 0 ? void 0 : _a.length)));
2192
+ }
2193
+ function createEntitySnapshotPatch(ids, previous, nextValues, setValue) {
2194
+ const applyValues = (state, values) => {
2195
+ for (const id of ids) {
2196
+ const value = values.get(id);
2197
+ if (value !== undefined)
2198
+ setValue(state, id, value);
2199
+ }
2200
+ return new Set(ids);
2201
+ };
2202
+ return {
2203
+ apply: (state) => applyValues(state, nextValues),
2204
+ inverse: { apply: (state) => applyValues(state, previous), inverse: undefined },
2205
+ };
2206
+ }
2207
+ function diagramEdgesEqual(first, second) {
2208
+ var _a, _b;
2209
+ return JSON.stringify(first.source) === JSON.stringify(second.source)
2210
+ && JSON.stringify(first.target) === JSON.stringify(second.target)
2211
+ && JSON.stringify((_a = first.points) !== null && _a !== void 0 ? _a : []) === JSON.stringify((_b = second.points) !== null && _b !== void 0 ? _b : []);
2212
+ }
1698
2213
  function isValidEndpoint(state, endpoint) {
1699
2214
  if (endpoint.kind === "point")
1700
2215
  return Number.isFinite(endpoint.point.x) && Number.isFinite(endpoint.point.y);
@@ -1708,6 +2223,13 @@ function isValidEdge(state, edge) {
1708
2223
  return isValidEndpoint(state, edge.source) && isValidEndpoint(state, edge.target) && ((_a = edge.points) !== null && _a !== void 0 ? _a : []).every((point) => Number.isFinite(point.x) && Number.isFinite(point.y));
1709
2224
  }
1710
2225
  function commandIds(command) {
2226
+ var _a;
2227
+ if (command.type === "createTable")
2228
+ return [command.table.id];
2229
+ if (command.type === "groupEntities")
2230
+ return [command.id, ...((_a = command.ids) !== null && _a !== void 0 ? _a : [])];
2231
+ if (command.type === "ungroupEntities")
2232
+ return [...command.ids];
1711
2233
  if ("id" in command)
1712
2234
  return [command.id];
1713
2235
  if ("ids" in command)
@@ -1725,11 +2247,18 @@ function commandIds(command) {
1725
2247
  function createDeleteSelectionPatch(state) {
1726
2248
  const candidates = state.selection.ids.filter((id) => hasEntity(state, id) && !isEntityNonDeletable(state, id) && !isEntityLockedOrReadOnly(state, id));
1727
2249
  const safe = candidates.filter((id) => {
1728
- const node = state.entities.nodesById.get(id);
1729
- if (node) {
1730
- if ([...state.entities.portsById.values()].some((port) => port.nodeId === id && (isEntityNonDeletable(state, port.id) || isEntityLockedOrReadOnly(state, port.id))))
2250
+ const group = state.entities.groupsById.get(id);
2251
+ const groupedIds = group ? groupMembers(state, id) : [];
2252
+ if (group && groupedIds.some((childId) => isEntityNonDeletable(state, childId) || isEntityLockedOrReadOnly(state, childId)))
2253
+ return false;
2254
+ const ownerGroup = [...state.entities.groupsById.values()].find((candidate) => candidate.childIds.includes(id));
2255
+ if (ownerGroup && groupMembers(state, ownerGroup.id).some((childId) => isEntityNonDeletable(state, childId) || isEntityLockedOrReadOnly(state, childId)))
2256
+ return false;
2257
+ const nodeIds = new Set([id, ...groupedIds].filter((nodeId) => state.entities.nodesById.has(nodeId)));
2258
+ if (nodeIds.size) {
2259
+ if ([...state.entities.portsById.values()].some((port) => nodeIds.has(port.nodeId) && (isEntityNonDeletable(state, port.id) || isEntityLockedOrReadOnly(state, port.id))))
1731
2260
  return false;
1732
- if ([...state.entities.edgesById.values()].some((edge) => (endpointDeleted(edge.source, new Set([id])) || endpointDeleted(edge.target, new Set([id]))) && (isEntityNonDeletable(state, edge.id) || isEntityLockedOrReadOnly(state, edge.id))))
2261
+ if ([...state.entities.edgesById.values()].some((edge) => (endpointDeleted(edge.source, nodeIds) || endpointDeleted(edge.target, nodeIds)) && (isEntityNonDeletable(state, edge.id) || isEntityLockedOrReadOnly(state, edge.id))))
1733
2262
  return false;
1734
2263
  }
1735
2264
  return true;
@@ -1766,12 +2295,16 @@ function createDeleteEntitiesPatch(ids) {
1766
2295
  textsById: pick(next.entities.textsById, ids),
1767
2296
  shapesById: pick(next.entities.shapesById, ids),
1768
2297
  portsById: pick(next.entities.portsById, ids),
2298
+ groupsById: pick(next.entities.groupsById, ids),
2299
+ tablesById: pick(next.entities.tablesById, ids),
1769
2300
  },
1770
2301
  order: {
1771
2302
  nodeIds: next.order.nodeIds.filter((id) => ids.includes(id)),
1772
2303
  edgeIds: next.order.edgeIds.filter((id) => ids.includes(id)),
1773
2304
  textIds: next.order.textIds.filter((id) => ids.includes(id)),
1774
2305
  shapeIds: next.order.shapeIds.filter((id) => ids.includes(id)),
2306
+ groupIds: next.order.groupIds.filter((id) => ids.includes(id)),
2307
+ tableIds: next.order.tableIds.filter((id) => ids.includes(id)),
1775
2308
  },
1776
2309
  selection: next.selection,
1777
2310
  viewport: next.viewport,
@@ -1897,6 +2430,8 @@ function createReplaceScenePatch(next, previous) {
1897
2430
  ...replacement.order.textIds,
1898
2431
  ...replacement.order.shapeIds,
1899
2432
  ...replacement.entities.portsById.keys(),
2433
+ ...replacement.order.groupIds,
2434
+ ...replacement.order.tableIds,
1900
2435
  ]);
1901
2436
  },
1902
2437
  inverse: {
@@ -1915,6 +2450,8 @@ function createReplaceScenePatch(next, previous) {
1915
2450
  ...replacement.order.textIds,
1916
2451
  ...replacement.order.shapeIds,
1917
2452
  ...replacement.entities.portsById.keys(),
2453
+ ...replacement.order.groupIds,
2454
+ ...replacement.order.tableIds,
1918
2455
  ]);
1919
2456
  },
1920
2457
  inverse: undefined,
@@ -1923,6 +2460,20 @@ function createReplaceScenePatch(next, previous) {
1923
2460
  }
1924
2461
  function expandDeletedIds(state, ids) {
1925
2462
  const deleted = new Set(ids.filter((id) => !isEntityNonDeletable(state, id) && !isEntityLockedOrReadOnly(state, id)));
2463
+ let groupsChanged = true;
2464
+ while (groupsChanged) {
2465
+ groupsChanged = false;
2466
+ for (const group of state.entities.groupsById.values()) {
2467
+ if (!deleted.has(group.id) && !group.childIds.some((childId) => deleted.has(childId)))
2468
+ continue;
2469
+ for (const id of [group.id, ...group.childIds]) {
2470
+ if (!deleted.has(id)) {
2471
+ deleted.add(id);
2472
+ groupsChanged = true;
2473
+ }
2474
+ }
2475
+ }
2476
+ }
1926
2477
  for (const id of [...deleted]) {
1927
2478
  if (state.entities.nodesById.has(id)) {
1928
2479
  for (const port of state.entities.portsById.values()) {
@@ -1974,10 +2525,10 @@ function composePatches(first, second) {
1974
2525
  };
1975
2526
  }
1976
2527
  function cloneOrder(order) {
1977
- return { nodeIds: [...order.nodeIds], edgeIds: [...order.edgeIds], textIds: [...order.textIds], shapeIds: [...order.shapeIds] };
2528
+ return { nodeIds: [...order.nodeIds], edgeIds: [...order.edgeIds], textIds: [...order.textIds], shapeIds: [...order.shapeIds], groupIds: [...order.groupIds], tableIds: [...order.tableIds] };
1978
2529
  }
1979
2530
  function ordersEqual(a, b) {
1980
- return sameIds(a.nodeIds, b.nodeIds) && sameIds(a.edgeIds, b.edgeIds) && sameIds(a.textIds, b.textIds) && sameIds(a.shapeIds, b.shapeIds);
2531
+ return sameIds(a.nodeIds, b.nodeIds) && sameIds(a.edgeIds, b.edgeIds) && sameIds(a.textIds, b.textIds) && sameIds(a.shapeIds, b.shapeIds) && sameIds(a.groupIds, b.groupIds) && sameIds(a.tableIds, b.tableIds);
1981
2532
  }
1982
2533
  function reorderIds(order, ids, mode) {
1983
2534
  if (order.length < 2 || !ids.length) {
@@ -2042,10 +2593,16 @@ function setEntityMetadata(state, id, metadata) {
2042
2593
  const port = state.entities.portsById.get(id);
2043
2594
  if (port)
2044
2595
  state.entities.portsById.set(id, { ...port, metadata });
2596
+ const group = state.entities.groupsById.get(id);
2597
+ if (group)
2598
+ state.entities.groupsById.set(id, { ...group, metadata });
2599
+ const table = state.entities.tablesById.get(id);
2600
+ if (table)
2601
+ state.entities.tablesById.set(id, { ...table, metadata });
2045
2602
  }
2046
2603
  function getEntityMetadata(state, id) {
2047
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2048
- return (_h = (_f = (_d = (_b = (_a = state.entities.nodesById.get(id)) === null || _a === void 0 ? void 0 : _a.metadata) !== null && _b !== void 0 ? _b : (_c = state.entities.edgesById.get(id)) === null || _c === void 0 ? void 0 : _c.metadata) !== null && _d !== void 0 ? _d : (_e = state.entities.textsById.get(id)) === null || _e === void 0 ? void 0 : _e.metadata) !== null && _f !== void 0 ? _f : (_g = state.entities.shapesById.get(id)) === null || _g === void 0 ? void 0 : _g.metadata) !== null && _h !== void 0 ? _h : (_j = state.entities.portsById.get(id)) === null || _j === void 0 ? void 0 : _j.metadata;
2604
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
2605
+ return (_m = (_k = (_h = (_f = (_d = (_b = (_a = state.entities.nodesById.get(id)) === null || _a === void 0 ? void 0 : _a.metadata) !== null && _b !== void 0 ? _b : (_c = state.entities.edgesById.get(id)) === null || _c === void 0 ? void 0 : _c.metadata) !== null && _d !== void 0 ? _d : (_e = state.entities.textsById.get(id)) === null || _e === void 0 ? void 0 : _e.metadata) !== null && _f !== void 0 ? _f : (_g = state.entities.shapesById.get(id)) === null || _g === void 0 ? void 0 : _g.metadata) !== null && _h !== void 0 ? _h : (_j = state.entities.portsById.get(id)) === null || _j === void 0 ? void 0 : _j.metadata) !== null && _k !== void 0 ? _k : (_l = state.entities.groupsById.get(id)) === null || _l === void 0 ? void 0 : _l.metadata) !== null && _m !== void 0 ? _m : (_o = state.entities.tablesById.get(id)) === null || _o === void 0 ? void 0 : _o.metadata;
2049
2606
  }
2050
2607
  function zIndex(metadata) {
2051
2608
  return typeof (metadata === null || metadata === void 0 ? void 0 : metadata.zIndex) === "number" && Number.isFinite(metadata.zIndex) ? metadata.zIndex : 0;
@@ -2057,10 +2614,12 @@ function allEntityIds(state) {
2057
2614
  ...state.order.nodeIds,
2058
2615
  ...state.order.textIds,
2059
2616
  ...state.entities.portsById.keys(),
2617
+ ...state.entities.groupsById.keys(),
2618
+ ...state.entities.tablesById.keys(),
2060
2619
  ];
2061
2620
  }
2062
2621
  function hasEntity(state, id) {
2063
- return state.entities.nodesById.has(id) || state.entities.edgesById.has(id) || state.entities.textsById.has(id) || state.entities.shapesById.has(id) || state.entities.portsById.has(id);
2622
+ return state.entities.nodesById.has(id) || state.entities.edgesById.has(id) || state.entities.textsById.has(id) || state.entities.shapesById.has(id) || state.entities.portsById.has(id) || state.entities.groupsById.has(id) || state.entities.tablesById.has(id);
2064
2623
  }
2065
2624
  function isEntityLockedOrReadOnly(state, id) {
2066
2625
  const metadata = getEntityMetadata(state, id);
@@ -2092,6 +2651,7 @@ function now() {
2092
2651
  return typeof performance !== "undefined" ? performance.now() : Date.now();
2093
2652
  }
2094
2653
  function restoreSerialized(state, scene) {
2654
+ var _a, _b;
2095
2655
  const changed = new Set();
2096
2656
  for (const node of scene.nodes) {
2097
2657
  state.entities.nodesById.set(node.id, node);
@@ -2122,6 +2682,18 @@ function restoreSerialized(state, scene) {
2122
2682
  state.versions.set(port.id, 0);
2123
2683
  changed.add(port.id);
2124
2684
  }
2685
+ for (const group of (_a = scene.groups) !== null && _a !== void 0 ? _a : []) {
2686
+ state.entities.groupsById.set(group.id, group);
2687
+ pushUnique(state.order.groupIds, group.id);
2688
+ state.versions.set(group.id, 0);
2689
+ changed.add(group.id);
2690
+ }
2691
+ for (const table of (_b = scene.tables) !== null && _b !== void 0 ? _b : []) {
2692
+ state.entities.tablesById.set(table.id, table);
2693
+ pushUnique(state.order.tableIds, table.id);
2694
+ state.versions.set(table.id, 0);
2695
+ changed.add(table.id);
2696
+ }
2125
2697
  state.selection = scene.selection;
2126
2698
  return changed;
2127
2699
  }
@@ -2131,10 +2703,14 @@ function deleteEntity(state, id) {
2131
2703
  deleted = state.entities.textsById.delete(id) || deleted;
2132
2704
  deleted = state.entities.shapesById.delete(id) || deleted;
2133
2705
  deleted = state.entities.portsById.delete(id) || deleted;
2706
+ deleted = state.entities.groupsById.delete(id) || deleted;
2707
+ deleted = state.entities.tablesById.delete(id) || deleted;
2134
2708
  state.order.nodeIds = state.order.nodeIds.filter((value) => value !== id);
2135
2709
  state.order.edgeIds = state.order.edgeIds.filter((value) => value !== id);
2136
2710
  state.order.textIds = state.order.textIds.filter((value) => value !== id);
2137
2711
  state.order.shapeIds = state.order.shapeIds.filter((value) => value !== id);
2712
+ state.order.groupIds = state.order.groupIds.filter((value) => value !== id);
2713
+ state.order.tableIds = state.order.tableIds.filter((value) => value !== id);
2138
2714
  state.versions.delete(id);
2139
2715
  return deleted;
2140
2716
  }