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

243 lines
6.9 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,
Indent,
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-11-01 01:57:01 +00:00
import { IParagraphStyleOptions2, IRunStyleOptions } from "../styles/style-options";
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 {
2019-11-01 01:57:01 +00:00
constructor(value: AlignmentType) {
2016-05-19 22:42:23 +01:00
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",
}
2019-11-01 01:57:01 +00:00
export interface ILevelsOptions {
readonly level: number;
2019-11-06 20:54:39 +00:00
readonly format?: string;
readonly text?: string;
2019-11-01 01:57:01 +00:00
readonly alignment?: AlignmentType;
readonly start?: number;
readonly suffix?: LevelSuffix;
readonly style?: {
readonly run?: IRunStyleOptions;
readonly paragraph?: IParagraphStyleOptions2;
};
}
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
2019-11-01 01:57:01 +00:00
constructor({ level, format, text, alignment = AlignmentType.START, start = 1, style, suffix }: ILevelsOptions) {
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
2019-11-01 01:57:01 +00:00
this.root.push(new Start(start));
this.root.push(new LevelJc(alignment));
2016-05-19 22:42:23 +01:00
2019-11-06 20:54:39 +00:00
if (format) {
this.root.push(new NumberFormat(format));
}
if (text) {
this.root.push(new LevelText(text));
}
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);
2019-11-01 01:57:01 +00:00
if (suffix) {
this.root.push(new Suffix(suffix));
}
2019-11-01 01:57:01 +00:00
if (style) {
if (style.run) {
if (style.run.size) {
this.runProperties.push(new formatting.Size(style.run.size));
}
if (style.run.bold) {
this.runProperties.push(new formatting.Bold());
}
if (style.run.italics) {
this.runProperties.push(new formatting.Italics());
}
if (style.run.smallCaps) {
this.runProperties.push(new formatting.SmallCaps());
}
if (style.run.allCaps) {
this.runProperties.push(new formatting.Caps());
}
if (style.run.strike) {
this.runProperties.push(new formatting.Strike());
}
if (style.run.doubleStrike) {
this.runProperties.push(new formatting.DoubleStrike());
}
if (style.run.subScript) {
this.runProperties.push(new formatting.SubScript());
}
if (style.run.superScript) {
this.runProperties.push(new formatting.SuperScript());
}
if (style.run.underline) {
this.runProperties.push(new formatting.Underline(style.run.underline.type, style.run.underline.color));
}
if (style.run.color) {
this.runProperties.push(new formatting.Color(style.run.color));
}
if (style.run.font) {
this.runProperties.push(new formatting.RunFonts(style.run.font));
}
if (style.run.highlight) {
this.runProperties.push(new formatting.Highlight(style.run.highlight));
}
if (style.run.shadow) {
this.runProperties.push(new formatting.Shading(style.run.shadow.type, style.run.shadow.fill, style.run.shadow.color));
}
}
if (style.paragraph) {
if (style.paragraph.alignment) {
this.paragraphProperties.push(new Alignment(style.paragraph.alignment));
}
if (style.paragraph.thematicBreak) {
this.paragraphProperties.push(new ThematicBreak());
}
if (style.paragraph.rightTabStop) {
this.paragraphProperties.push(new TabStop(TabStopType.RIGHT, style.paragraph.rightTabStop));
}
if (style.paragraph.leftTabStop) {
this.paragraphProperties.push(new TabStop(TabStopType.LEFT, style.paragraph.leftTabStop));
}
if (style.paragraph.indent) {
this.paragraphProperties.push(new Indent(style.paragraph.indent));
}
if (style.paragraph.spacing) {
this.paragraphProperties.push(new Spacing(style.paragraph.spacing));
}
if (style.paragraph.keepNext) {
this.paragraphProperties.push(new KeepNext());
}
if (style.paragraph.keepLines) {
this.paragraphProperties.push(new KeepLines());
}
}
}
}
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
2019-11-01 01:57:01 +00:00
constructor(options: ILevelsOptions) {
super(options);
}
}
export class LevelForOverride extends LevelBase {}