Documentation and Refactoring (#3028)
* Documentation and Refactoring * Documentation and Refactoring * Fix lint issues * Convert components to Builder style --------- Co-authored-by: Dolan Miu <dmiu@bloomberg.net>
This commit is contained in:
@ -2,26 +2,26 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { Formatter } from "@export/formatter";
|
||||
|
||||
import { DocumentGrid, DocumentGridType } from ".";
|
||||
import { DocumentGridType, createDocumentGrid } from ".";
|
||||
|
||||
describe("DocumentGrid", () => {
|
||||
describe("createDocumentGrid", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create documentGrid with specified linePitch", () => {
|
||||
const docGrid = new DocumentGrid(360);
|
||||
const docGrid = createDocumentGrid({ linePitch: 360 });
|
||||
const tree = new Formatter().format(docGrid);
|
||||
|
||||
expect(tree["w:docGrid"]).to.deep.equal({ _attr: { "w:linePitch": 360 } });
|
||||
});
|
||||
|
||||
it("should create documentGrid with specified linePitch and type", () => {
|
||||
const docGrid = new DocumentGrid(360, undefined, DocumentGridType.LINES);
|
||||
const docGrid = createDocumentGrid({ linePitch: 360, type: DocumentGridType.LINES });
|
||||
const tree = new Formatter().format(docGrid);
|
||||
|
||||
expect(tree["w:docGrid"]).to.deep.equal({ _attr: { "w:linePitch": 360, "w:type": "lines" } });
|
||||
});
|
||||
|
||||
it("should create documentGrid with specified linePitch,charSpace and type", () => {
|
||||
const docGrid = new DocumentGrid(346, -1541, DocumentGridType.LINES_AND_CHARS);
|
||||
const docGrid = createDocumentGrid({ linePitch: 346, charSpace: -1541, type: DocumentGridType.LINES_AND_CHARS });
|
||||
const tree = new Formatter().format(docGrid);
|
||||
|
||||
expect(tree["w:docGrid"]).to.deep.equal({ _attr: { "w:linePitch": 346, "w:charSpace": -1541, "w:type": "linesAndChars" } });
|
||||
|
@ -1,53 +1,113 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
|
||||
import { BuilderElement, XmlComponent } from "@file/xml-components";
|
||||
import { decimalNumber } from "@util/values";
|
||||
|
||||
// not implemented
|
||||
// <xsd:simpleType name="ST_DocGrid">
|
||||
// <xsd:restriction base="xsd:string">
|
||||
// <xsd:enumeration value="default"/>
|
||||
// <xsd:enumeration value="lines"/>
|
||||
// <xsd:enumeration value="linesAndChars"/>
|
||||
// <xsd:enumeration value="snapToChars"/>
|
||||
// </xsd:restriction>
|
||||
// </xsd:simpleType>
|
||||
|
||||
// <xsd:complexType name="CT_DocGrid">
|
||||
// <xsd:attribute name="type" type="ST_DocGrid"/>
|
||||
// <xsd:attribute name="linePitch" type="ST_DecimalNumber"/>
|
||||
// <xsd:attribute name="charSpace" type="ST_DecimalNumber"/>
|
||||
// </xsd:complexType>
|
||||
|
||||
/**
|
||||
* Specifies the type of the current document grid, which defines the grid behavior.
|
||||
*
|
||||
* The grid can define a grid which snaps all East Asian characters to grid positions, but leaves Latin text with its default spacing; a grid which adds the specified character pitch to all characters on each row; or a grid which affects only the line pitch for the current section.
|
||||
*
|
||||
* Reference: https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_ST_DocGrid_topic_ID0ELYP2.html
|
||||
*
|
||||
* ## XSD Schema
|
||||
* ```xml
|
||||
* <xsd:simpleType name="ST_DocGrid">
|
||||
* <xsd:restriction base="xsd:string">
|
||||
* <xsd:enumeration value="default"/>
|
||||
* <xsd:enumeration value="lines"/>
|
||||
* <xsd:enumeration value="linesAndChars"/>
|
||||
* <xsd:enumeration value="snapToChars"/>
|
||||
* </xsd:restriction>
|
||||
* </xsd:simpleType>
|
||||
* ```
|
||||
*/
|
||||
export const DocumentGridType = {
|
||||
/**
|
||||
* Specifies that no document grid shall be applied to the contents of the current section in the document.
|
||||
*/
|
||||
DEFAULT: "default",
|
||||
/**
|
||||
* Specifies that the parent section shall have additional line pitch added to each line within it (as specified on the <docGrid> element (§2.6.5)) in order to maintain the specified number of lines per page.
|
||||
*/
|
||||
LINES: "lines",
|
||||
/**
|
||||
* Specifies that the parent section shall have both the additional line pitch and character pitch added to each line and character within it (as specified on the <docGrid> element (§2.6.5)) in order to maintain a specific number of lines per page and characters per line.
|
||||
*
|
||||
* When this value is set, the input specified via the user interface may be allowed in exact number of line/character pitch units. */
|
||||
LINES_AND_CHARS: "linesAndChars",
|
||||
/**
|
||||
* Specifies that the parent section shall have both the additional line pitch and character pitch added to each line and character within it (as specified on the <docGrid> element (§2.6.5)) in order to maintain a specific number of lines per page and characters per line.
|
||||
*
|
||||
* When this value is set, the input specified via the user interface may be restricted to the number of lines per page and characters per line, with the consumer or producer translating this information based on the current font data to get the resulting line and character pitch values
|
||||
*/
|
||||
SNAP_TO_CHARS: "snapToChars",
|
||||
} as const;
|
||||
|
||||
export type IDocGridAttributesProperties = {
|
||||
/**
|
||||
* Specifies the type of the current document grid, which defines the grid behavior.
|
||||
*
|
||||
* The grid can define a grid which snaps all East Asian characters to grid positions, but leaves Latin text with its default spacing; a grid which adds the specified character pitch to each character on each row; or a grid which affects only the line pitch for the current section.
|
||||
*/
|
||||
readonly type?: (typeof DocumentGridType)[keyof typeof DocumentGridType];
|
||||
readonly linePitch?: number;
|
||||
/**
|
||||
* Specifies the number of lines to be allowed on the document grid for the current page assuming all lines have equal line pitch applied to them. This line pitch shall not be added to any line which appears within a table cell unless the <adjustLineHeightInTable> element (§2.15.3.1) is present in the document's compatibility settings.
|
||||
*
|
||||
* This attribute is specified in twentieths of a point, and defines the pitch for each line of text on this page such that the desired number of single spaced lines of text fits on the current page.
|
||||
*
|
||||
* ```xml
|
||||
* <w:docGrid w:linePitch="684" …/>
|
||||
* ```
|
||||
*
|
||||
* The `linePitch` attribute specifies that 34.2 points is to the amount of pitch allowed for each line on this page in order to maintain the specific document grid. ]
|
||||
*
|
||||
* Individual paragraphs can override the line pitch information specified for the document grid by either:
|
||||
*
|
||||
* Specifying an exact line spacing value using the `lineRule` attribute of value exact on the <spacing> element (§2.3.1.33).
|
||||
*
|
||||
* Specifying that the paragraph text shall not snap to the document grid via the <snapToGrid> element (§2.3.1.32).
|
||||
*
|
||||
* The possible values for this attribute are defined by the ST_DecimalNumber simple type (§2.18.16).
|
||||
*/
|
||||
readonly linePitch: number;
|
||||
/**
|
||||
* Specifies the number of characters to be allowed on the document grid for each line in this section.
|
||||
*
|
||||
* This attribute's value shall be specified by multiplying the difference between the desired character pitch and the character pitch for that character in the font size of the Normal font by 4096.
|
||||
*
|
||||
* This value shall then be used to add the character pitch for the specified point size to each character in the section [: This results in text in the Normal style having a specific number of characters per line. ]
|
||||
*
|
||||
* ```xml
|
||||
* <w:docGrid w:charSize="40960" …/>
|
||||
* ```
|
||||
* The `charSpace` attribute specifies a value of 40960, which means that the delta between the character pitch of each character in the grid and the Normal font is 10 points, resulting in a character pitch of 11+10 = 21 points for all characters in this section. ]
|
||||
*
|
||||
* Individual runs of text can override the line pitch information specified for the document grid by specifying that the run text shall not snap to the document grid via the <snapToGrid> element (§2.3.2.32).
|
||||
*
|
||||
* The possible values for this attribute are defined by the `ST_DecimalNumber` simple type (§2.18.16).
|
||||
*/
|
||||
readonly charSpace?: number;
|
||||
};
|
||||
|
||||
export class DocGridAttributes extends XmlAttributeComponent<IDocGridAttributesProperties> {
|
||||
protected readonly xmlKeys = {
|
||||
type: "w:type",
|
||||
linePitch: "w:linePitch",
|
||||
charSpace: "w:charSpace",
|
||||
};
|
||||
}
|
||||
|
||||
export class DocumentGrid extends XmlComponent {
|
||||
public constructor(linePitch: number, charSpace?: number, type?: (typeof DocumentGridType)[keyof typeof DocumentGridType]) {
|
||||
super("w:docGrid");
|
||||
|
||||
this.root.push(
|
||||
new DocGridAttributes({
|
||||
type: type,
|
||||
linePitch: decimalNumber(linePitch),
|
||||
charSpace: charSpace ? decimalNumber(charSpace) : undefined,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This element specifies the settings for the document grid, which enables precise layout of full-width East Asian language characters within a document by specifying the desired number of characters per line and lines per page for all East Asian text content in this section.
|
||||
*
|
||||
* Reference: https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_docGrid_topic_ID0EHU5S.html
|
||||
*
|
||||
* ```xml
|
||||
* <xsd:complexType name="CT_DocGrid">
|
||||
* <xsd:attribute name="type" type="ST_DocGrid"/>
|
||||
* <xsd:attribute name="linePitch" type="ST_DecimalNumber"/>
|
||||
* <xsd:attribute name="charSpace" type="ST_DecimalNumber"/>
|
||||
* </xsd:complexType>
|
||||
* ```
|
||||
* @returns
|
||||
*/
|
||||
export const createDocumentGrid = ({ type, linePitch, charSpace }: IDocGridAttributesProperties): XmlComponent =>
|
||||
new BuilderElement<IDocGridAttributesProperties>({
|
||||
name: "w:docGrid",
|
||||
attributes: {
|
||||
type: { key: "w:type", value: type },
|
||||
linePitch: { key: "w:linePitch", value: decimalNumber(linePitch) },
|
||||
charSpace: { key: "w:charSpace", value: charSpace ? decimalNumber(charSpace) : undefined },
|
||||
},
|
||||
});
|
||||
|
@ -1,19 +1,30 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
|
||||
|
||||
// <xsd:simpleType name="ST_HdrFtr">
|
||||
// <xsd:restriction base="xsd:string">
|
||||
// <xsd:enumeration value="even"/>
|
||||
// <xsd:enumeration value="default"/>
|
||||
// <xsd:enumeration value="first"/>
|
||||
// </xsd:restriction>
|
||||
// </xsd:simpleType>
|
||||
/**
|
||||
* This simple type specifies the possible types of headers and footers which may be specified for a given header or footer reference in a document. This value determines the page(s) on which the current header or footer shall be displayed.
|
||||
*
|
||||
* Reference: https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_ST_HdrFtr_topic_ID0E2UW2.html
|
||||
*
|
||||
* ## XSD Schema
|
||||
* ```xml
|
||||
* <xsd:simpleType name="ST_HdrFtr">
|
||||
* <xsd:restriction base="xsd:string">
|
||||
* <xsd:enumeration value="even"/>
|
||||
* <xsd:enumeration value="default"/>
|
||||
* <xsd:enumeration value="first"/>
|
||||
* </xsd:restriction>
|
||||
* </xsd:simpleType>
|
||||
* ```
|
||||
*/
|
||||
export const HeaderFooterReferenceType = {
|
||||
/** Specifies that this header or footer shall appear on every page in this section which is not overridden with a specific `even` or `first` page header/footer. In a section with all three types specified, this type shall be used on all odd numbered pages (counting from the `first` page in the section, not the section numbering). */
|
||||
DEFAULT: "default",
|
||||
/** Specifies that this header or footer shall appear on the first page in this section. The appearance of this header or footer is contingent on the setting of the `titlePg` element (§2.10.6). */
|
||||
FIRST: "first",
|
||||
/** Specifies that this header or footer shall appear on all even numbered pages in this section (counting from the first page in the section, not the section numbering). The appearance of this header or footer is contingent on the setting of the `evenAndOddHeaders` element (§2.10.1). */
|
||||
EVEN: "even",
|
||||
} as const;
|
||||
|
||||
// </xsd:complexType>
|
||||
// <xsd:group name="EG_HdrFtrReferences">
|
||||
// <xsd:choice>
|
||||
// <xsd:element name="headerReference" type="CT_HdrFtrRef" minOccurs="0"/>
|
||||
|
@ -1,35 +1,128 @@
|
||||
// http://officeopenxml.com/WPsectionLineNumbering.php
|
||||
import { BuilderElement, XmlComponent } from "@file/xml-components";
|
||||
import { PositiveUniversalMeasure, decimalNumber, twipsMeasureValue } from "@util/values";
|
||||
|
||||
// <xsd:simpleType name="ST_LineNumberRestart">
|
||||
// <xsd:restriction base="xsd:string">
|
||||
// <xsd:enumeration value="newPage"/>
|
||||
// <xsd:enumeration value="newSection"/>
|
||||
// <xsd:enumeration value="continuous"/>
|
||||
// </xsd:restriction>
|
||||
// </xsd:simpleType>
|
||||
|
||||
/**
|
||||
* This simple type specifies when the line numbering in the parent section shall be reset to its restart value. The line numbering increments for each line (even if the line number itself is not displayed) until it reaches the restart point specified by this element.
|
||||
*
|
||||
* Reference: https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_ST_LineNumberRestart_topic_ID0EUS42.html
|
||||
*
|
||||
* ## XSD Schema
|
||||
*
|
||||
* ```xml
|
||||
* <xsd:simpleType name="ST_LineNumberRestart">
|
||||
* <xsd:restriction base="xsd:string">
|
||||
* <xsd:enumeration value="newPage"/>
|
||||
* <xsd:enumeration value="newSection"/>
|
||||
* <xsd:enumeration value="continuous"/>
|
||||
* </xsd:restriction>
|
||||
* </xsd:simpleType>
|
||||
* ```
|
||||
*/
|
||||
export const LineNumberRestartFormat = {
|
||||
/**
|
||||
* ## Restart Line Numbering on Each Page
|
||||
*
|
||||
* Specifies that line numbering for the parent section shall restart to the starting value whenever a new page is displayed.
|
||||
*/
|
||||
NEW_PAGE: "newPage",
|
||||
/**
|
||||
* ## Restart Line Numbering for Each Section
|
||||
*
|
||||
* Specifies that line numbering for the parent section shall restart to the starting value whenever the parent begins.
|
||||
*/
|
||||
NEW_SECTION: "newSection",
|
||||
/**
|
||||
* ## Continue Line Numbering From Previous Section
|
||||
*
|
||||
* Specifies that line numbering for the parent section shall continue from the line numbering from the end of the previous section, if any.
|
||||
*/
|
||||
CONTINUOUS: "continuous",
|
||||
} as const;
|
||||
|
||||
// <xsd:complexType name="CT_LineNumber">
|
||||
// <xsd:attribute name="countBy" type="ST_DecimalNumber" use="optional"/>
|
||||
// <xsd:attribute name="start" type="ST_DecimalNumber" use="optional" default="1"/>
|
||||
// <xsd:attribute name="distance" type="s:ST_TwipsMeasure" use="optional"/>
|
||||
// <xsd:attribute name="restart" type="ST_LineNumberRestart" use="optional" default="newPage"/>
|
||||
// </xsd:complexType>
|
||||
|
||||
export type ILineNumberAttributes = {
|
||||
/**
|
||||
* Specifies the line number increments to be displayed in the current document.
|
||||
*
|
||||
* Although each line has an associated line number, only lines which are an even multiple of this value shall be displayed.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```xml
|
||||
* <w:lnNumType … w:countBy="5"/>
|
||||
* ```
|
||||
*
|
||||
* This setting ensures that only lines whose number is a multiple of (e.g. 5, 10, and 15) will have a line number displayed. ]
|
||||
*
|
||||
* The possible values for this attribute are defined by the ST_DecimalNumber simple type (§2.18.16).
|
||||
*/
|
||||
readonly countBy?: number;
|
||||
/**
|
||||
* ## Line Numbering Starting Value
|
||||
*
|
||||
* Specifies the starting value used for the first line whenever the line numbering is restarted by use of the `restart` attribute.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```xml
|
||||
* <w:lnNumType w:start="3" w:countBy="5"/>
|
||||
* ```
|
||||
*
|
||||
* The `start` attribute specifies that line numbers shall be counted starting from the number 3.
|
||||
*
|
||||
* The possible values for this attribute are defined by the ST_DecimalNumber simple type (§2.18.16).
|
||||
*/
|
||||
readonly start?: number;
|
||||
/**
|
||||
* ## Line Numbering Restart Setting
|
||||
*
|
||||
* Specifies when the line numbering in this section shall be reset to the line number specified by the `start` attribute's value.
|
||||
*
|
||||
* The line numbering increments for each line (even if it is not displayed) until it reaches the restart point specified by this element.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```xml
|
||||
* <w:sectPr>
|
||||
* ...
|
||||
* <w:lnNumType w:restart="newPage" ... />
|
||||
* </w:sectPr>
|
||||
* ```
|
||||
*
|
||||
* The value of `newPage` specifies that the line numbers shall restart at the top of each page to the value specified by the `start` attribute. In this case, `newPage` is the default, so this value could have been omitted entirely.
|
||||
*
|
||||
* The possible values for this attribute are defined by the ST_LineNumberRestart simple type (§2.18.54).
|
||||
*/
|
||||
readonly restart?: (typeof LineNumberRestartFormat)[keyof typeof LineNumberRestartFormat];
|
||||
/**
|
||||
* Specifies the distance between the text margin and the edge of any line numbers appearing in that section.
|
||||
*
|
||||
* ```.xml
|
||||
* <w:lnNumType ... w:distance="720"/>
|
||||
* ```
|
||||
*
|
||||
* The possible values for this attribute are defined by the ST_TwipsMeasure simple type (§2.18.105).
|
||||
*/
|
||||
readonly distance?: number | PositiveUniversalMeasure;
|
||||
};
|
||||
|
||||
/**
|
||||
* This element specifies the settings for line numbering to be displayed before each column of text in this section in the document.
|
||||
*
|
||||
* References:
|
||||
* - https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_lnNumType_topic_ID0EVRAT.html
|
||||
* - http://officeopenxml.com/WPsectionLineNumbering.php
|
||||
*
|
||||
* ## XSD Schema
|
||||
*
|
||||
* ```xml
|
||||
* <xsd:complexType name="CT_LineNumber">
|
||||
* <xsd:attribute name="countBy" type="ST_DecimalNumber" use="optional"/>
|
||||
* <xsd:attribute name="start" type="ST_DecimalNumber" use="optional" default="1"/>
|
||||
* <xsd:attribute name="distance" type="s:ST_TwipsMeasure" use="optional"/>
|
||||
* <xsd:attribute name="restart" type="ST_LineNumberRestart" use="optional" default="newPage"/>
|
||||
* </xsd:complexType>
|
||||
* ```
|
||||
*/
|
||||
export const createLineNumberType = ({ countBy, start, restart, distance }: ILineNumberAttributes): XmlComponent =>
|
||||
new BuilderElement<ILineNumberAttributes>({
|
||||
name: "w:lnNumType",
|
||||
|
@ -2,12 +2,12 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { Formatter } from "@export/formatter";
|
||||
|
||||
import { PageOrientation, PageSize } from "./page-size";
|
||||
import { PageOrientation, createPageSize } from "./page-size";
|
||||
|
||||
describe("PageSize", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create page size with portrait", () => {
|
||||
const properties = new PageSize(100, 200, PageOrientation.PORTRAIT);
|
||||
const properties = createPageSize({ width: 100, height: 200, orientation: PageOrientation.PORTRAIT });
|
||||
const tree = new Formatter().format(properties);
|
||||
|
||||
expect(Object.keys(tree)).to.deep.equal(["w:pgSz"]);
|
||||
@ -15,7 +15,7 @@ describe("PageSize", () => {
|
||||
});
|
||||
|
||||
it("should create page size with horizontal and invert the lengths", () => {
|
||||
const properties = new PageSize(100, 200, PageOrientation.LANDSCAPE);
|
||||
const properties = createPageSize({ width: 100, height: 200, orientation: PageOrientation.LANDSCAPE });
|
||||
const tree = new Formatter().format(properties);
|
||||
|
||||
expect(Object.keys(tree)).to.deep.equal(["w:pgSz"]);
|
||||
|
@ -1,48 +1,132 @@
|
||||
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
|
||||
import { BuilderElement, XmlComponent } from "@file/xml-components";
|
||||
import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
|
||||
|
||||
// <xsd:simpleType name="ST_PageOrientation">
|
||||
// <xsd:restriction base="xsd:string">
|
||||
// <xsd:enumeration value="portrait"/>
|
||||
// <xsd:enumeration value="landscape"/>
|
||||
// </xsd:restriction>
|
||||
// </xsd:simpleType>
|
||||
/**
|
||||
* This simple type specifies the orientation of all pages in the parent section. This information is used to determine the actual paper size to use when printing the file.
|
||||
*
|
||||
* Reference: https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_ST_PageOrientation_topic_ID0EKBK3.html
|
||||
*
|
||||
* ## XSD Schema
|
||||
*
|
||||
* ```xml
|
||||
* <xsd:simpleType name="ST_PageOrientation">
|
||||
* <xsd:restriction base="xsd:string">
|
||||
* <xsd:enumeration value="portrait"/>
|
||||
* <xsd:enumeration value="landscape"/>
|
||||
* </xsd:restriction>
|
||||
* </xsd:simpleType>
|
||||
* ```
|
||||
*/
|
||||
export const PageOrientation = {
|
||||
/**
|
||||
* ## Portrait Mode
|
||||
*
|
||||
* Specifies that pages in this section shall be printed in portrait mode.
|
||||
*/
|
||||
PORTRAIT: "portrait",
|
||||
/**
|
||||
* ## Landscape Mode
|
||||
*
|
||||
* Specifies that pages in this section shall be printed in landscape mode, which prints the page contents with a 90 degree rotation with respect to the normal page orientation.
|
||||
*/
|
||||
LANDSCAPE: "landscape",
|
||||
} as const;
|
||||
|
||||
// <xsd:complexType name="CT_PageSz">
|
||||
// <xsd:attribute name="w" type="s:ST_TwipsMeasure"/>
|
||||
// <xsd:attribute name="h" type="s:ST_TwipsMeasure"/>
|
||||
// <xsd:attribute name="orient" type="ST_PageOrientation" use="optional"/>
|
||||
// <xsd:attribute name="code" type="ST_DecimalNumber" use="optional"/>
|
||||
// </xsd:complexType>
|
||||
export type IPageSizeAttributes = {
|
||||
readonly width?: number | PositiveUniversalMeasure;
|
||||
readonly height?: number | PositiveUniversalMeasure;
|
||||
/**
|
||||
* ## Page Width
|
||||
*
|
||||
* This attribute indicates the width (in twentieths of a point) for all pages in the current section.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```xml
|
||||
* <w:pgSz w:w="15840" w:h="12240" />
|
||||
* ```
|
||||
*
|
||||
* All pages in this section are displayed on a page that is 15840 twentieths of a point (11") wide.
|
||||
*
|
||||
* The possible values for this attribute are defined by the ST_TwipsMeasure simple type (§2.18.105).
|
||||
*/
|
||||
readonly width: number | PositiveUniversalMeasure;
|
||||
/**
|
||||
* ## Page Height
|
||||
*
|
||||
* Specifies the height (in twentieths of a point) for all pages in the current section.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```xml
|
||||
* <w:pgSz w:w="15840" w:h="12240" />
|
||||
* ```
|
||||
*
|
||||
* All pages in this section are displayed on a page that is `12240` twentieths of a point (`8.5"`) tall.
|
||||
*
|
||||
* The possible values for this attribute are defined by the `ST_TwipsMeasure` simple type (§2.18.105).
|
||||
*/
|
||||
readonly height: number | PositiveUniversalMeasure;
|
||||
/**
|
||||
* ## Page Orientation
|
||||
*
|
||||
* Specifies the orientation of all pages in this section.
|
||||
*
|
||||
* This information is used to determine the actual paper size to use on the printer.
|
||||
*
|
||||
* This implies that the actual paper size width and height are reversed for pages in this section. If this attribute is omitted, then portrait shall be implied.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```xml
|
||||
* <w:pgSz w:w="15840" w:h="12240" w:orient="landscape" />
|
||||
* ```
|
||||
*
|
||||
* Although the page width is 11", and page height is 8.5", according to the `w` and `h` attributes, because the `orient` attribute is set to landscape, pages in this section are printed on 8.5x11" paper in landscape mode.
|
||||
*
|
||||
* The possible values for this attribute are defined by the `ST_PageOrientation` simple type (§2.18.71).
|
||||
*/
|
||||
readonly orientation?: (typeof PageOrientation)[keyof typeof PageOrientation];
|
||||
/**
|
||||
* ## Printer Paper Code
|
||||
*
|
||||
* Specifies a printer-specific paper code for the paper type, which shall be used by the printer for pages in this section.
|
||||
*
|
||||
* This code is stored to ensure the proper paper type is chosen if the specified paper size matches the sizes of multiple paper types supported by the current printer.
|
||||
*
|
||||
* It will be sent to the printer and used by the printer to determine the appropriate paper type to use when printing.
|
||||
*
|
||||
* This value is not interpreted or modified other than storing it as specified by the printer.
|
||||
*
|
||||
* The possible values for this attribute are defined by the `ST_DecimalNumber` simple type (§2.18.16).
|
||||
*/
|
||||
readonly code?: number;
|
||||
};
|
||||
|
||||
export class PageSize extends XmlComponent {
|
||||
public constructor(
|
||||
width: number | PositiveUniversalMeasure,
|
||||
height: number | PositiveUniversalMeasure,
|
||||
orientation: (typeof PageOrientation)[keyof typeof PageOrientation],
|
||||
) {
|
||||
super("w:pgSz");
|
||||
|
||||
const flip = orientation === PageOrientation.LANDSCAPE;
|
||||
|
||||
const widthTwips = twipsMeasureValue(width);
|
||||
const heightTwips = twipsMeasureValue(height);
|
||||
|
||||
this.root.push(
|
||||
new NextAttributeComponent<IPageSizeAttributes>({
|
||||
width: { key: "w:w", value: flip ? heightTwips : widthTwips },
|
||||
height: { key: "w:h", value: flip ? widthTwips : heightTwips },
|
||||
orientation: { key: "w:orient", value: orientation },
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This element specifies the properties (size and orientation) for all pages in the current section.
|
||||
*
|
||||
* Reference: https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_pgSz_topic_ID0ENEDT.html?hl=pgsz%2Cpage%2Csize
|
||||
*
|
||||
* ## XSD Schema
|
||||
*
|
||||
* ```xml
|
||||
* <xsd:complexType name="CT_PageSz">
|
||||
* <xsd:attribute name="w" type="s:ST_TwipsMeasure"/>
|
||||
* <xsd:attribute name="h" type="s:ST_TwipsMeasure"/>
|
||||
* <xsd:attribute name="orient" type="ST_PageOrientation" use="optional"/>
|
||||
* <xsd:attribute name="code" type="ST_DecimalNumber" use="optional"/>
|
||||
* </xsd:complexType>
|
||||
* ```
|
||||
*/
|
||||
export const createPageSize = ({ width, height, orientation, code }: IPageSizeAttributes): XmlComponent => {
|
||||
const widthTwips = twipsMeasureValue(width);
|
||||
const heightTwips = twipsMeasureValue(height);
|
||||
return new BuilderElement<IPageSizeAttributes>({
|
||||
name: "w:pgSz",
|
||||
attributes: {
|
||||
width: { key: "w:w", value: orientation === PageOrientation.LANDSCAPE ? heightTwips : widthTwips },
|
||||
height: { key: "w:h", value: orientation === PageOrientation.LANDSCAPE ? widthTwips : heightTwips },
|
||||
orientation: { key: "w:orient", value: orientation },
|
||||
code: { key: "w:code", value: code },
|
||||
},
|
||||
});
|
||||
};
|
||||
|
@ -6,13 +6,13 @@ import { VerticalAlign, VerticalAlignElement } from "@file/vertical-align";
|
||||
import { OnOffElement, XmlComponent } from "@file/xml-components";
|
||||
|
||||
import { Columns, IColumnsAttributes } from "./properties/columns";
|
||||
import { DocumentGrid, IDocGridAttributesProperties } from "./properties/doc-grid";
|
||||
import { IDocGridAttributesProperties, createDocumentGrid } from "./properties/doc-grid";
|
||||
import { HeaderFooterReference, HeaderFooterReferenceType, HeaderFooterType } from "./properties/header-footer-reference";
|
||||
import { ILineNumberAttributes, createLineNumberType } from "./properties/line-number";
|
||||
import { IPageBordersOptions, PageBorders } from "./properties/page-borders";
|
||||
import { IPageMarginAttributes, PageMargin } from "./properties/page-margin";
|
||||
import { IPageNumberTypeAttributes, PageNumberType } from "./properties/page-number";
|
||||
import { IPageSizeAttributes, PageOrientation, PageSize } from "./properties/page-size";
|
||||
import { IPageSizeAttributes, PageOrientation, createPageSize } from "./properties/page-size";
|
||||
import { PageTextDirection, PageTextDirectionType } from "./properties/page-text-direction";
|
||||
import { SectionType, Type } from "./properties/section-type";
|
||||
|
||||
@ -24,13 +24,13 @@ export type IHeaderFooterGroup<T> = {
|
||||
|
||||
export type ISectionPropertiesOptions = {
|
||||
readonly page?: {
|
||||
readonly size?: IPageSizeAttributes;
|
||||
readonly size?: Partial<IPageSizeAttributes>;
|
||||
readonly margin?: IPageMarginAttributes;
|
||||
readonly pageNumbers?: IPageNumberTypeAttributes;
|
||||
readonly borders?: IPageBordersOptions;
|
||||
readonly textDirection?: (typeof PageTextDirectionType)[keyof typeof PageTextDirectionType];
|
||||
};
|
||||
readonly grid?: IDocGridAttributesProperties;
|
||||
readonly grid?: Partial<IDocGridAttributesProperties>;
|
||||
readonly headerWrapperGroup?: IHeaderFooterGroup<HeaderWrapper>;
|
||||
readonly footerWrapperGroup?: IHeaderFooterGroup<FooterWrapper>;
|
||||
readonly lineNumbers?: ILineNumberAttributes;
|
||||
@ -128,7 +128,7 @@ export class SectionProperties extends XmlComponent {
|
||||
this.root.push(new Type(type));
|
||||
}
|
||||
|
||||
this.root.push(new PageSize(width, height, orientation));
|
||||
this.root.push(createPageSize({ width, height, orientation }));
|
||||
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter));
|
||||
|
||||
if (borders) {
|
||||
@ -157,7 +157,7 @@ export class SectionProperties extends XmlComponent {
|
||||
this.root.push(new PageTextDirection(textDirection));
|
||||
}
|
||||
|
||||
this.root.push(new DocumentGrid(linePitch, charSpace, gridType));
|
||||
this.root.push(createDocumentGrid({ linePitch, charSpace, type: gridType }));
|
||||
}
|
||||
|
||||
private addHeaderFooterGroup(
|
||||
|
Reference in New Issue
Block a user