documents.js 1.37.0 → 1.39.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.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AttributeSchema, BinaryPartSchema, CommentSchema, CompactPackageSchema, CompactPartSchema, CompactXmlNodeSchema, DefinedNameSchema, PackageSchema, PartSchema, XmlCdataSchema, XmlCommentSchema, XmlDeclarationSchema, XmlElementSchema, XmlNodeSchema, XmlPartSchema, XmlPiSchema, XmlTextSchema, attr, attr as attr$1, base64ToBytes, base64ToBytes as base64ToBytes$1, buildXml, bytesToBase64, bytesToBase64 as bytesToBase64$1, childrenWithTag, compactCodec, compactPackageCodec, decodeCompactPackage, decodeEntities, decodePackage, decodePackage as decodePackage$1, elementsWithTag, encodeCompactPackage, encodePackage, encodePackage as encodePackage$1, fromCompact, isCompactXmlNode, isXmlNode, packageCodec, parsePackage, parseXml, readDocx, readPptx, resolveRelationships, resolveRelationships as resolveRelationships$1, rootElement, rootElement as rootElement$1, serializePackage, textContent, textContent as textContent$1, toCompact, unzipPackage, walk, xmlCodec, zipPackage } from "ooxml.js";
2
2
  import { COLOR_BLACK, ContentBlockSchema, ContentImageBlockSchema, ContentPageBreakSchema, ContentParagraphSchema, ContentRunSchema, ContentSectionSchema, ContentSectionSchema as ContentSectionSchema$1, ContentShapeSchema, ContentSlideSchema, ContentSlideSchema as ContentSlideSchema$1, ContentTableCellSchema, ContentTableRowSchema, ContentTableSchema, DEFAULT_LAYOUT_FONT, LAYOUT_FORMAT_VERSION, LAYOUT_FORMAT_VERSION as LAYOUT_FORMAT_VERSION$1, LayoutDocumentSchema, LayoutMetadataSchema, PAGE_SIZE_A4, PAGE_SIZE_LETTER, SLIDE_SIZE_STANDARD, SLIDE_SIZE_WIDESCREEN, colorToRgbHex, isContentBlock, rgbHexToColor } from "document-content-model";
3
3
  import { z } from "zod";
4
- import { ODF_MEDIA_TYPES, decodePackage as decodePackage$2, readOdt } from "odf.js";
4
+ import { ODF_MEDIA_TYPES, StyleRegistry, decodeOdfText, decodePackage as decodePackage$2, encodePackage as encodePackage$2, formatOdfLength, readOdt, resolveStyle, setDocumentMediaType, syncManifest, xmlnsAttributes } from "odf.js";
5
5
  import { Unzlib, inflateSync, unzlibSync, zlibSync } from "fflate";
6
6
  //#region src/model/content.ts
7
7
  const CONTENT_FORMAT_VERSION = 1;
@@ -520,7 +520,7 @@ function needsSpacePreserve(text) {
520
520
  }
521
521
  //#endregion
522
522
  //#region src/edit/docx/run.ts
