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

278 lines
7.0 KiB
TypeScript
Raw Normal View History

import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
import {
Alignment,
2019-06-12 01:03:36 +01:00
AlignmentType,
2019-07-31 08:48:02 +01:00
IIndentAttributesProperties,
Indent,
ISpacingProperties,
KeepLines,
KeepNext,
Spacing,
2019-10-09 20:56:31 +01:00
TabStop,
TabStopType,
ThematicBreak,
} from "../paragraph/formatting";
2017-12-20 00:01:23 +00:00
import { ParagraphProperties } from "../paragraph/properties";
import * as formatting from "../paragraph/run/formatting";
import { RunProperties } from "../paragraph/run/properties";
2019-10-04 01:20:41 +01:00
import { UnderlineType } from "../paragraph/run/underline";
2016-05-19 22:42:23 +01:00
2017-03-08 17:13:27 +01:00
interface ILevelAttributesProperties {
readonly ilvl?: number;
readonly tentative?: number;
2016-05-19 22:42:23 +01:00
}
class LevelAttributes extends XmlAttributeComponent<ILevelAttributesProperties> {
protected readonly 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");
2018-01-23 01:33:12 +00:00
this.root.push(
new Attributes({
val: value,
}),
);
2016-05-19 22:42:23 +01:00
}
}
class NumberFormat extends XmlComponent {
constructor(value: string) {
super("w:numFmt");
2018-01-23 01:33:12 +00:00
this.root.push(
new Attributes({
val: value,
}),
);
2016-05-19 22:42:23 +01:00
}
}
class LevelText extends XmlComponent {
constructor(value: string) {
super("w:lvlText");
2018-01-23 01:33:12 +00:00
this.root.push(
new Attributes({
val: value,
}),
);
2016-05-19 22:42:23 +01:00
}
}
class LevelJc extends XmlComponent {
constructor(value: string) {
super("w:lvlJc");
2018-01-23 01:33:12 +00:00
this.root.push(
new Attributes({
val: value,
}),
);
2016-05-19 22:42:23 +01:00
}
}
export enum LevelSuffix {
NOTHING = "nothing",
SPACE = "space",
TAB = "tab",
}
class Suffix extends XmlComponent {
constructor(value: LevelSuffix) {
super("w:suff");
this.root.push(
new Attributes({
val: value,
}),
);
}
}
export class LevelBase extends XmlComponent {
2018-01-29 01:55:25 +00:00
private readonly paragraphProperties: ParagraphProperties;
private readonly runProperties: RunProperties;
2016-05-19 22:42:23 +01:00
constructor(level: number, start?: number, numberFormat?: string, levelText?: string, lvlJc?: string) {
2016-05-19 22:42:23 +01:00
super("w:lvl");
2018-01-23 01:33:12 +00:00
this.root.push(
new LevelAttributes({
ilvl: level,
tentative: 1,
}),
);
2016-05-19 22:42:23 +01:00
if (start !== undefined) {
this.root.push(new Start(start));
}
if (numberFormat !== undefined) {
this.root.push(new NumberFormat(numberFormat));
}
if (levelText !== undefined) {
this.root.push(new LevelText(levelText));
}
if (lvlJc !== undefined) {
this.root.push(new LevelJc(lvlJc));
}
2016-05-19 22:42:23 +01:00
2019-06-12 01:03:36 +01:00
this.paragraphProperties = new ParagraphProperties({});
2016-05-19 22:42:23 +01:00
this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties);
this.root.push(this.runProperties);
}
2018-08-09 23:22:03 +01:00
public setSuffix(value: LevelSuffix): LevelBase {
this.root.push(new Suffix(value));
return this;
}
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;
}
2019-10-04 01:20:41 +01:00
public underline(underlineType?: UnderlineType, color?: string): Level {
2017-03-13 12:30:36 +01:00
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;
}
2019-08-05 13:42:45 +03:00
public highlight(color: string): Level {
this.addRunProperty(new formatting.Highlight(color));
return this;
}
public shadow(value: string, fill: string, color: string): Level {
2019-08-06 23:08:21 +01:00
this.addRunProperty(new formatting.Shading(value, fill, color));
2019-08-05 13:42:45 +03:00
return this;
}
// --------------------- Paragraph formatting ------------------------ //
public center(): Level {
2019-06-12 01:03:36 +01:00
this.addParagraphProperty(new Alignment(AlignmentType.CENTER));
return this;
}
public left(): Level {
2019-06-12 01:03:36 +01:00
this.addParagraphProperty(new Alignment(AlignmentType.LEFT));
return this;
}
public right(): Level {
2019-06-12 01:03:36 +01:00
this.addParagraphProperty(new Alignment(AlignmentType.RIGHT));
return this;
}
public justified(): Level {
2019-06-12 01:03:36 +01:00
this.addParagraphProperty(new Alignment(AlignmentType.BOTH));
return this;
}
public thematicBreak(): Level {
this.addParagraphProperty(new ThematicBreak());
return this;
}
public rightTabStop(position: number): Level {
2019-10-09 20:56:31 +01:00
return this.addParagraphProperty(new TabStop(TabStopType.RIGHT, position));
}
public leftTabStop(position: number): Level {
2019-10-09 20:56:31 +01:00
this.addParagraphProperty(new TabStop(TabStopType.LEFT, position));
return this;
}
2019-07-31 08:48:02 +01:00
public indent(attrs: IIndentAttributesProperties): Level {
this.addParagraphProperty(new Indent(attrs));
return this;
}
public spacing(params: ISpacingProperties): Level {
this.addParagraphProperty(new Spacing(params));
return this;
2017-04-15 17:54:47 +01:00
}
public keepNext(): Level {
this.addParagraphProperty(new KeepNext());
return this;
}
public keepLines(): Level {
this.addParagraphProperty(new KeepLines());
return this;
}
2017-03-08 17:13:27 +01:00
}
export class Level extends LevelBase {
// This is the level that sits under abstractNum. We make a
// handful of properties required
constructor(level: number, numberFormat: string, levelText: string, lvlJc: string) {
super(level, 1, numberFormat, levelText, lvlJc);
}
}
export class LevelForOverride extends LevelBase {}