2017-09-19 15:21:07 +01:00
|
|
|
// http://officeopenxml.com/WPparagraph.php
|
2018-06-25 19:49:46 +01:00
|
|
|
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
|
2017-12-30 20:25:16 +00:00
|
|
|
import { Num } from "file/numbering/num";
|
2017-12-29 02:26:26 +00:00
|
|
|
import { XmlComponent } from "file/xml-components";
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
import { Alignment, AlignmentType } from "./formatting/alignment";
|
2018-08-07 02:47:24 +01:00
|
|
|
import { Bidirectional } from "./formatting/bidirectional";
|
2019-06-12 01:03:36 +01:00
|
|
|
import { IBorderOptions, ThematicBreak } from "./formatting/border";
|
2018-08-21 02:46:21 +01:00
|
|
|
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
|
2017-09-30 01:52:37 +01:00
|
|
|
import { KeepLines, KeepNext } from "./formatting/keep";
|
2018-05-10 19:43:58 +02:00
|
|
|
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
|
2018-09-26 17:47:17 +03:00
|
|
|
import { ContextualSpacing, ISpacingProperties, Spacing } from "./formatting/spacing";
|
2019-06-12 01:03:36 +01:00
|
|
|
import { HeadingLevel, Style } from "./formatting/style";
|
2019-09-30 22:56:21 +01:00
|
|
|
import { CenterTabStop, LeaderType, LeftTabStop, RightTabStop, TabStopPosition } from "./formatting/tab-stop";
|
2017-09-30 01:52:37 +01:00
|
|
|
import { NumberProperties } from "./formatting/unordered-list";
|
2019-01-15 15:39:48 +01:00
|
|
|
import { Bookmark, Hyperlink, OutlineLevel } from "./links";
|
2017-09-19 15:21:07 +01:00
|
|
|
import { ParagraphProperties } from "./properties";
|
2019-10-01 12:29:07 -05:00
|
|
|
import { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run";
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
interface ITabStopOptions {
|
2019-09-30 22:56:21 +01:00
|
|
|
readonly position: number | TabStopPosition;
|
2019-06-12 01:03:36 +01:00
|
|
|
readonly leader?: LeaderType;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IParagraphOptions {
|
|
|
|
readonly text?: string;
|
|
|
|
readonly border?: IBorderOptions;
|
|
|
|
readonly spacing?: ISpacingProperties;
|
|
|
|
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 center?: ITabStopOptions;
|
|
|
|
};
|
|
|
|
readonly style?: string;
|
|
|
|
readonly bullet?: {
|
|
|
|
readonly level: number;
|
|
|
|
};
|
|
|
|
readonly numbering?: {
|
|
|
|
readonly num: Num;
|
|
|
|
readonly level: number;
|
|
|
|
readonly custom?: boolean;
|
|
|
|
};
|
2019-10-03 20:48:34 +01:00
|
|
|
readonly children?: Array<TextRun | PictureRun | Hyperlink | SymbolRun | Bookmark | PageBreak>;
|
2019-06-12 01:03:36 +01:00
|
|
|
}
|
|
|
|
|
2017-09-19 15:21:07 +01:00
|
|
|
export class Paragraph extends XmlComponent {
|
2018-08-07 01:38:15 +01:00
|
|
|
private readonly properties: ParagraphProperties;
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-23 22:36:01 +01:00
|
|
|
constructor(options: string | PictureRun | IParagraphOptions) {
|
2017-09-19 15:21:07 +01:00
|
|
|
super("w:p");
|
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (typeof options === "string") {
|
|
|
|
this.properties = new ParagraphProperties({});
|
|
|
|
this.root.push(this.properties);
|
|
|
|
this.root.push(new TextRun(options));
|
|
|
|
return;
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-23 22:36:01 +01:00
|
|
|
if (options instanceof PictureRun) {
|
|
|
|
this.properties = new ParagraphProperties({});
|
|
|
|
this.root.push(this.properties);
|
|
|
|
this.root.push(options);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
this.properties = new ParagraphProperties({
|
|
|
|
border: options.border,
|
|
|
|
});
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
this.root.push(this.properties);
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.text) {
|
|
|
|
this.root.push(new TextRun(options.text));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.spacing) {
|
|
|
|
this.properties.push(new Spacing(options.spacing));
|
|
|
|
}
|
2018-04-02 12:55:43 +03:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.outlineLevel !== undefined) {
|
|
|
|
this.properties.push(new OutlineLevel(options.outlineLevel));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.alignment) {
|
|
|
|
this.properties.push(new Alignment(options.alignment));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.heading) {
|
|
|
|
this.properties.push(new Style(options.heading));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.bidirectional) {
|
|
|
|
this.properties.push(new Bidirectional());
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.thematicBreak) {
|
|
|
|
this.properties.push(new ThematicBreak());
|
|
|
|
}
|
2018-07-25 15:02:58 +03:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.pageBreakBefore) {
|
|
|
|
this.properties.push(new PageBreakBefore());
|
|
|
|
}
|
2018-07-25 15:02:58 +03:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.contextualSpacing) {
|
|
|
|
this.properties.push(new ContextualSpacing(options.contextualSpacing));
|
|
|
|
}
|
2018-07-25 15:02:58 +03:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.indent) {
|
|
|
|
this.properties.push(new Indent(options.indent));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.keepLines) {
|
|
|
|
this.properties.push(new KeepLines());
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.keepNext) {
|
|
|
|
this.properties.push(new KeepNext());
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.tabStop) {
|
|
|
|
if (options.tabStop.left) {
|
|
|
|
this.properties.push(new LeftTabStop(options.tabStop.left.position, options.tabStop.left.leader));
|
|
|
|
}
|
2018-05-10 19:43:58 +02:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.tabStop.right) {
|
|
|
|
this.properties.push(new RightTabStop(options.tabStop.right.position, options.tabStop.right.leader));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.tabStop.center) {
|
|
|
|
this.properties.push(new CenterTabStop(options.tabStop.center.position, options.tabStop.center.leader));
|
|
|
|
}
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.style) {
|
|
|
|
this.properties.push(new Style(options.style));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
if (options.bullet) {
|
|
|
|
this.properties.push(new Style("ListParagraph"));
|
|
|
|
this.properties.push(new NumberProperties(1, options.bullet.level));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
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));
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2019-06-23 22:36:01 +01:00
|
|
|
if (options.children) {
|
|
|
|
for (const child of options.children) {
|
2019-09-30 22:56:21 +01:00
|
|
|
if (child instanceof Bookmark) {
|
|
|
|
this.root.push(child.start);
|
|
|
|
this.root.push(child.text);
|
|
|
|
this.root.push(child.end);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:36:01 +01:00
|
|
|
this.root.push(child);
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 15:33:53 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 19:49:46 +01:00
|
|
|
public referenceFootnote(id: number): Paragraph {
|
|
|
|
this.root.push(new FootnoteReferenceRun(id));
|
|
|
|
return this;
|
|
|
|
}
|
2018-06-28 03:01:25 +01:00
|
|
|
|
|
|
|
public addRunToFront(run: Run): Paragraph {
|
|
|
|
this.root.splice(1, 0, run);
|
|
|
|
return this;
|
|
|
|
}
|
2018-07-22 17:03:45 +03:00
|
|
|
|
2018-10-19 16:50:51 -03:00
|
|
|
public addSequentialIdentifier(identifier: string): Paragraph {
|
|
|
|
this.root.push(new SequentialIdentifier(identifier));
|
|
|
|
return this;
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
}
|