Skip to content

Commit d0ec22b

Browse files
committed
refactor to utility functions
1 parent 05e019b commit d0ec22b

File tree

2 files changed

+95
-51
lines changed

2 files changed

+95
-51
lines changed

src/helper/modify-text-helper.ts

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ModifyColorHelper from './modify-color-helper';
33
import ModifyXmlHelper from './modify-xml-helper';
44
import { XmlElement } from '../types/xml-types';
55
import { vd } from './general-helper';
6+
import XmlElements from './xml-elements';
67

78
export default class ModifyTextHelper {
89
/**
@@ -35,57 +36,8 @@ export default class ModifyTextHelper {
3536

3637
static setBulletList =
3738
(list) => (element: XmlElement): void => {
38-
const namespaceURIs = {
39-
'a': 'http://schemas.openxmlformats.org/drawingml/2006/main',
40-
'p': 'http://schemas.openxmlformats.org/presentationml/2006/main'
41-
};
42-
const doc = element.ownerDocument;
43-
44-
let txBody = element.getElementsByTagName('p:txBody')[0];
45-
if (!txBody) {
46-
txBody = doc.createElementNS(namespaceURIs['p'], 'p:txBody');
47-
element.appendChild(txBody);
48-
} else {
49-
while (txBody.firstChild) {
50-
txBody.removeChild(txBody.firstChild);
51-
}
52-
}
53-
54-
const bodyPr = doc.createElementNS(namespaceURIs['a'], 'a:bodyPr');
55-
txBody.appendChild(bodyPr);
56-
const lstStyle = doc.createElementNS(namespaceURIs['a'], 'a:lstStyle');
57-
txBody.appendChild(lstStyle);
58-
59-
const processList = (items, level) => {
60-
items.forEach((item) => {
61-
if (Array.isArray(item)) {
62-
processList(item, level + 1);
63-
} else {
64-
const p = doc.createElementNS(namespaceURIs['a'], 'a:p');
65-
66-
const pPr = doc.createElementNS(namespaceURIs['a'], 'a:pPr');
67-
if (level > 0) {
68-
pPr.setAttribute('lvl', String(level));
69-
}
70-
p.appendChild(pPr);
71-
72-
const r = doc.createElementNS(namespaceURIs['a'], 'a:r');
73-
74-
const rPr = doc.createElementNS(namespaceURIs['a'], 'a:rPr');
75-
r.appendChild(rPr);
76-
77-
const t = doc.createElementNS(namespaceURIs['a'], 'a:t');
78-
const textNode = doc.createTextNode(String(item));
79-
t.appendChild(textNode);
80-
81-
r.appendChild(t);
82-
p.appendChild(r);
83-
txBody.appendChild(p);
84-
}
85-
});
86-
};
87-
88-
processList(list, 0);
39+
const xmlElements = new XmlElements(element);
40+
xmlElements.addBulletList(list);
8941
};
9042

9143
static content =

src/helper/xml-elements.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,98 @@ export default class XmlElements {
4242
return this;
4343
}
4444

45+
createTextBody(): XmlElement {
46+
let txBody = this.element.getElementsByTagName('p:txBody')[0];
47+
if (!txBody) {
48+
txBody = this.document.createElement('p:txBody');
49+
this.element.appendChild(txBody);
50+
} else {
51+
while (txBody.firstChild) {
52+
txBody.removeChild(txBody.firstChild);
53+
}
54+
}
55+
return txBody;
56+
}
57+
58+
// Method to create bodyPr element
59+
createBodyProperties(txBody: XmlElement): XmlElement {
60+
const bodyPr = this.document.createElement('a:bodyPr');
61+
txBody.appendChild(bodyPr);
62+
return bodyPr;
63+
}
64+
65+
// Method to create lstStyle element
66+
createListStyle(txBody: XmlElement): XmlElement {
67+
const lstStyle = this.document.createElement('a:lstStyle');
68+
69+
// Loop through levels (assuming 3 levels here)
70+
for (let level = 1; level <= 3; level++) {
71+
const lvlpPr = this.document.createElement(`a:lvl${level}pPr`);
72+
73+
// Set bullet font
74+
const buFont = this.document.createElement('a:buFont');
75+
buFont.setAttribute('typeface', 'Arial');
76+
lvlpPr.appendChild(buFont);
77+
78+
// Set bullet character (you can use different characters for each level)
79+
const buChar = this.document.createElement('a:buChar');
80+
buChar.setAttribute('char', '•');
81+
lvlpPr.appendChild(buChar);
82+
83+
lstStyle.appendChild(lvlpPr);
84+
}
85+
86+
txBody.appendChild(lstStyle);
87+
return lstStyle;
88+
}
89+
90+
// Method to process the bullet list
91+
addBulletList(list: []): void {
92+
const txBody = this.createTextBody();
93+
this.createBodyProperties(txBody);
94+
this.createListStyle(txBody);
95+
this.processList(txBody, list, 0);
96+
}
97+
98+
// Recursive method to create paragraphs and text runs for each list item
99+
processList(txBody: XmlElement, items: [], level: number): void {
100+
items.forEach((item) => {
101+
if (Array.isArray(item)) {
102+
this.processList(txBody, item, level + 1);
103+
} else {
104+
const p = this.createParagraph(level);
105+
const r = this.createTextRun(String(item));
106+
p.appendChild(r);
107+
txBody.appendChild(p);
108+
}
109+
});
110+
}
111+
112+
// Method to create a paragraph element
113+
createParagraph(level: number): XmlElement {
114+
const p = this.document.createElement('a:p');
115+
const pPr = this.document.createElement('a:pPr');
116+
if (level > 0) {
117+
pPr.setAttribute('lvl', String(level));
118+
}
119+
p.appendChild(pPr);
120+
return p;
121+
}
122+
123+
// Method to create a text run element
124+
createTextRun(text: string): XmlElement {
125+
const r = this.document.createElement('a:r');
126+
const rPr = this.document.createElement('a:rPr');
127+
r.appendChild(rPr);
128+
129+
const t = this.document.createElement('a:t');
130+
const textNode = this.document.createTextNode(text);
131+
t.appendChild(textNode);
132+
133+
r.appendChild(t);
134+
return r;
135+
}
136+
45137
paragraphProps() {
46138
const p = this.element.getElementsByTagName('a:p').item(0);
47139
p.appendChild(this.document.createElement('a:pPr'));

0 commit comments

Comments
 (0)