From 11fa77edc0cd017e7050f9ab76e7f60ef6cbd204 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Tue, 10 May 2016 00:32:00 +0100 Subject: [PATCH] added more styles --- ts/styles/defaults/index.ts | 18 +++++++-- ts/styles/factory.ts | 27 ++++++++------ ts/styles/index.ts | 8 ++++ ts/styles/style/index.ts | 74 +++++++++++++++++++++++++------------ ts/tests/localPackerTest.ts | 4 +- 5 files changed, 92 insertions(+), 39 deletions(-) diff --git a/ts/styles/defaults/index.ts b/ts/styles/defaults/index.ts index 0af193af48..0a944a978e 100644 --- a/ts/styles/defaults/index.ts +++ b/ts/styles/defaults/index.ts @@ -4,9 +4,21 @@ import {RunPropertiesDefaults} from "./run-properties"; export class DocumentDefaults extends XmlComponent { - constructor(runPropertiesDefaults: RunPropertiesDefaults, paragraphPropertiesDefaults: ParagraphPropertiesDefaults) { + private runPropertiesDefaults: RunPropertiesDefaults; + private paragraphPropertiesDefaults: ParagraphPropertiesDefaults; + + constructor() { super("w:docDefaults"); - this.root.push(runPropertiesDefaults); - this.root.push(paragraphPropertiesDefaults); + this.runPropertiesDefaults = new RunPropertiesDefaults(); + this.paragraphPropertiesDefaults = new ParagraphPropertiesDefaults(); + this.root.push(this.runPropertiesDefaults); + this.root.push(this.paragraphPropertiesDefaults); + } + + clearVariables(): void { + this.runPropertiesDefaults.clearVariables(); + this.paragraphPropertiesDefaults.clearVariables(); + delete this.runPropertiesDefaults; + delete this.paragraphPropertiesDefaults; } } \ No newline at end of file diff --git a/ts/styles/factory.ts b/ts/styles/factory.ts index 8176f10108..16e894f8db 100644 --- a/ts/styles/factory.ts +++ b/ts/styles/factory.ts @@ -2,29 +2,32 @@ import {Styles} from "./"; import {DocumentDefaults} from "./defaults"; import {ParagraphPropertiesDefaults} from "./defaults/paragraph-properties"; import {RunPropertiesDefaults} from "./defaults/run-properties"; -import {Heading1Style} from "./style"; +import {Heading1Style, Heading2Style, TitleStyle} from "./style"; //import {StyleAttributes} from "./style/attributes"; import {ParagraphProperties} from "../docx/paragraph/properties"; import {RunProperties} from "../docx/run/properties"; -import {Color} from "../docx/run/formatting"; +import {Color, Size} from "../docx/run/formatting"; export class DefaultStylesFactory { - constructor() { - - } newInstance(): Styles { var styles = new Styles(); - var paragraphProperties = new ParagraphPropertiesDefaults(); - var runProperties = new RunPropertiesDefaults(); - styles.push(new DocumentDefaults(paragraphProperties, runProperties)); + styles.push(new DocumentDefaults()); - var heading1ParagraphProperties = new ParagraphProperties(); - var heading1RunProperties = new RunProperties(); - heading1RunProperties.push(new Color("365F91")); - var heading1Style = new Heading1Style(heading1ParagraphProperties, heading1RunProperties); + var titleStyle = new TitleStyle(); + titleStyle.addRunProperty(new Size(56)); + styles.push(titleStyle); + + var heading1Style = new Heading1Style(); + heading1Style.addRunProperty(new Color("2E74B5")); + heading1Style.addRunProperty(new Size(32)); styles.push(heading1Style); + var heading2Style = new Heading2Style(); + heading2Style.addRunProperty(new Color("2E74B5")); + heading2Style.addRunProperty(new Size(26)); + styles.push(heading2Style); + console.log(JSON.stringify(styles, null, " ")); return styles; } diff --git a/ts/styles/index.ts b/ts/styles/index.ts index 098972d537..a4813d33fb 100644 --- a/ts/styles/index.ts +++ b/ts/styles/index.ts @@ -27,4 +27,12 @@ export class Styles extends XmlComponent { push(style: XmlComponent): void { this.root.push(style); } + + clearVariables() { + console.log(this); + console.log(this.root); + this.root.forEach(element => { + element.clearVariables(); + }) + } } \ No newline at end of file diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index 02cca5a22a..5c6a336808 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -18,21 +18,42 @@ export class Style extends XmlComponent { export class ParagraphStyle extends Style { - constructor(styleId: string, paragraphProperties: ParagraphProperties, runProperties: RunProperties) { + private paragraphProperties: ParagraphProperties; + private runProperties: RunProperties; + + constructor(styleId: string) { + var attributes = new StyleAttributes({ type: "paragraph", styleId: styleId }); super(attributes); - this.root.push(paragraphProperties); - this.root.push(runProperties); + this.paragraphProperties = new ParagraphProperties(); + this.runProperties = new RunProperties(); + this.root.push(this.paragraphProperties); + this.root.push(this.runProperties); + } + + clearVariables(): void { + this.paragraphProperties.clearVariables(); + this.runProperties.clearVariables(); + delete this.paragraphProperties; + delete this.runProperties; + } + + addParagraphProperty(property: XmlComponent): void { + this.paragraphProperties.push(property); + } + + addRunProperty(property: XmlComponent): void { + this.runProperties.push(property); } } export class HeadingStyle extends ParagraphStyle { - constructor(styleId: string, name: string, paragraphProperties: ParagraphProperties, runProperties: RunProperties) { - super(styleId, paragraphProperties, runProperties); + constructor(styleId: string, name: string) { + super(styleId); this.root.push(new Name(name)); this.root.push(new BasedOn("Normal")); this.root.push(new Next("Normal")); @@ -40,44 +61,51 @@ export class HeadingStyle extends ParagraphStyle { } } +export class TitleStyle extends HeadingStyle { + + constructor() { + super("Title", "Title"); + } +} + export class Heading1Style extends HeadingStyle { - - constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { - super("Heading1", "Heading 1", paragraphProperties, runProperties); + + constructor() { + super("Heading1", "Heading 1"); } } export class Heading2Style extends HeadingStyle { - - constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { - super("Heading2", "Heading 2", paragraphProperties, runProperties); + + constructor() { + super("Heading2", "Heading 2"); } } export class Heading3Style extends HeadingStyle { - - constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { - super("Heading3", "Heading 3", paragraphProperties, runProperties); + + constructor() { + super("Heading3", "Heading 3"); } } export class Heading4Style extends HeadingStyle { - - constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { - super("Heading4", "Heading 4", paragraphProperties, runProperties); + + constructor() { + super("Heading4", "Heading 4"); } } export class Heading5Style extends HeadingStyle { - - constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { - super("Heading5", "Heading 5", paragraphProperties, runProperties); + + constructor() { + super("Heading5", "Heading 5"); } } export class Heading6Style extends HeadingStyle { - - constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { - super("Heading6", "Heading 6", paragraphProperties, runProperties); + + constructor() { + super("Heading6", "Heading 6"); } } \ No newline at end of file diff --git a/ts/tests/localPackerTest.ts b/ts/tests/localPackerTest.ts index 5f82bb00ec..40569f0bd4 100644 --- a/ts/tests/localPackerTest.ts +++ b/ts/tests/localPackerTest.ts @@ -11,7 +11,7 @@ import {DefaultStyle} from "../styles/sample" import {Paragraph} from "../docx/paragraph" import {DefaultStylesFactory} from "../styles/factory" -describe("Packer", () => { +describe.only("Packer", () => { var packer: LocalPacker; var stylesFactory: DefaultStylesFactory; @@ -19,7 +19,9 @@ describe("Packer", () => { var document = new Document(); var paragraph = new Paragraph("test text"); var heading = new Paragraph("Hello world").heading1(); + document.addParagraph(new Paragraph("title").title()); document.addParagraph(heading); + document.addParagraph(new Paragraph("heading 2").heading2()); document.addParagraph(paragraph); var properties = new Properties({ creator: "Shan Fu",