diff --git a/ts/docx/document/body/section-properties.ts b/ts/docx/document/body/section-properties.ts index 8e25e78bac..30803ea6fa 100644 --- a/ts/docx/document/body/section-properties.ts +++ b/ts/docx/document/body/section-properties.ts @@ -4,7 +4,7 @@ export class SectionProperties implements XmlComponent { private sectPr: Array; xmlKeys = { - sectPr: 'sectPr' + sectPr: 'w:sectPr' } constructor() { diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index 299e95cd63..e9d89e1395 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -3,9 +3,14 @@ import {DocumentAttributes} from "../xml-components/document-attributes" import {Body} from "./body"; import {Paragraph} from "../paragraph"; -export class Document { +export class Document implements XmlComponent { private document: Array; private body: Body; + + xmlKeys = { + document: "w:document", + body: "w:body" + }; constructor() { this.document = new Array(); diff --git a/ts/docx/xml-components/document-attributes.ts b/ts/docx/xml-components/document-attributes.ts index a68991c59a..b10a05749a 100644 --- a/ts/docx/xml-components/document-attributes.ts +++ b/ts/docx/xml-components/document-attributes.ts @@ -27,7 +27,7 @@ interface DocumentAttributesProperties { } export class DocumentAttributes implements XmlComponent { - private _attrs: Object; + private _attr: Object; xmlKeys = { wpc: 'xmlns:wpc', @@ -56,11 +56,11 @@ export class DocumentAttributes implements XmlComponent { }; constructor(properties?: DocumentAttributesProperties) { - this._attrs = properties + this._attr = properties if (!properties) { - this._attrs = {}; + this._attr = {}; } - this._attrs["xmlKeys"] = this.xmlKeys; + this._attr["xmlKeys"] = this.xmlKeys; } } \ No newline at end of file diff --git a/ts/docx/xml-components/index.ts b/ts/docx/xml-components/index.ts index 96787024c2..b76235c6a0 100644 --- a/ts/docx/xml-components/index.ts +++ b/ts/docx/xml-components/index.ts @@ -24,7 +24,7 @@ interface AttributesProperties { } export class Attributes implements XmlComponent { - private _attrs: Object; + private _attr: Object; xmlKeys = { val: "w:val", @@ -48,13 +48,13 @@ export class Attributes implements XmlComponent { }; constructor(properties?: AttributesProperties) { - this._attrs = properties + this._attr = properties if (!properties) { - this._attrs = {}; + this._attr = {}; } - this._attrs["xmlKeys"] = this.xmlKeys; + this._attr["xmlKeys"] = this.xmlKeys; } } diff --git a/ts/export/packer/local.ts b/ts/export/packer/local.ts index 07b2b830b6..93bda55e4b 100644 --- a/ts/export/packer/local.ts +++ b/ts/export/packer/local.ts @@ -1,12 +1,13 @@ import {Packer} from "./packer"; import * as fs from 'fs'; import {Document} from "../../docx/document"; +import {Properties} from "../../properties"; export class LocalPacker extends Packer { private stream: fs.WriteStream - constructor(document: Document, path: string) { - super(document, null, null); + constructor(document: Document, style: any, properties: Properties, path: string) { + super(document, null, properties); this.stream = fs.createWriteStream(path); } diff --git a/ts/export/packer/packer.ts b/ts/export/packer/packer.ts index 6c03cb6011..09e33a30b9 100644 --- a/ts/export/packer/packer.ts +++ b/ts/export/packer/packer.ts @@ -1,5 +1,6 @@ import * as archiver from "archiver"; import * as fs from "fs"; +import * as xml from "xml"; import {Formatter} from "../formatter"; import {Document} from "../../docx"; import {Style} from "../../style"; @@ -36,16 +37,20 @@ export abstract class Packer { ]); //this.archive.directory(__dirname + "/template", "/"); - - this.archive.append(this.document, { + var xmlDocument = xml(this.formatter.format(this.document)); + //var xmlStyle = xml(this.style); + var xmlProperties = xml(this.formatter.format(this.properties)); + console.log(JSON.stringify(this.formatter.format(this.document), null, " ")); + console.log(xmlDocument); + this.archive.append(xmlDocument, { name: 'word/document.xml' }); - this.archive.append(this.style, { - name: 'word/newStyle.xml' - }); + //this.archive.append(xmlStyle, { + // name: 'word/newStyle.xml' + //}); - this.archive.append(this.properties, { + this.archive.append(xmlProperties, { name: 'docProps/core.xml' }); diff --git a/ts/tests/borderTest.ts b/ts/tests/borderTest.ts index 64f21f83ee..5de067f29f 100644 --- a/ts/tests/borderTest.ts +++ b/ts/tests/borderTest.ts @@ -28,6 +28,7 @@ describe("ThematicBreak", () => { val: "single", sz: "6" }; + delete newJson.pBdr[0].bottom[0]._attrs.xmlKeys; assert(JSON.stringify(newJson.pBdr[0].bottom[0]._attrs) === JSON.stringify(attributes)); }); }) diff --git a/ts/tests/formatterTest.ts b/ts/tests/formatterTest.ts index 7bafa9d09e..dd27502723 100644 --- a/ts/tests/formatterTest.ts +++ b/ts/tests/formatterTest.ts @@ -26,11 +26,16 @@ describe("Formatter", () => { var paragraph = new docx.Paragraph(); var newJson = formatter.format(paragraph); newJson = jsonify(newJson); - console.log(JSON.stringify(newJson, null, " ")); - assert.isDefined(newJson["w:p"]); }); + it("should remove xmlKeys", () => { + var paragraph = new docx.Paragraph(); + var newJson = formatter.format(paragraph); + var stringifiedJson = JSON.stringify(newJson); + assert(stringifiedJson.indexOf("xmlKeys") < 0); + }); + it("should format simple paragraph with bold text", () => { var paragraph = new docx.Paragraph(); paragraph.addText(new docx.TextRun("test").bold()); @@ -62,14 +67,14 @@ describe("Formatter", () => { assert.isDefined(newJson["w:p"]); }); - it.only("should format Properties object correctly", () => { + it("should format Properties object correctly", () => { var properties = new Properties({ title: "test document", creator: "Dolan" }); var newJson = formatter.format(properties); newJson = jsonify(newJson); - console.log(JSON.stringify(newJson, null, " ")); + assert.isDefined(newJson["cp:coreProperties"]); }); }); }); \ No newline at end of file diff --git a/ts/tests/localPackerTest.ts b/ts/tests/localPackerTest.ts index 6c11824f98..b0af4e625b 100644 --- a/ts/tests/localPackerTest.ts +++ b/ts/tests/localPackerTest.ts @@ -1,17 +1,22 @@ /// /// /// +/// import {LocalPacker} from "../export/packer/local"; import {assert} from "chai"; import {Document} from "../docx/document" +import {Properties} from "../properties" -describe("Packer", () => { +describe.only("Packer", () => { var packer: LocalPacker; beforeEach(() => { var document = new Document(); - packer = new LocalPacker(document, "build/tests/test.zip"); + var properties = new Properties({ + title: "test document" + }); + packer = new LocalPacker(document, undefined, properties, "build/tests/test.zip"); }); describe('#pack()', () => {