Skip to content

Commit b65a64a

Browse files
authored
Merge pull request #53 from singerla/feature-import-master
Feature import master
2 parents aff55de + b489d38 commit b65a64a

27 files changed

+1777
-272
lines changed

README.md

Lines changed: 173 additions & 88 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Automizer from '../src/automizer';
2+
import { ModifyTextHelper } from '../src';
3+
4+
test('Auto-import source slideLayout and -master', async () => {
5+
const automizer = new Automizer({
6+
templateDir: `${__dirname}/pptx-templates`,
7+
outputDir: `${__dirname}/pptx-output`,
8+
autoImportSlideMasters: true,
9+
});
10+
11+
const pres = await automizer
12+
.loadRoot(`EmptyTemplate.pptx`)
13+
.load('SlidesWithAdditionalMaster.pptx')
14+
.load('SlideMasters.pptx')
15+
16+
// We can disable .addMaster according to "autoImportSlideMasters: true"
17+
// .addMaster('SlidesWithAdditionalMaster.pptx', 1)
18+
19+
.addSlide('SlidesWithAdditionalMaster.pptx', 3, (slide) => {
20+
// We can also disable "slide.useSlideLayout()"
21+
// with "autoImportSlideMasters: true"
22+
// slide.useSlideLayout();
23+
})
24+
.addSlide('SlideMasters.pptx', 1)
25+
.addSlide('SlidesWithAdditionalMaster.pptx', 1)
26+
.addSlide('SlideMasters.pptx', 3)
27+
.addSlide('SlideMasters.pptx', 2)
28+
.write(`add-slide-master-auto-import.test.pptx`);
29+
30+
expect(pres.masters).toBe(5);
31+
});

__tests__/add-slide-master.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Automizer from '../src/automizer';
2+
import { ModifyTextHelper } from '../src';
3+
4+
test('Append and modify slideMastes and use slideLayouts', async () => {
5+
const automizer = new Automizer({
6+
templateDir: `${__dirname}/pptx-templates`,
7+
outputDir: `${__dirname}/pptx-output`,
8+
});
9+
10+
const pres = await automizer
11+
.loadRoot(`EmptyTemplate.pptx`)
12+
.load(`SlideWithNotes.pptx`, 'notes')
13+
.load('SlidesWithAdditionalMaster.pptx')
14+
.load('SlideWithShapes.pptx')
15+
.load('SlideWithCharts.pptx')
16+
17+
// Import another slide master and all its slide layouts:
18+
.addMaster('SlidesWithAdditionalMaster.pptx', 1, (master) => {
19+
master.modifyElement(
20+
`MasterRectangle`,
21+
ModifyTextHelper.setText('my text on master'),
22+
);
23+
master.addElement(`SlideWithCharts.pptx`, 1, 'StackedBars');
24+
})
25+
.addMaster('SlidesWithAdditionalMaster.pptx', 2, (master) => {
26+
master.addElement('SlideWithShapes.pptx', 1, 'Cloud 1');
27+
})
28+
29+
// Add a slide (which might require an imported master):
30+
.addSlide('SlidesWithAdditionalMaster.pptx', 3, (slide) => {
31+
// To use the original master from 'SlidesWithAdditionalMaster.pptx',
32+
// we can skip the argument. The required slideMaster & layout will be
33+
// auto imported.
34+
slide.useSlideLayout();
35+
})
36+
37+
// Add a slide and use the source slideLayout:
38+
.addSlide('SlidesWithAdditionalMaster.pptx', 3, (slide) => {
39+
// To use the original master from 'SlidesWithAdditionalMaster.pptx',
40+
// we can skip the argument.
41+
slide.useSlideLayout();
42+
})
43+
44+
// Add a slide (which might require an imported master):
45+
.addSlide('notes', 1, (slide) => {
46+
// use another master, e.g. the imported one from 'SlidesWithAdditionalMaster.pptx'
47+
// You need to pass the index of the desired layout after all
48+
// related layouts of all imported masters have been added to rootTemplate.
49+
slide.useSlideLayout(26);
50+
})
51+
52+
.write(`add-slide-master.test.pptx`);
53+
54+
expect(pres.masters).toBe(3);
55+
});
59.5 KB
Binary file not shown.
63.1 KB
Binary file not shown.

