Skip to content

Commit

Permalink
fix(master): use correct rels path on swapping image on slideMaster
Browse files Browse the repository at this point in the history
  • Loading branch information
singerla committed Feb 5, 2024
1 parent 1f2265c commit 436cf37
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
29 changes: 29 additions & 0 deletions __tests__/modify-master-external-image.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Automizer from '../src/automizer';
import { ModifyImageHelper, ModifyShapeHelper } from '../src';
import { CmToDxa } from '../src/helper/modify-helper';

test('Load external media, modify image target on slide master', async () => {
const automizer = new Automizer({
templateDir: `${__dirname}/../__tests__/pptx-templates`,
outputDir: `${__dirname}/../__tests__/pptx-output`,
mediaDir: `${__dirname}/../__tests__/images`,
removeExistingSlides: true,
cleanup: true,
});

const pres = automizer
.loadRoot(`RootTemplateWithImages.pptx`)
.loadMedia([`test.jpg`])
.load(`RootTemplateWithImages.pptx`, 'base');

pres.addMaster('base', 1, (master) => {
master.modifyElement('masterImagePNG', [
ModifyImageHelper.setRelationTarget('test.jpg'),
]);
});

// Expect imported slide master (#2) to have swapped (left top) background image
const result = await pres.write(`modify-master-add-external-image.test.pptx`);

expect(result.images).toBe(8);
});
Binary file modified __tests__/pptx-templates/RootTemplateWithImages.pptx
Binary file not shown.
37 changes: 24 additions & 13 deletions src/classes/has-shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { Image } from '../shapes/image';
import { ElementType } from '../enums/element-type';
import { GenericShape } from '../shapes/generic';
import { XmlSlideHelper } from '../helper/xml-slide-helper';
import { vd } from '../helper/general-helper';

export default class HasShapes {
/**
Expand Down Expand Up @@ -387,17 +388,27 @@ export default class HasShapes {
? this.getSlideNumber(template, importElement.slideNumber)
: importElement.slideNumber;

let sourcePath = `ppt/slides/slide${slideNumber}.xml`;

let currentMode = 'slideToSlide';
if (this.targetType === 'slideMaster') {
// It is possible to import shapes from loaded presentations,
// as well as to modify an existing shape on current slideMaster
sourcePath =
importElement.mode === 'append'
? `ppt/slides/slide${slideNumber}.xml`
: `ppt/slideMasters/slideMaster${slideNumber}.xml`;
if (importElement.mode === 'append') {
currentMode = 'slideToMaster';
} else {
currentMode = 'onMaster';
}
}

// It is possible to import shapes from loaded slides to slideMaster,
// as well as to modify an existing shape on current slideMaster
const sourcePath =
currentMode === 'onMaster'
? `ppt/slideMasters/slideMaster${slideNumber}.xml`
: `ppt/slides/slide${slideNumber}.xml`;

const sourceRelPath =
currentMode === 'onMaster'
? `ppt/slideMasters/_rels/slideMaster${slideNumber}.xml.rels`
: `ppt/slides/_rels/slide${slideNumber}.xml.rels`;

const sourceArchive = await template.archive;
const useCreationIds =
template.useCreationIds === true && template.creationIds !== undefined;
Expand All @@ -420,7 +431,7 @@ export default class HasShapes {
const appendElementParams = await this.analyzeElement(
sourceElement,
sourceArchive,
slideNumber,
sourceRelPath,
);

return {
Expand Down Expand Up @@ -754,13 +765,13 @@ export default class HasShapes {
async analyzeElement(
sourceElement: XmlElement,
sourceArchive: IArchive,
slideNumber: number,
relsPath: string,
): Promise<AnalyzedElementType> {
const isChart = sourceElement.getElementsByTagName('c:chart');
if (isChart.length) {
const target = await XmlHelper.getTargetByRelId(
sourceArchive,
slideNumber,
relsPath,
sourceElement,
'chart',
);
Expand All @@ -775,7 +786,7 @@ export default class HasShapes {
if (isChartEx.length) {
const target = await XmlHelper.getTargetByRelId(
sourceArchive,
slideNumber,
relsPath,
sourceElement,
'chartEx',
);
Expand All @@ -792,7 +803,7 @@ export default class HasShapes {
type: ElementType.Image,
target: await XmlHelper.getTargetByRelId(
sourceArchive,
slideNumber,
relsPath,
sourceElement,
'image',
),
Expand Down
3 changes: 1 addition & 2 deletions src/helper/xml-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,14 @@ export class XmlHelper {

static async getTargetByRelId(
archive: IArchive,
slideNumber: number,
relsPath: string,
element: XmlElement,
type: string,
): Promise<Target> {
const params = TargetByRelIdMap[type];
const sourceRid = element
.getElementsByTagName(params.relRootTag)[0]
.getAttribute(params.relAttribute);
const relsPath = `ppt/slides/_rels/slide${slideNumber}.xml.rels`;
const imageRels = await XmlHelper.getRelationshipTargetsByPrefix(
archive,
relsPath,
Expand Down
3 changes: 2 additions & 1 deletion src/shapes/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ export class Image extends Shape implements IImage {
this.applyImageCallbacks();

if (this.hasSvgRelation()) {
const relsPath = `ppt/slides/_rels/slide${this.sourceSlideNumber}.xml.rels`;
const target = await XmlHelper.getTargetByRelId(
this.sourceArchive,
this.sourceSlideNumber,
relsPath,
this.targetElement,
'image:svg',
);
Expand Down

0 comments on commit 436cf37

Please sign in to comment.