odf.js 1.4.0 → 1.5.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.cjs CHANGED
@@ -1942,21 +1942,21 @@ function walkDrawShapes(children, groupFunctions, pkg, out) {
1942
1942
  }
1943
1943
  //#endregion
1944
1944
  //#region src/typed/odp/read.ts
1945
- const CONTENT_PART = "content.xml";
1946
- const STYLES_PART = "styles.xml";
1947
- const AUTOMATIC_STYLE_PARTS = [CONTENT_PART, STYLES_PART];
1945
+ const CONTENT_PART$1 = "content.xml";
1946
+ const STYLES_PART$1 = "styles.xml";
1947
+ const AUTOMATIC_STYLE_PARTS$1 = [CONTENT_PART$1, STYLES_PART$1];
1948
1948
  function findMasterPageElement(pkg, masterPageName) {
1949
1949
  if (masterPageName === void 0) return;
1950
- const stylesPart = pkg.parts[STYLES_PART];
1950
+ const stylesPart = pkg.parts[STYLES_PART$1];
1951
1951
  if (stylesPart?.kind !== "xml") return;
1952
1952
  const root = rootElement(stylesPart.nodes);
1953
1953
  const masterStyles = root === void 0 ? void 0 : findChildElement(root.children, "office:master-styles");
1954
1954
  if (masterStyles === void 0) return;
1955
1955
  return childrenWithTag(masterStyles, "style:master-page").find((element) => attrValue(element, "style:name") === masterPageName);
1956
1956
  }