src/automizer.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
AutomizerParams,
44
AutomizerSummary,
55
ArchiveParams,
6-
SourceSlideIdentifier,
6+
SourceIdentifier,
77
StatusTracker,
88
} from './types/types';
99
import { IPresentationProps } from './interfaces/ipresentation-props';
@@ -17,7 +17,10 @@ import path from 'path';
1717
import * as fs from 'fs';
1818
import { XmlHelper } from './helper/xml-helper';
1919
import ModifyPresentationHelper from './helper/modify-presentation-helper';
20-
import { ContentTracker } from './helper/content-tracker';
20+
import {
21+
contentTracker as Tracker,
22+
ContentTracker,
23+
} from './helper/content-tracker';
2124
import JSZip, { OutputType } from 'jszip';
2225

2326
/**
@@ -79,6 +82,7 @@ export default class Automizer implements IPresentationProps {
7982
this.rootTemplate = Template.import(
8083
location,
8184
this.archiveParams,
85+
this,
8286
) as RootPresTemplate;
8387
}
8488

@@ -176,7 +180,7 @@ export default class Automizer implements IPresentationProps {
176180
name,
177181
};
178182

179-
const newTemplate = Template.import(location, importParams);
183+
const newTemplate = Template.import(location, importParams, this);
180184

181185
if (!this.isPresTemplate(newTemplate)) {
182186
this.rootTemplate = newTemplate;
@@ -227,7 +231,7 @@ export default class Automizer implements IPresentationProps {
227231
*/
228232
public addSlide(
229233
name: string,
230-
slideIdentifier: SourceSlideIdentifier,
234+
slideIdentifier: SourceIdentifier,
231235
callback?: (slide: Slide) => void,
232236
): this {
233237
if (this.rootTemplate === undefined) {
@@ -247,31 +251,42 @@ export default class Automizer implements IPresentationProps {
247251
callback(newSlide);
248252
}
249253

254+
if (this.params.autoImportSlideMasters) {
255+
newSlide.useSlideLayout();
256+
}
257+
250258
this.rootTemplate.slides.push(newSlide);
251259

252260
return this;
253261
}
254262

255263
/**
256-
* WIP: copy and modify a master from template to output
264+
* Copy and modify a master and the associated layouts from template to output.
265+
*
257266
* @param name
258-
* @param masterNumber
267+
* @param sourceIdentifier
259268
* @param callback
260269
*/
261270
public addMaster(
262271
name: string,
263-
masterNumber: number,
264-
callback?: (slide: Slide) => void,
272+
// sourceIdentifier: SourceIdentifier,
273+
sourceIdentifier: number,
274+
callback?: (slide: Master) => void,
265275
): this {
266276
const template = this.getTemplate(name);
267277

268278
const newMaster = new Master({
269279
presentation: this,
270280
template,
271-
masterNumber,
281+
sourceIdentifier,
272282
});
273283

274-
// this.rootTemplate.slides.push(newMaster);
284+
if (callback !== undefined) {
285+
newMaster.root = this;
286+
callback(newMaster);
287+
}
288+
289+
this.rootTemplate.masters.push(newMaster);
275290

276291
return this;
277292
}
@@ -314,6 +329,7 @@ export default class Automizer implements IPresentationProps {
314329
slides: this.rootTemplate.count('slides'),
315330
charts: this.rootTemplate.count('charts'),
316331
images: this.rootTemplate.count('images'),
332+
masters: this.rootTemplate.count('masters'),
317333
};
318334
}
319335

@@ -349,17 +365,28 @@ export default class Automizer implements IPresentationProps {
349365
}
350366

351367
async finalizePresentation() {
368+
await this.writeMasterSlides();
352369
await this.writeSlides();
353370
await this.normalizePresentation();
354371
await this.applyModifyPresentationCallbacks();
355372
}
356373

374+
/**
375+
* Write all masterSlides to archive.
376+
*/
377+
public async writeMasterSlides(): Promise<void> {
378+
for (const slide of this.rootTemplate.masters) {
379+
await this.rootTemplate.appendMasterSlide(slide);
380+
}
381+
}
382+
357383
/**
358384
* Write all slides to archive.
359385
*/
360386
public async writeSlides(): Promise<void> {
361387
await this.rootTemplate.countExistingSlides();
362-
this.status.max = this.rootTemplate.slides.length;
388+
this.status.max =
389+
this.rootTemplate.slides.length + this.rootTemplate.masters.length;
363390

364391
for (const slide of this.rootTemplate.slides) {
365392
await this.rootTemplate.appendSlide(slide);
@@ -391,6 +418,7 @@ export default class Automizer implements IPresentationProps {
391418
*/
392419
async normalizePresentation(): Promise<void> {
393420
this.modify(ModifyPresentationHelper.normalizeSlideIds);
421+
this.modify(ModifyPresentationHelper.normalizeSlideMasterIds);
394422

395423
if (this.params.cleanup) {
396424
if (this.params.removeExistingSlides) {

0 commit comments

Comments
 (0)