2017-09-19 15:21:07 +01:00
|
|
|
// http://officeopenxml.com/WPparagraph.php
|
2021-02-27 19:23:29 +00:00
|
|
|
import * as shortid from "shortid";
|
|
|
|
|
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";
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2020-10-10 13:41:26 +01:00
|
|
|
import { File } from "../file";
|
2021-02-27 19:23:29 +00:00
|
|
|
import { TargetModeType } from "../relationships/relationship/relationship";
|
2020-10-10 13:41:26 +01:00
|
|
|
import { DeletedTextRun, InsertedTextRun } from "../track-revision";
|
2020-07-11 17:01:32 +08:00
|
|
|
import { PageBreak } from "./formatting/page-break";
|
2021-02-27 19:23:29 +00:00
|
|
|
import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links";
|
2019-08-15 00:47:55 +01:00
|
|
|
import { Math } from "./math";
|
2020-07-11 17:01:32 +08:00
|
|
|
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
|
2019-10-01 12:29:07 -05:00
|
|
|
import { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run";
|
2019-06-12 01:03:36 +01:00
|
|
|
|
2021-02-27 19:23:29 +00:00
|
|
|
export type ParagraphChild =
|
|
|
|
| TextRun
|
|
|
|
| PictureRun
|
|
|
|
| SymbolRun
|
|
|
|
| Bookmark
|
|
|
|
| PageBreak
|
|
|
|
| SequentialIdentifier
|
|
|
|
| FootnoteReferenceRun
|
|
|
|
| InternalHyperlink
|
|
|
|
| ExternalHyperlink
|
|
|
|
| InsertedTextRun
|
|
|
|
| DeletedTextRun
|
|
|
|
| Math;
|
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
export interface IParagraphOptions extends IParagraphPropertiesOptions {
|
2019-06-12 01:03:36 +01:00
|
|
|
readonly text?: string;
|
2021-02-27 19:23:29 +00:00
|
|
|
readonly children?: ParagraphChild[];
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
this.properties = new ParagraphProperties(options);
|
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-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
|
|
|
}
|
|
|
|
|
2019-12-18 21:11:15 +00:00
|
|
|
public prepForXml(file: File): IXmlableObject | undefined {
|
|
|
|
for (const element of this.root) {
|
2021-02-27 19:23:29 +00:00
|
|
|
if (element instanceof ExternalHyperlink) {
|
2019-12-18 21:11:15 +00:00
|
|
|
const index = this.root.indexOf(element);
|
2021-02-27 19:23:29 +00:00
|
|
|
const concreteHyperlink = new ConcreteHyperlink(element.options.child, shortid.generate().toLowerCase());
|
|
|
|
file.DocumentRelationships.createRelationship(
|
|
|
|
concreteHyperlink.linkId,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
|
|
|
element.options.link,
|
|
|
|
TargetModeType.EXTERNAL,
|
|
|
|
);
|
|
|
|
this.root[index] = concreteHyperlink;
|
2019-12-18 21:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
}
|