js.documents 8.0.1 → 10.0.0

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 (39) hide show
  1. package/dist/edit/doc/table.cjs +10 -0
  2. package/dist/edit/doc/table.d.cts +4 -0
  3. package/dist/edit/doc/table.d.ts +4 -0
  4. package/dist/edit/doc/table.js +10 -0
  5. package/dist/edit/docx/content.cjs +2 -0
  6. package/dist/edit/docx/content.js +2 -0
  7. package/dist/edit/docx/table.cjs +110 -22
  8. package/dist/edit/docx/table.d.cts +5 -0
  9. package/dist/edit/docx/table.d.ts +5 -0
  10. package/dist/edit/docx/table.js +110 -22
  11. package/dist/edit/markdown/table.cjs +10 -0
  12. package/dist/edit/markdown/table.d.cts +4 -0
  13. package/dist/edit/markdown/table.d.ts +4 -0
  14. package/dist/edit/markdown/table.js +10 -0
  15. package/dist/edit/odp/slide.d.cts +1 -1
  16. package/dist/edit/odp/slide.d.ts +1 -1
  17. package/dist/edit/odt/content.cjs +2 -0
  18. package/dist/edit/odt/content.d.cts +1 -1
  19. package/dist/edit/odt/content.d.ts +1 -1
  20. package/dist/edit/odt/content.js +2 -0
  21. package/dist/edit/odt/editor.d.cts +1 -1
  22. package/dist/edit/odt/editor.d.ts +1 -1
  23. package/dist/edit/odt/table.cjs +45 -15
  24. package/dist/edit/odt/table.d.cts +1 -1
  25. package/dist/edit/odt/table.d.ts +1 -1
  26. package/dist/edit/odt/table.js +45 -15
  27. package/dist/edit/pptx/content.cjs +2 -0
  28. package/dist/edit/pptx/content.js +2 -0
  29. package/dist/edit/table-grid.cjs +66 -0
  30. package/dist/edit/table-grid.d.cts +2 -0
  31. package/dist/edit/table-grid.d.ts +2 -0
  32. package/dist/edit/table-grid.js +63 -0
  33. package/dist/index.d.cts +3 -2
  34. package/dist/index.d.ts +3 -2
  35. package/dist/{table-qWkzpadC.d.ts → table-BMYJo_-F.d.ts} +5 -1
  36. package/dist/{table-DvKlZmG0.d.cts → table-CCjF09Ho.d.cts} +5 -1
  37. package/dist/table-grid-Xbaddzz3.d.cts +49 -0
  38. package/dist/table-grid-Xbaddzz3.d.ts +49 -0
  39. package/package.json +13 -13
@@ -1,4 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_edit_table_grid = require("../table-grid.cjs");
2
3
  const require_edit_doc_paragraph = require("./paragraph.cjs");
3
4
  let document_schema_js = require("document-schema.js");
4
5
  //#region src/edit/doc/table.ts
