/
paragraph.ts
104 lines (90 loc) · 3.45 KB
/
paragraph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// http://officeopenxml.com/WPparagraph.php
import { FootnoteReferenceRun } from "@file/footnotes";
import { IContext, IXmlableObject } from "@file/xml-components";
import { uniqueId } from "@util/convenience-functions";
import { FileChild } from "@file/file-child";
import { TargetModeType } from "../relationships/relationship/relationship";
import { DeletedTextRun, InsertedTextRun } from "../track-revision";
import { CheckBox } from "../checkbox";
import { ColumnBreak, PageBreak } from "./formatting/break";
import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links";
import { Math } from "./math";
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
import { ImageRun, Run, SequentialIdentifier, SimpleField, SimpleMailMergeField, SymbolRun, TextRun } from "./run";
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
| CheckBox;
export interface IParagraphOptions extends IParagraphPropertiesOptions {
readonly text?: string;
readonly children?: readonly ParagraphChild[];
}
export class Paragraph extends FileChild {
private readonly properties: ParagraphProperties;
public constructor(options: string | IParagraphOptions) {
super("w:p");
if (typeof options === "string") {
this.properties = new ParagraphProperties({});
this.root.push(this.properties);
this.root.push(new TextRun(options));
return this;
}
this.properties = new ParagraphProperties(options);
this.root.push(this.properties);
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);
}
}
}
public prepForXml(context: IContext): IXmlableObject | undefined {
for (const element of this.root) {
if (element instanceof ExternalHyperlink) {
const index = this.root.indexOf(element);
const concreteHyperlink = new ConcreteHyperlink(element.options.children, uniqueId());
context.viewWrapper.Relationships.createRelationship(
concreteHyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
element.options.link,
TargetModeType.EXTERNAL,
);
this.root[index] = concreteHyperlink;
}
}
return super.prepForXml(context);
}
public addRunToFront(run: Run): Paragraph {
this.root.splice(1, 0, run);
return this;
}
}