@@ -15,6 +15,8 @@ export default class XmlElements {
1515 document : XmlDocument ;
1616 params : XmlElementParams ;
1717 defaultValues : Record < string , string > ;
18+ paragraphTemplate : XmlElement ;
19+ runTemplate : XmlElement ;
1820
1921 constructor ( element : XmlDocument | XmlElement , params ?: XmlElementParams ) {
2022 this . element = element ;
@@ -47,9 +49,44 @@ export default class XmlElements {
4749 if ( ! txBody ) {
4850 txBody = this . document . createElement ( 'p:txBody' ) ;
4951 this . element . appendChild ( txBody ) ;
52+
53+ const bodyPr = this . document . createElement ( 'a:bodyPr' ) ;
54+ txBody . appendChild ( bodyPr ) ;
55+
56+ const lstStyle = this . document . createElement ( 'a:lstStyle' ) ;
57+ txBody . appendChild ( lstStyle ) ;
58+
59+ this . paragraphTemplate = this . document . createElement ( 'a:p' ) ;
60+ txBody . appendChild ( this . paragraphTemplate ) ;
61+
62+ this . runTemplate = this . document . createElement ( 'a:r' ) ;
63+ const rPr = this . document . createElement ( 'a:rPr' ) ;
64+ this . runTemplate . appendChild ( rPr ) ;
5065 } else {
51- while ( txBody . firstChild ) {
52- txBody . removeChild ( txBody . firstChild ) ;
66+ let bodyPr = txBody . getElementsByTagName ( 'a:bodyPr' ) [ 0 ] ;
67+ if ( ! bodyPr ) {
68+ bodyPr = this . document . createElement ( 'a:bodyPr' ) ;
69+ txBody . insertBefore ( bodyPr , txBody . firstChild ) ;
70+ }
71+
72+ let lstStyle = txBody . getElementsByTagName ( 'a:lstStyle' ) [ 0 ] ;
73+ if ( ! lstStyle ) {
74+ lstStyle = this . document . createElement ( 'a:lstStyle' ) ;
75+ txBody . insertBefore ( lstStyle , bodyPr . nextSibling ) ;
76+ }
77+
78+ const paragraphs = txBody . getElementsByTagName ( 'a:p' ) ;
79+ this . paragraphTemplate = paragraphs [ 0 ] ;
80+ XmlHelper . sliceCollection ( paragraphs , 0 ) ;
81+
82+
83+ const runs = this . paragraphTemplate . getElementsByTagName ( 'a:r' ) ;
84+ if ( runs . length > 0 ) {
85+ this . runTemplate = runs [ 0 ] ;
86+ } else {
87+ this . runTemplate = this . document . createElement ( 'a:r' ) ;
88+ const rPr = this . document . createElement ( 'a:rPr' ) ;
89+ this . runTemplate . appendChild ( rPr ) ;
5390 }
5491 }
5592 return txBody ;
@@ -62,9 +99,11 @@ export default class XmlElements {
6299 }
63100
64101 addBulletList ( list : [ ] ) : void {
102+ XmlHelper . dump ( this . element ) ;
65103 const txBody = this . createTextBody ( ) ;
66104 this . createBodyProperties ( txBody ) ;
67105 this . processList ( txBody , list , 0 ) ;
106+ XmlHelper . dump ( this . element ) ;
68107 }
69108
70109 processList ( txBody : XmlElement , items : [ ] , level : number ) : void {
@@ -81,24 +120,39 @@ export default class XmlElements {
81120 }
82121
83122 createParagraph ( level : number ) : XmlElement {
84- const p = this . document . createElement ( 'a:p' ) ;
85- const pPr = this . document . createElement ( 'a:pPr' ) ;
86- if ( level > 0 ) {
87- pPr . setAttribute ( 'lvl' , String ( level ) ) ;
123+ const p = this . paragraphTemplate . cloneNode ( true ) as XmlElement ;
124+ const pPr = p . getElementsByTagName ( 'a:pPr' ) [ 0 ] ;
125+ if ( pPr ) {
126+ if ( level > 0 ) {
127+ pPr . setAttribute ( 'lvl' , String ( level ) ) ;
128+ pPr . removeAttribute ( 'indent' ) ;
129+ pPr . removeAttribute ( 'marL' ) ;
130+ } else {
131+ pPr . removeAttribute ( 'lvl' ) ;
132+ }
133+ } else {
134+ const newPPr = this . document . createElement ( 'a:pPr' ) ;
135+ if ( level > 0 ) {
136+ newPPr . setAttribute ( 'lvl' , String ( level ) ) ;
137+ }
138+ p . insertBefore ( newPPr , p . firstChild ) ;
88139 }
89- p . appendChild ( pPr ) ;
140+ const runs = p . getElementsByTagName ( 'a:r' ) ;
141+ XmlHelper . sliceCollection ( runs , 0 ) ;
90142 return p ;
91143 }
92144
93145 createTextRun ( text : string ) : XmlElement {
94- const r = this . document . createElement ( 'a:r' ) ;
95- const rPr = this . document . createElement ( 'a:rPr' ) ;
96- r . appendChild ( rPr ) ;
97-
98- const t = this . document . createElement ( 'a:t' ) ;
99- t . textContent = String ( text ) ;
146+ const r = this . runTemplate . cloneNode ( true ) as XmlElement ;
147+ const t = r . getElementsByTagName ( 'a:t' ) [ 0 ] ;
148+ if ( t ) {
149+ t . textContent = text ;
150+ } else {
151+ const newT = this . document . createElement ( 'a:t' ) ;
152+ newT . textContent = text ;
153+ r . appendChild ( newT ) ;
154+ }
100155
101- r . appendChild ( t ) ;
102156 return r ;
103157 }
104158
0 commit comments