Files
docx-js/ts/numbering/level.ts

201 lines
5.1 KiB
TypeScript
Raw Normal View History

import * as paragraph from "../docx/paragraph/formatting";
2017-03-08 17:13:27 +01:00
import { ParagraphProperties } from "../docx/paragraph/properties";
2017-03-13 12:30:36 +01:00
import * as formatting from "../docx/run/formatting";
2017-03-08 17:13:27 +01:00
import { RunProperties } from "../docx/run/properties";
2017-03-09 18:55:54 +01:00
import { Attributes, XmlAttributeComponent, XmlComponent } from "../docx/xml-components";
2016-05-19 22:42:23 +01:00
2017-03-08 17:13:27 +01:00
interface ILevelAttributesProperties {
2016-05-26 15:08:34 +01:00
ilvl?: number;
tentative?: number;
2016-05-19 22:42:23 +01:00
}
class LevelAttributes extends XmlAttributeComponent<ILevelAttributesProperties> {
protected xmlKeys = {
ilvl: "w:ilvl",
tentative: "w15:tentative",
};
2016-05-19 22:42:23 +01:00
}
class Start extends XmlComponent {
constructor(value: number) {
super("w:start");
this.root.push(new Attributes({
2017-03-08 17:13:27 +01:00
val: value,
2016-05-19 22:42:23 +01:00
}));
}
}
class NumberFormat extends XmlComponent {
constructor(value: string) {
super("w:numFmt");
this.root.push(new Attributes({
2017-03-08 17:13:27 +01:00
val: value,
2016-05-19 22:42:23 +01:00
}));
}
}
class LevelText extends XmlComponent {
constructor(value: string) {
super("w:lvlText");
this.root.push(new Attributes({
2017-03-08 17:13:27 +01:00
val: value,
2016-05-19 22:42:23 +01:00
}));
}
}
class LevelJc extends XmlComponent {
constructor(value: string) {
super("w:lvlJc");
this.root.push(new Attributes({
2017-03-08 17:13:27 +01:00
val: value,
2016-05-19 22:42:23 +01:00
}));
}
}
export class Level extends XmlComponent {
private paragraphProperties: ParagraphProperties;
private runProperties: RunProperties;
constructor(level: number, numberFormat: string, levelText: string, lvlJc: string) {
super("w:lvl");
this.root.push(new LevelAttributes({
ilvl: level,
2017-03-08 17:13:27 +01:00
tentative: 1,
2016-05-19 22:42:23 +01:00
}));
this.root.push(new Start(1));
this.root.push(new NumberFormat(numberFormat));
this.root.push(new LevelText(levelText));
this.root.push(new LevelJc(lvlJc));
this.paragraphProperties = new ParagraphProperties();
this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties);
this.root.push(this.runProperties);
}
public addParagraphProperty(property: XmlComponent): Level {
2016-05-19 22:42:23 +01:00
this.paragraphProperties.push(property);
return this;
2016-05-19 22:42:23 +01:00
}
public addRunProperty(property: XmlComponent): Level {
2016-05-19 22:42:23 +01:00
this.runProperties.push(property);
return this;
2016-05-19 22:42:23 +01:00
}
2017-03-13 12:30:36 +01:00
// ---------- Run formatting ---------------------- //
public size(twips: number): Level {
this.addRunProperty(new formatting.Size(twips));
return this;
}
public bold(): Level {
this.addRunProperty(new formatting.Bold());
return this;
}
public italics(): Level {
this.addRunProperty(new formatting.Italics());
return this;
}
public smallCaps(): Level {
this.addRunProperty(new formatting.SmallCaps());
return this;
}
public allCaps(): Level {
this.addRunProperty(new formatting.Caps());
return this;
}
public strike(): Level {
this.addRunProperty(new formatting.Strike());
return this;
}
public doubleStrike(): Level {
this.addRunProperty(new formatting.DoubleStrike());
return this;
}
public subScript(): Level {
this.addRunProperty(new formatting.SubScript());
return this;
}
public superScript(): Level {
this.addRunProperty(new formatting.SuperScript());
return this;
}
public underline(underlineType?: string, color?: string): Level {
this.addRunProperty(new formatting.Underline(underlineType, color));
return this;
}
public color(color: string): Level {
this.addRunProperty(new formatting.Color(color));
return this;
}
public font(fontName: string): Level {
this.addRunProperty(new formatting.RunFonts(fontName));
return this;
}
// --------------------- Paragraph formatting ------------------------ //
public center(): Level {
this.addParagraphProperty(new paragraph.Alignment("center"));
return this;
}
public left(): Level {
this.addParagraphProperty(new paragraph.Alignment("left"));
return this;
}
public right(): Level {
this.addParagraphProperty(new paragraph.Alignment("right"));
return this;
}
public justified(): Level {
this.addParagraphProperty(new paragraph.Alignment("both"));
return this;
}
public thematicBreak(): Level {
this.addParagraphProperty(new paragraph.ThematicBreak());
return this;
}
public maxRightTabStop(): Level {
this.addParagraphProperty(new paragraph.MaxRightTabStop());
return this;
}
public leftTabStop(position: number): Level {
this.addParagraphProperty(new paragraph.LeftTabStop(position));
return this;
}
public indent(left: number, hanging?: number): Level {
this.addParagraphProperty(new paragraph.Indent(left, hanging));
return this;
}
public spacing(params: paragraph.ISpacingProperties): Level {
this.addParagraphProperty(new paragraph.Spacing(params));
return this;
};
2017-03-08 17:13:27 +01:00
}