forked from VolodymyrBaydalka/docxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparagraph.ts
136 lines (107 loc) · 3.71 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { OpenXmlElement } from "./dom";
import { CommonProperties, Length, ns, parseCommonProperty } from "./common";
import { Borders } from "./border";
import { parseSectionProperties, SectionProperties } from "./section";
import { LineSpacing, parseLineSpacing } from "./line-spacing";
import { XmlParser } from "../parser/xml-parser";
import { parseRunProperties, RunProperties } from "./run";
export interface WmlParagraph extends OpenXmlElement, ParagraphProperties {
}
export interface ParagraphProperties extends CommonProperties {
sectionProps: SectionProperties;
tabs: ParagraphTab[];
numbering: ParagraphNumbering;
border: Borders;
textAlignment: "auto" | "baseline" | "bottom" | "center" | "top" | string;
lineSpacing: LineSpacing;
keepLines: boolean;
keepNext: boolean;
pageBreakBefore: boolean;
outlineLevel: number;
styleName?: string;
runProps: RunProperties;
}
export interface ParagraphTab {
style: "bar" | "center" | "clear" | "decimal" | "end" | "num" | "start" | "left" | "right";
leader: "none" | "dot" | "heavy" | "hyphen" | "middleDot" | "underscore";
position: Length;
}
export interface ParagraphNumbering {
id: string;
level: number;
}
export function parseParagraphProperties(elem: Element, xml: XmlParser): ParagraphProperties {
let result = <ParagraphProperties>{};
for(let el of xml.elements(elem)) {
parseParagraphProperty(el, result, xml);
}
return result;
}
export function parseParagraphProperty(elem: Element, props: ParagraphProperties, xml: XmlParser) {
if (elem.namespaceURI != ns.wordml)
return false;
if(parseCommonProperty(elem, props, xml))
return true;
switch (elem.localName) {
case "tabs":
props.tabs = parseTabs(elem, xml);
break;
case "sectPr":
props.sectionProps = parseSectionProperties(elem, xml);
break;
case "numPr":
props.numbering = parseNumbering(elem, xml);
break;
case "spacing":
props.lineSpacing = parseLineSpacing(elem, xml);
return false; // TODO
break;
case "textAlignment":
props.textAlignment = xml.attr(elem, "val");
return false; //TODO
break;
case "keepLines":
props.keepLines = xml.boolAttr(elem, "val", true);
break;
case "keepNext":
props.keepNext = xml.boolAttr(elem, "val", true);
break;
case "pageBreakBefore":
props.pageBreakBefore = xml.boolAttr(elem, "val", true);
break;
case "outlineLvl":
props.outlineLevel = xml.intAttr(elem, "val");
break;
case "pStyle":
props.styleName = xml.attr(elem, "val");
break;
case "rPr":
props.runProps = parseRunProperties(elem, xml);
break;
default:
return false;
}
return true;
}
export function parseTabs(elem: Element, xml: XmlParser): ParagraphTab[] {
return xml.elements(elem, "tab")
.map(e => <ParagraphTab>{
position: xml.lengthAttr(e, "pos"),
leader: xml.attr(e, "leader"),
style: xml.attr(e, "val")
});
}
export function parseNumbering(elem: Element, xml: XmlParser): ParagraphNumbering {
var result = <ParagraphNumbering>{};
for (let e of xml.elements(elem)) {
switch (e.localName) {
case "numId":
result.id = xml.attr(e, "val");
break;
case "ilvl":
result.level = xml.intAttr(e, "val");
break;
}
}
return result;
}