523
- function directChild$4(parent, tag) {
523
+ function directChild$5(parent, tag) {
524
524
  for (const child of parent.children) if (child.type === "element" && child.tag === tag) return child;
525
525
  }
526
526
  function appendUElement(rPr, value) {
@@ -529,7 +529,7 @@ function appendUElement(rPr, value) {
529
529
  return created;
530
530
  }
531
531
  function findOrCreateT(run) {
532
- const existing = directChild$4(run, "w:t");
532
+ const existing = directChild$5(run, "w:t");
533
533
  if (existing !== void 0) return existing;
534
534
  const created = el("w:t");
535
535
  run.children.push(created);
@@ -549,7 +549,7 @@ var DocxRun = class {
549
549
  }
550
550
  rPr(create) {
551
551
  const node = this.live();
552
- return create ? ensureFirstChild(node, "w:rPr") : directChild$4(node, "w:rPr");
552
+ return create ? ensureFirstChild(node, "w:rPr") : directChild$5(node, "w:rPr");
553
553
  }
554
554
  get text() {
555
555
  return textContent$1(this.live());
@@ -573,14 +573,14 @@ var DocxRun = class {
573
573
  }
574
574
  get underline() {
575
575
  const rPr = this.rPr(false);
576
- const uElement = rPr === void 0 ? void 0 : directChild$4(rPr, "w:u");
576
+ const uElement = rPr === void 0 ? void 0 : directChild$5(rPr, "w:u");
577
577
  if (uElement === void 0) return false;
578
578
  for (const a of uElement.attributes) if (a.name === "w:val") return a.value !== "none";
579
579
  return true;
580
580
  }
581
581
  set underline(value) {
582
582
  const rPr = this.rPr(true);
583
- const existing = directChild$4(rPr, "w:u");
583
+ const existing = directChild$5(rPr, "w:u");
584
584
  if (existing === void 0) {
585
585
  appendUElement(rPr, value);
586
586
  return;
@@ -627,7 +627,7 @@ function insertRPrChildInOrder(rPr, child) {
627
627
  }
628
628
  rPr.children.push(child);
629
629
  }
630
- function buildRun(init = {}) {
630
+ function buildRun$1(init = {}) {
631
631
  const rPrChildren = [];
632
632
  if (init.bold === true) rPrChildren.push(el("w:b"));
633
633
  if (init.italic === true) rPrChildren.push(el("w:i"));
@@ -654,7 +654,7 @@ function buildRun(init = {}) {
654
654
  }
655
655
  //#endregion
656
656
  //#region src/edit/docx/paragraph.ts
657
- function directChild$3(parent, tag) {
657
+ function directChild$4(parent, tag) {
658
658
  for (const child of parent.children) if (child.type === "element" && child.tag === tag) return child;
659
659
  }
660
660
  var DocxParagraph = class {
@@ -673,7 +673,7 @@ var DocxParagraph = class {
673
673
  }
674
674
  pPr(create) {
675
675
  const node = this.live();
676
- return create ? ensureFirstChild(node, "w:pPr") : directChild$3(node, "w:pPr");
676
+ return create ? ensureFirstChild(node, "w:pPr") : directChild$4(node, "w:pPr");
677
677
  }
678
678
  get text() {
679
679
  return textContent$1(this.live());
@@ -686,7 +686,7 @@ var DocxParagraph = class {
686
686
  }
687
687
  appendRun(init) {
688
688
  const node = this.live();
689
- const runElement = buildRun(init);
689
+ const runElement = buildRun$1(init);
690
690
  node.children.push(runElement);
691
691
  return new DocxRun(node.children, runElement);
692
692
  }
@@ -695,7 +695,7 @@ var DocxParagraph = class {
695
695
  }
696
696
  insertRunAt(index, init) {
697
697
  const node = this.live();
698
- const runElement = buildRun(init);
698
+ const runElement = buildRun$1(init);
699
699
  const runIndices = [];
700
700
  node.children.forEach((child, i) => {
701
701
  if (child.type === "element" && child.tag === "w:r") runIndices.push(i);
@@ -710,7 +710,7 @@ var DocxParagraph = class {
710
710
  set styleId(value) {
711
711
  if (value === void 0) {
712
712
  const pPr = this.pPr(false);
713
- const existing = pPr === void 0 ? void 0 : directChild$3(pPr, "w:pStyle");
713
+ const existing = pPr === void 0 ? void 0 : directChild$4(pPr, "w:pStyle");
714
714
  if (existing !== void 0 && pPr !== void 0) removeChild(pPr.children, existing);
715
715
  return;
716
716
  }
@@ -722,7 +722,7 @@ var DocxParagraph = class {
722
722
  set alignment(value) {
723
723
  if (value === void 0) {
724
724
  const pPr = this.pPr(false);
725
- const existing = pPr === void 0 ? void 0 : directChild$3(pPr, "w:jc");
725
+ const existing = pPr === void 0 ? void 0 : directChild$4(pPr, "w:jc");
726
726
  if (existing !== void 0 && pPr !== void 0) removeChild(pPr.children, existing);
727
727
  return;
728
728
  }
@@ -730,12 +730,12 @@ var DocxParagraph = class {
730
730
  }
731
731
  get list() {
732
732
  const pPr = this.pPr(false);
733
- const numPr = pPr === void 0 ? void 0 : directChild$3(pPr, "w:numPr");
733
+ const numPr = pPr === void 0 ? void 0 : directChild$4(pPr, "w:numPr");
734
734
  if (numPr === void 0) return;
735
- const numIdElement = directChild$3(numPr, "w:numId");
735
+ const numIdElement = directChild$4(numPr, "w:numId");
736
736
  const numId = numIdElement === void 0 ? void 0 : attr$1(numIdElement, "w:val");
737
737
  if (numId === void 0) return;
738
- const ilvlElement = directChild$3(numPr, "w:ilvl");
738
+ const ilvlElement = directChild$4(numPr, "w:ilvl");
739
739
  const ilvl = ilvlElement === void 0 ? void 0 : attr$1(ilvlElement, "w:val");
740
740
  return {
741
741
  numId,
@@ -745,7 +745,7 @@ var DocxParagraph = class {
745
745
  set list(value) {
746
746
  const pPr = this.pPr(true);
747
747
  if (value === void 0) {
748
- const existing = directChild$3(pPr, "w:numPr");
748
+ const existing = directChild$4(pPr, "w:numPr");
749
749
  if (existing !== void 0) removeChild(pPr.children, existing);
750
750
  return;
751
751
  }
@@ -763,7 +763,7 @@ var DocxParagraph = class {
763
763
  this.removed = true;
764
764
  }
765
765
  };
766
- function buildParagraph(init = {}) {
766
+ function buildParagraph$1(init = {}) {
767
767
  const paragraph = el("w:p");
768
768
  if (init.styleId !== void 0 || init.alignment !== void 0) {
769
769
  const pPr = el("w:pPr");
@@ -771,7 +771,7 @@ function buildParagraph(init = {}) {
771
771
  if (init.alignment !== void 0) pPr.children.push(el("w:jc", { "w:val": init.alignment === "justify" ? "both" : init.alignment }));
772
772
  paragraph.children.push(pPr);
773
773
  }
774
- if (init.text !== void 0) paragraph.children.push(buildRun({ text: init.text }));
774
+ if (init.text !== void 0) paragraph.children.push(buildRun$1({ text: init.text }));
775
775
  return paragraph;
776
776
  }
777
777
  //#endregion
@@ -779,7 +779,7 @@ function buildParagraph(init = {}) {
779
779
  const CONTENT_TYPES_NS$1 = "http://schemas.openxmlformats.org/package/2006/content-types";
780
780
  const RELS_NS$1 = "http://schemas.openxmlformats.org/package/2006/relationships";
781
781
  const WORDML_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
782
- function declaration$1() {
782
+ function declaration$2() {
783
783
  return {
784
784
  type: "declaration",
785
785
  attributes: [
@@ -848,23 +848,23 @@ function createEmptyDocxPackage() {
848
848
  return { parts: {
849
849
  "[Content_Types].xml": {
850
850
  kind: "xml",
851
- nodes: [declaration$1(), contentTypes]
851
+ nodes: [declaration$2(), contentTypes]
852
852
  },
853
853
  "_rels/.rels": {
854
854
  kind: "xml",
855
- nodes: [declaration$1(), rootRels]
855
+ nodes: [declaration$2(), rootRels]
856
856
  },
857
857
  "word/document.xml": {
858
858
  kind: "xml",
859
- nodes: [declaration$1(), document]
859
+ nodes: [declaration$2(), document]
860
860
  },
861
861
  "word/_rels/document.xml.rels": {
862
862
  kind: "xml",
863
- nodes: [declaration$1(), documentRels]
863
+ nodes: [declaration$2(), documentRels]
864
864
  },
865
865
  "word/styles.xml": {
866
866
  kind: "xml",
867
- nodes: [declaration$1(), styles]
867
+ nodes: [declaration$2(), styles]
868
868
  }
869
869
  } };
870
870
  }
@@ -882,7 +882,7 @@ var DocxTableCell = class {
882
882
  return out;
883
883
  }
884
884
  appendParagraph(init) {
885
- const paragraphElement = buildParagraph(init);
885
+ const paragraphElement = buildParagraph$1(init);
886
886
  this.node.children.push(paragraphElement);
887
887
  return new DocxParagraph(this.node.children, paragraphElement);
888
888
  }
@@ -901,8 +901,8 @@ var DocxTableRow = class {
901
901
  return out;
902
902
  }
903
903
  };
904
- function buildCell() {
905
- return el("w:tc", {}, [buildParagraph()]);
904
+ function buildCell$1() {
905
+ return el("w:tc", {}, [buildParagraph$1()]);
906
906
  }
907
907
  var DocxTable = class {
908
908
  container;
@@ -931,7 +931,7 @@ var DocxTable = class {
931
931
  appendRow(columnCount) {
932
932
  const node = this.live();
933
933
  const cells = [];
934
- for (let i = 0; i < columnCount; i++) cells.push(buildCell());
934
+ for (let i = 0; i < columnCount; i++) cells.push(buildCell$1());
935
935
  const row = el("w:tr", {}, cells);
936
936
  node.children.push(row);
937
937
  return new DocxTableRow(row);
@@ -950,11 +950,11 @@ function buildTblGrid(columns, columnWidthsTwips) {
950
950
  }
951
951
  return el("w:tblGrid", {}, cols);
952
952
  }
953
- function buildTable(init) {
953
+ function buildTable$1(init) {
954
954
  const rows = [];
955
955
  for (let r = 0; r < init.rows; r++) {
956
956
  const cells = [];
957
- for (let c = 0; c < init.columns; c++) cells.push(buildCell());
957
+ for (let c = 0; c < init.columns; c++) cells.push(buildCell$1());
958
958
  rows.push(el("w:tr", {}, cells));
959
959
  }
960
960
  return el("w:tbl", {}, [buildTblGrid(init.columns, init.columnWidthsTwips), ...rows]);
@@ -991,19 +991,19 @@ var DocxBodyImpl = class {
991
991
  this.imageContext = imageContext;
992
992
  }
993
993
  appendParagraph(init) {
994
- const paragraphElement = buildParagraph(init);
994
+ const paragraphElement = buildParagraph$1(init);
995
995
  this.body.children.splice(bodyInsertionPoint(this.body), 0, paragraphElement);
996
996
  return new DocxParagraph(this.body.children, paragraphElement, this.imageContext);
997
997
  }
998
998
  insertParagraphAt(index, init) {
999
- const paragraphElement = buildParagraph(init);
999
+ const paragraphElement = buildParagraph$1(init);
1000
1000
  const indices = bodyElementIndicesByTag(this.body, "w:p");
1001
1001
  const insertAt = index < indices.length ? indices[index] ?? bodyInsertionPoint(this.body) : bodyInsertionPoint(this.body);
1002
1002
  this.body.children.splice(insertAt, 0, paragraphElement);
1003
1003
  return new DocxParagraph(this.body.children, paragraphElement, this.imageContext);
1004
1004
  }
1005
1005
  appendTable(init) {
1006
- const tableElement = buildTable(init);
1006
+ const tableElement = buildTable$1(init);
1007
1007
  this.body.children.splice(bodyInsertionPoint(this.body), 0, tableElement);
1008
1008
  return new DocxTable(this.body.children, tableElement);
1009
1009
  }
@@ -1072,11 +1072,11 @@ function buildDocxPackage(content) {
1072
1072
  const editor = createDocx();
1073
1073
  content.sections.forEach((section, sectionIndex) => {
1074
1074
  if (sectionIndex > 0) editor.body.appendPageBreak();
1075
- for (const block of section.blocks) appendBlock(editor.body, block);
1075
+ for (const block of section.blocks) appendBlock$1(editor.body, block);
1076
1076
  });
1077
1077
  return editor.toPackage();
1078
1078
  }
1079
- function populateParagraph(paragraph, block) {
1079
+ function populateParagraph$1(paragraph, block) {
1080
1080
  paragraph.styleId = block.styleId;
1081
1081
  paragraph.alignment = block.alignment;
1082
1082
  for (const run of block.runs) {
@@ -1093,7 +1093,7 @@ function populateParagraph(paragraph, block) {
1093
1093
  if (run.color !== void 0) docxRun.color = run.color;
1094
1094
  }
1095
1095
  }
1096
- function appendTable(body, block) {
1096
+ function appendTable$1(body, block) {
1097
1097
  const columns = block.columnWidthsPt.length;
1098
1098
  if (block.rows.length === 0 || columns === 0) return;
1099
1099
  const table = body.appendTable({
@@ -1109,17 +1109,17 @@ function appendTable(body, block) {
1109
1109
  if (tableCell === void 0) return;
1110
1110
  const [firstBlock, ...restBlocks] = cell.blocks;
1111
1111
  const firstParagraph = tableCell.paragraphs()[0];
1112
- if (firstBlock?.kind === "paragraph" && firstParagraph !== void 0) populateParagraph(firstParagraph, firstBlock);
1113
- else if (firstBlock !== void 0) appendCellBlock(tableCell, firstBlock);
1114
- for (const remaining of restBlocks) appendCellBlock(tableCell, remaining);
1112
+ if (firstBlock?.kind === "paragraph" && firstParagraph !== void 0) populateParagraph$1(firstParagraph, firstBlock);
1113
+ else if (firstBlock !== void 0) appendCellBlock$1(tableCell, firstBlock);
1114
+ for (const remaining of restBlocks) appendCellBlock$1(tableCell, remaining);
1115
1115
  });
1116
1116
  });
1117
1117
  }
1118
- function appendCellBlock(cell, block) {
1119
- if (block.kind === "paragraph") populateParagraph(cell.appendParagraph(), block);
1118
+ function appendCellBlock$1(cell, block) {
1119
+ if (block.kind === "paragraph") populateParagraph$1(cell.appendParagraph(), block);
1120
1120
  }
1121
- function appendBlock(body, block) {
1122
- if (block.kind === "paragraph") populateParagraph(body.appendParagraph(), block);
1121
+ function appendBlock$1(body, block) {
1122
+ if (block.kind === "paragraph") populateParagraph$1(body.appendParagraph(), block);
1123
1123
  else if (block.kind === "image") body.appendParagraph().insertImageAfter({
1124
1124
  format: block.format,
1125
1125
  bytes: base64ToBytes$1(block.base64),
@@ -1128,7 +1128,7 @@ function appendBlock(body, block) {
1128
1128
  altText: block.altText
1129
1129
  });
1130
1130
  else if (block.kind === "pageBreak") body.appendPageBreak();
1131
- else if (block.kind === "table") appendTable(body, block);
1131
+ else if (block.kind === "table") appendTable$1(body, block);
1132
1132
  }
1133
1133
  //#endregion
1134
1134
  //#region src/edit/pptx/scaffold.ts
@@ -1155,7 +1155,7 @@ const SLIDE_LAYOUT_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/
1155
1155
  const THEME_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme";
1156
1156
  const NOTES_MASTER_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesMaster";
1157
1157
  const MIN_MASTER_OR_LAYOUT_ID = 2147483648;
1158
- function declaration() {
1158
+ function declaration$1() {
1159
1159
  return {
1160
1160
  type: "declaration",
1161
1161
  attributes: [
@@ -1305,7 +1305,7 @@ function ensureNotesMaster(pkg) {
1305
1305
  });
1306
1306
  pkg.parts[NOTES_MASTER_PART_PATH] = {
1307
1307
  kind: "xml",
1308
- nodes: [declaration(), buildNotesMaster()]
1308
+ nodes: [declaration$1(), buildNotesMaster()]
1309
1309
  };
1310
1310
  ensureContentTypeOverride(pkg, NOTES_MASTER_PART_PATH, NOTES_MASTER_CONTENT_TYPE);
1311
1311
  const presentationToNotesMasterTarget = buildRelativeTarget(PRESENTATION_PART_PATH$1, NOTES_MASTER_PART_PATH);
@@ -1344,15 +1344,15 @@ function createEmptyPptxPackage() {
1344
1344
  const pkg = { parts: {
1345
1345
  "[Content_Types].xml": {
1346
1346
  kind: "xml",
1347
- nodes: [declaration(), contentTypes]
1347
+ nodes: [declaration$1(), contentTypes]
1348
1348
  },
1349
1349
  "_rels/.rels": {
1350
1350
  kind: "xml",
1351
- nodes: [declaration(), rootRels]
1351
+ nodes: [declaration$1(), rootRels]
1352
1352
  },
1353
1353
  [PRESENTATION_PART_PATH$1]: {
1354
1354
  kind: "xml",
1355
- nodes: [declaration(), el("p:presentation", {
1355
+ nodes: [declaration$1(), el("p:presentation", {
1356
1356
  "xmlns:p": PML_NS,
1357
1357
  "xmlns:r": R_NS
1358
1358
  })]
@@ -1365,7 +1365,7 @@ function createEmptyPptxPackage() {
1365
1365
  });
1366
1366
  pkg.parts[SLIDE_LAYOUT_PART_PATH] = {
1367
1367
  kind: "xml",
1368
- nodes: [declaration(), buildSlideLayout()]
1368
+ nodes: [declaration$1(), buildSlideLayout()]
1369
1369
  };
1370
1370
  ensureContentTypeOverride(pkg, SLIDE_LAYOUT_PART_PATH, SLIDE_LAYOUT_CONTENT_TYPE);
1371
1371
  const masterToLayoutTarget = buildRelativeTarget(SLIDE_MASTER_PART_PATH, SLIDE_LAYOUT_PART_PATH);
@@ -1380,12 +1380,12 @@ function createEmptyPptxPackage() {
1380
1380
  });
1381
1381
  pkg.parts[SLIDE_MASTER_PART_PATH] = {
1382
1382
  kind: "xml",
1383
- nodes: [declaration(), buildSlideMaster(masterToLayoutRelId)]
1383
+ nodes: [declaration$1(), buildSlideMaster(masterToLayoutRelId)]
1384
1384
  };
1385
1385
  ensureContentTypeOverride(pkg, SLIDE_MASTER_PART_PATH, SLIDE_MASTER_CONTENT_TYPE);
1386
1386
  pkg.parts[THEME_PART_PATH] = {
1387
1387
  kind: "xml",
1388
- nodes: [declaration(), buildTheme()]
1388
+ nodes: [declaration$1(), buildTheme()]
1389
1389
  };
1390
1390
  ensureContentTypeOverride(pkg, THEME_PART_PATH, THEME_CONTENT_TYPE);
1391
1391
  const presentationToMasterTarget = buildRelativeTarget(PRESENTATION_PART_PATH$1, SLIDE_MASTER_PART_PATH);
@@ -1410,7 +1410,7 @@ function createEmptyPptxPackage() {
1410
1410
  }
1411
1411
  //#endregion
1412
1412
  //#region src/edit/pptx/shape.ts
1413
- function directChild$2(parent, tag) {
1413
+ function directChild$3(parent, tag) {
1414
1414
  for (const child of parent.children) if (child.type === "element" && child.tag === tag) return child;
1415
1415
  }
1416
1416
  function parseEmuAttr(element, name) {
@@ -1431,7 +1431,7 @@ var PptxShape = class {
1431
1431
  }
1432
1432
  spPrElement(create) {
1433
1433
  const node = this.live();
1434
- const existing = directChild$2(node, "p:spPr");
1434
+ const existing = directChild$3(node, "p:spPr");
1435
1435
  if (existing !== void 0 || !create) return existing;
1436
1436
  const created = el("p:spPr");
1437
1437
  node.children.push(created);
@@ -1439,10 +1439,10 @@ var PptxShape = class {
1439
1439
  }
1440
1440
  get frame() {
1441
1441
  const spPr = this.spPrElement(false);
1442
- const xfrm = spPr === void 0 ? void 0 : directChild$2(spPr, "a:xfrm");
1442
+ const xfrm = spPr === void 0 ? void 0 : directChild$3(spPr, "a:xfrm");
1443
1443
  if (xfrm === void 0) return;
1444
- const off = directChild$2(xfrm, "a:off");
1445
- const ext = directChild$2(xfrm, "a:ext");
1444
+ const off = directChild$3(xfrm, "a:off");
1445
+ const ext = directChild$3(xfrm, "a:ext");
1446
1446
  if (off === void 0 || ext === void 0) return;
1447
1447
  const x = parseEmuAttr(off, "x");
1448
1448
  const y = parseEmuAttr(off, "y");
@@ -1458,7 +1458,7 @@ var PptxShape = class {
1458
1458
  }
1459
1459
  set frame(value) {
1460
1460
  const spPr = this.spPrElement(true);
1461
- let xfrm = directChild$2(spPr, "a:xfrm");
1461
+ let xfrm = directChild$3(spPr, "a:xfrm");
1462
1462
  if (xfrm === void 0) {
1463
1463
  xfrm = el("a:xfrm");
1464
1464
  spPr.children.unshift(xfrm);
@@ -1472,12 +1472,12 @@ var PptxShape = class {
1472
1472
  })];
1473
1473
  }
1474
1474
  get text() {
1475
- const txBody = directChild$2(this.live(), "p:txBody");
1475
+ const txBody = directChild$3(this.live(), "p:txBody");
1476
1476
  return txBody === void 0 ? "" : textContent$1(txBody);
1477
1477
  }
1478
1478
  set text(value) {
1479
1479
  const node = this.live();
1480
- let txBody = directChild$2(node, "p:txBody");
1480
+ let txBody = directChild$3(node, "p:txBody");
1481
1481
  if (txBody === void 0) {
1482
1482
  txBody = el("p:txBody", {}, [el("a:bodyPr"), el("a:lstStyle")]);
1483
1483
  node.children.push(txBody);
@@ -1488,7 +1488,7 @@ var PptxShape = class {
1488
1488
  }
1489
1489
  setParagraphs(paragraphs) {
1490
1490
  const node = this.live();
1491
- let txBody = directChild$2(node, "p:txBody");
1491
+ let txBody = directChild$3(node, "p:txBody");
1492
1492
  if (txBody === void 0) {
1493
1493
  txBody = el("p:txBody", {}, [el("a:bodyPr"), el("a:lstStyle")]);
1494
1494
  node.children.push(txBody);
@@ -1599,12 +1599,12 @@ function insertPictureShapeMedia(context, slideRoot, frame, image) {
1599
1599
  }
1600
1600
  //#endregion
1601
1601
  //#region src/edit/pptx/slide.ts
1602
- function directChild$1(parent, tag) {
1602
+ function directChild$2(parent, tag) {
1603
1603
  for (const child of parent.children) if (child.type === "element" && child.tag === tag) return child;
1604
1604
  }
1605
1605
  function findSpTree(slideRoot) {
1606
- const cSld = directChild$1(slideRoot, "p:cSld");
1607
- const spTree = cSld === void 0 ? void 0 : directChild$1(cSld, "p:spTree");
1606
+ const cSld = directChild$2(slideRoot, "p:cSld");
1607
+ const spTree = cSld === void 0 ? void 0 : directChild$2(cSld, "p:spTree");
1608
1608
  if (spTree === void 0) throw new Error("slide has no p:cSld/p:spTree element");
1609
1609
  return spTree;
1610
1610
  }
@@ -1736,7 +1736,7 @@ const PRESENTATION_PART_PATH = "ppt/presentation.xml";
1736
1736
  const MEDIA_DIR = "ppt/media";
1737
1737
  const SLIDE_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.slide+xml";
1738
1738
  const SLIDE_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide";
1739
- function directChild(parent, tag) {
1739
+ function directChild$1(parent, tag) {
1740
1740
  for (const child of parent.children) if (child.type === "element" && child.tag === tag) return child;
1741
1741
  }
1742
1742
  function attrValue(element, name) {
@@ -1748,12 +1748,12 @@ function findPresentationRoot(pkg) {
1748
1748
  return root;
1749
1749
  }
1750
1750
  function findSldIdLst(presentationRoot) {
1751
- const sldIdLst = directChild(presentationRoot, "p:sldIdLst");
1751
+ const sldIdLst = directChild$1(presentationRoot, "p:sldIdLst");
1752
1752
  if (sldIdLst === void 0) throw new Error(`${PRESENTATION_PART_PATH} has no p:sldIdLst element`);
1753
1753
  return sldIdLst;
1754
1754
  }
1755
1755
  function findSldSz(presentationRoot) {
1756
- const sldSz = directChild(presentationRoot, "p:sldSz");
1756
+ const sldSz = directChild$1(presentationRoot, "p:sldSz");
1757
1757
  if (sldSz === void 0) throw new Error(`${PRESENTATION_PART_PATH} has no p:sldSz element`);
1758
1758
  return sldSz;
1759
1759
  }
@@ -1951,6 +1951,697 @@ function appendShape(slide, shape) {
1951
1951
  }).setParagraphs(paragraphs);
1952
1952
  }
1953
1953
  //#endregion
1954
+ //#region src/edit/odt/automatic-styles.ts
1955
+ const CONTENT_PART_PATH$1 = "content.xml";
1956
+ function ensureAutomaticStyles(pkg) {
1957
+ const part = pkg.parts[CONTENT_PART_PATH$1];
1958
+ if (part?.kind !== "xml") throw new Error(`ensureAutomaticStyles: package has no ${CONTENT_PART_PATH$1} part`);
1959
+ const root = part.nodes.find((n) => n.type === "element");
1960
+ if (root === void 0) throw new Error(`ensureAutomaticStyles: ${CONTENT_PART_PATH$1} has no root element`);
1961
+ for (const child of root.children) if (child.type === "element" && child.tag === "office:automatic-styles") return child;
1962
+ const created = el("office:automatic-styles");
1963
+ const insertBeforeTags = /* @__PURE__ */ new Set([
1964
+ "office:body",
1965
+ "office:master-styles",
1966
+ "office:settings"
1967
+ ]);
1968
+ const insertIndex = root.children.findIndex((n) => n.type === "element" && insertBeforeTags.has(n.tag));
1969
+ if (insertIndex === -1) root.children.push(created);
1970
+ else root.children.splice(insertIndex, 0, created);
1971
+ return created;
1972
+ }
1973
+ function nextStyleName(automaticStyles, tag, prefix) {
1974
+ let max = 0;
1975
+ for (const child of automaticStyles.children) {
1976
+ if (child.type !== "element" || child.tag !== tag) continue;
1977
+ const name = attr$1(child, "style:name");
1978
+ if (!name?.startsWith(prefix)) continue;
1979
+ const rest = name.slice(prefix.length);
1980
+ const n = Number.parseInt(rest, 10);
1981
+ if (!Number.isNaN(n) && String(n) === rest && n > max) max = n;
1982
+ }
1983
+ return `${prefix}${max + 1}`;
1984
+ }
1985
+ const PAGE_BREAK_STYLE_NAME = "OdtPageBreak";
1986
+ function ensurePageBreakStyleName(pkg) {
1987
+ const automaticStyles = ensureAutomaticStyles(pkg);
1988
+ if (automaticStyles.children.find((c) => c.type === "element" && c.tag === "style:style" && attr$1(c, "style:name") === PAGE_BREAK_STYLE_NAME) !== void 0) return PAGE_BREAK_STYLE_NAME;
1989
+ automaticStyles.children.push(el("style:style", {
1990
+ "style:name": PAGE_BREAK_STYLE_NAME,
1991
+ "style:family": "paragraph"
1992
+ }, [el("style:paragraph-properties", { "fo:break-before": "page" })]));
1993
+ return PAGE_BREAK_STYLE_NAME;
1994
+ }
1995
+ //#endregion
1996
+ //#region src/xml/odf-text.ts
1997
+ const MIN_SPACE_RUN_FOR_TEXT_S = 2;
1998
+ function encodeOdfText(text) {
1999
+ const nodes = [];
2000
+ let literal = "";
2001
+ const flushLiteral = () => {
2002
+ if (literal.length > 0) {
2003
+ nodes.push(txt(encodeXmlText(literal)));
2004
+ literal = "";
2005
+ }
2006
+ };
2007
+ let i = 0;
2008
+ while (i < text.length) {
2009
+ const ch = text[i];
2010
+ if (ch === " ") {
2011
+ let runLength = 1;
2012
+ while (i + runLength < text.length && text[i + runLength] === " ") runLength += 1;
2013
+ if (runLength >= MIN_SPACE_RUN_FOR_TEXT_S) {
2014
+ flushLiteral();
2015
+ nodes.push(el("text:s", { "text:c": String(runLength) }));
2016
+ } else literal += " ";
2017
+ i += runLength;
2018
+ continue;
2019
+ }
2020
+ if (ch === " ") {
2021
+ flushLiteral();
2022
+ nodes.push(el("text:tab"));
2023
+ i += 1;
2024
+ continue;
2025
+ }
2026
+ if (ch === "\n") {
2027
+ flushLiteral();
2028
+ nodes.push(el("text:line-break"));
2029
+ i += 1;
2030
+ continue;
2031
+ }
2032
+ literal += ch;
2033
+ i += 1;
2034
+ }
2035
+ flushLiteral();
2036
+ return nodes;
2037
+ }
2038
+ function decodeOdfText$1(nodes) {
2039
+ const wrapper = el("_odf-text-container", {}, [...nodes]);
2040
+ return decodeOdfText(wrapper);
2041
+ }
2042
+ //#endregion
2043
+ //#region src/edit/odt/props.ts
2044
+ const STYLE_NAME_ATTR = "text:style-name";
2045
+ function readCurrentStyleProperties(pkg, element, family) {
2046
+ const styleName = attr$1(element, STYLE_NAME_ATTR);
2047
+ return resolveStyle(styleName, family, pkg).properties;
2048
+ }
2049
+ function applyStyleChange(pkg, element, family, change) {
2050
+ const merged = {
2051
+ ...readCurrentStyleProperties(pkg, element, family),
2052
+ ...change
2053
+ };
2054
+ const name = StyleRegistry.forPart(pkg, CONTENT_PART_PATH$1).intern({
2055
+ family,
2056
+ properties: merged
2057
+ });
2058
+ setAttr(element, STYLE_NAME_ATTR, name);
2059
+ }
2060
+ //#endregion
2061
+ //#region src/edit/odt/run.ts
2062
+ var OdtRun = class {
2063
+ container;
2064
+ node;
2065
+ pkg;
2066
+ removed = false;
2067
+ constructor(container, node, pkg) {
2068
+ this.container = container;
2069
+ this.node = node;
2070
+ this.pkg = pkg;
2071
+ }
2072
+ live() {
2073
+ if (this.removed) throw new Error("this OdtRun has been removed from its paragraph and can no longer be used");
2074
+ return this.node;
2075
+ }
2076
+ get text() {
2077
+ return decodeOdfText$1(this.live().children);
2078
+ }
2079
+ set text(value) {
2080
+ this.live().children = encodeOdfText(value);
2081
+ }
2082
+ get bold() {
2083
+ return readCurrentStyleProperties(this.pkg, this.live(), "text").bold ?? false;
2084
+ }
2085
+ set bold(value) {
2086
+ applyStyleChange(this.pkg, this.live(), "text", { bold: value });
2087
+ }
2088
+ get italic() {
2089
+ return readCurrentStyleProperties(this.pkg, this.live(), "text").italic ?? false;
2090
+ }
2091
+ set italic(value) {
2092
+ applyStyleChange(this.pkg, this.live(), "text", { italic: value });
2093
+ }
2094
+ get underline() {
2095
+ return readCurrentStyleProperties(this.pkg, this.live(), "text").underline ?? false;
2096
+ }
2097
+ set underline(value) {
2098
+ applyStyleChange(this.pkg, this.live(), "text", { underline: value });
2099
+ }
2100
+ get fontFamily() {
2101
+ return readCurrentStyleProperties(this.pkg, this.live(), "text").fontFamily;
2102
+ }
2103
+ set fontFamily(value) {
2104
+ applyStyleChange(this.pkg, this.live(), "text", { fontFamily: value });
2105
+ }
2106
+ get sizePt() {
2107
+ return readCurrentStyleProperties(this.pkg, this.live(), "text").sizePt;
2108
+ }
2109
+ set sizePt(value) {
2110
+ applyStyleChange(this.pkg, this.live(), "text", { sizePt: value });
2111
+ }
2112
+ get color() {
2113
+ return readCurrentStyleProperties(this.pkg, this.live(), "text").color;
2114
+ }
2115
+ set color(value) {
2116
+ applyStyleChange(this.pkg, this.live(), "text", { color: value });
2117
+ }
2118
+ remove() {
2119
+ removeChild(this.container, this.live());
2120
+ this.removed = true;
2121
+ }
2122
+ };
2123
+ function buildRun(pkg, init = {}) {
2124
+ const node = el("text:span");
2125
+ if (init.text !== void 0) node.children = encodeOdfText(init.text);
2126
+ const run = new OdtRun([], node, pkg);
2127
+ if (init.bold !== void 0) run.bold = init.bold;
2128
+ if (init.italic !== void 0) run.italic = init.italic;
2129
+ if (init.underline !== void 0) run.underline = init.underline;
2130
+ if (init.fontFamily !== void 0) run.fontFamily = init.fontFamily;
2131
+ if (init.sizePt !== void 0) run.sizePt = init.sizePt;
2132
+ if (init.color !== void 0) run.color = init.color;
2133
+ return node;
2134
+ }
2135
+ //#endregion
2136
+ //#region src/edit/odt/paragraph.ts
2137
+ var OdtParagraph = class {
2138
+ container;
2139
+ node;
2140
+ pkg;
2141
+ removed = false;
2142
+ constructor(container, node, pkg) {
2143
+ this.container = container;
2144
+ this.node = node;
2145
+ this.pkg = pkg;
2146
+ }
2147
+ live() {
2148
+ if (this.removed) throw new Error("this OdtParagraph has been removed from its body and can no longer be used");
2149
+ return this.node;
2150
+ }
2151
+ get text() {
2152
+ return decodeOdfText$1(this.live().children);
2153
+ }
2154
+ runs() {
2155
+ const node = this.live();
2156
+ const out = [];
2157
+ for (const child of node.children) if (child.type === "element" && child.tag === "text:span") out.push(new OdtRun(node.children, child, this.pkg));
2158
+ return out;
2159
+ }
2160
+ appendRun(init) {
2161
+ const node = this.live();
2162
+ const span = buildRun(this.pkg, init);
2163
+ node.children.push(span);
2164
+ return new OdtRun(node.children, span, this.pkg);
2165
+ }
2166
+ appendTab() {
2167
+ this.live().children.push(el("text:tab"));
2168
+ }
2169
+ insertRunAt(index, init) {
2170
+ const node = this.live();
2171
+ const span = buildRun(this.pkg, init);
2172
+ const spanIndices = [];
2173
+ node.children.forEach((child, i) => {
2174
+ if (child.type === "element" && child.tag === "text:span") spanIndices.push(i);
2175
+ });
2176
+ const insertAt = index < spanIndices.length ? spanIndices[index] ?? node.children.length : node.children.length;
2177
+ node.children.splice(insertAt, 0, span);
2178
+ return new OdtRun(node.children, span, this.pkg);
2179
+ }
2180
+ get styleId() {
2181
+ return attr$1(this.live(), "text:style-name");
2182
+ }
2183
+ set styleId(value) {
2184
+ const node = this.live();
2185
+ if (value === void 0) {
2186
+ removeAttr(node, "text:style-name");
2187
+ return;
2188
+ }
2189
+ setAttr(node, "text:style-name", value);
2190
+ }
2191
+ get alignment() {
2192
+ return readCurrentStyleProperties(this.pkg, this.live(), "paragraph").alignment;
2193
+ }
2194
+ set alignment(value) {
2195
+ applyStyleChange(this.pkg, this.live(), "paragraph", { alignment: value });
2196
+ }
2197
+ remove() {
2198
+ removeChild(this.container, this.live());
2199
+ this.removed = true;
2200
+ }
2201
+ };
2202
+ function buildParagraph(pkg, init = {}) {
2203
+ const node = el("text:p");
2204
+ const paragraph = new OdtParagraph([], node, pkg);
2205
+ if (init.styleId !== void 0) paragraph.styleId = init.styleId;
2206
+ if (init.alignment !== void 0) paragraph.alignment = init.alignment;
2207
+ if (init.text !== void 0) node.children.push(buildRun(pkg, { text: init.text }));
2208
+ return node;
2209
+ }
2210
+ //#endregion
2211
+ //#region src/edit/odt/list.ts
2212
+ const LIST_STYLE_PREFIX = "OdtList";
2213
+ const MAX_LIST_LEVELS = 10;
2214
+ const LIST_LEVEL_INDENT_PT = 18;
2215
+ const LIST_LEVEL_LABEL_WIDTH_PT = 18;
2216
+ function buildBulletListStyle(name) {
2217
+ const levels = [];
2218
+ for (let level = 1; level <= MAX_LIST_LEVELS; level++) levels.push(el("text:list-level-style-bullet", {
2219
+ "text:level": String(level),
2220
+ "text:bullet-char": "•"
2221
+ }, [el("style:list-level-properties", {
2222
+ "text:space-before": formatOdfLength(LIST_LEVEL_INDENT_PT * (level - 1), "pt"),
2223
+ "text:min-label-width": formatOdfLength(LIST_LEVEL_LABEL_WIDTH_PT, "pt")
2224
+ })]));
2225
+ return el("text:list-style", { "style:name": name }, levels);
2226
+ }
2227
+ function internBulletListStyle(pkg) {
2228
+ const automaticStyles = ensureAutomaticStyles(pkg);
2229
+ const name = nextStyleName(automaticStyles, "text:list-style", LIST_STYLE_PREFIX);
2230
+ automaticStyles.children.push(buildBulletListStyle(name));
2231
+ return name;
2232
+ }
2233
+ var OdtListItem = class {
2234
+ node;
2235
+ pkg;
2236
+ constructor(node, pkg) {
2237
+ this.node = node;
2238
+ this.pkg = pkg;
2239
+ }
2240
+ appendParagraph(init) {
2241
+ const paragraphElement = buildParagraph(this.pkg, init);
2242
+ this.node.children.push(paragraphElement);
2243
+ return new OdtParagraph(this.node.children, paragraphElement, this.pkg);
2244
+ }
2245
+ addNestedList() {
2246
+ const listElement = el("text:list", { "text:style-name": internBulletListStyle(this.pkg) });
2247
+ this.node.children.push(listElement);
2248
+ return new OdtList(this.node.children, listElement, this.pkg);
2249
+ }
2250
+ };
2251
+ var OdtList = class {
2252
+ container;
2253
+ node;
2254
+ pkg;
2255
+ removed = false;
2256
+ constructor(container, node, pkg) {
2257
+ this.container = container;
2258
+ this.node = node;
2259
+ this.pkg = pkg;
2260
+ }
2261
+ live() {
2262
+ if (this.removed) throw new Error("this OdtList has been removed from its body and can no longer be used");
2263
+ return this.node;
2264
+ }
2265
+ items() {
2266
+ const out = [];
2267
+ for (const child of this.live().children) if (child.type === "element" && child.tag === "text:list-item") out.push(new OdtListItem(child, this.pkg));
2268
+ return out;
2269
+ }
2270
+ addItem() {
2271
+ const itemElement = el("text:list-item");
2272
+ this.live().children.push(itemElement);
2273
+ return new OdtListItem(itemElement, this.pkg);
2274
+ }
2275
+ remove() {
2276
+ removeChild(this.container, this.live());
2277
+ this.removed = true;
2278
+ }
2279
+ };
2280
+ function buildList(pkg) {
2281
+ return el("text:list", { "text:style-name": internBulletListStyle(pkg) });
2282
+ }
2283
+ //#endregion
2284
+ //#region src/edit/odt/scaffold.ts
2285
+ const CONTENT_NS_PREFIXES = [
2286
+ "office",
2287
+ "style",
2288
+ "text",
2289
+ "table",
2290
+ "draw",
2291
+ "fo",
2292
+ "svg",
2293
+ "xlink"
2294
+ ];
2295
+ const STYLES_NS_PREFIXES = [
2296
+ "office",
2297
+ "style",
2298
+ "text",
2299
+ "table",
2300
+ "draw",
2301
+ "fo",
2302
+ "svg",
2303
+ "xlink"
2304
+ ];
2305
+ const META_NS_PREFIXES = [
2306
+ "office",
2307
+ "meta",
2308
+ "dc"
2309
+ ];
2310
+ const ODF_VERSION = "1.3";
2311
+ const PAGE_LAYOUT_NAME = "PM1";
2312
+ const MASTER_PAGE_NAME = "Standard";
2313
+ const DEFAULT_MARGIN_PT = 72;
2314
+ function declaration() {
2315
+ return {
2316
+ type: "declaration",
2317
+ attributes: [
2318
+ {
2319
+ name: "version",
2320
+ value: "1.0"
2321
+ },
2322
+ {
2323
+ name: "encoding",
2324
+ value: "UTF-8"
2325
+ },
2326
+ {
2327
+ name: "standalone",
2328
+ value: "yes"
2329
+ }
2330
+ ]
2331
+ };
2332
+ }
2333
+ function buildPageLayout() {
2334
+ return el("style:page-layout", { "style:name": PAGE_LAYOUT_NAME }, [el("style:page-layout-properties", {
2335
+ "fo:page-width": `${PAGE_SIZE_LETTER.widthPt}pt`,
2336
+ "fo:page-height": `${PAGE_SIZE_LETTER.heightPt}pt`,
2337
+ "fo:margin-top": `${DEFAULT_MARGIN_PT}pt`,
2338
+ "fo:margin-right": `${DEFAULT_MARGIN_PT}pt`,
2339
+ "fo:margin-bottom": `${DEFAULT_MARGIN_PT}pt`,
2340
+ "fo:margin-left": `${DEFAULT_MARGIN_PT}pt`
2341
+ })]);
2342
+ }
2343
+ function buildStylesXml() {
2344
+ return el("office:document-styles", {
2345
+ ...xmlnsAttributes([...STYLES_NS_PREFIXES]),
2346
+ "office:version": ODF_VERSION
2347
+ }, [
2348
+ el("office:styles"),
2349
+ el("office:automatic-styles", {}, [buildPageLayout()]),
2350
+ el("office:master-styles", {}, [el("style:master-page", {
2351
+ "style:name": MASTER_PAGE_NAME,
2352
+ "style:page-layout-name": PAGE_LAYOUT_NAME
2353
+ })])
2354
+ ]);
2355
+ }
2356
+ function buildContentXml() {
2357
+ return el("office:document-content", {
2358
+ ...xmlnsAttributes([...CONTENT_NS_PREFIXES]),
2359
+ "office:version": ODF_VERSION
2360
+ }, [el("office:automatic-styles"), el("office:body", {}, [el("office:text")])]);
2361
+ }
2362
+ function buildMetaXml() {
2363
+ return el("office:document-meta", {
2364
+ ...xmlnsAttributes([...META_NS_PREFIXES]),
2365
+ "office:version": ODF_VERSION
2366
+ }, [el("office:meta")]);
2367
+ }
2368
+ function createEmptyOdtPackage() {
2369
+ const pkg = { parts: {
2370
+ "content.xml": {
2371
+ kind: "xml",
2372
+ nodes: [declaration(), buildContentXml()]
2373
+ },
2374
+ "styles.xml": {
2375
+ kind: "xml",
2376
+ nodes: [declaration(), buildStylesXml()]
2377
+ },
2378
+ "meta.xml": {
2379
+ kind: "xml",
2380
+ nodes: [declaration(), buildMetaXml()]
2381
+ }
2382
+ } };
2383
+ setDocumentMediaType(pkg, ODF_MEDIA_TYPES.odt);
2384
+ syncManifest(pkg);
2385
+ return pkg;
2386
+ }
2387
+ //#endregion
2388
+ //#region src/edit/odt/table.ts
2389
+ const DEFAULT_TABLE_WIDTH_PT = 468;
2390
+ const TABLE_COLUMN_STYLE_PREFIX = "OdtCol";
2391
+ function internTableColumnWidth(pkg, widthPt) {
2392
+ const automaticStyles = ensureAutomaticStyles(pkg);
2393
+ const formatted = formatOdfLength(widthPt, "pt");
2394
+ for (const child of automaticStyles.children) {
2395
+ if (child.type !== "element" || child.tag !== "style:style" || attr$1(child, "style:family") !== "table-column") continue;
2396
+ const props = child.children.find((c) => c.type === "element" && c.tag === "style:table-column-properties");
2397
+ if (props !== void 0 && attr$1(props, "style:column-width") === formatted) {
2398
+ const existingName = attr$1(child, "style:name");
2399
+ if (existingName !== void 0) return existingName;
2400
+ }
2401
+ }
2402
+ const name = nextStyleName(automaticStyles, "style:style", TABLE_COLUMN_STYLE_PREFIX);
2403
+ automaticStyles.children.push(el("style:style", {
2404
+ "style:name": name,
2405
+ "style:family": "table-column"
2406
+ }, [el("style:table-column-properties", { "style:column-width": formatted })]));
2407
+ return name;
2408
+ }
2409
+ var OdtTableCell = class {
2410
+ node;
2411
+ pkg;
2412
+ constructor(node, pkg) {
2413
+ this.node = node;
2414
+ this.pkg = pkg;
2415
+ }
2416
+ paragraphs() {
2417
+ const out = [];
2418
+ for (const child of this.node.children) if (child.type === "element" && child.tag === "text:p") out.push(new OdtParagraph(this.node.children, child, this.pkg));
2419
+ return out;
2420
+ }
2421
+ appendParagraph(init) {
2422
+ const paragraphElement = buildParagraph(this.pkg, init);
2423
+ this.node.children.push(paragraphElement);
2424
+ return new OdtParagraph(this.node.children, paragraphElement, this.pkg);
2425
+ }
2426
+ get text() {
2427
+ return this.paragraphs().map((p) => p.text).join("\n");
2428
+ }
2429
+ };
2430
+ var OdtTableRow = class {
2431
+ node;
2432
+ pkg;
2433
+ constructor(node, pkg) {
2434
+ this.node = node;
2435
+ this.pkg = pkg;
2436
+ }
2437
+ cells() {
2438
+ const out = [];
2439
+ for (const child of this.node.children) if (child.type === "element" && child.tag === "table:table-cell") out.push(new OdtTableCell(child, this.pkg));
2440
+ return out;
2441
+ }
2442
+ };
2443
+ function buildCell(pkg) {
2444
+ return el("table:table-cell", {}, [buildParagraph(pkg)]);
2445
+ }
2446
+ function buildRow(pkg, columnCount) {
2447
+ const cells = [];
2448
+ for (let i = 0; i < columnCount; i++) cells.push(buildCell(pkg));
2449
+ return el("table:table-row", {}, cells);
2450
+ }
2451
+ var OdtTable = class {
2452
+ container;
2453
+ node;
2454
+ pkg;
2455
+ removed = false;
2456
+ constructor(container, node, pkg) {
2457
+ this.container = container;
2458
+ this.node = node;
2459
+ this.pkg = pkg;
2460
+ }
2461
+ live() {
2462
+ if (this.removed) throw new Error("this OdtTable has been removed and can no longer be used");
2463
+ return this.node;
2464
+ }
2465
+ rows() {
2466
+ const out = [];
2467
+ for (const child of this.live().children) if (child.type === "element" && child.tag === "table:table-row") out.push(new OdtTableRow(child, this.pkg));
2468
+ return out;
2469
+ }
2470
+ cell(rowIndex, columnIndex) {
2471
+ const row = this.rows()[rowIndex];
2472
+ if (row === void 0) throw new Error(`row ${rowIndex} does not exist in this table`);
2473
+ const cell = row.cells()[columnIndex];
2474
+ if (cell === void 0) throw new Error(`column ${columnIndex} does not exist in row ${rowIndex}`);
2475
+ return cell;
2476
+ }
2477
+ appendRow(columnCount) {
2478
+ const node = this.live();
2479
+ const row = buildRow(this.pkg, columnCount);
2480
+ node.children.push(row);
2481
+ return new OdtTableRow(row, this.pkg);
2482
+ }
2483
+ remove() {
2484
+ removeChild(this.container, this.live());
2485
+ this.removed = true;
2486
+ }
2487
+ };
2488
+ function buildTable(pkg, init) {
2489
+ const defaultWidth = DEFAULT_TABLE_WIDTH_PT / init.columns;
2490
+ const columns = [];
2491
+ for (let i = 0; i < init.columns; i++) {
2492
+ const widthPt = init.columnWidthsPt?.[i] ?? defaultWidth;
2493
+ columns.push(el("table:table-column", { "table:style-name": internTableColumnWidth(pkg, widthPt) }));
2494
+ }
2495
+ const rows = [];
2496
+ for (let r = 0; r < init.rows; r++) rows.push(buildRow(pkg, init.columns));
2497
+ return el("table:table", {}, [...columns, ...rows]);
2498
+ }
2499
+ //#endregion
2500
+ //#region src/edit/odt/editor.ts
2501
+ const CONTENT_PART_PATH = "content.xml";
2502
+ function findContentRoot(pkg) {
2503
+ const part = pkg.parts[CONTENT_PART_PATH];
2504
+ const root = part?.kind === "xml" ? part.nodes.find((n) => n.type === "element") : void 0;
2505
+ if (root === void 0) throw new Error(`package has no root element at ${CONTENT_PART_PATH}`);
2506
+ return root;
2507
+ }
2508
+ function directChild(parent, tag) {
2509
+ for (const child of parent.children) if (child.type === "element" && child.tag === tag) return child;
2510
+ }
2511
+ function findOfficeText(contentRoot) {
2512
+ const body = directChild(contentRoot, "office:body");
2513
+ const text = body === void 0 ? void 0 : directChild(body, "office:text");
2514
+ if (text === void 0) throw new Error(`${CONTENT_PART_PATH} has no office:body/office:text element`);
2515
+ return text;
2516
+ }
2517
+ var OdtBodyImpl = class {
2518
+ officeText;
2519
+ pkg;
2520
+ constructor(officeText, pkg) {
2521
+ this.officeText = officeText;
2522
+ this.pkg = pkg;
2523
+ }
2524
+ appendParagraph(init) {
2525
+ const paragraphElement = buildParagraph(this.pkg, init);
2526
+ this.officeText.children.push(paragraphElement);
2527
+ return new OdtParagraph(this.officeText.children, paragraphElement, this.pkg);
2528
+ }
2529
+ appendTable(init) {
2530
+ const tableElement = buildTable(this.pkg, init);
2531
+ this.officeText.children.push(tableElement);
2532
+ return new OdtTable(this.officeText.children, tableElement, this.pkg);
2533
+ }
2534
+ appendList() {
2535
+ const listElement = buildList(this.pkg);
2536
+ this.officeText.children.push(listElement);
2537
+ return new OdtList(this.officeText.children, listElement, this.pkg);
2538
+ }
2539
+ appendPageBreak() {
2540
+ const paragraphElement = buildParagraph(this.pkg);
2541
+ paragraphElement.attributes.push({
2542
+ name: "text:style-name",
2543
+ value: ensurePageBreakStyleName(this.pkg)
2544
+ });
2545
+ this.officeText.children.push(paragraphElement);
2546
+ }
2547
+ };
2548
+ var OdtEditor = class {
2549
+ body;
2550
+ pkg;
2551
+ constructor(pkg) {
2552
+ this.pkg = pkg;
2553
+ const officeText = findOfficeText(findContentRoot(pkg));
2554
+ this.body = new OdtBodyImpl(officeText, pkg);
2555
+ }
2556
+ paragraphs() {
2557
+ const officeText = findOfficeText(findContentRoot(this.pkg));
2558
+ const out = [];
2559
+ for (const child of officeText.children) if (child.type === "element" && child.tag === "text:p") out.push(new OdtParagraph(officeText.children, child, this.pkg));
2560
+ return out;
2561
+ }
2562
+ tables() {
2563
+ const officeText = findOfficeText(findContentRoot(this.pkg));
2564
+ const out = [];
2565
+ for (const child of officeText.children) if (child.type === "element" && child.tag === "table:table") out.push(new OdtTable(officeText.children, child, this.pkg));
2566
+ return out;
2567
+ }
2568
+ lists() {
2569
+ const officeText = findOfficeText(findContentRoot(this.pkg));
2570
+ const out = [];
2571
+ for (const child of officeText.children) if (child.type === "element" && child.tag === "text:list") out.push(new OdtList(officeText.children, child, this.pkg));
2572
+ return out;
2573
+ }
2574
+ toPackage() {
2575
+ return this.pkg;
2576
+ }
2577
+ toBytes() {
2578
+ return encodePackage$2(this.pkg);
2579
+ }
2580
+ };
2581
+ function openOdt(bytes) {
2582
+ return new OdtEditor(decodePackage$2(bytes));
2583
+ }
2584
+ function createOdt() {
2585
+ return new OdtEditor(createEmptyOdtPackage());
2586
+ }
2587
+ //#endregion
2588
+ //#region src/edit/odt/content.ts
2589
+ function buildOdtPackage(content) {
2590
+ if (content.kind !== "wordprocessing") throw new Error("buildOdtPackage requires a wordprocessing ContentDocument");
2591
+ const editor = createOdt();
2592
+ content.sections.forEach((section, sectionIndex) => {
2593
+ if (sectionIndex > 0) editor.body.appendPageBreak();
2594
+ for (const block of section.blocks) appendBlock(editor.body, block);
2595
+ });
2596
+ return editor.toPackage();
2597
+ }
2598
+ function populateParagraph(paragraph, block) {
2599
+ if (block.alignment !== void 0) paragraph.alignment = block.alignment;
2600
+ for (const run of block.runs) {
2601
+ if (run.text === " ") {
2602
+ paragraph.appendTab();
2603
+ continue;
2604
+ }
2605
+ const odtRun = paragraph.appendRun({ text: run.text });
2606
+ if (run.bold === true) odtRun.bold = true;
2607
+ if (run.italic === true) odtRun.italic = true;
2608
+ if (run.underline === true) odtRun.underline = true;
2609
+ if (run.fontFamily !== void 0) odtRun.fontFamily = run.fontFamily;
2610
+ if (run.sizePt !== void 0) odtRun.sizePt = run.sizePt;
2611
+ if (run.color !== void 0) odtRun.color = run.color;
2612
+ }
2613
+ }
2614
+ function appendTable(body, block) {
2615
+ const columns = block.columnWidthsPt.length;
2616
+ if (block.rows.length === 0 || columns === 0) return;
2617
+ const table = body.appendTable({
2618
+ rows: block.rows.length,
2619
+ columns,
2620
+ columnWidthsPt: block.columnWidthsPt
2621
+ });
2622
+ block.rows.forEach((row, rowIndex) => {
2623
+ const tableRow = table.rows()[rowIndex];
2624
+ if (tableRow === void 0) return;
2625
+ row.cells.forEach((cell, cellIndex) => {
2626
+ const tableCell = tableRow.cells()[cellIndex];
2627
+ if (tableCell === void 0) return;
2628
+ const [firstBlock, ...restBlocks] = cell.blocks;
2629
+ const firstParagraph = tableCell.paragraphs()[0];
2630
+ if (firstBlock?.kind === "paragraph" && firstParagraph !== void 0) populateParagraph(firstParagraph, firstBlock);
2631
+ else if (firstBlock !== void 0) appendCellBlock(tableCell, firstBlock);
2632
+ for (const remaining of restBlocks) appendCellBlock(tableCell, remaining);
2633
+ });
2634
+ });
2635
+ }
2636
+ function appendCellBlock(cell, block) {
2637
+ if (block.kind === "paragraph") populateParagraph(cell.appendParagraph(), block);
2638
+ }
2639
+ function appendBlock(body, block) {
2640
+ if (block.kind === "paragraph") populateParagraph(body.appendParagraph(), block);
2641
+ else if (block.kind === "pageBreak") body.appendPageBreak();
2642
+ else if (block.kind === "table") appendTable(body, block);
2643
+ }
2644
+ //#endregion
1954
2645
  //#region src/bytes/crc32.ts
1955
2646
  const CRC32_POLYNOMIAL = 3988292384;
1956
2647
  const BYTE_VALUES = 256;
@@ -7746,6 +8437,13 @@ function pdfToPptx(bytes, options) {
7746
8437
  }), { signal: options?.signal });
7747
8438
  return encodePackage$1(buildPptxPackage(content));
7748
8439
  }
8440
+ function pdfToOdt(bytes, options) {
8441
+ const content = reconstructWordprocessing(readPdf(bytes, {
8442
+ signal: options?.signal,
8443
+ sink: options?.sink
8444
+ }), { signal: options?.signal });
8445
+ return encodePackage$2(buildOdtPackage(content));
8446
+ }
7749
8447
  //#endregion
7750
8448
  //#region src/convert/codec.ts
7751
8449
  const docxPdfCodec = z.codec(DocxBytesSchema, PdfBytesSchema, {
@@ -7756,6 +8454,10 @@ const pptxPdfCodec = z.codec(PptxBytesSchema, PdfBytesSchema, {
7756
8454
  decode: (pptxBytes) => pptxToPdf(pptxBytes),
7757
8455
  encode: (pdfBytes) => pdfToPptx(pdfBytes)
7758
8456
  });
8457
+ z.codec(OdtBytesSchema, PdfBytesSchema, {
8458
+ decode: (odtBytes) => odtToPdf(odtBytes),
8459
+ encode: (pdfBytes) => pdfToOdt(pdfBytes)
8460
+ });
7759
8461
  //#endregion
7760
8462
  //#region src/convert/local.ts
7761
8463
  const SUPPORTED_CONVERSIONS = [
@@ -7778,6 +8480,10 @@ const SUPPORTED_CONVERSIONS = [
7778
8480
  {
7779
8481
  source: "pdf",
7780
8482
  target: "pptx"
8483
+ },
8484
+ {
8485
+ source: "pdf",
8486
+ target: "odt"
7781
8487
  }
7782
8488
  ];
7783
8489
  function substitutionDiagnostic(substitution, context) {
@@ -7868,6 +8574,19 @@ function createLocalDocumentConverter() {
7868
8574
  diagnostics
7869
8575
  });
7870
8576
  }
8577
+ if (source.format === "pdf" && targetFormat === "odt") {
8578
+ const bytes = pdfToOdt(source.bytes, {
8579
+ signal: options.signal,
8580
+ sink: (d) => diagnostics.push(fromPdfDiagnostic(d))
8581
+ });
8582
+ return Promise.resolve({
8583
+ document: {
8584
+ format: "odt",
8585
+ bytes
8586
+ },
8587
+ diagnostics
8588
+ });
8589
+ }
7871
8590
  return Promise.reject(/* @__PURE__ */ new Error(`unsupported conversion: ${source.format} -> ${targetFormat}`));
7872
8591
  }
7873
8592
  };
@@ -7879,4 +8598,4 @@ function fixedClock(date) {
7879
8598
  return { now: () => date };
7880
8599
  }
7881
8600
  //#endregion
7882
- export { AttributeSchema, BinaryPartSchema, COLOR_BLACK, CONTENT_FORMAT_VERSION, CommentSchema, CompactPackageSchema, CompactPartSchema, CompactXmlNodeSchema, ContentBlockSchema, ContentDocumentSchema, ContentImageBlockSchema, ContentPageBreakSchema, ContentParagraphSchema, ContentRunSchema, ContentSectionSchema, ContentShapeSchema, ContentSlideSchema, ContentTableCellSchema, ContentTableRowSchema, ContentTableSchema, DEFAULT_LAYOUT_FONT, DefinedNameSchema, DocxBytesSchema, DocxEditor, DocxParagraph, DocxRun, DocxTable, DocxTableCell, DocxTableRow, LAYOUT_FORMAT_VERSION, NOOP_DIAGNOSTIC_SINK, OdgBytesSchema, OdpBytesSchema, OdsBytesSchema, OdtBytesSchema, PAGE_SIZE_A4, PAGE_SIZE_LETTER, PackageSchema, PartSchema, PdfBytesSchema, PdfEncryptedError, PdfParseError, PptxBytesSchema, PptxEditor, PptxShape, PptxSlide, SLIDE_SIZE_STANDARD, SLIDE_SIZE_WIDESCREEN, XmlCdataSchema, XmlCommentSchema, XmlDeclarationSchema, XmlElementSchema, XmlNodeSchema, XmlPartSchema, XmlPiSchema, XmlTextSchema, attr, base64ToBytes, buildDocxPackage, buildPptxPackage, buildXml, bytesToBase64, childrenWithTag, compactCodec, compactPackageCodec, convertPresentationToLayout, convertWordprocessingToLayout, createDocx, createLocalDocumentConverter, createPptx, decodeCompactPackage, decodeEntities, decodePackage, docxPdfCodec, docxToPdf, elementsWithTag, encodeCompactPackage, encodePackage, fixedClock, flipY, fromCompact, isCompactXmlNode, isContentBlock, isXmlNode, odtToPdf, openDocx, openPptx, packageCodec, parsePackage, parseXml, pdfCodec, pdfToDocx, pdfToPptx, pptxPdfCodec, pptxToPdf, readDocxContent, readOdtContent, readPdf, readPptxContent, reconstructPresentation, reconstructWordprocessing, resolveRelationships, rgbHexToColor, rootElement, serializePackage, systemClock, textContent, throwIfAborted, toCompact, unzipPackage, walk, writePdf, xmlCodec, zipPackage };
8601
+ export { AttributeSchema, BinaryPartSchema, COLOR_BLACK, CONTENT_FORMAT_VERSION, CommentSchema, CompactPackageSchema, CompactPartSchema, CompactXmlNodeSchema, ContentBlockSchema, ContentDocumentSchema, ContentImageBlockSchema, ContentPageBreakSchema, ContentParagraphSchema, ContentRunSchema, ContentSectionSchema, ContentShapeSchema, ContentSlideSchema, ContentTableCellSchema, ContentTableRowSchema, ContentTableSchema, DEFAULT_LAYOUT_FONT, DefinedNameSchema, DocxBytesSchema, DocxEditor, DocxParagraph, DocxRun, DocxTable, DocxTableCell, DocxTableRow, LAYOUT_FORMAT_VERSION, NOOP_DIAGNOSTIC_SINK, OdgBytesSchema, OdpBytesSchema, OdsBytesSchema, OdtBytesSchema, OdtEditor, OdtList, OdtListItem, OdtParagraph, OdtRun, OdtTable, OdtTableCell, OdtTableRow, PAGE_SIZE_A4, PAGE_SIZE_LETTER, PackageSchema, PartSchema, PdfBytesSchema, PdfEncryptedError, PdfParseError, PptxBytesSchema, PptxEditor, PptxShape, PptxSlide, SLIDE_SIZE_STANDARD, SLIDE_SIZE_WIDESCREEN, XmlCdataSchema, XmlCommentSchema, XmlDeclarationSchema, XmlElementSchema, XmlNodeSchema, XmlPartSchema, XmlPiSchema, XmlTextSchema, attr, base64ToBytes, buildDocxPackage, buildOdtPackage, buildPptxPackage, buildXml, bytesToBase64, childrenWithTag, compactCodec, compactPackageCodec, convertPresentationToLayout, convertWordprocessingToLayout, createDocx, createLocalDocumentConverter, createOdt, createPptx, decodeCompactPackage, decodeEntities, decodePackage, docxPdfCodec, docxToPdf, elementsWithTag, encodeCompactPackage, encodePackage, fixedClock, flipY, fromCompact, isCompactXmlNode, isContentBlock, isXmlNode, odtToPdf, openDocx, openOdt, openPptx, packageCodec, parsePackage, parseXml, pdfCodec, pdfToDocx, pdfToPptx, pptxPdfCodec, pptxToPdf, readDocxContent, readOdtContent, readPdf, readPptxContent, reconstructPresentation, reconstructWordprocessing, resolveRelationships, rgbHexToColor, rootElement, serializePackage, systemClock, textContent, throwIfAborted, toCompact, unzipPackage, walk, writePdf, xmlCodec, zipPackage };