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

101 lines
3.3 KiB
TypeScript
Raw Normal View History

// http://officeopenxml.com/WPparagraph.php
2021-03-12 03:58:05 +00:00
import { uniqueId } from "convenience-functions";
2018-06-25 19:49:46 +01:00
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
2021-03-11 01:06:55 +00:00
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { TargetModeType } from "../relationships/relationship/relationship";
2020-10-10 13:41:26 +01:00
import { DeletedTextRun, InsertedTextRun } from "../track-revision";
import { PageBreak } from "./formatting/page-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 { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run";
export type ParagraphChild =
| TextRun
| PictureRun
| SymbolRun
| Bookmark
| PageBreak
| SequentialIdentifier
| FootnoteReferenceRun
| InternalHyperlink
| ExternalHyperlink
| InsertedTextRun
| DeletedTextRun
| Math;
export interface IParagraphOptions extends IParagraphPropertiesOptions {
2019-06-12 01:03:36 +01:00
readonly text?: string;
readonly children?: ParagraphChild[];
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);
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-03-12 03:58:05 +00:00
const concreteHyperlink = new ConcreteHyperlink(element.options.child, 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;
}
}