Skip to content

Commit 9927424

Browse files
committed
feature(pres): apply presentation modifiers
1 parent 4841d3a commit 9927424

File tree

4 files changed

+79
-21
lines changed

4 files changed

+79
-21
lines changed

src/automizer.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { IPresentationProps } from './interfaces/ipresentation-props';
1010
import { PresTemplate } from './interfaces/pres-template';
1111
import { RootPresTemplate } from './interfaces/root-pres-template';
1212
import { Template } from './classes/template';
13-
import { TemplateInfo } from './types/xml-types';
13+
import { ModifyPresentationCallback, TemplateInfo } from './types/xml-types';
1414
import { vd } from './helper/general-helper';
1515
import { Master } from './classes/master';
1616
import path from 'path';
1717
import * as fs from 'fs';
18+
import { XmlHelper } from './helper/xml-helper';
1819

1920
/**
2021
* Automizer
@@ -40,12 +41,15 @@ export default class Automizer implements IPresentationProps {
4041
params: AutomizerParams;
4142
status: StatusTracker;
4243

44+
modifyPresentation: ModifyPresentationCallback[];
45+
4346
/**
4447
* Creates an instance of `pptx-automizer`.
4548
* @param [params]
4649
*/
4750
constructor(params: AutomizerParams) {
4851
this.templates = [];
52+
this.modifyPresentation = [];
4953
this.params = params;
5054

5155
this.templateDir = params?.templateDir ? params.templateDir + '/' : '';
@@ -173,6 +177,11 @@ export default class Automizer implements IPresentationProps {
173177
return templateCreationId;
174178
}
175179

180+
public async modify(cb: ModifyPresentationCallback): Promise<this> {
181+
this.modifyPresentation.push(cb);
182+
return this;
183+
}
184+
176185
/**
177186
* Determines whether template is root or default template.
178187
* @param template
@@ -262,8 +271,40 @@ export default class Automizer implements IPresentationProps {
262271
* @returns summary object.
263272
*/
264273
public async write(location: string): Promise<AutomizerSummary> {
274+
await this.writeSlides();
275+
await this.applyModifyPresentationCallbacks();
276+
265277
const rootArchive = await this.rootTemplate.archive;
278+
const content = await rootArchive.generateAsync({ type: 'nodebuffer' });
279+
280+
return FileHelper.writeOutputFile(
281+
this.getLocation(location, 'output'),
282+
content,
283+
this,
284+
);
285+
}
286+
287+
async applyModifyPresentationCallbacks() {
288+
const presentationXml = await XmlHelper.getXmlFromArchive(
289+
await this.rootTemplate.archive,
290+
`ppt/presentation.xml`,
291+
);
266292

293+
for (const cb of this.modifyPresentation) {
294+
cb(presentationXml);
295+
}
296+
297+
await XmlHelper.writeXmlToArchive(
298+
await this.rootTemplate.archive,
299+
`ppt/presentation.xml`,
300+
presentationXml,
301+
);
302+
}
303+
304+
/**
305+
* Write all slides into archive.
306+
*/
307+
public async writeSlides(): Promise<void> {
267308
await this.rootTemplate.countExistingSlides();
268309
this.status.max = this.rootTemplate.slides.length;
269310

@@ -274,14 +315,6 @@ export default class Automizer implements IPresentationProps {
274315
if (this.params.removeExistingSlides) {
275316
await this.rootTemplate.truncate();
276317
}
277-
278-
const content = await rootArchive.generateAsync({ type: 'nodebuffer' });
279-
280-
return FileHelper.writeOutputFile(
281-
this.getLocation(location, 'output'),
282-
content,
283-
this,
284-
);
285318
}
286319

287320
/**

src/dev.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import Automizer, { ChartData, modify, TableRow, TableRowStyle } from './index';
1+
import Automizer, {
2+
ChartData,
3+
modify,
4+
TableRow,
5+
TableRowStyle,
6+
XmlHelper,
7+
} from './index';
28
import { vd } from './helper/general-helper';
39

410
const automizer = new Automizer({
@@ -8,21 +14,37 @@ const automizer = new Automizer({
814
});
915

1016
const run = async () => {
11-
const pres = automizer
17+
const ppt = automizer
1218
.loadRoot(`RootTemplate.pptx`)
1319
.load(`SlideWithCharts.pptx`, 'charts')
1420
.load(`SlideWithImages.pptx`, 'images');
1521

16-
const result = await pres
17-
.addSlide('charts', 2, (slide) => {
18-
slide.removeElement('ColumnChart');
19-
})
20-
.addSlide('images', 2, (slide) => {
21-
slide.removeElement('imageJPG');
22-
slide.removeElement('Textfeld 5');
23-
slide.addElement('images', 2, 'imageJPG');
24-
})
25-
.write(`remove-element.test.pptx`);
22+
ppt.addSlide('charts', 1);
23+
ppt.addSlide('images', 1);
24+
ppt.addSlide('images', 2);
25+
26+
ppt.modify((xml: XMLDocument) => {
27+
// Dump before to console
28+
XmlHelper.dump(xml);
29+
30+
const sldIdLst = xml.getElementsByTagName('p:sldIdLst')[0];
31+
const existingSlides = sldIdLst.getElementsByTagName('p:sldId');
32+
33+
// reordering logic here
34+
const order = [3, 2, 1];
35+
let id = 256;
36+
order.forEach((sourceSlideNumber) => {
37+
const slide = existingSlides[sourceSlideNumber - 1];
38+
slide.setAttribute('id', String(id));
39+
sldIdLst.appendChild(slide);
40+
id++;
41+
});
42+
43+
// Dump after to console
44+
XmlHelper.dump(sldIdLst);
45+
});
46+
47+
const summary = await ppt.write('reorder.pptx');
2648
};
2749

2850
run().catch((error) => {

src/interfaces/itemplate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export interface ITemplate {
44
location: string;
55
file: InputType;
66
archive: Promise<JSZip>;
7+
getSlideIdList: () => Promise<Document>;
78
}

src/types/xml-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ export type ElementInfo = {
6262
cy: number;
6363
};
6464
};
65+
66+
export type ModifyPresentationCallback = (xml: XMLDocument) => void;

0 commit comments

Comments
 (0)