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

88 lines
2.6 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";
2019-12-18 21:11:15 +00:00
import { IXmlableObject, XmlComponent } from "file/xml-components";
2020-10-10 13:41:26 +01:00
import { File } from "../file";
import { DeletedTextRun, InsertedTextRun } from "../track-revision";
import { PageBreak } from "./formatting/page-break";
import { Bookmark, HyperlinkRef } from "./links";
2019-08-15 00:47:55 +01:00
import { Math } from "./math";
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
import { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run";
2019-06-12 01:03:36 +01:00
export interface IParagraphOptions extends IParagraphPropertiesOptions {
2019-06-12 01:03:36 +01:00
readonly text?: string;
2020-08-01 17:58:16 +01:00
readonly children?: (
| TextRun
| PictureRun
| SymbolRun
| Bookmark
| PageBreak
| SequentialIdentifier
| FootnoteReferenceRun
| HyperlinkRef
| InsertedTextRun
| DeletedTextRun
2020-10-08 10:25:45 +01:00
| Math
2020-08-01 17:58:16 +01:00
)[];
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;
}
this.properties = new ParagraphProperties(options);
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));
}
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);
}
}
}
2019-12-18 21:11:15 +00:00
public prepForXml(file: File): IXmlableObject | undefined {
for (const element of this.root) {
if (element instanceof HyperlinkRef) {
const index = this.root.indexOf(element);
this.root[index] = file.HyperlinkCache[element.id];
}
}
return super.prepForXml();
2018-06-25 19:49:46 +01:00
}
2018-06-28 03:01:25 +01:00
public addRunToFront(run: Run): Paragraph {
this.root.splice(1, 0, run);
return this;
}
}