Skip to content

Commit b489d38

Browse files
committed
feature(master): add autoImportSlideMasters; add tests & readme
1 parent e6cc9bc commit b489d38

File tree

9 files changed

+352
-160
lines changed

9 files changed

+352
-160
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.

src/automizer.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export default class Automizer implements IPresentationProps {
180180
name,
181181
};
182182

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

185185
if (!this.isPresTemplate(newTemplate)) {
186186
this.rootTemplate = newTemplate;
@@ -251,21 +251,26 @@ export default class Automizer implements IPresentationProps {
251251
callback(newSlide);
252252
}
253253

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

256260
return this;
257261
}
258262

259263
/**
260-
* WIP: Copy and modify a master and the associated layouts from template
261-
* to output.
264+
* Copy and modify a master and the associated layouts from template to output.
265+
*
262266
* @param name
263267
* @param sourceIdentifier
264268
* @param callback
265269
*/
266270
public addMaster(
267271
name: string,
268-
sourceIdentifier: SourceIdentifier,
272+
// sourceIdentifier: SourceIdentifier,
273+
sourceIdentifier: number,
269274
callback?: (slide: Master) => void,
270275
): this {
271276
const template = this.getTemplate(name);

src/classes/master.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ export class Master implements IMaster {
123123
sourceIdentifier: SourceIdentifier;
124124
}) {
125125
this.sourceTemplate = params.template;
126+
127+
// ToDo analogue for slideMasters
126128
// this.sourceNumber = this.getSlideNumber(
127129
// params.template,
128130
// params.sourceIdentifier,
@@ -141,6 +143,8 @@ export class Master implements IMaster {
141143
}
142144

143145
/**
146+
* ToDo: Implement creationIds as well for slideMasters
147+
*
144148
* Try to convert a given slide's creationId to corresponding slide number.
145149
* Used if automizer is run with useCreationIds: true
146150
* @internal
@@ -195,7 +199,7 @@ export class Master implements IMaster {
195199
await this.copySlideFiles();
196200
await this.copyRelatedLayouts();
197201
await this.copyRelatedContent();
198-
await this.addSlideToPresentation();
202+
await this.addSlideMasterToPresentation();
199203
await this.copyThemeFiles();
200204

201205
if (this.importElements.length) {
@@ -306,17 +310,22 @@ export class Master implements IMaster {
306310
* @internal
307311
* @returns slide to presentation
308312
*/
309-
async addSlideToPresentation(): Promise<void> {
313+
async addSlideMasterToPresentation(): Promise<void> {
310314
const relId = await XmlHelper.getNextRelId(
311315
this.targetArchive,
312316
'ppt/_rels/presentation.xml.rels',
313317
);
314318
await this.appendToSlideRel(this.targetArchive, relId, this.targetNumber);
315319
await this.appendToSlideList(this.targetArchive, relId);
316-
await this.appendSlideToContentType(this.targetArchive, this.targetNumber);
320+
await this.appendSlideMasterToContentType(
321+
this.targetArchive,
322+
this.targetNumber,
323+
);
317324
}
318325

319326
/**
327+
* ToDo: This equals the corresponding method in slide.ts
328+
*
320329
* Select and modify a single element on an added slide.
321330
* @param {string} selector - Element's name on the slide.
322331
* Should be a unique string defined on the "Selection"-pane within ppt.
@@ -340,6 +349,8 @@ export class Master implements IMaster {
340349
}
341350

342351
/**
352+
* ToDo: This equals the corresponding method in slide.ts
353+
*
343354
* Select, insert and (optionally) modify a single element to a slide.
344355
* @param {string} presName - Filename or alias name of the template presentation.
345356
* Must have been importet with Automizer.load().
@@ -363,6 +374,8 @@ export class Master implements IMaster {
363374
}
364375

365376
/**
377+
* ToDo: This equals the corresponding method in slide.ts
378+
*
366379
* Remove a single element from slide.
367380
* @param {string} selector - Element's name on the slide.
368381
*/
@@ -380,6 +393,8 @@ export class Master implements IMaster {
380393
}
381394

382395
/**
396+
* ToDo: This equals the corresponding method in slide.ts
397+
*
383398
* Adds element to modifications list
384399
* @internal
385400
* @param presName
@@ -408,6 +423,8 @@ export class Master implements IMaster {
408423
}
409424

410425
/**
426+
* ToDo: This equals the corresponding method in slide.ts
427+
*
411428
* Imported selected elements
412429
* @internal
413430
* @returns selected elements
@@ -445,6 +462,8 @@ export class Master implements IMaster {
445462
}
446463

447464
/**
465+
* ToDo: This *ALMOST* equals the corresponding method in slide.ts
466+
*
448467
* Gets element info
449468
* @internal
450469
* @param importElement
@@ -506,6 +525,14 @@ export class Master implements IMaster {
506525
};
507526
}
508527

528+
/**
529+
* ToDo: This equals the corresponding method in slide.ts
530+
*
531+
* @param selector
532+
* @param sourceArchive
533+
* @param sourcePath
534+
* @param useCreationIds
535+
*/
509536
async findElementOnSlide(
510537
selector: FindElementSelector,
511538
sourceArchive: IArchive,
@@ -556,6 +583,8 @@ export class Master implements IMaster {
556583
}
557584

558585
/**
586+
* ToDo: This equals the corresponding method in slide.ts
587+
*
559588
* Analyzes element
560589
* @internal
561590
* @param sourceElement
@@ -617,6 +646,8 @@ export class Master implements IMaster {
617646
}
618647

619648
/**
649+
* ToDo: This equals the corresponding method in slide.ts
650+
*
620651
* Applys modifications
621652
* @internal
622653
* @returns modifications
@@ -633,6 +664,8 @@ export class Master implements IMaster {
633664
}
634665

635666
/**
667+
* ToDo: This equals the corresponding method in slide.ts
668+
*
636669
* Removes all unsupported tags from slide xml.
637670
* E.g. added relations & tags by Thinkcell cannot
638671
* be processed by pptx-automizer at the moment.
@@ -733,7 +766,7 @@ export class Master implements IMaster {
733766
* @param slideCount
734767
* @returns slide to content type
735768
*/
736-
appendSlideToContentType(
769+
appendSlideMasterToContentType(
737770
rootArchive: IArchive,
738771
slideCount: number,
739772
): Promise<HelperElement> {
@@ -770,6 +803,8 @@ export class Master implements IMaster {
770803
}
771804

772805
/**
806+
* ToDo: This equals the corresponding method in slide.ts
807+
*
773808
* Appends notes to content type
774809
* @internal
775810
* @param rootArchive
@@ -789,6 +824,8 @@ export class Master implements IMaster {
789824
}
790825

791826
/**
827+
* ToDo: This equals the corresponding method in slide.ts
828+
*
792829
* Copys related content
793830
* @internal
794831
* @returns related content

src/classes/template.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export class Template implements ITemplate {
4646
slides: ISlide[];
4747

4848
/**
49-
* Array containing all slides coming from Automizer.addSlide()
50-
* @type: ISlide[]
49+
* Array containing all slideMasters coming from Automizer.addMaster()
50+
* @type: IMaster[]
5151
*/
5252
masters: IMaster[];
5353

0 commit comments

Comments
 (0)