Make Paragraph declaritive

This commit is contained in:
Dolan
2019-06-12 01:03:36 +01:00
parent 5c1a731314
commit cb42c74a8d
33 changed files with 451 additions and 497 deletions

View File

@ -4,43 +4,165 @@ import { Image } from "file/media";
import { Num } from "file/numbering/num";
import { XmlComponent } from "file/xml-components";
import { Alignment, AlignmentOptions } from "./formatting/alignment";
import { Alignment, AlignmentType } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
import { Border, ThematicBreak } from "./formatting/border";
import { IBorderOptions, ThematicBreak } from "./formatting/border";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
import { ContextualSpacing, ISpacingProperties, Spacing } from "./formatting/spacing";
import { Style } from "./formatting/style";
import { HeadingLevel, Style } from "./formatting/style";
import { CenterTabStop, LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list";
import { Bookmark, Hyperlink, OutlineLevel } from "./links";
import { ParagraphProperties } from "./properties";
import { PictureRun, Run, SequentialIdentifier, TextRun } from "./run";
interface ITabStopOptions {
readonly position: number;
readonly leader?: LeaderType;
}
export interface IParagraphOptions {
readonly text?: string;
readonly border?: IBorderOptions;
readonly spacing?: ISpacingProperties;
readonly runs?: Run[];
readonly outlineLevel?: number;
readonly alignment?: AlignmentType;
readonly heading?: HeadingLevel;
readonly bidirectional?: boolean;
readonly thematicBreak?: boolean;
readonly pageBreakBefore?: boolean;
readonly contextualSpacing?: boolean;
readonly indent?: IIndentAttributesProperties;
readonly keepLines?: boolean;
readonly keepNext?: boolean;
readonly tabStop?: {
readonly left?: ITabStopOptions;
readonly right?: ITabStopOptions;
readonly maxRight?: {
readonly leader?: LeaderType;
};
readonly center?: ITabStopOptions;
};
readonly style?: string;
readonly bullet?: {
readonly level: number;
};
readonly numbering?: {
readonly num: Num;
readonly level: number;
readonly custom?: boolean;
};
}
export class Paragraph extends XmlComponent {
private readonly properties: ParagraphProperties;
constructor(text?: string) {
constructor(options: string | IParagraphOptions) {
super("w:p");
this.properties = new ParagraphProperties();
this.root.push(this.properties);
if (text !== undefined) {
this.root.push(new TextRun(text));
if (typeof options === "string") {
this.properties = new ParagraphProperties({});
this.root.push(this.properties);
this.root.push(new TextRun(options));
return;
}
}
public get paragraphProperties(): ParagraphProperties {
return this.properties;
}
this.properties = new ParagraphProperties({
border: options.border,
});
public get Borders(): Border {
return this.properties.paragraphBorder;
}
this.root.push(this.properties);
public createBorder(): Paragraph {
this.properties.createBorder();
return this;
if (options.text) {
this.root.push(new TextRun(options.text));
}
if (options.spacing) {
this.properties.push(new Spacing(options.spacing));
}
if (options.outlineLevel !== undefined) {
this.properties.push(new OutlineLevel(options.outlineLevel));
}
if (options.alignment) {
this.properties.push(new Alignment(options.alignment));
}
if (options.heading) {
this.properties.push(new Style(options.heading));
}
if (options.bidirectional) {
this.properties.push(new Bidirectional());
}
if (options.thematicBreak) {
this.properties.push(new ThematicBreak());
}
if (options.pageBreakBefore) {
this.properties.push(new PageBreakBefore());
}
if (options.contextualSpacing) {
this.properties.push(new ContextualSpacing(options.contextualSpacing));
}
if (options.indent) {
this.properties.push(new Indent(options.indent));
}
if (options.keepLines) {
this.properties.push(new KeepLines());
}
if (options.keepNext) {
this.properties.push(new KeepNext());
}
if (options.tabStop) {
if (options.tabStop.left) {
this.properties.push(new LeftTabStop(options.tabStop.left.position, options.tabStop.left.leader));
}
if (options.tabStop.right) {
this.properties.push(new RightTabStop(options.tabStop.right.position, options.tabStop.right.leader));
}
if (options.tabStop.maxRight) {
this.properties.push(new MaxRightTabStop(options.tabStop.maxRight.leader));
}
if (options.tabStop.center) {
this.properties.push(new CenterTabStop(options.tabStop.center.position, options.tabStop.center.leader));
}
}
if (options.style) {
this.properties.push(new Style(options.style));
}
if (options.bullet) {
this.properties.push(new Style("ListParagraph"));
this.properties.push(new NumberProperties(1, options.bullet.level));
}
if (options.numbering) {
if (!options.numbering.custom) {
this.properties.push(new Style("ListParagraph"));
}
this.properties.push(new NumberProperties(options.numbering.num.id, options.numbering.level));
}
if (options.runs) {
for (const run of options.runs) {
this.root.push(run);
}
}
}
public addRun(run: Run): Paragraph {
@ -61,12 +183,6 @@ export class Paragraph extends XmlComponent {
return this;
}
public createTextRun(text: string): TextRun {
const run = new TextRun(text);
this.addRun(run);
return run;
}
public addImage(image: Image): PictureRun {
const run = image.Run;
this.addRun(run);
@ -74,158 +190,11 @@ export class Paragraph extends XmlComponent {
return run;
}
public heading1(): Paragraph {
this.properties.push(new Style("Heading1"));
return this;
}
public heading2(): Paragraph {
this.properties.push(new Style("Heading2"));
return this;
}
public heading3(): Paragraph {
this.properties.push(new Style("Heading3"));
return this;
}
public heading4(): Paragraph {
this.properties.push(new Style("Heading4"));
return this;
}
public heading5(): Paragraph {
this.properties.push(new Style("Heading5"));
return this;
}
public heading6(): Paragraph {
this.properties.push(new Style("Heading6"));
return this;
}
public title(): Paragraph {
this.properties.push(new Style("Title"));
return this;
}
public center(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.CENTER));
return this;
}
public left(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.LEFT));
return this;
}
public right(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.RIGHT));
return this;
}
public start(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.START));
return this;
}
public end(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.END));
return this;
}
public distribute(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.DISTRIBUTE));
return this;
}
public justified(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.BOTH));
return this;
}
public thematicBreak(): Paragraph {
this.properties.push(new ThematicBreak());
return this;
}
public pageBreak(): Paragraph {
this.root.push(new PageBreak());
return this;
}
public pageBreakBefore(): Paragraph {
this.properties.push(new PageBreakBefore());
return this;
}
public maxRightTabStop(leader?: LeaderType): Paragraph {
this.properties.push(new MaxRightTabStop(leader));
return this;
}
public leftTabStop(position: number, leader?: LeaderType): Paragraph {
this.properties.push(new LeftTabStop(position, leader));
return this;
}
public rightTabStop(position: number, leader?: LeaderType): Paragraph {
this.properties.push(new RightTabStop(position, leader));
return this;
}
public centerTabStop(position: number, leader?: LeaderType): Paragraph {
this.properties.push(new CenterTabStop(position, leader));
return this;
}
public bullet(indentLevel: number = 0): Paragraph {
this.properties.push(new Style("ListParagraph"));
this.properties.push(new NumberProperties(1, indentLevel));
return this;
}
public setNumbering(numbering: Num, indentLevel: number): Paragraph {
this.properties.push(new Style("ListParagraph"));
this.properties.push(new NumberProperties(numbering.id, indentLevel));
return this;
}
public setCustomNumbering(numberId: number, indentLevel: number): Paragraph {
this.properties.push(new NumberProperties(numberId, indentLevel));
return this;
}
public style(styleId: string): Paragraph {
this.properties.push(new Style(styleId));
return this;
}
public indent(attrs: IIndentAttributesProperties): Paragraph {
this.properties.push(new Indent(attrs));
return this;
}
public spacing(params: ISpacingProperties): Paragraph {
this.properties.push(new Spacing(params));
return this;
}
public contextualSpacing(value: boolean): Paragraph {
this.properties.push(new ContextualSpacing(value));
return this;
}
public keepNext(): Paragraph {
this.properties.push(new KeepNext());
return this;
}
public keepLines(): Paragraph {
this.properties.push(new KeepLines());
return this;
}
public referenceFootnote(id: number): Paragraph {
this.root.push(new FootnoteReferenceRun(id));
return this;
@ -236,18 +205,8 @@ export class Paragraph extends XmlComponent {
return this;
}
public bidirectional(): Paragraph {
this.properties.push(new Bidirectional());
return this;
}
public addSequentialIdentifier(identifier: string): Paragraph {
this.root.push(new SequentialIdentifier(identifier));
return this;
}
public outlineLevel(level: string): Paragraph {
this.properties.push(new OutlineLevel(level));
return this;
}
}