Skip to content

Commit 10187bf

Browse files
committed
feature(status): use statusTracker to show progress during generation
1 parent a20a95d commit 10187bf

File tree

4 files changed

+91
-19
lines changed

4 files changed

+91
-19
lines changed

src/automizer.ts

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { Slide } from './classes/slide';
22
import { FileHelper } from './helper/file-helper';
3-
import {AutomizerParams, AutomizerSummary, SourceSlideIdentifier} from './types/types';
3+
import {
4+
AutomizerParams,
5+
AutomizerSummary,
6+
SourceSlideIdentifier,
7+
StatusTracker,
8+
} from './types/types';
49
import { IPresentationProps } from './interfaces/ipresentation-props';
510
import { PresTemplate } from './interfaces/pres-template';
611
import { RootPresTemplate } from './interfaces/root-pres-template';
712
import { Template } from './classes/template';
813
import { TemplateInfo } from './types/xml-types';
914
import { vd } from './helper/general-helper';
10-
import {Master} from './classes/master';
15+
import { Master } from './classes/master';
1116

1217
/**
1318
* Automizer
@@ -30,6 +35,7 @@ export default class Automizer implements IPresentationProps {
3035
*/
3136
timer: number;
3237
params: AutomizerParams;
38+
status: StatusTracker;
3339

