Declarative styles

This commit is contained in:
Dolan
2019-10-04 01:20:41 +01:00
parent 2536fbe752
commit 591b2f4e04
20 changed files with 920 additions and 484 deletions

View File

@ -1,36 +1,48 @@
import { BaseXmlComponent, XmlComponent } from "file/xml-components";
import { BaseXmlComponent, ImportedXmlComponent, XmlComponent } from "file/xml-components";
import { DocumentDefaults } from "./defaults";
import { CharacterStyle, ParagraphStyle } from "./style";
import { ICharacterStyleOptions } from "./style/character-style";
import { IParagraphStyleOptions } from "./style/paragraph-style";
export * from "./border";
export class Styles extends XmlComponent {
constructor(initialStyles?: BaseXmlComponent) {
super("w:styles");
if (initialStyles) {
this.root.push(initialStyles);
}
}
interface IStylesOptions {
readonly initialStyles?: BaseXmlComponent;
readonly paragraphStyles?: IParagraphStyleOptions[];
readonly characterStyles?: ICharacterStyleOptions[];
readonly importedStyles?: Array<XmlComponent | ParagraphStyle | CharacterStyle | ImportedXmlComponent>;
}
public push(style: XmlComponent): Styles {
this.root.push(style);
return this;
export class Styles extends XmlComponent {
constructor(options: IStylesOptions) {
super("w:styles");
if (options.initialStyles) {
this.root.push(options.initialStyles);
}
if (options.paragraphStyles) {
for (const style of options.paragraphStyles) {
this.root.push(new ParagraphStyle(style));
}
}
if (options.characterStyles) {
for (const style of options.characterStyles) {
this.root.push(new CharacterStyle(style));
}
}
if (options.importedStyles) {
for (const style of options.importedStyles) {
this.root.push(style);
}
}
}
public createDocumentDefaults(): DocumentDefaults {
const defaults = new DocumentDefaults();
this.push(defaults);
this.root.push(defaults);
return defaults;
}
public createParagraphStyle(styleId: string, name?: string): ParagraphStyle {
const paragraphStyle = new ParagraphStyle(styleId, name);
this.push(paragraphStyle);
return paragraphStyle;
}
public createCharacterStyle(styleId: string, name?: string): CharacterStyle {
const characterStyle = new CharacterStyle(styleId, name);
this.push(characterStyle);
return characterStyle;
}
}