PptxGenJS
PptxGenJS copied to clipboard
defineSlideMaster() alters configuration object which causes lost placeholder formatting when reusing config object
Issue
I'm using pptxgenjs 2.3.0. I create two PPTX files using the same shared slide master configuration, which contains a placeholder:
const PptxGenJS = require('pptxgenjs');
const SLIDE_MASTER = {
title: 'SLIDE_MASTER',
objects: [
{
placeholder: {
options: {
name: 'name',
type: 'body',
x: '20%',
y: 2,
},
},
},
]
};
function createPptx(name) {
const pptx = new PptxGenJS();
pptx.defineSlideMaster(SLIDE_MASTER);
const slide = pptx.addNewSlide('SLIDE_MASTER');
slide.addText('Hello World1!', { placeholder: 'name' });
pptx.save(name);
}
createPptx('Presentation1');
createPptx('Presentation2');
For the first run, the slide is correct:

For the second run, the slide lost its formatting:

Elements in the master slides without placeholders are not effected by this.
Workaround
Create a deep copy of the slide master configuration before passing it to defineSlideMaster:
pptx.defineSlideMaster(JSON.parse(JSON.stringify(SLIDE_MASTER)));
I was just burned by this bug.
In src/gen-objects.ts, method createSlideMaster() deletes keys from the passed props of every placeholder. You can't reuse a placeholder slide definition. Was this a hack to get the media demo to work?