From 8b8c664f0fb1f43bdb8fd27011c42e249c3f988e Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 29 Jan 2018 02:56:35 +0000 Subject: [PATCH] Add header objects and demo --- .vscode/settings.json | 13 +++---------- demo/demo8.js | 12 ++++++++++++ src/export/packer/compiler.ts | 5 +++++ .../header-reference-attributes.ts | 13 +++++++++++++ .../header-reference/header-reference.ts | 14 ++++++++++++++ .../body/section-properties/section-properties.ts | 8 +++++--- src/file/file.ts | 2 +- src/file/media/media.ts | 1 - template/[Content_Types].xml | 1 + 9 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 demo/demo8.js create mode 100644 src/file/document/body/section-properties/header-reference/header-reference-attributes.ts create mode 100644 src/file/document/body/section-properties/header-reference/header-reference.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 034279a342..f144f59202 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,16 +1,9 @@ { - "cSpell.words": [ - "clippy", - "docx", - "dolan", - "miu", - "officegen", - "typedoc" - ], + "cSpell.words": ["clippy", "docx", "dolan", "miu", "officegen", "typedoc"], "prettier.trailingComma": "all", "prettier.printWidth": 140, - "editor.formatOnSave": true, + "editor.formatOnSave": false, "prettier.tabWidth": 4, "prettier.arrowParens": "always", - "prettier.bracketSpacing": true, + "prettier.bracketSpacing": true } diff --git a/demo/demo8.js b/demo/demo8.js new file mode 100644 index 0000000000..4bde11e1fc --- /dev/null +++ b/demo/demo8.js @@ -0,0 +1,12 @@ +const docx = require('../build'); + +var doc = new docx.File(); + +doc.createParagraph("Hello World"); + +doc.Header.createParagraph("Header text"); + +var exporter = new docx.LocalPacker(doc); +exporter.pack('My Document'); + +console.log('Document created succesfully at project root!'); diff --git a/src/export/packer/compiler.ts b/src/export/packer/compiler.ts index 33ac1fa604..76bdcc2dad 100644 --- a/src/export/packer/compiler.ts +++ b/src/export/packer/compiler.ts @@ -42,6 +42,7 @@ export class Compiler { }); const xmlNumbering = xml(this.formatter.format(this.file.Numbering)); const xmlRelationships = xml(this.formatter.format(this.file.Relationships)); + const xmlHeader = xml(this.formatter.format(this.file.Header)); this.archive.append(xmlDocument, { name: "word/document.xml", @@ -59,6 +60,10 @@ export class Compiler { name: "word/numbering.xml", }); + this.archive.append(xmlHeader, { + name: "word/header1.xml", + }); + this.archive.append(xmlRelationships, { name: "word/_rels/document.xml.rels", }); diff --git a/src/file/document/body/section-properties/header-reference/header-reference-attributes.ts b/src/file/document/body/section-properties/header-reference/header-reference-attributes.ts new file mode 100644 index 0000000000..b0407c4a0a --- /dev/null +++ b/src/file/document/body/section-properties/header-reference/header-reference-attributes.ts @@ -0,0 +1,13 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IHeaderReferenceAttributes { + type: string; + id: string; +} + +export class HeaderReferenceAttributes extends XmlAttributeComponent { + protected xmlKeys = { + type: "w:type", + id: "r:id", + }; +} diff --git a/src/file/document/body/section-properties/header-reference/header-reference.ts b/src/file/document/body/section-properties/header-reference/header-reference.ts new file mode 100644 index 0000000000..3809047bef --- /dev/null +++ b/src/file/document/body/section-properties/header-reference/header-reference.ts @@ -0,0 +1,14 @@ +import { XmlComponent } from "file/xml-components"; +import { HeaderReferenceAttributes } from "./header-reference-attributes"; + +export class HeaderReference extends XmlComponent { + constructor() { + super("w:headerReference"); + this.root.push( + new HeaderReferenceAttributes({ + type: "default", + id: `rId${3}`, + }), + ); + } +} diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index 097fff225f..a7aae06f10 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -1,13 +1,14 @@ // 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 { IColumnsAttributes } from "./columns/columns-attributes"; import { DocumentGrid } from "./doc-grid/doc-grid"; import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes"; +import { HeaderReference } from "./header-reference/header-reference"; import { PageMargin } from "./page-margin/page-margin"; +import { IPageMarginAttributes } from "./page-margin/page-margin-attributes"; import { PageSize } from "./page-size/page-size"; +import { IPageSizeAttributes } from "./page-size/page-size-attributes"; export type SectionPropertiesOptions = IPageSizeAttributes & IPageMarginAttributes & IColumnsAttributes & IDocGridAttributesProperties; @@ -49,5 +50,6 @@ export class SectionProperties extends XmlComponent { ); this.root.push(new Columns(mergedOptions.space)); this.root.push(new DocumentGrid(mergedOptions.linePitch)); + this.root.push(new HeaderReference()); } } diff --git a/src/file/file.ts b/src/file/file.ts index 91ec51895b..9f6f3016a8 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -44,7 +44,7 @@ export class File { } public createParagraph(text?: string): Paragraph { - return this.document.createParagraph(); + return this.document.createParagraph(text); } public addTable(table: Table): void { diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 92e8bea1fe..a244807599 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -2,7 +2,6 @@ import * as fs from "fs"; import * as sizeOf from "image-size"; import * as path from "path"; -import { Relationships } from "file/relationships/relationships"; import { IMediaData } from "./data"; export class Media { diff --git a/template/[Content_Types].xml b/template/[Content_Types].xml index 996f6a2bea..978bf4e3fe 100644 --- a/template/[Content_Types].xml +++ b/template/[Content_Types].xml @@ -8,6 +8,7 @@ +