Skip to content

Commit f1d78f5

Browse files
committed
fix(text): apply alignment to paragraph props
1 parent 1e6dfd5 commit f1d78f5

File tree

5 files changed

+97
-285
lines changed

5 files changed

+97
-285
lines changed

__tests__/replace-multi-text.test.ts

Lines changed: 90 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Automizer, { modify } from '../src/index';
22

3-
43
test('create presentation, replace multi text.', async () => {
54
const automizer = new Automizer({
65
templateDir: `${__dirname}/pptx-templates`,
@@ -11,84 +10,102 @@ test('create presentation, replace multi text.', async () => {
1110

1211
await pres
1312
.addSlide('TextReplace.pptx', 1, (slide) => {
14-
slide.modifyElement('setText', modify.setMultiText([
15-
{
16-
paragraph: {
17-
bullet: true,
18-
level: 0,
19-
marginLeft: 41338,
20-
indent: -87325,
21-
alignment: 'left'
22-
},
23-
textRuns: [
24-
{
25-
text: 'test 0',
26-
style: {
27-
color: {
28-
type: 'srgbClr',
29-
value: 'CCCCCC'
30-
}
31-
}
13+
slide.modifyElement(
14+
'setText',
15+
modify.setMultiText([
16+
{
17+
paragraph: {
18+
bullet: true,
19+
level: 0,
20+
marginLeft: 41338,
21+
indent: -87325,
22+
alignment: 'l',
3223
},
33-
]
34-
},
35-
{
36-
paragraph: {
37-
bullet: true,
38-
level: 1,
39-
marginLeft: 541338,
40-
indent: -187325,
41-
alignment: 'left'
24+
textRuns: [
25+
{
26+
text: 'test 0',
27+
style: {
28+
color: {
29+
type: 'srgbClr',
30+
value: 'CCCCCC',
31+
},
32+
},
33+
},
34+
],
4235
},
43-
textRuns: [
44-
{
45-
text: 'test ',
46-
style: {
47-
color: {
48-
type: 'srgbClr',
49-
value: 'CCCCCC'
50-
}
51-
}
36+
{
37+
paragraph: {
38+
bullet: true,
39+
level: 1,
40+
marginLeft: 541338,
41+
indent: -187325,
5242
},
53-
{
54-
text: 'test 2',
55-
style: {
56-
size: 700,
57-
color: {
58-
type: 'srgbClr',
59-
value: 'FF0000'
60-
}
61-
}
43+
textRuns: [
44+
{
45+
text: 'test ',
46+
style: {
47+
color: {
48+
type: 'srgbClr',
49+
value: 'CCCCCC',
50+
},
51+
},
52+
},
53+
{
54+
text: 'test 2',
55+
style: {
56+
size: 700,
57+
color: {
58+
type: 'srgbClr',
59+
value: 'FF0000',
60+
},
61+
},
62+
},
63+
{
64+
text: 'test 3',
65+
style: {
66+
size: 1200,
67+
color: {
68+
type: 'srgbClr',
69+
value: '00FF00',
70+
},
71+
},
72+
},
73+
],
74+
},
75+
{
76+
paragraph: {
77+
alignment: 'r',
6278
},
63-
{
64-
text: 'test 3',
65-
style: {
66-
size: 1200,
67-
color: {
68-
type: 'srgbClr',
69-
value: '00FF00'
70-
}
71-
}
72-
}
73-
]
74-
},
75-
{
76-
paragraph: {
77-
alignment: 'right'
79+
textRuns: [
80+
{
81+
text: 'aligned Right',
82+
style: {
83+
color: {
84+
type: 'srgbClr',
85+
value: '00FF00',
86+
},
87+
},
88+
},
89+
],
7890
},
79-
textRuns: [
80-
{
81-
text: 'aligned Center',
82-
style: {
83-
color: {
84-
type: 'srgbClr',
85-
value: '00FF00'
86-
}
87-
}
91+
{
92+
paragraph: {
93+
alignment: 'ctr',
8894
},
89-
]
90-
},
91-
]));
95+
textRuns: [
96+
{
97+
text: 'aligned Center',
98+
style: {
99+
color: {
100+
type: 'srgbClr',
101+
value: '0000FF',
102+
},
103+
},
104+
},
105+
],
106+
},
107+
]),
108+
);
92109
})
93110
.write(`modify-multi-text.test.pptx`);
94111

src/dev.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const run = async () => {
4343
level: 1,
4444
marginLeft: 541338,
4545
indent: -187325,
46-
alignment: 'left'
4746
},
4847
textRuns: [
4948
{

src/helper/html-to-multitext-helper.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Color, TextStyle } from '../types/modify-types';
1+
import { TextStyle } from '../types/modify-types';
22
import { MultiTextParagraph } from '../interfaces/imulti-text';
33
import { DOMParser, Node } from '@xmldom/xmldom';
4-
import { vd } from './general-helper';
54

65
type TextRun = { text: string; style?: TextStyle };
76

@@ -54,7 +53,7 @@ export class HtmlToMultiTextHelper {
5453

5554
case 'ul':
5655
case 'ol':
57-
this.processList(node, level, paragraphs, bulletLevel);
56+
this.processList(node, paragraphs, bulletLevel);
5857
break;
5958

6059
case 'li':
@@ -88,7 +87,7 @@ export class HtmlToMultiTextHelper {
8887
paragraph: {
8988
level: 0,
9089
bullet: false,
91-
alignment: 'left',
90+
alignment: 'l',
9291
},
9392
textRuns,
9493
});
@@ -99,7 +98,6 @@ export class HtmlToMultiTextHelper {
9998
*/
10099
private processList(
101100
node: ChildNode,
102-
level: number,
103101
paragraphs: MultiTextParagraph[],
104102
bulletLevel: { value: number },
105103
): void {
@@ -152,7 +150,7 @@ export class HtmlToMultiTextHelper {
152150
paragraph: {
153151
level,
154152
bullet: true,
155-
alignment: 'left',
153+
alignment: 'l',
156154
},
157155
textRuns,
158156
});
@@ -187,7 +185,6 @@ export class HtmlToMultiTextHelper {
187185
return textRuns;
188186
}
189187

190-
191188
/**
192189
* Processes a text node and creates a TextRun
193190
*/
@@ -220,7 +217,7 @@ export class HtmlToMultiTextHelper {
220217
private applyElementStyles(element: Element, style: TextStyle): TextStyle {
221218
const newStyle = { ...style };
222219

223-
const tagName = element.tagName.toLowerCase()
220+
const tagName = element.tagName.toLowerCase();
224221

225222
// Handle styling based on element type
226223
if (tagName === 'strong' || tagName === 'b') {
@@ -257,7 +254,6 @@ export class HtmlToMultiTextHelper {
257254
}
258255
}
259256

260-
261257
/**
262258
* Processes an element with child nodes
263259
*/

0 commit comments

Comments
 (0)