Skip to content

Commit 9185ee7

Browse files
committed
fix: make cleanupPlaceholders optional; seek closest parent; fixes #139
1 parent f73f0cc commit 9185ee7

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ const automizer = new Automizer({
178178

179179
// Use 1 to show warnings or 2 for detailed information
180180
// 0 disables logging
181-
verbosity: 1
181+
verbosity: 1,
182+
183+
// Remove all unused placeholders to prevent unwanted overlays:
184+
cleanupPlaceholders: false
182185
});
183186

184187
// Now we can start and load a pptx template.

src/classes/has-shapes.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ export default class HasShapes {
137137
targetType: ShapeTargetType;
138138
params: AutomizerParams;
139139

140-
constructor(params) {
140+
cleanupPlaceholders: boolean = false;
141+
142+
constructor(params: {
143+
presentation: IPresentationProps;
144+
template: PresTemplate;
145+
}) {
141146
this.sourceTemplate = params.template;
142147

143148
this.modifications = [];
@@ -147,6 +152,8 @@ export default class HasShapes {
147152

148153
this.status = params.presentation.status;
149154
this.content = params.presentation.content;
155+
156+
this.cleanupPlaceholders = params.presentation.params.cleanupPlaceholders;
150157
}
151158

152159
/**
@@ -939,7 +946,7 @@ export default class HasShapes {
939946
targetPath,
940947
);
941948

942-
if (sourcePlaceholderTypes) {
949+
if (this.cleanupPlaceholders && sourcePlaceholderTypes) {
943950
this.removeDuplicatePlaceholders(xml, sourcePlaceholderTypes);
944951
this.normalizePlaceholderShapes(xml, sourcePlaceholderTypes);
945952
}
@@ -983,9 +990,14 @@ export default class HasShapes {
983990
(sourcePlaceholder) => sourcePlaceholder.type === usedType,
984991
);
985992
removePlaceholders.forEach((removePlaceholder) => {
986-
const removePlaceholderShape = removePlaceholder.xml.parentNode
987-
.parentNode.parentNode as XmlElement;
988-
XmlHelper.remove(removePlaceholderShape);
993+
const parentShapeTag = 'p:sp';
994+
const parentShape = XmlHelper.getClosestParent(
995+
parentShapeTag,
996+
removePlaceholder.xml,
997+
);
998+
if (parentShape) {
999+
XmlHelper.remove(parentShape);
1000+
}
9891001
});
9901002
}
9911003
}

src/dev.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const run = async () => {
88
templateDir,
99
outputDir,
1010
autoImportSlideMasters: true,
11+
cleanupPlaceholders: true,
1112
});
1213

1314
let pres = automizer

src/helper/xml-helper.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,19 @@ export class XmlHelper {
548548
}
549549
}
550550

551+
static getClosestParent(tag: string, element: XmlElement): XmlElement {
552+
if (element.parentNode) {
553+
if (element.parentNode.nodeName === tag) {
554+
return element.parentNode as XmlElement;
555+
}
556+
return XmlHelper.getClosestParent(tag, element.parentNode as XmlElement);
557+
}
558+
}
559+
551560
static remove(toRemove: XmlElement): void {
552-
toRemove.parentNode.removeChild(toRemove);
561+
if (toRemove?.parentNode) {
562+
toRemove.parentNode.removeChild(toRemove);
563+
}
553564
}
554565

555566
static moveChild(childToMove: XmlElement, insertBefore?: XmlElement): void {

src/types/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ export type AutomizerParams = {
105105
* Eventually remove all unnecessary files from archive.
106106
*/
107107
cleanup?: boolean;
108+
/**
109+
* Remove all unused shape placeholders from slide.
110+
*/
111+
cleanupPlaceholders?: boolean;
108112
/**
109113
* statusTracker will be triggered on each appended slide.
110114
* You can e.g. attach a custom callback to a progress bar.

0 commit comments

Comments
 (0)