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

336 lines
9.5 KiB
TypeScript
Raw Normal View History

import {
Alignment,
AlignmentOptions,
Indent,
ISpacingProperties,
KeepLines,
KeepNext,
LeftTabStop,
MaxRightTabStop,
ParagraphProperties,
Spacing,
ThematicBreak,
} from "file/paragraph";
2018-10-26 01:04:07 +01:00
import * as formatting from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
2017-03-09 09:45:01 +01:00
2018-06-28 03:01:25 +01:00
import { BasedOn, Link, Name, Next, QuickFormat, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
2016-04-09 04:27:49 +01:00
2017-03-09 10:54:40 +01:00
export interface IStyleAttributes {
readonly type?: string;
readonly styleId?: string;
readonly default?: boolean;
readonly customStyle?: string;
2017-03-09 10:54:40 +01:00
}
class StyleAttributes extends XmlAttributeComponent<IStyleAttributes> {
protected readonly 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 {
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 readonly paragraphProperties: ParagraphProperties;
2018-01-29 01:55:25 +00:00
private readonly runProperties: RunProperties;
2016-05-10 00:32:00 +01:00
constructor(styleId: string, name?: string) {
2018-01-23 01:33:12 +00:00
super({ type: "paragraph", styleId: styleId }, name);
this.paragraphProperties = new ParagraphProperties();
2016-05-10 00:32:00 +01:00
this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties);
this.root.push(this.runProperties);
}
public addParagraphProperty(property: XmlComponent): ParagraphStyle {
2016-05-10 00:32:00 +01:00
this.paragraphProperties.push(property);
return this;
2016-05-10 00:32:00 +01:00
}
public addRunProperty(property: XmlComponent): ParagraphStyle {
2016-05-10 00:32:00 +01:00
this.runProperties.push(property);
return this;
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 {
2018-09-20 16:54:58 -03:00
return this.addRunProperty(new formatting.Size(twips)).addRunProperty(new formatting.SizeComplexScript(twips));
}
public bold(): ParagraphStyle {
return this.addRunProperty(new formatting.Bold());
}
public italics(): ParagraphStyle {
return this.addRunProperty(new formatting.Italics());
}
public smallCaps(): ParagraphStyle {
return this.addRunProperty(new formatting.SmallCaps());
}
public allCaps(): ParagraphStyle {
return this.addRunProperty(new formatting.Caps());
}
public strike(): ParagraphStyle {
return this.addRunProperty(new formatting.Strike());
}
public doubleStrike(): ParagraphStyle {
return this.addRunProperty(new formatting.DoubleStrike());
}
public subScript(): ParagraphStyle {
return this.addRunProperty(new formatting.SubScript());
}
public superScript(): ParagraphStyle {
return this.addRunProperty(new formatting.SuperScript());
}
public underline(underlineType?: string, color?: string): ParagraphStyle {
return this.addRunProperty(new formatting.Underline(underlineType, color));
}
public color(color: string): ParagraphStyle {
return this.addRunProperty(new formatting.Color(color));
}
2017-03-12 17:53:32 +01:00
public font(fontName: string): ParagraphStyle {
return this.addRunProperty(new formatting.RunFonts(fontName));
2017-03-12 17:53:32 +01:00
}
2018-09-19 18:41:55 -03:00
public characterSpacing(value: number): ParagraphStyle {
return this.addRunProperty(new formatting.CharacterSpacing(value));
2018-09-19 18:41:55 -03:00
}
2017-03-12 17:53:32 +01:00
// --------------------- Paragraph formatting ------------------------ //
public center(): ParagraphStyle {
return this.addParagraphProperty(new Alignment(AlignmentOptions.CENTER));
}
public left(): ParagraphStyle {
return this.addParagraphProperty(new Alignment(AlignmentOptions.LEFT));
}
public right(): ParagraphStyle {
return this.addParagraphProperty(new Alignment(AlignmentOptions.RIGHT));
}
public justified(): ParagraphStyle {
return this.addParagraphProperty(new Alignment(AlignmentOptions.BOTH));
}
public thematicBreak(): ParagraphStyle {
return this.addParagraphProperty(new ThematicBreak());
}
public maxRightTabStop(): ParagraphStyle {
return this.addParagraphProperty(new MaxRightTabStop());
}
public leftTabStop(position: number): ParagraphStyle {
return this.addParagraphProperty(new LeftTabStop(position));
}
public indent(attrs: object): ParagraphStyle {
return this.addParagraphProperty(new Indent(attrs));
}
public spacing(params: ISpacingProperties): ParagraphStyle {
return this.addParagraphProperty(new Spacing(params));
2017-04-15 17:54:47 +01:00
}
public keepNext(): ParagraphStyle {
return this.addParagraphProperty(new KeepNext());
}
public keepLines(): ParagraphStyle {
return this.addParagraphProperty(new KeepLines());
}
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 {
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
}
2018-05-06 22:23:04 -05:00
export class CharacterStyle extends Style {
private readonly runProperties: RunProperties;
constructor(styleId: string, name?: string) {
super({ type: "character", styleId: styleId }, name);
this.runProperties = new RunProperties();
this.root.push(this.runProperties);
this.root.push(new UiPriority("99"));
2018-06-28 03:01:25 +01:00
this.root.push(new UnhideWhenUsed());
2018-05-06 22:23:04 -05:00
}
public basedOn(parentId: string): CharacterStyle {
this.root.push(new BasedOn(parentId));
return this;
}
public addRunProperty(property: XmlComponent): CharacterStyle {
2018-05-06 22:23:04 -05:00
this.runProperties.push(property);
return this;
2018-05-06 22:23:04 -05:00
}
public color(color: string): CharacterStyle {
return this.addRunProperty(new formatting.Color(color));
2018-05-06 22:23:04 -05:00
}
public underline(underlineType?: string, color?: string): CharacterStyle {
return this.addRunProperty(new formatting.Underline(underlineType, color));
2018-05-06 22:23:04 -05:00
}
2018-06-28 03:01:25 +01:00
public size(twips: number): CharacterStyle {
2018-09-20 16:54:58 -03:00
return this.addRunProperty(new formatting.Size(twips)).addRunProperty(new formatting.SizeComplexScript(twips));
2018-06-28 03:01:25 +01:00
}
2018-05-06 22:23:04 -05:00
}
export class HyperlinkStyle extends CharacterStyle {
constructor() {
super("Hyperlink", "Hyperlink");
this.basedOn("DefaultParagraphFont")
.color("0563C1")
.underline("single");
}
}
2018-06-28 03:01:25 +01:00
export class FootnoteReferenceStyle extends Style {
private readonly runProperties: RunProperties;
constructor() {
super({ type: "character", styleId: "FootnoteReference" });
this.root.push(new Name("footnote reference"));
this.root.push(new BasedOn("DefaultParagraphFont"));
this.root.push(new UiPriority("99"));
this.root.push(new SemiHidden());
this.root.push(new UnhideWhenUsed());
this.runProperties = new RunProperties();
this.runProperties.addChildElement(new formatting.SuperScript());
this.root.push(this.runProperties);
}
}
export class FootnoteText extends ParagraphStyle {
constructor() {
super("FootnoteText");
this.root.push(new Name("footnote text"));
this.root.push(new BasedOn("Normal"));
this.root.push(new Link("FootnoteTextChar"));
this.root.push(new UiPriority("99"));
this.root.push(new SemiHidden());
this.root.push(new UnhideWhenUsed());
this.spacing({
after: 0,
line: 240,
lineRule: "auto",
});
this.size(20);
}
}
export class FootnoteTextChar extends CharacterStyle {
constructor() {
super("FootnoteTextChar", "Footnote Text Char");
this.basedOn("DefaultParagraphFont");
this.root.push(new Link("FootnoteText"));
this.root.push(new UiPriority("99"));
this.root.push(new SemiHidden());
this.size(20);
}
}