Skip to content

Commit 6859770

Browse files
committed
feat(generate): interface for supported pptxGenJS items; auto-insert objectName
1 parent d6f40b4 commit 6859770

File tree

5 files changed

+96
-12
lines changed

5 files changed

+96
-12
lines changed

__tests__/generate-pptxgenjs-charts.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ test('generate a chart with pptxgenjs and add it to a template slide', async ()
2727

2828
pres.addSlide('empty', 1, (slide) => {
2929
// Use pptxgenjs to add generated contents from scratch:
30-
slide.generate((pSlide, objectName, pptxGenJs) => {
30+
slide.generate((pSlide, pptxGenJs) => {
3131
pSlide.addChart(pptxGenJs.ChartType.line, dataChartAreaLine, {
3232
x: 1,
3333
y: 1,
3434
w: 8,
3535
h: 4,
36-
objectName,
3736
});
3837
});
3938
});

__tests__/generate-pptxgenjs-image.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ test('insert an image with pptxgenjs on a template slide', async () => {
1313

1414
pres.addSlide('empty', 1, (slide) => {
1515
// Use pptxgenjs to add image from file:
16-
slide.generate((pptxGenJSSlide, objectName) => {
16+
slide.generate((pptxGenJSSlide) => {
1717
pptxGenJSSlide.addImage({
1818
path: `${__dirname}/media/test.png`,
1919
x: 1,
2020
y: 2,
21-
objectName,
2221
});
2322
});
2423
});

__tests__/generate-pptxgenjs-text.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ test('insert a textbox with pptxgenjs on a template slide', async () => {
1313

1414
pres.addSlide('empty', 1, (slide) => {
1515
// Use pptxgenjs to add text from scratch:
16-
slide.generate((pptxGenJSSlide, objectName) => {
16+
slide.generate((pptxGenJSSlide) => {
1717
pptxGenJSSlide.addText('Test', {
1818
x: 1,
1919
y: 1,
20+
h: 5,
21+
w: 10,
2022
color: '363636',
21-
objectName,
2223
});
2324
}, 'custom object name');
2425
});

src/helper/generate/generate-pptxgenjs.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import PptxGenJS from 'pptxgenjs';
33
import fs from 'fs';
44
import { ISlide } from '../../interfaces/islide';
55
import Automizer from '../../automizer';
6-
import { GenerateElements } from '../../types/types';
6+
import { GenerateElements, SupportedPptxGenJSSlide } from '../../types/types';
77
import { IGenerator } from '../../interfaces/igenerator';
8-
import { vd } from '../general-helper';
98

109
export default class GeneratePptxGenJs implements IGenerator {
1110
tmpFile: string;
@@ -51,8 +50,7 @@ export default class GeneratePptxGenJs implements IGenerator {
5150
generateElement.objectName = generateElement.objectName || randomUUID();
5251
generateElement.tmpSlideNumber = this.countSlides;
5352
generateElement.callback(
54-
pgenSlide,
55-
generateElement.objectName,
53+
this.supportedSlideItems(pgenSlide, generateElement.objectName),
5654
this.generator,
5755
);
5856
slide.addElement(
@@ -63,6 +61,37 @@ export default class GeneratePptxGenJs implements IGenerator {
6361
});
6462
}
6563

64+
supportedSlideItems = (
65+
pgenSlide: PptxGenJS.Slide,
66+
objectName: string,
67+
): SupportedPptxGenJSSlide => {
68+
return {
69+
addChart: (type, data, options) => {
70+
pgenSlide.addChart(type, data, this.getOptions(options, objectName));
71+
},
72+
addImage: (options) => {
73+
pgenSlide.addImage(this.getOptions(options, objectName));
74+
},
75+
addShape: (shapeName, options?) => {
76+
pgenSlide.addShape(shapeName, this.getOptions(options, objectName));
77+
},
78+
addTable: (tableRows, options?) => {
79+
pgenSlide.addTable(tableRows, this.getOptions(options, objectName));
80+
},
81+
addText: (text, options?) => {
82+
pgenSlide.addText(text, this.getOptions(options, objectName));
83+
},
84+
};
85+
};
86+
87+
getOptions = (options, objectName) => {
88+
options = options || {};
89+
return {
90+
...options,
91+
objectName,
92+
};
93+
};
94+
6695
appendPptxGenSlide(): PptxGenJS.Slide {
6796
return this.generator.addSlide();
6897
}

src/types/types.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,65 @@ export type ImportElement = {
225225
callback?: ShapeModificationCallback | ShapeModificationCallback[];
226226
info?: any;
227227
};
228+
229+
export interface SupportedPptxGenJSSlide {
230+
/**
231+
* Add chart to Slide
232+
* @param {CHART_NAME|IChartMulti[]} type - chart type
233+
* @param {object[]} data - data object
234+
* @param {IChartOpts} options - chart options
235+
* @return {Slide} this Slide
236+
* @type {Function}
237+
*/
238+
addChart(
239+
type: PptxGenJS.CHART_NAME | PptxGenJS['IChartMulti'][],
240+
data: any[],
241+
options?: PptxGenJS.IChartOpts,
242+
): void;
243+
244+
/**
245+
* Add image to Slide
246+
* @param {ImageProps} options - image options
247+
* @return {Slide} this Slide
248+
*/
249+
addImage(options: PptxGenJS.ImageProps): void;
250+
251+
/**
252+
* Add shape to Slide
253+
* @param {SHAPE_NAME} shapeName - shape name
254+
* @param {ShapeProps} options - shape options
255+
* @return {Slide} this Slide
256+
*/
257+
addShape(
258+
shapeName: PptxGenJS.SHAPE_NAME,
259+
options?: PptxGenJS.ShapeProps,
260+
): void;
261+
262+
/**
263+
* Add table to Slide
264+
* @param {TableRow[]} tableRows - table rows
265+
* @param {TableProps} options - table options
266+
* @return {Slide} this Slide
267+
*/
268+
addTable(
269+
tableRows: PptxGenJS.TableRow[],
270+
options?: PptxGenJS.TableProps,
271+
): void;
272+
273+
/**
274+
* Add text to Slide
275+
* @param {string|TextProps[]} text - text string or complex object
276+
* @param {TextPropsOptions} options - text options
277+
* @return {Slide} this Slide
278+
*/
279+
addText(
280+
text: string | PptxGenJS.TextProps[],
281+
options?: PptxGenJS.TextPropsOptions,
282+
): void;
283+
}
284+
228285
export type GenerateOnSlideCallback = (
229-
pptxGenJSSlide: PptxGenJS.Slide,
230-
objectName: string,
286+
pptxGenJSSlide: SupportedPptxGenJSSlide,
231287
pptxGenJS: PptxGenJS,
232288
) => void;
233289
export type GenerateElements = {

0 commit comments

Comments
 (0)