added more styles

This commit is contained in:
Dolan Miu
2016-05-10 00:32:00 +01:00
parent 80661ac551
commit 11fa77edc0
5 changed files with 92 additions and 39 deletions

View File

@ -4,9 +4,21 @@ import {RunPropertiesDefaults} from "./run-properties";
export class DocumentDefaults extends XmlComponent { export class DocumentDefaults extends XmlComponent {
constructor(runPropertiesDefaults: RunPropertiesDefaults, paragraphPropertiesDefaults: ParagraphPropertiesDefaults) { private runPropertiesDefaults: RunPropertiesDefaults;
private paragraphPropertiesDefaults: ParagraphPropertiesDefaults;
constructor() {
super("w:docDefaults"); super("w:docDefaults");
this.root.push(runPropertiesDefaults); this.runPropertiesDefaults = new RunPropertiesDefaults();
this.root.push(paragraphPropertiesDefaults); 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;
} }
} }

View File

@ -2,29 +2,32 @@ import {Styles} from "./";
import {DocumentDefaults} from "./defaults"; import {DocumentDefaults} from "./defaults";
import {ParagraphPropertiesDefaults} from "./defaults/paragraph-properties"; import {ParagraphPropertiesDefaults} from "./defaults/paragraph-properties";
import {RunPropertiesDefaults} from "./defaults/run-properties"; import {RunPropertiesDefaults} from "./defaults/run-properties";
import {Heading1Style} from "./style"; import {Heading1Style, Heading2Style, TitleStyle} from "./style";
//import {StyleAttributes} from "./style/attributes"; //import {StyleAttributes} from "./style/attributes";
import {ParagraphProperties} from "../docx/paragraph/properties"; import {ParagraphProperties} from "../docx/paragraph/properties";
import {RunProperties} from "../docx/run/properties"; import {RunProperties} from "../docx/run/properties";
import {Color} from "../docx/run/formatting"; import {Color, Size} from "../docx/run/formatting";
export class DefaultStylesFactory { export class DefaultStylesFactory {
constructor() {
}
newInstance(): Styles { newInstance(): Styles {
var styles = new Styles(); var styles = new Styles();
var paragraphProperties = new ParagraphPropertiesDefaults(); styles.push(new DocumentDefaults());
var runProperties = new RunPropertiesDefaults();
styles.push(new DocumentDefaults(paragraphProperties, runProperties));
var heading1ParagraphProperties = new ParagraphProperties(); var titleStyle = new TitleStyle();
var heading1RunProperties = new RunProperties(); titleStyle.addRunProperty(new Size(56));
heading1RunProperties.push(new Color("365F91")); styles.push(titleStyle);
var heading1Style = new Heading1Style(heading1ParagraphProperties, heading1RunProperties);
var heading1Style = new Heading1Style();
heading1Style.addRunProperty(new Color("2E74B5"));
heading1Style.addRunProperty(new Size(32));
styles.push(heading1Style); 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, " ")); console.log(JSON.stringify(styles, null, " "));
return styles; return styles;
} }

View File

@ -27,4 +27,12 @@ export class Styles extends XmlComponent {
push(style: XmlComponent): void { push(style: XmlComponent): void {
this.root.push(style); this.root.push(style);
} }
clearVariables() {
console.log(this);
console.log(this.root);
this.root.forEach(element => {
element.clearVariables();
})
}
} }

View File

@ -18,21 +18,42 @@ export class Style extends XmlComponent {
export class ParagraphStyle extends Style { 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({ var attributes = new StyleAttributes({
type: "paragraph", type: "paragraph",
styleId: styleId styleId: styleId
}); });
super(attributes); super(attributes);
this.root.push(paragraphProperties); this.paragraphProperties = new ParagraphProperties();
this.root.push(runProperties); 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 { export class HeadingStyle extends ParagraphStyle {
constructor(styleId: string, name: string, paragraphProperties: ParagraphProperties, runProperties: RunProperties) { constructor(styleId: string, name: string) {
super(styleId, paragraphProperties, runProperties); super(styleId);
this.root.push(new Name(name)); this.root.push(new Name(name));
this.root.push(new BasedOn("Normal")); this.root.push(new BasedOn("Normal"));
this.root.push(new Next("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 { export class Heading1Style extends HeadingStyle {
constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { constructor() {
super("Heading1", "Heading 1", paragraphProperties, runProperties); super("Heading1", "Heading 1");
} }
} }
export class Heading2Style extends HeadingStyle { export class Heading2Style extends HeadingStyle {
constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { constructor() {
super("Heading2", "Heading 2", paragraphProperties, runProperties); super("Heading2", "Heading 2");
} }
} }
export class Heading3Style extends HeadingStyle { export class Heading3Style extends HeadingStyle {
constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { constructor() {
super("Heading3", "Heading 3", paragraphProperties, runProperties); super("Heading3", "Heading 3");
} }
} }
export class Heading4Style extends HeadingStyle { export class Heading4Style extends HeadingStyle {
constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { constructor() {
super("Heading4", "Heading 4", paragraphProperties, runProperties); super("Heading4", "Heading 4");
} }
} }
export class Heading5Style extends HeadingStyle { export class Heading5Style extends HeadingStyle {
constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { constructor() {
super("Heading5", "Heading 5", paragraphProperties, runProperties); super("Heading5", "Heading 5");
} }
} }
export class Heading6Style extends HeadingStyle { export class Heading6Style extends HeadingStyle {
constructor(paragraphProperties: ParagraphProperties, runProperties: RunProperties) { constructor() {
super("Heading6", "Heading 6", paragraphProperties, runProperties); super("Heading6", "Heading 6");
} }
} }

View File

@ -11,7 +11,7 @@ import {DefaultStyle} from "../styles/sample"
import {Paragraph} from "../docx/paragraph" import {Paragraph} from "../docx/paragraph"
import {DefaultStylesFactory} from "../styles/factory" import {DefaultStylesFactory} from "../styles/factory"
describe("Packer", () => { describe.only("Packer", () => {
var packer: LocalPacker; var packer: LocalPacker;
var stylesFactory: DefaultStylesFactory; var stylesFactory: DefaultStylesFactory;
@ -19,7 +19,9 @@ describe("Packer", () => {
var document = new Document(); var document = new Document();
var paragraph = new Paragraph("test text"); var paragraph = new Paragraph("test text");
var heading = new Paragraph("Hello world").heading1(); var heading = new Paragraph("Hello world").heading1();
document.addParagraph(new Paragraph("title").title());
document.addParagraph(heading); document.addParagraph(heading);
document.addParagraph(new Paragraph("heading 2").heading2());
document.addParagraph(paragraph); document.addParagraph(paragraph);
var properties = new Properties({ var properties = new Properties({
creator: "Shan Fu", creator: "Shan Fu",