diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index d78fa8992d..299e95cd63 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -1,4 +1,5 @@ -import {XmlComponent, Attributes} from "../xml-components"; +import {XmlComponent} from "../xml-components"; +import {DocumentAttributes} from "../xml-components/document-attributes" import {Body} from "./body"; import {Paragraph} from "../paragraph"; @@ -8,7 +9,25 @@ export class Document { constructor() { this.document = new Array(); - this.document.push(new Attributes({})); + this.document.push(new DocumentAttributes({ + wpc: 'http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas', + mc: 'http://schemas.openxmlformats.org/markup-compatibility/2006', + o: 'urn:schemas-microsoft-com:office:office', + r: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships', + m: 'http://schemas.openxmlformats.org/officeDocument/2006/math', + v: 'urn:schemas-microsoft-com:vml', + wp14: 'http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing', + wp: 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing', + w10: 'urn:schemas-microsoft-com:office:word', + w: 'http://schemas.openxmlformats.org/wordprocessingml/2006/main', + w14: 'http://schemas.microsoft.com/office/word/2010/wordml', + w15: 'http://schemas.microsoft.com/office/word/2012/wordml', + wpg: 'http://schemas.microsoft.com/office/word/2010/wordprocessingGroup', + wpi: 'http://schemas.microsoft.com/office/word/2010/wordprocessingInk', + wne: 'http://schemas.microsoft.com/office/word/2006/wordml', + wps: 'http://schemas.microsoft.com/office/word/2010/wordprocessingShape', + Ignorable: 'w14 w15 wp14' + })); this.body = new Body(); this.document.push(this.body); } diff --git a/ts/docx/index.ts b/ts/docx/index.ts index 8fd51cddd3..7556c20cd3 100644 --- a/ts/docx/index.ts +++ b/ts/docx/index.ts @@ -1,3 +1,4 @@ export {Document} from "./document"; export {Paragraph} from "./paragraph"; -export {Run} from "./run" \ No newline at end of file +export {Run} from "./run" +export {TextRun} from "./run/text-run" \ No newline at end of file diff --git a/ts/docx/xml-components/document-attributes.ts b/ts/docx/xml-components/document-attributes.ts new file mode 100644 index 0000000000..49a6c13108 --- /dev/null +++ b/ts/docx/xml-components/document-attributes.ts @@ -0,0 +1,54 @@ +import {XmlComponent} from "./"; + +interface DocumentAttributesProperties { + wpc?: string; + mc?: string; + o?: string; + r?: string; + m?: string; + v?: string; + wp14?: string; + wp?: string; + w10?: string; + w?: string; + w14?: string; + w15?: string; + wpg?: string; + wpi?: string; + wne?: string; + wps?: string; + Ignorable?: string; +} + +export class DocumentAttributes implements XmlComponent { + private _attrs: Object; + + xmlKeys = { + wpc: 'xmlns:wpc', + mc: 'xmlns:mc', + o: 'xmlns:o', + r: 'xmlns:r', + m: 'xmlns:m', + v: 'xmlns:v', + wp14: 'xmlns:wp14', + wp: 'xmlns:wp', + w10: 'xmlns:w10', + w: 'xmlns:w', + w14: 'xmlns:w14', + w15: 'xmlns:w15', + wpg: 'xmlns:wpg', + wpi: 'xmlns:wpi', + wne: 'xmlns:wne', + wps: 'xmlns:wps', + Ignorable: 'mc:Ignorable' + }; + + constructor(properties?: DocumentAttributesProperties) { + this._attrs = properties + + if (!properties) { + this._attrs = {}; + } + this._attrs["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 a04f48a579..96787024c2 100644 --- a/ts/docx/xml-components/index.ts +++ b/ts/docx/xml-components/index.ts @@ -13,20 +13,39 @@ interface AttributesProperties { rsidSect?: string; w?: string; h?: string; - top?: string, - right?: string, - bottom?: string, - left?: string, - header?: string, - footer?: string, - gutter?: string, - linePitch?: string + top?: string; + right?: string; + bottom?: string; + left?: string; + header?: string; + footer?: string; + gutter?: string; + linePitch?: string; } export class Attributes implements XmlComponent { private _attrs: Object; - xmlKeys = {}; + xmlKeys = { + val: "w:val", + color: "w:color", + space: "w:space", + sz: "w:sz", + type: "w:type", + rsidR: "w:rsidR", + rsidRPr: "w:rsidRPr", + rsidSect: "w:rsidSect", + w: "w:w", + h: "w:h", + top: "w:top", + right: "w:right", + bottom: "w:bottom", + left: "w:left", + header: "w:header", + footer: "w:footer", + gutter: "w:gutter", + linePitch: "w:linePitch" + }; constructor(properties?: AttributesProperties) { this._attrs = properties @@ -34,6 +53,8 @@ export class Attributes implements XmlComponent { if (!properties) { this._attrs = {}; } + + this._attrs["xmlKeys"] = this.xmlKeys; } } diff --git a/ts/style/index.ts b/ts/style/index.ts index 2e89774120..33cd86e1c9 100644 --- a/ts/style/index.ts +++ b/ts/style/index.ts @@ -1,3 +1,18 @@ +import {XmlComponent} from "../docx/xml-components"; +import {DocumentAttributes} from "../docx/xml-components/document-attributes" + export class Style { - + private styles: Array; + + constructor() { + this.styles = new Array(); + this.styles.push(new DocumentAttributes({ + mc: 'http://schemas.openxmlformats.org/markup-compatibility/2006', + r: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships', + w: 'http://schemas.openxmlformats.org/wordprocessingml/2006/main', + w14: 'http://schemas.microsoft.com/office/word/2010/wordml', + w15: 'http://schemas.microsoft.com/office/word/2012/wordml', + Ignorable: 'w14 w15' + })) + } } \ No newline at end of file diff --git a/ts/tests/formatterTest.ts b/ts/tests/formatterTest.ts index ba3da3b9e4..35a7296549 100644 --- a/ts/tests/formatterTest.ts +++ b/ts/tests/formatterTest.ts @@ -4,6 +4,8 @@ import {Formatter} from "../export/Formatter"; import * as docx from "../docx"; +import {Attributes} from "../docx/xml-components"; + import {assert} from "chai"; function jsonify(obj: Object) { @@ -11,7 +13,7 @@ function jsonify(obj: Object) { return JSON.parse(stringifiedJson); } -describe('Formatter', () => { +describe.only('Formatter', () => { var formatter: Formatter; beforeEach(() => { @@ -19,15 +21,41 @@ describe('Formatter', () => { }); describe('#format()', () => { - it.only("should format simple paragraph", () => { + it("should format simple paragraph", () => { var paragraph = new docx.Paragraph(); var newJson = formatter.format(paragraph); newJson = jsonify(newJson); - console.log(newJson); + assert.isDefined(newJson["w:p"]); + }); + + it("should format simple paragraph with bold text", () => { + var paragraph = new docx.Paragraph(); + paragraph.addText(new docx.TextRun("test").bold()); + var newJson = formatter.format(paragraph); + newJson = jsonify(newJson); + assert.isDefined(newJson["w:p"][3]["w:r"][0]["w:rPr"][0]["w:b"][0]["_attrs"]["w:val"]); + }); + + it("should format attributes (rsidSect)", () => { + var attributes = new Attributes({ + rsidSect: "test2" + }); + var newJson = formatter.format(attributes); + newJson = jsonify(newJson); + assert.isDefined(newJson["_attrs"]["w:rsidSect"]); + }); + + it("should format attributes (val)", () => { + var attributes = new Attributes({ + val: "test" + }); + var newJson = formatter.format(attributes); + newJson = jsonify(newJson); + assert.isDefined(newJson["_attrs"]["w:val"]); }); it("should should change 'p' tag into 'w:p' tag", () => { - var newJson = formatter.format({ "p": "test" }); + var newJson = formatter.format({ "p": "test", "xmlKeys": { "p": "w:p" } }); assert.isDefined(newJson["w:p"]); }); });