2017-09-19 15:21:07 +01:00
|
|
|
// http://officeopenxml.com/WPparagraph.php
|
2022-06-26 23:26:42 +01:00
|
|
|
import { FootnoteReferenceRun } from "@file/footnotes";
|
|
|
|
import { IContext, IXmlableObject, XmlComponent } from "@file/xml-components";
|
|
|
|
import { uniqueId } from "@util/convenience-functions";
|
2017-09-19 15:21:07 +01:00
|
|
|
|
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";
|
2021-07-08 17:46:13 +10:00
|
|
|
import { ColumnBreak, PageBreak } from "./formatting/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";
|
2021-05-04 23:38:25 +02:00
|
|
|
import { ImageRun, Run, SequentialIdentifier, SimpleField, SimpleMailMergeField, SymbolRun, TextRun } from "./run";
|
2022-06-19 00:31:36 +01:00
|
|
|
import { Comment, CommentRangeEnd, CommentRangeStart, CommentReference, Comments } from "./run/comment-run";
|
2017-09-19 15:21:07 +01:00
|
|
|
|
2021-02-27 19:23:29 +00:00
|
|
|
export type ParagraphChild =
|
|
|
|
| TextRun
|
2021-03-18 02:48:37 +00:00
|
|
|
| ImageRun
|
2021-02-27 19:23:29 +00:00
|
|
|
| SymbolRun
|
|
|
|
| Bookmark
|
|
|
|
| PageBreak
|
2021-07-08 17:39:21 +10:00
|
|
|
| ColumnBreak
|
2021-02-27 19:23:29 +00:00
|
|
|
| SequentialIdentifier
|
|
|
|
| FootnoteReferenceRun
|
|
|
|
| InternalHyperlink
|
|
|
|
| ExternalHyperlink
|
|
|
|
| InsertedTextRun
|
|
|
|
| DeletedTextRun
|
2021-05-04 23:38:25 +02:00
|
|
|
| Math
|
|
|
|
| SimpleField
|
2022-03-03 09:59:09 +08:00
|
|
|
| SimpleMailMergeField
|
|
|
|
| Comments
|
|
|
|
| Comment
|
|
|
|
| CommentRangeStart
|
|
|
|
| CommentRangeEnd
|
|
|
|
| CommentReference;
|
2021-02-27 19:23:29 +00:00
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
export interface IParagraphOptions extends IParagraphPropertiesOptions {
|
2019-06-12 01:03:36 +01:00
|
|
|
readonly text?: string;
|
2022-09-15 20:00:50 +01:00
|
|
|
readonly children?: readonly 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
|
|
|
|
2022-08-31 07:52:27 +01:00
|
|
|
public constructor(options: string | 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));
|
2021-05-20 01:06:07 +03:00
|
|
|
return this;
|
2019-06-12 01:03:36 +01:00
|
|
|
}
|
2017-09-19 15:21:07 +01:00
|
|
|
|
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);
|
2020-01-01 22:54:42 +00:00
|
|
|
for (const textRun of child.children) {
|
|
|
|
this.root.push(textRun);
|
|
|
|
}
|
2019-09-30 22:56:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-11 01:06:55 +00:00
|
|
|
public prepForXml(context: IContext): IXmlableObject | undefined {
|
2019-12-18 21:11:15 +00:00
|
|
|
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-08-27 16:53:11 -06:00
|
|
|
const concreteHyperlink = new ConcreteHyperlink(element.options.children, uniqueId());
|
2021-03-11 01:06:55 +00:00
|
|
|
context.viewWrapper.Relationships.createRelationship(
|
2021-02-27 19:23:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 01:06:55 +00:00
|
|
|
return super.prepForXml(context);
|
2019-12-18 21:11:15 +00: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
|
|
|
}
|