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

103 lines
3.4 KiB
TypeScript
Raw Normal View History

// http://officeopenxml.com/WPparagraph.php
import { FootnoteReferenceRun } from "@file/footnotes";
2023-01-23 14:14:05 +00:00
import { IContext, IXmlableObject } from "@file/xml-components";
import { uniqueId } from "@util/convenience-functions";
2023-01-23 14:14:05 +00:00
import { FileChild } from "@file/file-child";
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";
import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links";
2019-08-15 00:47:55 +01:00
import { Math } from "./math";
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
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";
export type ParagraphChild =
| TextRun
| ImageRun
| SymbolRun
| Bookmark
| PageBreak
| ColumnBreak
| SequentialIdentifier
| FootnoteReferenceRun
| InternalHyperlink
| ExternalHyperlink
| InsertedTextRun
| DeletedTextRun
| Math
| SimpleField
| SimpleMailMergeField
| Comments
| Comment
| CommentRangeStart
| CommentRangeEnd
| CommentReference;
export interface IParagraphOptions extends IParagraphPropertiesOptions {
2019-06-12 01:03:36 +01:00
readonly text?: string;
readonly children?: readonly ParagraphChild[];
2019-06-12 01:03:36 +01:00
}
2023-01-23 14:14:05 +00:00
export class Paragraph extends FileChild {
2018-08-07 01:38:15 +01:00
private readonly properties: ParagraphProperties;
2022-08-31 07:52:27 +01:00
public constructor(options: string | 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 this;
2019-06-12 01:03:36 +01:00
}
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);
for (const textRun of child.children) {
this.root.push(textRun);
}
this.root.push(child.end);
continue;
}
this.root.push(child);
}
}
}
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) {
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(
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;
}
}