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

220 lines
6.8 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";
2018-08-09 01:55:50 +01:00
import { Image } from "file/media";
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";
import { CenterTabStop, LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop } 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, TextRun } from "./run";
2019-06-12 01:03:36 +01:00
interface ITabStopOptions {
readonly position: number;
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 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;
};
readonly children?: Array<TextRun | PictureRun | Hyperlink>;
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-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));
}
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));
}
2019-06-12 01:03:36 +01:00
if (options.tabStop.maxRight) {
this.properties.push(new MaxRightTabStop(options.tabStop.maxRight.leader));
}
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));
}
}
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) {
this.root.push(child);
}
}
}
2019-06-12 01:03:36 +01:00
public addRun(run: Run): Paragraph {
this.root.push(run);
return this;
}
2019-06-12 01:03:36 +01:00
public addHyperLink(hyperlink: Hyperlink): Paragraph {
this.root.push(hyperlink);
return this;
}
2019-06-12 01:03:36 +01:00
public addBookmark(bookmark: Bookmark): Paragraph {
// Bookmarks by spec have three components, a start, text, and end
this.root.push(bookmark.start);
this.root.push(bookmark.text);
this.root.push(bookmark.end);
return this;
}
2019-06-12 01:03:36 +01:00
public addImage(image: Image): PictureRun {
const run = image.Run;
this.addRun(run);
2018-09-26 17:47:17 +03:00
2019-06-12 01:03:36 +01:00
return run;
}
2019-06-12 01:03:36 +01:00
public pageBreak(): Paragraph {
this.root.push(new PageBreak());
return this;
}
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
public addSequentialIdentifier(identifier: string): Paragraph {
this.root.push(new SequentialIdentifier(identifier));
return this;
}
}