@@ -69,6 +70,15 @@ var DocTable = class {
69
70
  rows() {
70
71
  return this.live().rows.map((row) => new DocTableRow(row));
71
72
  }
73
+ gridRows() {
74
+ return this.grid().rows;
75
+ }
76
+ gridColumnCount() {
77
+ return this.grid().columnCount;
78
+ }
79
+ grid() {
80
+ return require_edit_table_grid.resolvePivotTableGrid(this.live(), (cell) => new DocTableCell(cell));
81
+ }
72
82
  appendRow() {
73
83
  const node = this.live();
74
84
  const row = buildRow(node.columnWidthsPt.length);
@@ -1,4 +1,5 @@
1
1
  import { n as ParagraphInit, t as DocParagraph } from "../../paragraph-DM9n9I4T.cjs";
2
+ import { i as TableGridRows } from "../../table-grid-Xbaddzz3.cjs";
2
3
  import { ContentBlock, ContentTable, ContentTableCell, ContentTableRow } from "document-schema.js";
3
4
  //#region src/edit/doc/table.d.ts
4
5
  interface TableInit {
@@ -29,6 +30,9 @@ declare class DocTable {
29
30
  constructor(container: ContentBlock[], node: ContentTable);
30
31
  private live;
31
32
  rows(): DocTableRow[];
33
+ gridRows(): TableGridRows<DocTableCell>;
34
+ gridColumnCount(): number;
35
+ private grid;
32
36
  appendRow(): DocTableRow;
33
37
  mergeCells(startRow: number, startColumn: number, rowSpan: number, colSpan: number): DocTableCell;
34
38
  remove(): void;
@@ -1,4 +1,5 @@
1
1
  import { n as ParagraphInit, t as DocParagraph } from "../../paragraph-Byw2Y7xu.js";
2
+ import { i as TableGridRows } from "../../table-grid-Xbaddzz3.js";
2
3
  import { ContentBlock, ContentTable, ContentTableCell, ContentTableRow } from "document-schema.js";
3
4
  //#region src/edit/doc/table.d.ts
4
5
  interface TableInit {
@@ -29,6 +30,9 @@ declare class DocTable {
29
30
  constructor(container: ContentBlock[], node: ContentTable);
30
31
  private live;
31
32
  rows(): DocTableRow[];
33
+ gridRows(): TableGridRows<DocTableCell>;
34
+ gridColumnCount(): number;
35
+ private grid;
32
36
  appendRow(): DocTableRow;
33
37
  mergeCells(startRow: number, startColumn: number, rowSpan: number, colSpan: number): DocTableCell;
34
38
  remove(): void;
@@ -1,3 +1,4 @@
1
+ import { resolvePivotTableGrid } from "../table-grid.js";
1
2
  import { DocParagraph, buildParagraph } from "./paragraph.js";
2
3
  import { tableCellColumnSpan, tableCellRowSpan, tableGridColumnCount, walkTableGrid } from "document-schema.js";
3
4
  //#region src/edit/doc/table.ts
@@ -68,6 +69,15 @@ var DocTable = class {
68
69
  rows() {
69
70
  return this.live().rows.map((row) => new DocTableRow(row));
70
71
  }
72
+ gridRows() {
73
+ return this.grid().rows;
74
+ }
75
+ gridColumnCount() {
76
+ return this.grid().columnCount;
77
+ }
78
+ grid() {
79
+ return resolvePivotTableGrid(this.live(), (cell) => new DocTableCell(cell));
80
+ }
71
81
  appendRow() {
72
82
  const node = this.live();
73
83
  const row = buildRow(node.columnWidthsPt.length);
@@ -6,6 +6,7 @@ const require_ports_clock = require("../../ports/clock.cjs");
6
6
  const require_model_units = require("../../model/units.cjs");
7
7
  const require_edit_docx_image = require("./image.cjs");
8
8
  const require_edit_docx_scaffold = require("./scaffold.cjs");
9
+ const require_edit_table_grid = require("../table-grid.cjs");
9
10
  const require_edit_docx_editor = require("./editor.cjs");
10
11
  const require_model_embedded_drawing = require("../../model/embedded-drawing.cjs");
11
12
  const require_model_formula = require("../../model/formula.cjs");
@@ -208,6 +209,7 @@ function docxRowCells(positions, positionsByRow) {
208
209
  });
209
210
  }
210
211
  function appendTable(body, block) {
212
+ require_edit_table_grid.assertTableObeysGridRule(block, "buildDocxPackage");
211
213
  const columns = block.columnWidthsPt.length;
212
214
  if (block.rows.length === 0 || columns === 0) return;
213
215
  const table = body.appendTable({
@@ -5,6 +5,7 @@ import { systemClock } from "../../ports/clock.js";
5
5
  import { ptToTwips } from "../../model/units.js";
6
6
  import { assertDocxWritableImageFormat } from "./image.js";
7
7
  import { createEmptyDocxPackage } from "./scaffold.js";
8
+ import { assertTableObeysGridRule } from "../table-grid.js";
8
9
  import { DocxEditor } from "./editor.js";
9
10
  import { FLOW_CONTAINER_ORIGIN, drawingOfBlock, embeddedDrawingVectors } from "../../model/embedded-drawing.js";
10
11
  import { formulaOfBlock, formulaPlaceholderText } from "../../model/formula.js";
@@ -207,6 +208,7 @@ function docxRowCells(positions, positionsByRow) {
207
208
  });
208
209
  }
209
210
  function appendTable(body, block) {
211
+ assertTableObeysGridRule(block, "buildDocxPackage");
210
212
  const columns = block.columnWidthsPt.length;
211
213
  if (block.rows.length === 0 || columns === 0) return;
212
214
  const table = body.appendTable({
@@ -3,6 +3,7 @@ const require_xml_fragment = require("../../xml/fragment.cjs");
3
3
  const require_model_units = require("../../model/units.cjs");
4
4
  const require_xml_edit = require("../../xml/edit.cjs");
5
5
  const require_edit_docx_paragraph = require("./paragraph.cjs");
6
+ const require_edit_table_grid = require("../table-grid.cjs");
6
7
  let ooxml_js = require("ooxml.js");
7
8
  let document_schema_js = require("document-schema.js");
8
9
  //#region src/edit/docx/table.ts
@@ -197,23 +198,93 @@ var DocxTableRow = class {
197
198
  }
198
199
  mergeCellsHorizontally(startColumnIndex, colSpan) {
199
200
  if (!Number.isInteger(colSpan) || colSpan < 1) throw new Error(`mergeCellsHorizontally: colSpan must be a positive integer, got ${colSpan}`);
200
- const cellElements = [];
201
- for (const child of this.node.children) if (child.type === "element" && child.tag === "w:tc") cellElements.push(child);
202
- const anchorElement = cellElements[startColumnIndex];
203
- if (anchorElement === void 0) throw new Error(`mergeCellsHorizontally: column ${startColumnIndex} does not exist in this row`);
204
- if (startColumnIndex + colSpan > cellElements.length) throw new Error(`mergeCellsHorizontally: colSpan ${colSpan} starting at column ${startColumnIndex} exceeds this row's own ${cellElements.length} columns`);
205
- for (let i = 1; i < colSpan; i++) {
206
- const consumedElement = cellElements[startColumnIndex + i];
207
- if (consumedElement !== void 0) require_xml_edit.removeChild(this.node.children, consumedElement);
208
- }
209
- const anchor = new DocxTableCell(anchorElement);
210
- anchor.colSpan = colSpan;
211
- return anchor;
201
+ const plan = planHorizontalMerge(this.node, startColumnIndex, colSpan, false);
202
+ if ("refusal" in plan) throw new Error(`mergeCellsHorizontally: ${plan.refusal}`);
203
+ return applyHorizontalMerge(plan, colSpan);
212
204
  }
213
205
  };
206
+ function rowCells(row) {
207
+ const out = [];
208
+ let column = 0;
209
+ for (const child of row.children) if (child.type === "element" && child.tag === "w:tc") {
210
+ const cell = new DocxTableCell(child);
211
+ const span = cell.colSpan ?? 1;
212
+ out.push({
213
+ element: child,
214
+ startColumn: column,
215
+ span,
216
+ takesPartInVerticalMerge: cell.verticalMerge !== void 0
217
+ });
218
+ column += span;
219
+ }
220
+ return out;
221
+ }
222
+ function refuse(refusal) {
223
+ return { refusal };
224
+ }
225
+ function planHorizontalMerge(row, startColumn, colSpan, joinsVerticalMerge) {
226
+ const cells = rowCells(row);
227
+ const last = cells[cells.length - 1];
228
+ const width = last === void 0 ? 0 : last.startColumn + last.span;
229
+ const anchor = cells.find((cell) => cell.startColumn <= startColumn && startColumn < cell.startColumn + cell.span);
230
+ if (anchor === void 0) return refuse(`column ${startColumn} does not exist in this row`);
231
+ if (anchor.startColumn !== startColumn) return refuse(`column ${startColumn} is covered by the cell starting at column ${anchor.startColumn}`);
232
+ const endColumn = startColumn + colSpan;
233
+ if (endColumn > width) return refuse(`colSpan ${colSpan} starting at column ${startColumn} exceeds this row's own ${width} grid columns`);
234
+ const region = cells.filter((cell) => cell.startColumn >= startColumn && cell.startColumn < endColumn);
235
+ const straddling = region.find((cell) => cell.startColumn + cell.span > endColumn);
236
+ if (straddling !== void 0) return refuse(`the cell at column ${straddling.startColumn} spans columns ${straddling.startColumn} to ${straddling.startColumn + straddling.span - 1}, past the last merged column ${endColumn - 1}`);
237
+ const consumed = region.filter((cell) => cell !== anchor);
238
+ const verticallyMerged = joinsVerticalMerge || consumed.length > 0 ? region.find((cell) => cell.takesPartInVerticalMerge) : void 0;
239
+ if (verticallyMerged !== void 0) return refuse(`column ${verticallyMerged.startColumn} takes part in a vertical merge and cannot be merged over`);
240
+ return {
241
+ row,
242
+ anchor: anchor.element,
243
+ consumed: consumed.map((cell) => cell.element)
244
+ };
245
+ }
246
+ function applyHorizontalMerge(plan, colSpan) {
247
+ for (const consumed of plan.consumed) require_xml_edit.removeChild(plan.row.children, consumed);
248
+ const anchor = new DocxTableCell(plan.anchor);
249
+ anchor.colSpan = colSpan;
250
+ return anchor;
251
+ }
252
+ function physicalCells(cells) {
253
+ let column = 0;
254
+ return cells.map((cell) => {
255
+ const columnIndex = column;
256
+ column += cell.colSpan ?? 1;
257
+ return {
258
+ cell,
259
+ columnIndex,
260
+ continuesVerticalMerge: cell.verticalMerge === "continue"
261
+ };
262
+ });
263
+ }
264
+ function verticalSpan(rowsBelow, columnIndex) {
265
+ let span = 1;
266
+ for (const row of rowsBelow) {
267
+ if (!row.find((cell) => cell.columnIndex === columnIndex)?.continuesVerticalMerge) break;
268
+ span++;
269
+ }
270
+ return span;
271
+ }
272
+ function placeCells(rows) {
273
+ const physical = rows.map(physicalCells);
274
+ return physical.map((row, rowIndex) => row.map(({ cell, columnIndex, continuesVerticalMerge }) => ({
275
+ cell,
276
+ columnIndex,
277
+ colSpan: continuesVerticalMerge ? void 0 : cell.colSpan,
278
+ rowSpan: continuesVerticalMerge ? void 0 : verticalSpan(physical.slice(rowIndex + 1), columnIndex)
279
+ })));
280
+ }
214
281
  function buildCell() {
215
282
  return require_xml_fragment.el("w:tc", {}, [require_edit_docx_paragraph.buildParagraph()]);
216
283
  }
284
+ function declaredGridColumnCount(table) {
285
+ const tblGrid = require_xml_edit.directChildElement(table, "w:tblGrid");
286
+ return tblGrid === void 0 ? 0 : tblGrid.children.filter((child) => child.type === "element" && child.tag === "w:gridCol").length;
287
+ }
217
288
  var DocxTable = class {
218
289
  container;
219
290
  node;
@@ -226,11 +297,24 @@ var DocxTable = class {
226
297
  if (this.removed) throw new Error("this DocxTable has been removed and can no longer be used");
227
298
  return this.node;
228
299
  }
229
- rows() {
300
+ rowElements() {
230
301
  const out = [];
231
- for (const child of this.live().children) if (child.type === "element" && child.tag === "w:tr") out.push(new DocxTableRow(child));
302
+ for (const child of this.live().children) if (child.type === "element" && child.tag === "w:tr") out.push(child);
232
303
  return out;
233
304
  }
305
+ rows() {
306
+ return this.rowElements().map((row) => new DocxTableRow(row));
307
+ }
308
+ gridRows() {
309
+ return this.grid().rows;
310
+ }
311
+ gridColumnCount() {
312
+ return this.grid().columnCount;
313
+ }
314
+ grid() {
315
+ const table = this.live();
316
+ return require_edit_table_grid.resolveLiveTableGrid(placeCells(this.rows().map((row) => row.cells())), declaredGridColumnCount(table));
317
+ }
234
318
  cell(rowIndex, columnIndex) {
235
319
  const row = this.rows()[rowIndex];
236
320
  if (row === void 0) throw new Error(`row ${rowIndex} does not exist in this table`);
@@ -248,18 +332,22 @@ var DocxTable = class {
248
332
  }
249
333
  mergeCells(startRow, startColumn, rowSpan, colSpan) {
250
334
  if (!Number.isInteger(rowSpan) || rowSpan < 1 || !Number.isInteger(colSpan) || colSpan < 1) throw new Error(`mergeCells: rowSpan and colSpan must be positive integers, got rowSpan=${rowSpan}, colSpan=${colSpan}`);
251
- const rows = this.rows();
335
+ const rows = this.rowElements();
252
336
  const anchorRow = rows[startRow];
253
337
  if (anchorRow === void 0) throw new Error(`mergeCells: row ${startRow} does not exist in this table`);
254
- const anchor = anchorRow.mergeCellsHorizontally(startColumn, colSpan);
338
+ if (startRow + rowSpan > rows.length) throw new Error(`mergeCells: rowSpan ${rowSpan} starting at row ${startRow} exceeds this table's own ${rows.length} rows`);
339
+ const planRow = (row, rowIndex) => {
340
+ const plan = planHorizontalMerge(row, startColumn, colSpan, rowSpan > 1);
341
+ if ("refusal" in plan) throw new Error(`mergeCells: row ${rowIndex}: ${plan.refusal}`);
342
+ return plan;
343
+ };
344
+ const anchorPlan = planRow(anchorRow, startRow);
345
+ const coveredPlans = rows.slice(startRow + 1, startRow + rowSpan).map((row, offset) => planRow(row, startRow + 1 + offset));
346
+ const anchor = applyHorizontalMerge(anchorPlan, colSpan);
347
+ const coveredCells = coveredPlans.map((plan) => applyHorizontalMerge(plan, colSpan));
255
348
  if (rowSpan > 1) {
256
349
  anchor.verticalMerge = "restart";
257
- for (let r = 1; r < rowSpan; r++) {
258
- const coveredRow = rows[startRow + r];
259
- if (coveredRow === void 0) throw new Error(`mergeCells: rowSpan ${rowSpan} starting at row ${startRow} exceeds this table's own ${rows.length} rows`);
260
- const coveredAnchor = coveredRow.mergeCellsHorizontally(startColumn, colSpan);
261
- coveredAnchor.verticalMerge = "continue";
262
- }
350
+ for (const covered of coveredCells) covered.verticalMerge = "continue";
263
351
  }
264
352
  return anchor;
265
353
  }
@@ -1,3 +1,4 @@
1
+ import { i as TableGridRows } from "../../table-grid-Xbaddzz3.cjs";
1
2
  import { DocxParagraph, ParagraphInit } from "./paragraph.cjs";
2
3
  import { Color, ContentCellBorders } from "document-schema.js";
3
4
  import { XmlElement, XmlNode } from "ooxml.js";
@@ -40,7 +41,11 @@ declare class DocxTable {
40
41
  private removed;
41
42
  constructor(container: XmlNode[], node: XmlElement);
42
43
  private live;
44
+ private rowElements;
43
45
  rows(): DocxTableRow[];
46
+ gridRows(): TableGridRows<DocxTableCell>;
47
+ gridColumnCount(): number;
48
+ private grid;
44
49
  cell(rowIndex: number, columnIndex: number): DocxTableCell;
45
50
  appendRow(columnCount: number): DocxTableRow;
46
51
  mergeCells(startRow: number, startColumn: number, rowSpan: number, colSpan: number): DocxTableCell;
@@ -1,3 +1,4 @@
1
+ import { i as TableGridRows } from "../../table-grid-Xbaddzz3.js";
1
2
  import { DocxParagraph, ParagraphInit } from "./paragraph.js";
2
3
  import { XmlElement, XmlNode } from "ooxml.js";
3
4
  import { Color, ContentCellBorders } from "document-schema.js";
@@ -40,7 +41,11 @@ declare class DocxTable {
40
41
  private removed;
41
42
  constructor(container: XmlNode[], node: XmlElement);
42
43
  private live;
44
+ private rowElements;
43
45
  rows(): DocxTableRow[];
46
+ gridRows(): TableGridRows<DocxTableCell>;
47
+ gridColumnCount(): number;
48
+ private grid;
44
49
  cell(rowIndex: number, columnIndex: number): DocxTableCell;
45
50
  appendRow(columnCount: number): DocxTableRow;
46
51
  mergeCells(startRow: number, startColumn: number, rowSpan: number, colSpan: number): DocxTableCell;
@@ -2,6 +2,7 @@ import { el } from "../../xml/fragment.js";
2
2
  import { ptToTwips, twipsToPt } from "../../model/units.js";
3
3
  import { directChildElement, insertInSchemaOrder, removeChild } from "../../xml/edit.js";
4
4
  import { DocxParagraph, buildParagraph } from "./paragraph.js";
5
+ import { resolveLiveTableGrid } from "../table-grid.js";
5
6
  import { attr, readCellShading } from "ooxml.js";
6
7
  import { colorToRgbHex, resolveCellFillColor, rgbHexToColor } from "document-schema.js";
7
8
  //#region src/edit/docx/table.ts
@@ -196,23 +197,93 @@ var DocxTableRow = class {
196
197
  }
197
198
  mergeCellsHorizontally(startColumnIndex, colSpan) {
198
199
  if (!Number.isInteger(colSpan) || colSpan < 1) throw new Error(`mergeCellsHorizontally: colSpan must be a positive integer, got ${colSpan}`);
199
- const cellElements = [];
200
- for (const child of this.node.children) if (child.type === "element" && child.tag === "w:tc") cellElements.push(child);
201
- const anchorElement = cellElements[startColumnIndex];
202
- if (anchorElement === void 0) throw new Error(`mergeCellsHorizontally: column ${startColumnIndex} does not exist in this row`);
203
- if (startColumnIndex + colSpan > cellElements.length) throw new Error(`mergeCellsHorizontally: colSpan ${colSpan} starting at column ${startColumnIndex} exceeds this row's own ${cellElements.length} columns`);
204
- for (let i = 1; i < colSpan; i++) {
205
- const consumedElement = cellElements[startColumnIndex + i];
206
- if (consumedElement !== void 0) removeChild(this.node.children, consumedElement);
207
- }
208
- const anchor = new DocxTableCell(anchorElement);
209
- anchor.colSpan = colSpan;
210
- return anchor;
200
+ const plan = planHorizontalMerge(this.node, startColumnIndex, colSpan, false);
201
+ if ("refusal" in plan) throw new Error(`mergeCellsHorizontally: ${plan.refusal}`);
202
+ return applyHorizontalMerge(plan, colSpan);
211
203
  }
212
204
  };
205
+ function rowCells(row) {
206
+ const out = [];
207
+ let column = 0;
208
+ for (const child of row.children) if (child.type === "element" && child.tag === "w:tc") {
209
+ const cell = new DocxTableCell(child);
210
+ const span = cell.colSpan ?? 1;
211
+ out.push({
212
+ element: child,
213
+ startColumn: column,
214
+ span,
215
+ takesPartInVerticalMerge: cell.verticalMerge !== void 0
216
+ });
217
+ column += span;
218
+ }
219
+ return out;
220
+ }
221
+ function refuse(refusal) {
222
+ return { refusal };
223
+ }
224
+ function planHorizontalMerge(row, startColumn, colSpan, joinsVerticalMerge) {
225
+ const cells = rowCells(row);
226
+ const last = cells[cells.length - 1];
227
+ const width = last === void 0 ? 0 : last.startColumn + last.span;
228
+ const anchor = cells.find((cell) => cell.startColumn <= startColumn && startColumn < cell.startColumn + cell.span);
229
+ if (anchor === void 0) return refuse(`column ${startColumn} does not exist in this row`);
230
+ if (anchor.startColumn !== startColumn) return refuse(`column ${startColumn} is covered by the cell starting at column ${anchor.startColumn}`);
231
+ const endColumn = startColumn + colSpan;
232
+ if (endColumn > width) return refuse(`colSpan ${colSpan} starting at column ${startColumn} exceeds this row's own ${width} grid columns`);
233
+ const region = cells.filter((cell) => cell.startColumn >= startColumn && cell.startColumn < endColumn);
234
+ const straddling = region.find((cell) => cell.startColumn + cell.span > endColumn);
235
+ if (straddling !== void 0) return refuse(`the cell at column ${straddling.startColumn} spans columns ${straddling.startColumn} to ${straddling.startColumn + straddling.span - 1}, past the last merged column ${endColumn - 1}`);
236
+ const consumed = region.filter((cell) => cell !== anchor);
237
+ const verticallyMerged = joinsVerticalMerge || consumed.length > 0 ? region.find((cell) => cell.takesPartInVerticalMerge) : void 0;
238
+ if (verticallyMerged !== void 0) return refuse(`column ${verticallyMerged.startColumn} takes part in a vertical merge and cannot be merged over`);
239
+ return {
240
+ row,
241
+ anchor: anchor.element,
242
+ consumed: consumed.map((cell) => cell.element)
243
+ };
244
+ }
245
+ function applyHorizontalMerge(plan, colSpan) {
246
+ for (const consumed of plan.consumed) removeChild(plan.row.children, consumed);
247
+ const anchor = new DocxTableCell(plan.anchor);
248
+ anchor.colSpan = colSpan;
249
+ return anchor;
250
+ }
251
+ function physicalCells(cells) {
252
+ let column = 0;
253
+ return cells.map((cell) => {
254
+ const columnIndex = column;
255
+ column += cell.colSpan ?? 1;
256
+ return {
257
+ cell,
258
+ columnIndex,
259
+ continuesVerticalMerge: cell.verticalMerge === "continue"
260
+ };
261
+ });
262
+ }
263
+ function verticalSpan(rowsBelow, columnIndex) {
264
+ let span = 1;
265
+ for (const row of rowsBelow) {
266
+ if (!row.find((cell) => cell.columnIndex === columnIndex)?.continuesVerticalMerge) break;
267
+ span++;
268
+ }
269
+ return span;
270
+ }
271
+ function placeCells(rows) {
272
+ const physical = rows.map(physicalCells);
273
+ return physical.map((row, rowIndex) => row.map(({ cell, columnIndex, continuesVerticalMerge }) => ({
274
+ cell,
275
+ columnIndex,
276
+ colSpan: continuesVerticalMerge ? void 0 : cell.colSpan,
277
+ rowSpan: continuesVerticalMerge ? void 0 : verticalSpan(physical.slice(rowIndex + 1), columnIndex)
278
+ })));
279
+ }
213
280
  function buildCell() {
214
281
  return el("w:tc", {}, [buildParagraph()]);
215
282
  }
283
+ function declaredGridColumnCount(table) {
284
+ const tblGrid = directChildElement(table, "w:tblGrid");
285
+ return tblGrid === void 0 ? 0 : tblGrid.children.filter((child) => child.type === "element" && child.tag === "w:gridCol").length;
286
+ }
216
287
  var DocxTable = class {
217
288
  container;
218
289
  node;
@@ -225,11 +296,24 @@ var DocxTable = class {
225
296
  if (this.removed) throw new Error("this DocxTable has been removed and can no longer be used");
226
297
  return this.node;
227
298
  }
228
- rows() {
299
+ rowElements() {
229
300
  const out = [];
230
- for (const child of this.live().children) if (child.type === "element" && child.tag === "w:tr") out.push(new DocxTableRow(child));
301
+ for (const child of this.live().children) if (child.type === "element" && child.tag === "w:tr") out.push(child);
231
302
  return out;
232
303
  }
304
+ rows() {
305
+ return this.rowElements().map((row) => new DocxTableRow(row));
306
+ }
307
+ gridRows() {
308
+ return this.grid().rows;
309
+ }
310
+ gridColumnCount() {
311
+ return this.grid().columnCount;
312
+ }
313
+ grid() {
314
+ const table = this.live();
315
+ return resolveLiveTableGrid(placeCells(this.rows().map((row) => row.cells())), declaredGridColumnCount(table));
316
+ }
233
317
  cell(rowIndex, columnIndex) {
234
318
  const row = this.rows()[rowIndex];
235
319
  if (row === void 0) throw new Error(`row ${rowIndex} does not exist in this table`);
@@ -247,18 +331,22 @@ var DocxTable = class {
247
331
  }
248
332
  mergeCells(startRow, startColumn, rowSpan, colSpan) {
249
333
  if (!Number.isInteger(rowSpan) || rowSpan < 1 || !Number.isInteger(colSpan) || colSpan < 1) throw new Error(`mergeCells: rowSpan and colSpan must be positive integers, got rowSpan=${rowSpan}, colSpan=${colSpan}`);
250
- const rows = this.rows();
334
+ const rows = this.rowElements();
251
335
  const anchorRow = rows[startRow];
252
336
  if (anchorRow === void 0) throw new Error(`mergeCells: row ${startRow} does not exist in this table`);
253
- const anchor = anchorRow.mergeCellsHorizontally(startColumn, colSpan);
337
+ if (startRow + rowSpan > rows.length) throw new Error(`mergeCells: rowSpan ${rowSpan} starting at row ${startRow} exceeds this table's own ${rows.length} rows`);
338
+ const planRow = (row, rowIndex) => {
339
+ const plan = planHorizontalMerge(row, startColumn, colSpan, rowSpan > 1);
340
+ if ("refusal" in plan) throw new Error(`mergeCells: row ${rowIndex}: ${plan.refusal}`);
341
+ return plan;
342
+ };
343
+ const anchorPlan = planRow(anchorRow, startRow);
344
+ const coveredPlans = rows.slice(startRow + 1, startRow + rowSpan).map((row, offset) => planRow(row, startRow + 1 + offset));
345
+ const anchor = applyHorizontalMerge(anchorPlan, colSpan);
346
+ const coveredCells = coveredPlans.map((plan) => applyHorizontalMerge(plan, colSpan));
254
347
  if (rowSpan > 1) {
255
348
  anchor.verticalMerge = "restart";
256
- for (let r = 1; r < rowSpan; r++) {
257
- const coveredRow = rows[startRow + r];
258
- if (coveredRow === void 0) throw new Error(`mergeCells: rowSpan ${rowSpan} starting at row ${startRow} exceeds this table's own ${rows.length} rows`);
259
- const coveredAnchor = coveredRow.mergeCellsHorizontally(startColumn, colSpan);
260
- coveredAnchor.verticalMerge = "continue";
261
- }
349
+ for (const covered of coveredCells) covered.verticalMerge = "continue";
262
350
  }
263
351
  return anchor;
264
352
  }
@@ -1,4 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_edit_table_grid = require("../table-grid.cjs");
2
3
  const require_edit_markdown_paragraph = require("./paragraph.cjs");
3
4
  //#region src/edit/markdown/table.ts
4
5
  const DEFAULT_TABLE_WIDTH_PT = 468;
@@ -54,6 +55,15 @@ var MarkdownTable = class {
54
55
  rows() {
55
56
  return this.live().rows.map((row) => new MarkdownTableRow(row));
56
57
  }
58
+ gridRows() {
59
+ return this.grid().rows;
60
+ }
61
+ gridColumnCount() {
62
+ return this.grid().columnCount;
63
+ }
64
+ grid() {
65
+ return require_edit_table_grid.resolvePivotTableGrid(this.live(), (cell) => new MarkdownTableCell(cell));
66
+ }
57
67
  appendRow() {
58
68
  const node = this.live();
59
69
  const row = buildRow(node.columnWidthsPt.length);
@@ -1,3 +1,4 @@
1
+ import { i as TableGridRows } from "../../table-grid-Xbaddzz3.cjs";
1
2
  import { MarkdownParagraph, ParagraphInit } from "./paragraph.cjs";
2
3
  import { ContentBlock, ContentTable, ContentTableCell, ContentTableRow } from "document-schema.js";
3
4
  //#region src/edit/markdown/table.d.ts
@@ -25,6 +26,9 @@ declare class MarkdownTable {
25
26
  constructor(container: ContentBlock[], node: ContentTable);
26
27
  private live;
27
28
  rows(): MarkdownTableRow[];
29
+ gridRows(): TableGridRows<MarkdownTableCell>;
30
+ gridColumnCount(): number;
31
+ private grid;
28
32
  appendRow(): MarkdownTableRow;
29
33
  remove(): void;
30
34
  }
@@ -1,3 +1,4 @@
1
+ import { i as TableGridRows } from "../../table-grid-Xbaddzz3.js";
1
2
  import { MarkdownParagraph, ParagraphInit } from "./paragraph.js";
2
3
  import { ContentBlock, ContentTable, ContentTableCell, ContentTableRow } from "document-schema.js";
3
4
  //#region src/edit/markdown/table.d.ts
@@ -25,6 +26,9 @@ declare class MarkdownTable {
25
26
  constructor(container: ContentBlock[], node: ContentTable);
26
27
  private live;
27
28
  rows(): MarkdownTableRow[];
29
+ gridRows(): TableGridRows<MarkdownTableCell>;
30
+ gridColumnCount(): number;
31
+ private grid;
28
32
  appendRow(): MarkdownTableRow;
29
33
  remove(): void;
30
34
  }
@@ -1,3 +1,4 @@
1
+ import { resolvePivotTableGrid } from "../table-grid.js";
1
2
  import { MarkdownParagraph, buildParagraph } from "./paragraph.js";
2
3
  //#region src/edit/markdown/table.ts
3
4
  const DEFAULT_TABLE_WIDTH_PT = 468;
@@ -53,6 +54,15 @@ var MarkdownTable = class {
53
54
  rows() {
54
55
  return this.live().rows.map((row) => new MarkdownTableRow(row));
55
56
  }
57
+ gridRows() {
58
+ return this.grid().rows;
59
+ }
60
+ gridColumnCount() {
61
+ return this.grid().columnCount;
62
+ }
63
+ grid() {
64
+ return resolvePivotTableGrid(this.live(), (cell) => new MarkdownTableCell(cell));
65
+ }
56
66
  appendRow() {
57
67
  const node = this.live();
58
68
  const row = buildRow(node.columnWidthsPt.length);
@@ -1,7 +1,7 @@
1
1
  import { t as ImageInit } from "../../image-D1z0T9Za.cjs";
2
2
  import { t as OdpShape } from "../../shape-Kmmxib5z.cjs";
3
3
  import { s as OdgVector } from "../../vector-DDP7hgvm.cjs";
4
- import { o as TableInit, r as OdtTable } from "../../table-DvKlZmG0.cjs";
4
+ import { o as TableInit, r as OdtTable } from "../../table-CCjF09Ho.cjs";
5
5
  import { Box, ContentFormula, ContentVector } from "document-schema.js";
6
6
  import { Package, XmlElement, XmlNode } from "odf.js";
7
7
  //#region src/edit/odp/slide.d.ts
@@ -1,7 +1,7 @@
1
1
  import { t as ImageInit } from "../../image-D1z0T9Za.js";
2
2
  import { t as OdpShape } from "../../shape-ZAfNsCC5.js";
3
3
  import { s as OdgVector } from "../../vector-DDP7hgvm.js";
4
- import { o as TableInit, r as OdtTable } from "../../table-qWkzpadC.js";
4
+ import { o as TableInit, r as OdtTable } from "../../table-BMYJo_-F.js";
5
5
  import { Box, ContentFormula, ContentVector } from "document-schema.js";
6
6
  import { Package, XmlElement, XmlNode } from "odf.js";
7
7
  //#region src/edit/odp/slide.d.ts
@@ -1,6 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_model_metadata = require("../../model/metadata.cjs");
3
3
  const require_ports_clock = require("../../ports/clock.cjs");
4
+ const require_edit_table_grid = require("../table-grid.cjs");
4
5
  const require_model_embedded_drawing = require("../../model/embedded-drawing.cjs");
5
6
  const require_model_formula = require("../../model/formula.cjs");
6
7
  const require_edit_odt_paragraph = require("./paragraph.cjs");
@@ -132,6 +133,7 @@ function populateCellBlocks(cell, blocks) {
132
133
  for (const remaining of restBlocks) appendCellBlock(cell, remaining);
133
134
  }
134
135
  function populateOdtTable(table, block) {
136
+ require_edit_table_grid.assertTableObeysGridRule(block, "populateOdtTable");
135
137
  const gridPositions = (0, document_schema_js.walkTableGrid)(block);
136
138
  block.rows.forEach((row, rowIndex) => {
137
139
  const tableRow = table.appendEmptyRow();
@@ -1,6 +1,6 @@
1
1
  import { t as ClockPort } from "../../clock-C7SUuYN0.cjs";
2
2
  import { t as OdtParagraph } from "../../paragraph-CRaLSzWz.cjs";
3
- import { r as OdtTable } from "../../table-DvKlZmG0.cjs";
3
+ import { r as OdtTable } from "../../table-CCjF09Ho.cjs";
4
4
  import { ContentDocument, ContentParagraph, ContentTable } from "document-schema.js";
5
5
  import { Package } from "odf.js";
6
6
  //#region src/edit/odt/content.d.ts
@@ -1,6 +1,6 @@
1
1
  import { t as ClockPort } from "../../clock-C7SUuYN0.js";
2
2
  import { t as OdtParagraph } from "../../paragraph-D4f705GG.js";
3
- import { r as OdtTable } from "../../table-qWkzpadC.js";
3
+ import { r as OdtTable } from "../../table-BMYJo_-F.js";
4
4
  import { ContentDocument, ContentParagraph, ContentTable } from "document-schema.js";
5
5
  import { Package } from "odf.js";
6
6
  //#region src/edit/odt/content.d.ts
@@ -1,5 +1,6 @@
1
1
  import { resolveMetadataTimestamps } from "../../model/metadata.js";
2
2
  import { systemClock } from "../../ports/clock.js";
3
+ import { assertTableObeysGridRule } from "../table-grid.js";
3
4
  import { FLOW_CONTAINER_ORIGIN, drawingOfBlock, embeddedDrawingVectors } from "../../model/embedded-drawing.js";
4
5
  import { formulaOfBlock, formulaPlaceholderText } from "../../model/formula.js";
5
6
  import { headingStyleName } from "./paragraph.js";
@@ -131,6 +132,7 @@ function populateCellBlocks(cell, blocks) {
131
132
  for (const remaining of restBlocks) appendCellBlock(cell, remaining);
132
133
  }
133
134
  function populateOdtTable(table, block) {
135
+ assertTableObeysGridRule(block, "populateOdtTable");
134
136
  const gridPositions = walkTableGrid(block);
135
137
  block.rows.forEach((row, rowIndex) => {
136
138
  const tableRow = table.appendEmptyRow();
@@ -1,7 +1,7 @@
1
1
  import { t as ClockPort } from "../../clock-C7SUuYN0.cjs";
2
2
  import { n as ParagraphInit, t as OdtParagraph } from "../../paragraph-CRaLSzWz.cjs";
3
3
  import { t as OdtList } from "../../list-C_UPIZWM.cjs";
4
- import { o as TableInit, r as OdtTable } from "../../table-DvKlZmG0.cjs";
4
+ import { o as TableInit, r as OdtTable } from "../../table-CCjF09Ho.cjs";
5
5
  import { Box, ContentControlDescriptor, ContentFormula, ContentVector, DivisionDescriptor, LayoutMetadata } from "document-schema.js";
6
6
  import { Package } from "odf.js";
7
7
  //#region src/edit/odt/editor.d.ts
@@ -1,7 +1,7 @@
1
1
  import { t as ClockPort } from "../../clock-C7SUuYN0.js";
2
2
  import { n as ParagraphInit, t as OdtParagraph } from "../../paragraph-D4f705GG.js";
3
3
  import { t as OdtList } from "../../list-C2GWIe6E.js";
4
- import { o as TableInit, r as OdtTable } from "../../table-qWkzpadC.js";
4
+ import { o as TableInit, r as OdtTable } from "../../table-BMYJo_-F.js";
5
5
  import { Box, ContentControlDescriptor, ContentFormula, ContentVector, DivisionDescriptor, LayoutMetadata } from "document-schema.js";
6
6
  import { Package } from "odf.js";
7
7
  //#region src/edit/odt/editor.d.ts