Files
docx-js/src/file/paragraph/paragraph.ts

171 lines
5.5 KiB
TypeScript
Raw Normal View History

// http://officeopenxml.com/WPparagraph.php
2018-06-25 19:49:46 +01:00
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { Num } from "file/numbering/num";
2017-12-29 02:26:26 +00:00
import { XmlComponent } from "file/xml-components";
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";
import { KeepLines, KeepNext } from "./formatting/keep";
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-10-09 20:56:31 +01:00
import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list";
2019-01-15 15:39:48 +01:00
import { Bookmark, Hyperlink, OutlineLevel } from "./links";
import { ParagraphProperties } from "./properties";
import { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run";
2019-06-12 01:03:36 +01:00
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;
2019-10-09 20:56:31 +01:00
readonly tabStops?: Array<{
readonly position: number | TabStopPosition;
readonly type: TabStopType;
readonly leader?: LeaderType;
}>;
2019-06-12 01:03:36 +01:00
readonly style?: string;
readonly bullet?: {
readonly level: number;
};
readonly numbering?: {
readonly num: Num;
readonly level: number;
readonly custom?: boolean;
};
2019-10-10 01:19:55 +01:00
readonly children?: Array<TextRun | PictureRun | Hyperlink | SymbolRun | Bookmark | PageBreak | SequentialIdentifier>;
2019-06-12 01:03:36 +01:00
}
export class Paragraph extends XmlComponent {
2018-08-07 01:38:15 +01:00
private readonly properties: ParagraphProperties;
constructor(options: string | PictureRun | IParagraphOptions) {
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;
}
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,
});
2019-06-12 01:03:36 +01:00
this.root.push(this.properties);
2019-06-12 01:03:36 +01:00
if (options.text) {
this.root.push(new TextRun(options.text));
}
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));
}
2019-06-12 01:03:36 +01:00
if (options.alignment) {
this.properties.push(new Alignment(options.alignment));
}
2019-06-12 01:03:36 +01:00
if (options.heading) {
this.properties.push(new Style(options.heading));
}
2019-06-12 01:03:36 +01:00
if (options.bidirectional) {
this.properties.push(new Bidirectional());
}
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));
}
2019-06-12 01:03:36 +01:00
if (options.keepLines) {
this.properties.push(new KeepLines());
}
2019-06-12 01:03:36 +01:00
if (options.keepNext) {
this.properties.push(new KeepNext());
}
2019-10-09 20:56:31 +01:00
if (options.tabStops) {
for (const tabStop of options.tabStops) {
this.properties.push(new TabStop(tabStop.type, tabStop.position, tabStop.leader));
2019-06-12 01:03:36 +01:00
}
}
2019-06-12 01:03:36 +01:00
if (options.style) {
this.properties.push(new Style(options.style));
}
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));
}
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));
}
if (options.children) {
for (const child of options.children) {
if (child instanceof Bookmark) {
this.root.push(child.start);
this.root.push(child.text);
this.root.push(child.end);
continue;
}
this.root.push(child);
}
}
}
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;
}
}