1957
- function findPageLayoutElement(pkg, pageLayoutName) {
1957
+ function findPageLayoutElement$1(pkg, pageLayoutName) {
1958
1958
  if (pageLayoutName === void 0) return;
1959
- for (const partPath of AUTOMATIC_STYLE_PARTS) {
1959
+ for (const partPath of AUTOMATIC_STYLE_PARTS$1) {
1960
1960
  const part = pkg.parts[partPath];
1961
1961
  if (part?.kind !== "xml") continue;
1962
1962
  const root = rootElement(part.nodes);
@@ -1968,7 +1968,7 @@ function findPageLayoutElement(pkg, pageLayoutName) {
1968
1968
  }
1969
1969
  function readSlideSize(page, pkg) {
1970
1970
  const masterPage = findMasterPageElement(pkg, attrValue(page, "draw:master-page-name"));
1971
- const pageLayout = findPageLayoutElement(pkg, masterPage === void 0 ? void 0 : attrValue(masterPage, "style:page-layout-name"));
1971
+ const pageLayout = findPageLayoutElement$1(pkg, masterPage === void 0 ? void 0 : attrValue(masterPage, "style:page-layout-name"));
1972
1972
  const properties = pageLayout === void 0 ? void 0 : childrenWithTag(pageLayout, "style:page-layout-properties")[0];
1973
1973
  return (properties === void 0 ? void 0 : parsePageSize(properties)) ?? document_content_model.SLIDE_SIZE_WIDESCREEN;
1974
1974
  }
@@ -1987,7 +1987,7 @@ function readSlide(page, pkg) {
1987
1987
  };
1988
1988
  }
1989
1989
  function readOdp(pkg) {
1990
- const contentPart = pkg.parts[CONTENT_PART];
1990
+ const contentPart = pkg.parts[CONTENT_PART$1];
1991
1991
  const root = contentPart?.kind === "xml" ? rootElement(contentPart.nodes) : void 0;
1992
1992
  const body = root === void 0 ? void 0 : findChildElement(root.children, "office:body");
1993
1993
  const presentation = body === void 0 ? void 0 : findChildElement(body.children, "office:presentation");
@@ -1998,6 +1998,110 @@ function readOdp(pkg) {
1998
1998
  };
1999
1999
  }
2000
2000
  //#endregion
2001
+ //#region src/typed/odt/read.ts
2002
+ const CONTENT_PART = "content.xml";
2003
+ const STYLES_PART = "styles.xml";
2004
+ const AUTOMATIC_STYLE_PARTS = [CONTENT_PART, STYLES_PART];
2005
+ function readOutlineLevel(headingElement) {
2006
+ const raw = attrValue(headingElement, "text:outline-level");
2007
+ if (raw === void 0) return 1;
2008
+ const parsed = Number.parseInt(raw, 10);
2009
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
2010
+ }
2011
+ function readParagraphOrHeading(element, pkg, list) {
2012
+ const paragraph = readOdfParagraph(element, pkg);
2013
+ if (element.tag === "text:h") paragraph.styleId = `Heading${readOutlineLevel(element)}`;
2014
+ if (list !== void 0) paragraph.list = list;
2015
+ return paragraph;
2016
+ }
2017
+ function readListItems(listElement, context, pkg, blocks) {
2018
+ for (const item of listElement.children) {
2019
+ if (item.type !== "element" || item.tag !== "text:list-item") continue;
2020
+ for (const itemChild of item.children) {
2021
+ if (itemChild.type !== "element") continue;
2022
+ if (itemChild.tag === "text:p" || itemChild.tag === "text:h") blocks.push(readParagraphOrHeading(itemChild, pkg, context));
2023
+ else if (itemChild.tag === "text:list") readListItems(itemChild, {
2024
+ numId: context.numId,
2025
+ level: context.level + 1
2026
+ }, pkg, blocks);
2027
+ }
2028
+ }
2029
+ }
2030
+ function readBlocks(nodes, pkg, listIdState) {
2031
+ const blocks = [];
2032
+ for (const node of nodes) {
2033
+ if (node.type !== "element") continue;
2034
+ if (node.tag === "text:p" || node.tag === "text:h") blocks.push(readParagraphOrHeading(node, pkg, void 0));
2035
+ else if (node.tag === "text:list") {
2036
+ const numId = `list${listIdState.next}`;
2037
+ listIdState.next += 1;
2038
+ readListItems(node, {
2039
+ numId,
2040
+ level: 0
2041
+ }, pkg, blocks);
2042
+ } else if (node.tag === "table:table") blocks.push(readOdfTable(node, pkg));
2043
+ else if (node.tag === "text:section") blocks.push(...readBlocks(node.children, pkg, listIdState));
2044
+ }
2045
+ return blocks;
2046
+ }
2047
+ function parseKnownOdfLength(value) {
2048
+ const parsed = parseOdfLength(value);
2049
+ if (parsed === void 0) throw new Error(`readOdt: internal error -- "${value}" is not a valid ODF length literal`);
2050
+ return parsed;
2051
+ }
2052
+ const DEFAULT_PAGE_SIZE = document_content_model.PAGE_SIZE_A4;
2053
+ const DEFAULT_MARGIN_PT = parseKnownOdfLength("2cm");
2054
+ const DEFAULT_MARGINS = {
2055
+ topPt: DEFAULT_MARGIN_PT,
2056
+ rightPt: DEFAULT_MARGIN_PT,
2057
+ bottomPt: DEFAULT_MARGIN_PT,
2058
+ leftPt: DEFAULT_MARGIN_PT
2059
+ };
2060
+ function findPageLayoutElement(pkg, pageLayoutName) {
2061
+ if (pageLayoutName === void 0) return;
2062
+ for (const partPath of AUTOMATIC_STYLE_PARTS) {
2063
+ const part = pkg.parts[partPath];
2064
+ if (part?.kind !== "xml") continue;
2065
+ const root = rootElement(part.nodes);
2066
+ const automaticStyles = root === void 0 ? void 0 : findChildElement(root.children, "office:automatic-styles");
2067
+ if (automaticStyles === void 0) continue;
2068
+ const found = childrenWithTag(automaticStyles, "style:page-layout").find((element) => attrValue(element, "style:name") === pageLayoutName);
2069
+ if (found !== void 0) return found;
2070
+ }
2071
+ }
2072
+ function readFirstMasterPageGeometry(pkg) {
2073
+ const stylesPart = pkg.parts[STYLES_PART];
2074
+ const stylesRoot = stylesPart?.kind === "xml" ? rootElement(stylesPart.nodes) : void 0;
2075
+ const masterStyles = stylesRoot === void 0 ? void 0 : findChildElement(stylesRoot.children, "office:master-styles");
2076
+ const masterPage = masterStyles === void 0 ? void 0 : findChildElement(masterStyles.children, "style:master-page");
2077
+ const layout = findPageLayoutElement(pkg, masterPage === void 0 ? void 0 : attrValue(masterPage, "style:page-layout-name"));
2078
+ const properties = layout === void 0 ? void 0 : findChildElement(layout.children, "style:page-layout-properties");
2079
+ const pageSize = properties === void 0 ? void 0 : parsePageSize(properties);
2080
+ const margins = properties === void 0 ? void 0 : parseMargins(properties);
2081
+ return {
2082
+ pageSize: pageSize ?? DEFAULT_PAGE_SIZE,
2083
+ margins: margins ?? DEFAULT_MARGINS
2084
+ };
2085
+ }
2086
+ function readOdt(pkg) {
2087
+ const contentPart = pkg.parts[CONTENT_PART];
2088
+ if (contentPart?.kind !== "xml") throw new Error(`readOdt: package has no ${CONTENT_PART} part`);
2089
+ const contentRoot = rootElement(contentPart.nodes);
2090
+ const body = contentRoot === void 0 ? void 0 : findChildElement(contentRoot.children, "office:body");
2091
+ const textElement = body === void 0 ? void 0 : findChildElement(body.children, "office:text");
2092
+ if (textElement === void 0) throw new Error(`readOdt: ${CONTENT_PART} has no office:body/office:text element`);
2093
+ const metadata = readOdfMetadata(pkg);
2094
+ const { pageSize, margins } = readFirstMasterPageGeometry(pkg);
2095
+ return {
2096
+ metadata,
2097
+ sections: [{
2098
+ pageSize,
2099
+ margins,
2100
+ blocks: readBlocks(textElement.children, pkg, { next: 1 })
2101
+ }]
2102
+ };
2103
+ }
2104
+ //#endregion
2001
2105
  Object.defineProperty(exports, "AlignmentSchema", {
2002
2106
  enumerable: true,
2003
2107
  get: function() {
@@ -2081,6 +2185,7 @@ exports.readOdfMetadata = readOdfMetadata;
2081
2185
  exports.readOdfParagraph = readOdfParagraph;
2082
2186
  exports.readOdfTable = readOdfTable;
2083
2187
  exports.readOdp = readOdp;
2188
+ exports.readOdt = readOdt;
2084
2189
  exports.resolveOdfShapeGeometry = resolveOdfShapeGeometry;
2085
2190
  exports.resolveStyle = resolveStyle;
2086
2191
  exports.resolveStyleElementChain = resolveStyleElementChain;
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { Alignment, AlignmentSchema, Box, Color, ContentParagraph, ContentShape, ContentSlide, ContentTable, LayoutMetadata, Margins, PageSize } from "document-content-model";
2
+ import { Alignment, AlignmentSchema, Box, Color, ContentParagraph, ContentSection, ContentShape, ContentSlide, ContentTable, LayoutMetadata, Margins, PageSize } from "document-content-model";
3
3
  //#region src/model/node.d.ts
4
4
  declare const AttributeSchema: z.ZodObject<{
5
5
  name: z.ZodString;
@@ -571,4 +571,11 @@ interface OdpDocument {
571
571
  }
572
572
  declare function readOdp(pkg: Package): OdpDocument;
573
573
  //#endregion
574
- export { type Alignment, AlignmentSchema, type Attribute, AttributeSchema, type BinaryPart, BinaryPartSchema, type BuildManifestOptions, type CascadeDiagnostic, type ImageFormat, type InternRequest, type LengthUnit, MANIFEST_PART, META_PART, MIMETYPE_PART, type Manifest, type ManifestEntry, ManifestEntrySchema, type ManifestProblem, ManifestProblemSchema, ManifestSchema, ODF_MEDIA_TYPES, ODF_NAMESPACES, type OdfExtension, type OdfNamespacePrefix, type OdfPoint, type OdfShapeGeometry, type OdfTransformFunction, type OdpDocument, type OtherPartRef, type Package, PackageSchema, type ParsedProperties, type Part, PartSchema, STYLE_FAMILIES, type StyleCascadeResult, type StyleElementChainResult, type StyleFamily, type StyleProperties, StylePropertiesSchema, StyleRegistry, type StyleRegistryOptions, TableCursor, type XmlCdata, XmlCdataSchema, type XmlComment, XmlCommentSchema, type XmlDeclaration, XmlDeclarationSchema, type XmlElement, XmlElementSchema, type XmlNode, XmlNodeSchema, type XmlPart, XmlPartSchema, type XmlPi, XmlPiSchema, type XmlText, XmlTextSchema, type ZipEntry, applyOdfTransform, attrValue, base64ToBytes, buildManifest, buildStylePropertyElements, buildXml, bytesToBase64, canonicalPropertiesString, cellReference, childrenWithTag, columnIndexToLetters, composeOdfGroupTransform, decodeOdfText, decodePackage, decodeXmlText, el, elementsWithTag, encodePackage, encodeXmlText, ensureSpan, findChildElement, findStyleElement, formatOdfColor, formatOdfLength, formatPercentageMultiplier, formatPt, getOdfSpaceCount, isStyleFamily, isXmlNode, measureOdfNodeLength, mediaTypeForExtension, netRotationDeg, packageCodec, paragraphPropertiesToAttributes, parseBox, parseLength, parseMargins, parseOdfColor, parseOdfLength, parseOdfTransform, parsePackage, parsePageSize, parseParagraphProperties, parseStyleElementProperties, parseTextProperties, parseXml, readDrawFrame, readManifest, readMimetype, readOdfMetadata, readOdfParagraph, readOdfTable, readOdp, resolveOdfShapeGeometry, resolveStyle, resolveStyleElementChain, rootElement, serializePackage, setDocumentMediaType, sniffImageFormat, sumOdfNodeLength, syncManifest, textPropertiesToAttributes, txt, unzipPackage, validateManifest, walkDrawShapes, writeManifest, writeMimetype, xmlCodec, xmlnsAttributes, zipPackage };
574
+ //#region src/typed/odt/read.d.ts
575
+ interface OdtDocument {
576
+ metadata: LayoutMetadata;
577
+ sections: ContentSection[];
578
+ }
579
+ declare function readOdt(pkg: Package): OdtDocument;
580
+ //#endregion
581
+ export { type Alignment, AlignmentSchema, type Attribute, AttributeSchema, type BinaryPart, BinaryPartSchema, type BuildManifestOptions, type CascadeDiagnostic, type ImageFormat, type InternRequest, type LengthUnit, MANIFEST_PART, META_PART, MIMETYPE_PART, type Manifest, type ManifestEntry, ManifestEntrySchema, type ManifestProblem, ManifestProblemSchema, ManifestSchema, ODF_MEDIA_TYPES, ODF_NAMESPACES, type OdfExtension, type OdfNamespacePrefix, type OdfPoint, type OdfShapeGeometry, type OdfTransformFunction, type OdpDocument, type OdtDocument, type OtherPartRef, type Package, PackageSchema, type ParsedProperties, type Part, PartSchema, STYLE_FAMILIES, type StyleCascadeResult, type StyleElementChainResult, type StyleFamily, type StyleProperties, StylePropertiesSchema, StyleRegistry, type StyleRegistryOptions, TableCursor, type XmlCdata, XmlCdataSchema, type XmlComment, XmlCommentSchema, type XmlDeclaration, XmlDeclarationSchema, type XmlElement, XmlElementSchema, type XmlNode, XmlNodeSchema, type XmlPart, XmlPartSchema, type XmlPi, XmlPiSchema, type XmlText, XmlTextSchema, type ZipEntry, applyOdfTransform, attrValue, base64ToBytes, buildManifest, buildStylePropertyElements, buildXml, bytesToBase64, canonicalPropertiesString, cellReference, childrenWithTag, columnIndexToLetters, composeOdfGroupTransform, decodeOdfText, decodePackage, decodeXmlText, el, elementsWithTag, encodePackage, encodeXmlText, ensureSpan, findChildElement, findStyleElement, formatOdfColor, formatOdfLength, formatPercentageMultiplier, formatPt, getOdfSpaceCount, isStyleFamily, isXmlNode, measureOdfNodeLength, mediaTypeForExtension, netRotationDeg, packageCodec, paragraphPropertiesToAttributes, parseBox, parseLength, parseMargins, parseOdfColor, parseOdfLength, parseOdfTransform, parsePackage, parsePageSize, parseParagraphProperties, parseStyleElementProperties, parseTextProperties, parseXml, readDrawFrame, readManifest, readMimetype, readOdfMetadata, readOdfParagraph, readOdfTable, readOdp, readOdt, resolveOdfShapeGeometry, resolveStyle, resolveStyleElementChain, rootElement, serializePackage, setDocumentMediaType, sniffImageFormat, sumOdfNodeLength, syncManifest, textPropertiesToAttributes, txt, unzipPackage, validateManifest, walkDrawShapes, writeManifest, writeMimetype, xmlCodec, xmlnsAttributes, zipPackage };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { Alignment, AlignmentSchema, Box, Color, ContentParagraph, ContentShape, ContentSlide, ContentTable, LayoutMetadata, Margins, PageSize } from "document-content-model";
2
+ import { Alignment, AlignmentSchema, Box, Color, ContentParagraph, ContentSection, ContentShape, ContentSlide, ContentTable, LayoutMetadata, Margins, PageSize } from "document-content-model";
3
3
  //#region src/model/node.d.ts
4
4
  declare const AttributeSchema: z.ZodObject<{
5
5
  name: z.ZodString;
@@ -571,4 +571,11 @@ interface OdpDocument {
571
571
  }
572
572
  declare function readOdp(pkg: Package): OdpDocument;
573
573
  //#endregion
574
- export { type Alignment, AlignmentSchema, type Attribute, AttributeSchema, type BinaryPart, BinaryPartSchema, type BuildManifestOptions, type CascadeDiagnostic, type ImageFormat, type InternRequest, type LengthUnit, MANIFEST_PART, META_PART, MIMETYPE_PART, type Manifest, type ManifestEntry, ManifestEntrySchema, type ManifestProblem, ManifestProblemSchema, ManifestSchema, ODF_MEDIA_TYPES, ODF_NAMESPACES, type OdfExtension, type OdfNamespacePrefix, type OdfPoint, type OdfShapeGeometry, type OdfTransformFunction, type OdpDocument, type OtherPartRef, type Package, PackageSchema, type ParsedProperties, type Part, PartSchema, STYLE_FAMILIES, type StyleCascadeResult, type StyleElementChainResult, type StyleFamily, type StyleProperties, StylePropertiesSchema, StyleRegistry, type StyleRegistryOptions, TableCursor, type XmlCdata, XmlCdataSchema, type XmlComment, XmlCommentSchema, type XmlDeclaration, XmlDeclarationSchema, type XmlElement, XmlElementSchema, type XmlNode, XmlNodeSchema, type XmlPart, XmlPartSchema, type XmlPi, XmlPiSchema, type XmlText, XmlTextSchema, type ZipEntry, applyOdfTransform, attrValue, base64ToBytes, buildManifest, buildStylePropertyElements, buildXml, bytesToBase64, canonicalPropertiesString, cellReference, childrenWithTag, columnIndexToLetters, composeOdfGroupTransform, decodeOdfText, decodePackage, decodeXmlText, el, elementsWithTag, encodePackage, encodeXmlText, ensureSpan, findChildElement, findStyleElement, formatOdfColor, formatOdfLength, formatPercentageMultiplier, formatPt, getOdfSpaceCount, isStyleFamily, isXmlNode, measureOdfNodeLength, mediaTypeForExtension, netRotationDeg, packageCodec, paragraphPropertiesToAttributes, parseBox, parseLength, parseMargins, parseOdfColor, parseOdfLength, parseOdfTransform, parsePackage, parsePageSize, parseParagraphProperties, parseStyleElementProperties, parseTextProperties, parseXml, readDrawFrame, readManifest, readMimetype, readOdfMetadata, readOdfParagraph, readOdfTable, readOdp, resolveOdfShapeGeometry, resolveStyle, resolveStyleElementChain, rootElement, serializePackage, setDocumentMediaType, sniffImageFormat, sumOdfNodeLength, syncManifest, textPropertiesToAttributes, txt, unzipPackage, validateManifest, walkDrawShapes, writeManifest, writeMimetype, xmlCodec, xmlnsAttributes, zipPackage };
574
+ //#region src/typed/odt/read.d.ts
575
+ interface OdtDocument {
576
+ metadata: LayoutMetadata;
577
+ sections: ContentSection[];
578
+ }
579
+ declare function readOdt(pkg: Package): OdtDocument;
580
+ //#endregion
581
+ export { type Alignment, AlignmentSchema, type Attribute, AttributeSchema, type BinaryPart, BinaryPartSchema, type BuildManifestOptions, type CascadeDiagnostic, type ImageFormat, type InternRequest, type LengthUnit, MANIFEST_PART, META_PART, MIMETYPE_PART, type Manifest, type ManifestEntry, ManifestEntrySchema, type ManifestProblem, ManifestProblemSchema, ManifestSchema, ODF_MEDIA_TYPES, ODF_NAMESPACES, type OdfExtension, type OdfNamespacePrefix, type OdfPoint, type OdfShapeGeometry, type OdfTransformFunction, type OdpDocument, type OdtDocument, type OtherPartRef, type Package, PackageSchema, type ParsedProperties, type Part, PartSchema, STYLE_FAMILIES, type StyleCascadeResult, type StyleElementChainResult, type StyleFamily, type StyleProperties, StylePropertiesSchema, StyleRegistry, type StyleRegistryOptions, TableCursor, type XmlCdata, XmlCdataSchema, type XmlComment, XmlCommentSchema, type XmlDeclaration, XmlDeclarationSchema, type XmlElement, XmlElementSchema, type XmlNode, XmlNodeSchema, type XmlPart, XmlPartSchema, type XmlPi, XmlPiSchema, type XmlText, XmlTextSchema, type ZipEntry, applyOdfTransform, attrValue, base64ToBytes, buildManifest, buildStylePropertyElements, buildXml, bytesToBase64, canonicalPropertiesString, cellReference, childrenWithTag, columnIndexToLetters, composeOdfGroupTransform, decodeOdfText, decodePackage, decodeXmlText, el, elementsWithTag, encodePackage, encodeXmlText, ensureSpan, findChildElement, findStyleElement, formatOdfColor, formatOdfLength, formatPercentageMultiplier, formatPt, getOdfSpaceCount, isStyleFamily, isXmlNode, measureOdfNodeLength, mediaTypeForExtension, netRotationDeg, packageCodec, paragraphPropertiesToAttributes, parseBox, parseLength, parseMargins, parseOdfColor, parseOdfLength, parseOdfTransform, parsePackage, parsePageSize, parseParagraphProperties, parseStyleElementProperties, parseTextProperties, parseXml, readDrawFrame, readManifest, readMimetype, readOdfMetadata, readOdfParagraph, readOdfTable, readOdp, readOdt, resolveOdfShapeGeometry, resolveStyle, resolveStyleElementChain, rootElement, serializePackage, setDocumentMediaType, sniffImageFormat, sumOdfNodeLength, syncManifest, textPropertiesToAttributes, txt, unzipPackage, validateManifest, walkDrawShapes, writeManifest, writeMimetype, xmlCodec, xmlnsAttributes, zipPackage };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { XMLBuilder, XMLParser } from "fast-xml-parser";
3
3
  import { unzipSync, zipSync } from "fflate";
4
- import { AlignmentSchema, AlignmentSchema as AlignmentSchema$1, ColorSchema, SLIDE_SIZE_WIDESCREEN, colorToRgbHex, rgbHexToColor } from "document-content-model";
4
+ import { AlignmentSchema, AlignmentSchema as AlignmentSchema$1, ColorSchema, PAGE_SIZE_A4, SLIDE_SIZE_WIDESCREEN, colorToRgbHex, rgbHexToColor } from "document-content-model";
5
5
  //#region src/model/node.ts
6
6
  const AttributeSchema = z.object({
7
7
  name: z.string(),
@@ -1941,21 +1941,21 @@ function walkDrawShapes(children, groupFunctions, pkg, out) {
1941
1941
  }
1942
1942
  //#endregion
1943
1943
  //#region src/typed/odp/read.ts
1944
- const CONTENT_PART = "content.xml";
1945
- const STYLES_PART = "styles.xml";
1946
- const AUTOMATIC_STYLE_PARTS = [CONTENT_PART, STYLES_PART];
1944
+ const CONTENT_PART$1 = "content.xml";
1945
+ const STYLES_PART$1 = "styles.xml";
1946
+ const AUTOMATIC_STYLE_PARTS$1 = [CONTENT_PART$1, STYLES_PART$1];
1947
1947
  function findMasterPageElement(pkg, masterPageName) {
1948
1948
  if (masterPageName === void 0) return;
1949
- const stylesPart = pkg.parts[STYLES_PART];
1949
+ const stylesPart = pkg.parts[STYLES_PART$1];
1950
1950
  if (stylesPart?.kind !== "xml") return;
1951
1951
  const root = rootElement(stylesPart.nodes);
1952
1952
  const masterStyles = root === void 0 ? void 0 : findChildElement(root.children, "office:master-styles");
1953
1953
  if (masterStyles === void 0) return;
1954
1954
  return childrenWithTag(masterStyles, "style:master-page").find((element) => attrValue(element, "style:name") === masterPageName);
1955
1955
  }
1956
- function findPageLayoutElement(pkg, pageLayoutName) {
1956
+ function findPageLayoutElement$1(pkg, pageLayoutName) {
1957
1957
  if (pageLayoutName === void 0) return;
1958
- for (const partPath of AUTOMATIC_STYLE_PARTS) {
1958
+ for (const partPath of AUTOMATIC_STYLE_PARTS$1) {
1959
1959
  const part = pkg.parts[partPath];
1960
1960
  if (part?.kind !== "xml") continue;
1961
1961
  const root = rootElement(part.nodes);
@@ -1967,7 +1967,7 @@ function findPageLayoutElement(pkg, pageLayoutName) {
1967
1967
  }
1968
1968
  function readSlideSize(page, pkg) {
1969
1969
  const masterPage = findMasterPageElement(pkg, attrValue(page, "draw:master-page-name"));
1970
- const pageLayout = findPageLayoutElement(pkg, masterPage === void 0 ? void 0 : attrValue(masterPage, "style:page-layout-name"));
1970
+ const pageLayout = findPageLayoutElement$1(pkg, masterPage === void 0 ? void 0 : attrValue(masterPage, "style:page-layout-name"));
1971
1971
  const properties = pageLayout === void 0 ? void 0 : childrenWithTag(pageLayout, "style:page-layout-properties")[0];
1972
1972
  return (properties === void 0 ? void 0 : parsePageSize(properties)) ?? SLIDE_SIZE_WIDESCREEN;
1973
1973
  }
@@ -1986,7 +1986,7 @@ function readSlide(page, pkg) {
1986
1986
  };
1987
1987
  }
1988
1988
  function readOdp(pkg) {
1989
- const contentPart = pkg.parts[CONTENT_PART];
1989
+ const contentPart = pkg.parts[CONTENT_PART$1];
1990
1990
  const root = contentPart?.kind === "xml" ? rootElement(contentPart.nodes) : void 0;
1991
1991
  const body = root === void 0 ? void 0 : findChildElement(root.children, "office:body");
1992
1992
  const presentation = body === void 0 ? void 0 : findChildElement(body.children, "office:presentation");
@@ -1997,4 +1997,108 @@ function readOdp(pkg) {
1997
1997
  };
1998
1998
  }
1999
1999
  //#endregion
2000
- export { AlignmentSchema, AttributeSchema, BinaryPartSchema, MANIFEST_PART, META_PART, MIMETYPE_PART, ManifestEntrySchema, ManifestProblemSchema, ManifestSchema, ODF_MEDIA_TYPES, ODF_NAMESPACES, PackageSchema, PartSchema, STYLE_FAMILIES, StylePropertiesSchema, StyleRegistry, TableCursor, XmlCdataSchema, XmlCommentSchema, XmlDeclarationSchema, XmlElementSchema, XmlNodeSchema, XmlPartSchema, XmlPiSchema, XmlTextSchema, applyOdfTransform, attrValue, base64ToBytes, buildManifest, buildStylePropertyElements, buildXml, bytesToBase64, canonicalPropertiesString, cellReference, childrenWithTag, columnIndexToLetters, composeOdfGroupTransform, decodeOdfText, decodePackage, decodeXmlText, el, elementsWithTag, encodePackage, encodeXmlText, ensureSpan, findChildElement, findStyleElement, formatOdfColor, formatOdfLength, formatPercentageMultiplier, formatPt, getOdfSpaceCount, isStyleFamily, isXmlNode, measureOdfNodeLength, mediaTypeForExtension, netRotationDeg, packageCodec, paragraphPropertiesToAttributes, parseBox, parseLength, parseMargins, parseOdfColor, parseOdfLength, parseOdfTransform, parsePackage, parsePageSize, parseParagraphProperties, parseStyleElementProperties, parseTextProperties, parseXml, readDrawFrame, readManifest, readMimetype, readOdfMetadata, readOdfParagraph, readOdfTable, readOdp, resolveOdfShapeGeometry, resolveStyle, resolveStyleElementChain, rootElement, serializePackage, setDocumentMediaType, sniffImageFormat, sumOdfNodeLength, syncManifest, textPropertiesToAttributes, txt, unzipPackage, validateManifest, walkDrawShapes, writeManifest, writeMimetype, xmlCodec, xmlnsAttributes, zipPackage };
2000
+ //#region src/typed/odt/read.ts
2001
+ const CONTENT_PART = "content.xml";
2002
+ const STYLES_PART = "styles.xml";
2003
+ const AUTOMATIC_STYLE_PARTS = [CONTENT_PART, STYLES_PART];
2004
+ function readOutlineLevel(headingElement) {
2005
+ const raw = attrValue(headingElement, "text:outline-level");
2006
+ if (raw === void 0) return 1;
2007
+ const parsed = Number.parseInt(raw, 10);
2008
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
2009
+ }
2010
+ function readParagraphOrHeading(element, pkg, list) {
2011
+ const paragraph = readOdfParagraph(element, pkg);
2012
+ if (element.tag === "text:h") paragraph.styleId = `Heading${readOutlineLevel(element)}`;
2013
+ if (list !== void 0) paragraph.list = list;
2014
+ return paragraph;
2015
+ }
2016
+ function readListItems(listElement, context, pkg, blocks) {
2017
+ for (const item of listElement.children) {
2018
+ if (item.type !== "element" || item.tag !== "text:list-item") continue;
2019
+ for (const itemChild of item.children) {
2020
+ if (itemChild.type !== "element") continue;
2021
+ if (itemChild.tag === "text:p" || itemChild.tag === "text:h") blocks.push(readParagraphOrHeading(itemChild, pkg, context));
2022
+ else if (itemChild.tag === "text:list") readListItems(itemChild, {
2023
+ numId: context.numId,
2024
+ level: context.level + 1
2025
+ }, pkg, blocks);
2026
+ }
2027
+ }
2028
+ }
2029
+ function readBlocks(nodes, pkg, listIdState) {
2030
+ const blocks = [];
2031
+ for (const node of nodes) {
2032
+ if (node.type !== "element") continue;
2033
+ if (node.tag === "text:p" || node.tag === "text:h") blocks.push(readParagraphOrHeading(node, pkg, void 0));
2034
+ else if (node.tag === "text:list") {
2035
+ const numId = `list${listIdState.next}`;
2036
+ listIdState.next += 1;
2037
+ readListItems(node, {
2038
+ numId,
2039
+ level: 0
2040
+ }, pkg, blocks);
2041
+ } else if (node.tag === "table:table") blocks.push(readOdfTable(node, pkg));
2042
+ else if (node.tag === "text:section") blocks.push(...readBlocks(node.children, pkg, listIdState));
2043
+ }
2044
+ return blocks;
2045
+ }
2046
+ function parseKnownOdfLength(value) {
2047
+ const parsed = parseOdfLength(value);
2048
+ if (parsed === void 0) throw new Error(`readOdt: internal error -- "${value}" is not a valid ODF length literal`);
2049
+ return parsed;
2050
+ }
2051
+ const DEFAULT_PAGE_SIZE = PAGE_SIZE_A4;
2052
+ const DEFAULT_MARGIN_PT = parseKnownOdfLength("2cm");
2053
+ const DEFAULT_MARGINS = {
2054
+ topPt: DEFAULT_MARGIN_PT,
2055
+ rightPt: DEFAULT_MARGIN_PT,
2056
+ bottomPt: DEFAULT_MARGIN_PT,
2057
+ leftPt: DEFAULT_MARGIN_PT
2058
+ };
2059
+ function findPageLayoutElement(pkg, pageLayoutName) {
2060
+ if (pageLayoutName === void 0) return;
2061
+ for (const partPath of AUTOMATIC_STYLE_PARTS) {
2062
+ const part = pkg.parts[partPath];
2063
+ if (part?.kind !== "xml") continue;
2064
+ const root = rootElement(part.nodes);
2065
+ const automaticStyles = root === void 0 ? void 0 : findChildElement(root.children, "office:automatic-styles");
2066
+ if (automaticStyles === void 0) continue;
2067
+ const found = childrenWithTag(automaticStyles, "style:page-layout").find((element) => attrValue(element, "style:name") === pageLayoutName);
2068
+ if (found !== void 0) return found;
2069
+ }
2070
+ }
2071
+ function readFirstMasterPageGeometry(pkg) {
2072
+ const stylesPart = pkg.parts[STYLES_PART];
2073
+ const stylesRoot = stylesPart?.kind === "xml" ? rootElement(stylesPart.nodes) : void 0;
2074
+ const masterStyles = stylesRoot === void 0 ? void 0 : findChildElement(stylesRoot.children, "office:master-styles");
2075
+ const masterPage = masterStyles === void 0 ? void 0 : findChildElement(masterStyles.children, "style:master-page");
2076
+ const layout = findPageLayoutElement(pkg, masterPage === void 0 ? void 0 : attrValue(masterPage, "style:page-layout-name"));
2077
+ const properties = layout === void 0 ? void 0 : findChildElement(layout.children, "style:page-layout-properties");
2078
+ const pageSize = properties === void 0 ? void 0 : parsePageSize(properties);
2079
+ const margins = properties === void 0 ? void 0 : parseMargins(properties);
2080
+ return {
2081
+ pageSize: pageSize ?? DEFAULT_PAGE_SIZE,
2082
+ margins: margins ?? DEFAULT_MARGINS
2083
+ };
2084
+ }
2085
+ function readOdt(pkg) {
2086
+ const contentPart = pkg.parts[CONTENT_PART];
2087
+ if (contentPart?.kind !== "xml") throw new Error(`readOdt: package has no ${CONTENT_PART} part`);
2088
+ const contentRoot = rootElement(contentPart.nodes);
2089
+ const body = contentRoot === void 0 ? void 0 : findChildElement(contentRoot.children, "office:body");
2090
+ const textElement = body === void 0 ? void 0 : findChildElement(body.children, "office:text");
2091
+ if (textElement === void 0) throw new Error(`readOdt: ${CONTENT_PART} has no office:body/office:text element`);
2092
+ const metadata = readOdfMetadata(pkg);
2093
+ const { pageSize, margins } = readFirstMasterPageGeometry(pkg);
2094
+ return {
2095
+ metadata,
2096
+ sections: [{
2097
+ pageSize,
2098
+ margins,
2099
+ blocks: readBlocks(textElement.children, pkg, { next: 1 })
2100
+ }]
2101
+ };
2102
+ }
2103
+ //#endregion
2104
+ export { AlignmentSchema, AttributeSchema, BinaryPartSchema, MANIFEST_PART, META_PART, MIMETYPE_PART, ManifestEntrySchema, ManifestProblemSchema, ManifestSchema, ODF_MEDIA_TYPES, ODF_NAMESPACES, PackageSchema, PartSchema, STYLE_FAMILIES, StylePropertiesSchema, StyleRegistry, TableCursor, XmlCdataSchema, XmlCommentSchema, XmlDeclarationSchema, XmlElementSchema, XmlNodeSchema, XmlPartSchema, XmlPiSchema, XmlTextSchema, applyOdfTransform, attrValue, base64ToBytes, buildManifest, buildStylePropertyElements, buildXml, bytesToBase64, canonicalPropertiesString, cellReference, childrenWithTag, columnIndexToLetters, composeOdfGroupTransform, decodeOdfText, decodePackage, decodeXmlText, el, elementsWithTag, encodePackage, encodeXmlText, ensureSpan, findChildElement, findStyleElement, formatOdfColor, formatOdfLength, formatPercentageMultiplier, formatPt, getOdfSpaceCount, isStyleFamily, isXmlNode, measureOdfNodeLength, mediaTypeForExtension, netRotationDeg, packageCodec, paragraphPropertiesToAttributes, parseBox, parseLength, parseMargins, parseOdfColor, parseOdfLength, parseOdfTransform, parsePackage, parsePageSize, parseParagraphProperties, parseStyleElementProperties, parseTextProperties, parseXml, readDrawFrame, readManifest, readMimetype, readOdfMetadata, readOdfParagraph, readOdfTable, readOdp, readOdt, resolveOdfShapeGeometry, resolveStyle, resolveStyleElementChain, rootElement, serializePackage, setDocumentMediaType, sniffImageFormat, sumOdfNodeLength, syncManifest, textPropertiesToAttributes, txt, unzipPackage, validateManifest, walkDrawShapes, writeManifest, writeMimetype, xmlCodec, xmlnsAttributes, zipPackage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "odf.js",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Type-safe, lossless round-trip conversion between OpenDocument Format packages (odt, ods, odp) and JSON, hand-written and dependency-minimal, built on Zod 4 codecs.",
5
5
  "type": "module",
6
6
  "repository": {