3440
/**
3541
* Creates an instance of `pptx-automizer`.
@@ -43,6 +49,7 @@ export default class Automizer implements IPresentationProps {
4349
this.outputDir = params?.outputDir ? params.outputDir + '/' : '';
4450

4551
this.timer = Date.now();
52+
this.setStatusTracker(params?.statusTracker);
4653

4754
if (params.rootTemplate) {
4855
const location = this.getLocation(params.rootTemplate, 'template');
@@ -58,6 +65,32 @@ export default class Automizer implements IPresentationProps {
5865
}
5966
}
6067

68+
setStatusTracker(statusTracker: StatusTracker['next']): void {
69+
const defaultStatusTracker = (status: StatusTracker) => {
70+
console.log(status.info + '(' + status.share + '%)');
71+
};
72+
73+
this.status = {
74+
current: 0,
75+
max: 0,
76+
share: 0,
77+
info: undefined,
78+
increment: () => {
79+
this.status.current++;
80+
const nextShare =
81+
this.status.max > 0
82+
? Math.round((this.status.current / this.status.max) * 100)
83+
: 0;
84+
85+
if (this.status.share !== nextShare) {
86+
this.status.share = nextShare;
87+
this.status.next(this.status);
88+
}
89+
},
90+
next: statusTracker || defaultStatusTracker,
91+
};
92+
}
93+
6194
/**
6295
6396
*/
@@ -98,9 +131,11 @@ export default class Automizer implements IPresentationProps {
98131
private loadTemplate(location: string, name?: string): this {
99132
location = this.getLocation(location, 'template');
100133

101-
const alreadyLoaded = this.templates.find(template => template.name === name)
102-
if(alreadyLoaded) {
103-
return this
134+
const alreadyLoaded = this.templates.find(
135+
(template) => template.name === name,
136+
);
137+
if (alreadyLoaded) {
138+
return this;
104139
}
105140

106141
const newTemplate = Template.import(location, name);
@@ -123,7 +158,8 @@ export default class Automizer implements IPresentationProps {
123158
public async setCreationIds(): Promise<TemplateInfo[]> {
124159
const templateCreationId = [];
125160
for (const template of this.templates) {
126-
const creationIds = template.creationIds || await template.setCreationIds()
161+
const creationIds =
162+
template.creationIds || (await template.setCreationIds());
127163
templateCreationId.push({
128164
name: template.name,
129165
slides: creationIds,
@@ -177,12 +213,17 @@ export default class Automizer implements IPresentationProps {
177213
return this;
178214
}
179215

216+
/**
217+
* WIP: copy and modify a master from template to output
218+
* @param name
219+
* @param masterNumber
220+
* @param callback
221+
*/
180222
public addMaster(
181223
name: string,
182224
masterNumber: number,
183225
callback?: (slide: Slide) => void,
184226
): this {
185-
186227
const template = this.getTemplate(name);
187228

188229
const newMaster = new Master({
@@ -218,6 +259,7 @@ export default class Automizer implements IPresentationProps {
218259
public async write(location: string): Promise<AutomizerSummary> {
219260
const rootArchive = await this.rootTemplate.archive;
220261

262+
this.status.max = this.rootTemplate.slides.length;
221263
for (const slide of this.rootTemplate.slides) {
222264
await this.rootTemplate.appendSlide(slide);
223265
}

src/classes/slide.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
ImportedElement,
88
ImportElement,
99
SlideModificationCallback,
10-
ShapeModificationCallback, SourceSlideIdentifier,
10+
ShapeModificationCallback,
11+
SourceSlideIdentifier,
12+
StatusTracker,
1113
} from '../types/types';
1214
import { ISlide } from '../interfaces/islide';
1315
import { IPresentationProps } from '../interfaces/ipresentation-props';
@@ -22,7 +24,7 @@ import {
2224
import { Image } from '../shapes/image';
2325
import { Chart } from '../shapes/chart';
2426
import { GenericShape } from '../shapes/generic';
25-
import {GeneralHelper, vd} from '../helper/general-helper';
27+
import { GeneralHelper, vd } from '../helper/general-helper';
2628

2729
export class Slide implements ISlide {
2830
/**
@@ -95,6 +97,7 @@ export class Slide implements ISlide {
9597
* @internal
9698
*/
9799
targetRelsPath: string;
100+
status: StatusTracker;
98101

99102
constructor(params: {
100103
presentation: IPresentationProps;
@@ -112,6 +115,8 @@ export class Slide implements ISlide {
112115

113116
this.modifications = [];
114117
this.importElements = [];
118+
119+
this.status = params.presentation.status;
115120
}
116121

117122
/**
@@ -122,17 +127,25 @@ export class Slide implements ISlide {
122127
* @slideNumber SourceSlideIdentifier
123128
* @returns number
124129
*/
125-
getSlideNumber(template: PresTemplate, slideIdentifier: SourceSlideIdentifier): number {
130+
getSlideNumber(
131+
template: PresTemplate,
132+
slideIdentifier: SourceSlideIdentifier,
133+
): number {
126134
if (template.creationIds !== undefined) {
127-
const matchCreationId = template.creationIds.find(
135+
const matchCreationId = template.creationIds.find(
128136
(slideInfo) => slideInfo.id === Number(slideIdentifier),
129-
)
137+
);
130138

131-
if(matchCreationId) {
132-
return matchCreationId.number
139+
if (matchCreationId) {
140+
return matchCreationId.number;
133141
}
134142

135-
throw('Could not find slide number for creationId: ' + slideIdentifier + '@' + template.name)
143+
throw (
144+
'Could not find slide number for creationId: ' +
145+
slideIdentifier +
146+
'@' +
147+
template.name
148+
);
136149
}
137150
return slideIdentifier as number;
138151
}
@@ -152,7 +165,7 @@ export class Slide implements ISlide {
152165
this.targetRelsPath = `ppt/slides/_rels/slide${this.targetNumber}.xml.rels`;
153166
this.sourceArchive = await this.sourceTemplate.archive;
154167

155-
console.log('Appending slide ' + this.targetNumber)
168+
this.status.info = 'Appending slide ' + this.targetNumber;
156169

157170
await this.copySlideFiles();
158171
await this.copyRelatedContent();
@@ -173,10 +186,12 @@ export class Slide implements ISlide {
173186
}
174187

175188
await this.applyModifications();
189+
190+
this.status.increment();
176191
}
177192

178193
/**
179-
* Modifys slide
194+
* Modifies slide
180195
* @internal
181196
* @param callback
182197
*/
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AutomizerParams } from '../types/types';
1+
import { AutomizerParams, StatusTracker } from '../types/types';
22
import { PresTemplate } from './pres-template';
33
import { RootPresTemplate } from './root-pres-template';
44

@@ -7,6 +7,7 @@ export interface IPresentationProps {
77
templates: PresTemplate[];
88
params: AutomizerParams;
99
timer: number;
10+
status?: StatusTracker;
1011

1112
getTemplate(name: string): PresTemplate;
1213
}

src/types/types.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import JSZip from 'jszip';
22
import { ElementType } from '../enums/element-type';
33

4-
export type SourceSlideIdentifier = number | string
4+
export type SourceSlideIdentifier = number | string;
55
export type SlideModificationCallback = (document: Document) => void;
66
export type ShapeModificationCallback = (
77
XMLDocument: XMLDocument | Element,
@@ -27,6 +27,20 @@ export type AutomizerParams = {
2727
rootTemplate?: string;
2828
presTemplates?: string[];
2929
useCreationIds?: boolean;
30+
31+
/**
32+
* statusTracker will be triggered on each appended slide.
33+
* You can e.g. attach a custom callback to a progress bar.
34+
*/
35+
statusTracker?: StatusTracker['next'];
36+
};
37+
export type StatusTracker = {
38+
current: number;
39+
max: number;
40+
share: number;
41+
info: string | undefined;
42+
next: (tracker: StatusTracker) => void;
43+
increment: () => void;
3044
};
3145
export type AutomizerSummary = {
3246
status: string;

0 commit comments

Comments
 (0)