Files
docx-js/ts/styles/style/index.ts

263 lines
6.6 KiB
TypeScript
Raw Normal View History

import * as paragraph from "../../docx/paragraph/formatting";
import * as formatting from "../../docx/run/formatting";
2017-03-09 09:45:01 +01:00
import { RunProperties } from "../../docx/run/properties";
2017-03-09 10:54:40 +01:00
import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
2017-03-09 09:45:01 +01:00
import { BasedOn, Name, Next, QuickFormat } from "./components";
2016-04-09 04:27:49 +01:00
2017-03-09 10:54:40 +01:00
export interface IStyleAttributes {
type?: string;
styleId?: string;
default?: boolean;
customStyle?: string;
}
class StyleAttributes extends XmlAttributeComponent<IStyleAttributes> {
protected xmlKeys = {
type: "w:type",
styleId: "w:styleId",
default: "w:default",
customStyle: "w:customStyle",
};
2017-03-09 10:54:40 +01:00
}
2016-04-09 20:16:35 +01:00
export class Style extends XmlComponent {
2016-05-08 17:01:20 +01:00
constructor(attributes: IStyleAttributes, name?: string) {
2016-04-09 20:16:35 +01:00
super("w:style");
2017-03-09 10:54:40 +01:00
this.root.push(new StyleAttributes(attributes));
if (name) {
this.root.push(new Name(name));
}
2016-04-09 04:27:49 +01:00
}
2016-05-08 17:01:20 +01:00
2017-03-09 09:45:01 +01:00
public push(styleSegment: XmlComponent): void {
2016-04-09 20:16:35 +01:00
this.root.push(styleSegment);
2016-04-09 04:27:49 +01:00
}
2016-05-07 20:17:18 +01:00
}
2016-05-08 17:01:20 +01:00
export class ParagraphStyle extends Style {
private paragraphProperties: paragraph.ParagraphProperties;
2016-05-10 00:32:00 +01:00
private runProperties: RunProperties;
constructor(styleId: string, name?: string) {
super({type: "paragraph", styleId: styleId}, name);
this.paragraphProperties = new paragraph.ParagraphProperties();
2016-05-10 00:32:00 +01:00
this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties);
this.root.push(this.runProperties);
}
2017-03-09 09:45:01 +01:00
public addParagraphProperty(property: XmlComponent): void {
2016-05-10 00:32:00 +01:00
this.paragraphProperties.push(property);
}
2017-03-09 09:45:01 +01:00
public addRunProperty(property: XmlComponent): void {
2016-05-10 00:32:00 +01:00
this.runProperties.push(property);
2016-05-08 17:01:20 +01:00
}
public basedOn(parentId: string): ParagraphStyle {
this.root.push(new BasedOn(parentId));
return this;
}
public quickFormat(): ParagraphStyle {
this.root.push(new QuickFormat());
return this;
}
public next(nextId: string): ParagraphStyle {
this.root.push(new Next(nextId));
return this;
}
// ---------- Run formatting ---------------------- //
public size(twips: number): ParagraphStyle {
this.addRunProperty(new formatting.Size(twips));
return this;
}
public bold(): ParagraphStyle {
this.addRunProperty(new formatting.Bold());
return this;
}
public italics(): ParagraphStyle {
this.addRunProperty(new formatting.Italics());
return this;
}
public smallCaps(): ParagraphStyle {
this.addRunProperty(new formatting.SmallCaps());
return this;
}
public allCaps(): ParagraphStyle {
this.addRunProperty(new formatting.Caps());
return this;
}
public strike(): ParagraphStyle {
this.addRunProperty(new formatting.Strike());
return this;
}
public doubleStrike(): ParagraphStyle {
this.addRunProperty(new formatting.DoubleStrike());
return this;
}
public subScript(): ParagraphStyle {
this.addRunProperty(new formatting.SubScript());
return this;
}
public superScript(): ParagraphStyle {
this.addRunProperty(new formatting.SuperScript());
return this;
}
public underline(underlineType?: string, color?: string): ParagraphStyle {
this.addRunProperty(new formatting.Underline(underlineType, color));
return this;
}
public color(color: string): ParagraphStyle {
this.addRunProperty(new formatting.Color(color));
return this;
}
2017-03-12 17:53:32 +01:00
public font(fontName: string): ParagraphStyle {
this.addRunProperty(new formatting.RunFonts(fontName));
return this;
}
// --------------------- Paragraph formatting ------------------------ //
public center(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("center"));
return this;
}
public left(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("left"));
return this;
}
public right(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("right"));
return this;
}
public justified(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("both"));
return this;
}
public thematicBreak(): ParagraphStyle {
this.addParagraphProperty(new paragraph.ThematicBreak());
return this;
}
public maxRightTabStop(): ParagraphStyle {
this.addParagraphProperty(new paragraph.MaxRightTabStop());
return this;
}
public leftTabStop(position: number): ParagraphStyle {
this.addParagraphProperty(new paragraph.LeftTabStop(position));
return this;
}
public indent(left: number, hanging?: number): ParagraphStyle {
this.addParagraphProperty(new paragraph.Indent(left, hanging));
return this;
}
public spacing(params: paragraph.ISpacingProperties): ParagraphStyle {
this.addParagraphProperty(new paragraph.Spacing(params));
return this;
2017-04-15 17:54:47 +01:00
}
public keepNext(): ParagraphStyle {
this.addParagraphProperty(new paragraph.KeepNext());
return this;
}
public keepLines(): ParagraphStyle {
this.addParagraphProperty(new paragraph.KeepLines());
return this;
}
2016-05-08 17:01:20 +01:00
}
export class HeadingStyle extends ParagraphStyle {
2016-05-10 00:32:00 +01:00
constructor(styleId: string, name: string) {
super(styleId, name);
this.basedOn("Normal");
this.next("Normal");
this.quickFormat();
2016-05-08 17:01:20 +01:00
}
}
2016-05-10 00:32:00 +01:00
export class TitleStyle extends HeadingStyle {
constructor() {
super("Title", "Title");
}
}
2016-05-08 17:01:20 +01:00
export class Heading1Style extends HeadingStyle {
2016-05-10 00:32:00 +01:00
constructor() {
super("Heading1", "Heading 1");
2016-05-08 17:01:20 +01:00
}
}
export class Heading2Style extends HeadingStyle {
2016-05-10 00:32:00 +01:00
constructor() {
super("Heading2", "Heading 2");
2016-05-08 17:01:20 +01:00
}
}
export class Heading3Style extends HeadingStyle {
2016-05-10 00:32:00 +01:00
constructor() {
super("Heading3", "Heading 3");
2016-05-08 17:01:20 +01:00
}
}
export class Heading4Style extends HeadingStyle {
2016-05-10 00:32:00 +01:00
constructor() {
super("Heading4", "Heading 4");
2016-05-08 17:01:20 +01:00
}
}
export class Heading5Style extends HeadingStyle {
2016-05-10 00:32:00 +01:00
constructor() {
super("Heading5", "Heading 5");
2016-05-08 17:01:20 +01:00
}
}
export class Heading6Style extends HeadingStyle {
2016-05-10 00:32:00 +01:00
constructor() {
super("Heading6", "Heading 6");
2016-05-08 17:01:20 +01:00
}
}
export class ListParagraph extends ParagraphStyle {
2016-05-26 15:08:34 +01:00
constructor() {
super("ListParagraph");
this.root.push(new Name("List Paragraph"));
this.root.push(new BasedOn("Normal"));
this.root.push(new QuickFormat());
}
2017-03-09 09:45:01 +01:00
}