diff --git a/.vscode/settings.json b/.vscode/settings.json index 2469f92403..034279a342 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,7 +9,7 @@ ], "prettier.trailingComma": "all", "prettier.printWidth": 140, - "editor.formatOnSave": false, + "editor.formatOnSave": true, "prettier.tabWidth": 4, "prettier.arrowParens": "always", "prettier.bracketSpacing": true, diff --git a/src/file/document/body/body.spec.ts b/src/file/document/body/body.spec.ts index 26beb7628c..b16269bcf8 100644 --- a/src/file/document/body/body.spec.ts +++ b/src/file/document/body/body.spec.ts @@ -2,22 +2,22 @@ import { assert } from "chai"; import { Utility } from "../../../tests/utility"; import { Body } from "./"; -import { Columns } from "./columns"; -import { DocumentGrid } from "./doc-grid"; -import { PageMargin } from "./page-margin"; -import { PageSize } from "./page-size"; -import { SectionProperties } from "./section-properties"; +import { Columns } from "./section-properties/columns/columns"; +import { DocumentGrid } from "./section-properties/doc-grid/doc-grid"; +import { PageMargin } from "./section-properties/page-margin/page-margin"; +import { PageSize } from "./section-properties/page-size/page-size"; +import { SectionProperties } from "./section-properties/section-properties"; describe("Body", () => { let body: Body; beforeEach(() => { body = new Body(); - body.push(new SectionProperties()); - body.push(new PageSize()); - body.push(new PageMargin()); - body.push(new Columns()); - body.push(new DocumentGrid()); + body.push(new SectionProperties(0)); + body.push(new PageSize(0, 0)); + body.push(new PageMargin(0, 0, 0, 0, 0, 0, 0)); + body.push(new Columns(0)); + body.push(new DocumentGrid(0)); }); describe("#constructor()", () => { diff --git a/src/file/document/body/body.ts b/src/file/document/body/body.ts index 797242093d..776c7fe0f0 100644 --- a/src/file/document/body/body.ts +++ b/src/file/document/body/body.ts @@ -1,4 +1,5 @@ import { XmlComponent } from "file/xml-components"; +import { SectionProperties } from "./section-properties/section-properties"; export class Body extends XmlComponent { constructor() { @@ -7,5 +8,21 @@ export class Body extends XmlComponent { public push(component: XmlComponent): void { this.root.push(component); + + this.root.push( + new SectionProperties({ + width: 11906, + height: 16838, + top: 1440, + right: 1440, + bottom: 1440, + left: 1440, + header: 708, + footer: 708, + gutter: 0, + space: 708, + linePitch: 360, + }), + ); } } diff --git a/src/file/document/body/columns.ts b/src/file/document/body/columns.ts deleted file mode 100644 index e95798ccb9..0000000000 --- a/src/file/document/body/columns.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Attributes, XmlComponent } from "file/xml-components"; - -export class Columns extends XmlComponent { - constructor() { - super("w:cols"); - this.root.push( - new Attributes({ - space: "708", - }), - ); - } -} diff --git a/src/file/document/body/doc-grid.ts b/src/file/document/body/doc-grid.ts deleted file mode 100644 index 4b23a87d3b..0000000000 --- a/src/file/document/body/doc-grid.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Attributes, XmlComponent } from "file/xml-components"; - -export class DocumentGrid extends XmlComponent { - constructor() { - super("w:docGrid"); - this.root.push( - new Attributes({ - linePitch: "360", - }), - ); - } -} diff --git a/src/file/document/body/page-margin.ts b/src/file/document/body/page-margin.ts deleted file mode 100644 index 272dd63786..0000000000 --- a/src/file/document/body/page-margin.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Attributes, XmlComponent } from "file/xml-components"; - -export class PageMargin extends XmlComponent { - constructor() { - super("w:pgMar"); - this.root.push( - new Attributes({ - top: "1440", - right: "1440", - bottom: "1440", - left: "1440", - header: "708", - footer: "708", - gutter: "0", - }), - ); - } -} diff --git a/src/file/document/body/page-size.ts b/src/file/document/body/page-size.ts deleted file mode 100644 index cf70ade845..0000000000 --- a/src/file/document/body/page-size.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Attributes, XmlComponent } from "file/xml-components"; - -export class PageSize extends XmlComponent { - constructor() { - super("w:pgSz"); - this.root.push( - new Attributes({ - w: "11906", - h: "16838", - }), - ); - } -} diff --git a/src/file/document/body/section-properties.ts b/src/file/document/body/section-properties.ts deleted file mode 100644 index 0310ad5f01..0000000000 --- a/src/file/document/body/section-properties.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Attributes, XmlComponent } from "file/xml-components"; -import { Columns } from "./columns"; -import { DocumentGrid } from "./doc-grid"; -import { PageMargin } from "./page-margin"; -import { PageSize } from "./page-size"; - -export class SectionProperties extends XmlComponent { - constructor() { - super("w:sectPr"); - this.root.push( - new Attributes({ - rsidR: "00B64E8F", - rsidRPr: "00D842E4", - rsidSect: "000A6AD0", - }), - ); - this.root.push(new PageSize()); - this.root.push(new PageMargin()); - this.root.push(new Columns()); - this.root.push(new DocumentGrid()); - } -} diff --git a/src/file/document/body/section-properties/columns/columns-attributes.ts b/src/file/document/body/section-properties/columns/columns-attributes.ts new file mode 100644 index 0000000000..6a711abf93 --- /dev/null +++ b/src/file/document/body/section-properties/columns/columns-attributes.ts @@ -0,0 +1,11 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IColumnsAttributes { + space?: number; +} + +export class ColumnsAttributes extends XmlAttributeComponent { + protected xmlKeys = { + space: "w:space", + }; +} diff --git a/src/file/document/body/section-properties/columns/columns.ts b/src/file/document/body/section-properties/columns/columns.ts new file mode 100644 index 0000000000..aa6e144f62 --- /dev/null +++ b/src/file/document/body/section-properties/columns/columns.ts @@ -0,0 +1,13 @@ +import { XmlComponent } from "file/xml-components"; +import { ColumnsAttributes } from "./columns-attributes"; + +export class Columns extends XmlComponent { + constructor(space: number) { + super("w:cols"); + this.root.push( + new ColumnsAttributes({ + space: space, + }), + ); + } +} diff --git a/src/file/document/body/section-properties/doc-grid/doc-grid-attributes.ts b/src/file/document/body/section-properties/doc-grid/doc-grid-attributes.ts new file mode 100644 index 0000000000..23f9c3efb4 --- /dev/null +++ b/src/file/document/body/section-properties/doc-grid/doc-grid-attributes.ts @@ -0,0 +1,11 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IDocGridAttributesProperties { + linePitch?: number; +} + +export class DocGridAttributes extends XmlAttributeComponent { + protected xmlKeys = { + linePitch: "w:linePitch", + }; +} diff --git a/src/file/document/body/section-properties/doc-grid/doc-grid.ts b/src/file/document/body/section-properties/doc-grid/doc-grid.ts new file mode 100644 index 0000000000..e4df527443 --- /dev/null +++ b/src/file/document/body/section-properties/doc-grid/doc-grid.ts @@ -0,0 +1,13 @@ +import { XmlComponent } from "file/xml-components"; +import { DocGridAttributes } from "./doc-grid-attributes"; + +export class DocumentGrid extends XmlComponent { + constructor(linePitch: number) { + super("w:docGrid"); + this.root.push( + new DocGridAttributes({ + linePitch: linePitch, + }), + ); + } +} diff --git a/src/file/document/body/section-properties/page-margin/page-margin-attributes.ts b/src/file/document/body/section-properties/page-margin/page-margin-attributes.ts new file mode 100644 index 0000000000..39c4a67a24 --- /dev/null +++ b/src/file/document/body/section-properties/page-margin/page-margin-attributes.ts @@ -0,0 +1,23 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IPageMarginAttributes { + top: number; + right: number; + bottom: number; + left: number; + header: number; + footer: number; + gutter: number; +} + +export class PageMarginAttributes extends XmlAttributeComponent { + protected xmlKeys = { + top: "w:top", + right: "w:right", + bottom: "w:bottom", + left: "w:left", + header: "w:header", + footer: "w:footer", + gutter: "w:gutter", + }; +} diff --git a/src/file/document/body/section-properties/page-margin/page-margin.ts b/src/file/document/body/section-properties/page-margin/page-margin.ts new file mode 100644 index 0000000000..bd9a8e5fa6 --- /dev/null +++ b/src/file/document/body/section-properties/page-margin/page-margin.ts @@ -0,0 +1,19 @@ +import { XmlComponent } from "file/xml-components"; +import { PageMarginAttributes } from "./page-margin-attributes"; + +export class PageMargin extends XmlComponent { + constructor(top: number, right: number, bottom: number, left: number, header: number, footer: number, gutter: number) { + super("w:pgMar"); + this.root.push( + new PageMarginAttributes({ + top: top, + right: right, + bottom: bottom, + left: left, + header: header, + footer: footer, + gutter: gutter, + }), + ); + } +} diff --git a/src/file/document/body/section-properties/page-size/page-size-attributes.ts b/src/file/document/body/section-properties/page-size/page-size-attributes.ts new file mode 100644 index 0000000000..ee9ba2b4e7 --- /dev/null +++ b/src/file/document/body/section-properties/page-size/page-size-attributes.ts @@ -0,0 +1,13 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IPageSizeAttributes { + width: number; + height: number; +} + +export class PageSizeAttributes extends XmlAttributeComponent { + protected xmlKeys = { + width: "w:w", + height: "w:h", + }; +} diff --git a/src/file/document/body/section-properties/page-size/page-size.ts b/src/file/document/body/section-properties/page-size/page-size.ts new file mode 100644 index 0000000000..a70584a896 --- /dev/null +++ b/src/file/document/body/section-properties/page-size/page-size.ts @@ -0,0 +1,15 @@ +import { XmlComponent } from "file/xml-components"; +import { PageSizeAttributes } from "./page-size-attributes"; + +export class PageSize extends XmlComponent { + constructor(width: number, height: number) { + super("w:pgSz"); + + this.root.push( + new PageSizeAttributes({ + width: width, + height: height, + }), + ); + } +} diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts new file mode 100644 index 0000000000..88f1299b7d --- /dev/null +++ b/src/file/document/body/section-properties/section-properties.ts @@ -0,0 +1,23 @@ +// http://officeopenxml.com/WPsection.php +import { IColumnsAttributes } from "file/document/body/section-properties/columns/columns-attributes"; +import { IPageMarginAttributes } from "file/document/body/section-properties/page-margin/page-margin-attributes"; +import { IPageSizeAttributes } from "file/document/body/section-properties/page-size/page-size-attributes"; +import { XmlComponent } from "file/xml-components"; +import { Columns } from "./columns/columns"; +import { DocumentGrid } from "./doc-grid/doc-grid"; +import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes"; +import { PageMargin } from "./page-margin/page-margin"; +import { PageSize } from "./page-size/page-size"; + +export type SectionPropertiesOptions = IPageSizeAttributes & IPageMarginAttributes & IColumnsAttributes & IDocGridAttributesProperties; + +export class SectionProperties extends XmlComponent { + constructor(options: SectionPropertiesOptions) { + super("w:sectPr"); + + this.root.push(new PageSize(11906, 16838)); + this.root.push(new PageMargin(1440, 1440, 1440, 1440, 708, 708, 0)); + this.root.push(new Columns(708)); + this.root.push(new DocumentGrid(308)); + } +}