@@ -10,11 +10,12 @@ import { IPresentationProps } from './interfaces/ipresentation-props';
1010import { PresTemplate } from './interfaces/pres-template' ;
1111import { RootPresTemplate } from './interfaces/root-pres-template' ;
1212import { Template } from './classes/template' ;
13- import { TemplateInfo } from './types/xml-types' ;
13+ import { ModifyPresentationCallback , TemplateInfo } from './types/xml-types' ;
1414import { vd } from './helper/general-helper' ;
1515import { Master } from './classes/master' ;
1616import path from 'path' ;
1717import * as fs from 'fs' ;
18+ import { XmlHelper } from './helper/xml-helper' ;
1819
1920/**
2021 * Automizer
@@ -40,12 +41,15 @@ export default class Automizer implements IPresentationProps {
4041 params : AutomizerParams ;
4142 status : StatusTracker ;
4243
44+ modifyPresentation : ModifyPresentationCallback [ ] ;
45+
4346 /**
4447 * Creates an instance of `pptx-automizer`.
4548 * @param [params]
4649 */
4750 constructor ( params : AutomizerParams ) {
4851 this . templates = [ ] ;
52+ this . modifyPresentation = [ ] ;
4953 this . params = params ;
5054
5155 this . templateDir = params ?. templateDir ? params . templateDir + '/' : '' ;
@@ -173,6 +177,11 @@ export default class Automizer implements IPresentationProps {
173177 return templateCreationId ;
174178 }
175179
180+ public async modify ( cb : ModifyPresentationCallback ) : Promise < this> {
181+ this . modifyPresentation . push ( cb ) ;
182+ return this ;
183+ }
184+
176185 /**
177186 * Determines whether template is root or default template.
178187 * @param template
@@ -262,8 +271,40 @@ export default class Automizer implements IPresentationProps {
262271 * @returns summary object.
263272 */
264273 public async write ( location : string ) : Promise < AutomizerSummary > {
274+ await this . writeSlides ( ) ;
275+ await this . applyModifyPresentationCallbacks ( ) ;
276+
265277 const rootArchive = await this . rootTemplate . archive ;
278+ const content = await rootArchive . generateAsync ( { type : 'nodebuffer' } ) ;
279+
280+ return FileHelper . writeOutputFile (
281+ this . getLocation ( location , 'output' ) ,
282+ content ,
283+ this ,
284+ ) ;
285+ }
286+
287+ async applyModifyPresentationCallbacks ( ) {
288+ const presentationXml = await XmlHelper . getXmlFromArchive (
289+ await this . rootTemplate . archive ,
290+ `ppt/presentation.xml` ,
291+ ) ;
266292
293+ for ( const cb of this . modifyPresentation ) {
294+ cb ( presentationXml ) ;
295+ }
296+
297+ await XmlHelper . writeXmlToArchive (
298+ await this . rootTemplate . archive ,
299+ `ppt/presentation.xml` ,
300+ presentationXml ,
301+ ) ;
302+ }
303+
304+ /**
305+ * Write all slides into archive.
306+ */
307+ public async writeSlides ( ) : Promise < void > {
267308 await this . rootTemplate . countExistingSlides ( ) ;
268309 this . status . max = this . rootTemplate . slides . length ;
269310
@@ -274,14 +315,6 @@ export default class Automizer implements IPresentationProps {
274315 if ( this . params . removeExistingSlides ) {
275316 await this . rootTemplate . truncate ( ) ;
276317 }
277-
278- const content = await rootArchive . generateAsync ( { type : 'nodebuffer' } ) ;
279-
280- return FileHelper . writeOutputFile (
281- this . getLocation ( location , 'output' ) ,
282- content ,
283- this ,
284- ) ;
285318 }
286319
287320 /**
0 commit comments