Skip to content

Commit a223614

Browse files
committed
docs: comments on modifiers and api functions
1 parent 0ad4f80 commit a223614

File tree

9 files changed

+220
-163
lines changed

9 files changed

+220
-163
lines changed

src/automizer.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { Template } from './classes/template';
88

99
/**
1010
* Automizer
11-
*
12-
* The basic class for `pptx-automizer` package.
11+
*
12+
* The basic class for `pptx-automizer` package.
1313
* This class will be exported as `Automizer` by `index.ts`.
1414
*/
1515
export default class Automizer implements IPresentationProps {
@@ -22,7 +22,7 @@ export default class Automizer implements IPresentationProps {
2222

2323
/**
2424
* Creates an instance of `pptx-automizer`.
25-
* @param [params]
25+
* @param [params]
2626
*/
2727
constructor(params?: AutomizerParams) {
2828
this.templates = [];
@@ -57,9 +57,9 @@ export default class Automizer implements IPresentationProps {
5757
/**
5858
* Loads a pptx file either as a root template as a template file.
5959
* A name can be specified to give templates an alias.
60-
* @param location
61-
* @param [name]
62-
* @returns template
60+
* @param location
61+
* @param [name]
62+
* @returns template
6363
*/
6464
private loadTemplate(location: string, name?: string): this {
6565
location = this.getLocation(location, 'template');
@@ -77,8 +77,8 @@ export default class Automizer implements IPresentationProps {
7777

7878
/**
7979
* Determines whether template is root or default template.
80-
* @param template
81-
* @returns pres template
80+
* @param template
81+
* @returns pres template
8282
*/
8383
private isPresTemplate(
8484
template: PresTemplate | RootPresTemplate,
@@ -87,7 +87,7 @@ export default class Automizer implements IPresentationProps {
8787
}
8888

8989
/**
90-
* Find a slide in by number in one of the imported templates.
90+
* Add a slide from one of the imported templates by slide number.
9191
* @param name - Name or alias of the template; must have been loaded with `Automizer.load()`
9292
* @param slideNumber - Number of slide in template presentation
9393
* @param callback - Executed after slide was added. The newly created slide will be passed to the callback as first argument.
@@ -123,9 +123,9 @@ export default class Automizer implements IPresentationProps {
123123
/**
124124
* Searches this.templates to find template by given name.
125125
* @param name Alias name if given to loaded template.
126-
* @returns template
126+
* @returns template
127127
*/
128-
private getTemplate(name: string): PresTemplate {
128+
public getTemplate(name: string): PresTemplate {
129129
const template = this.templates.find((t) => t.name === name);
130130
if (template === undefined) {
131131
throw new Error(`Template not found: ${name}`);
@@ -155,10 +155,10 @@ export default class Automizer implements IPresentationProps {
155155
}
156156

157157
/**
158-
* Applies path prefix to given location string.
159-
* @param location path and/or filename
158+
* Applies path prefix to given location string.
159+
* @param location path and/or filename
160160
* @param [type] template or output
161-
* @returns location
161+
* @returns location
162162
*/
163163
private getLocation(location: string, type?: string): string {
164164
switch (type) {

src/dev.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ const pres = automizer
99
.loadRoot(`RootTemplate.pptx`)
1010
.load(`SlideWithTables.pptx`, 'table');
1111

12-
const data1 = {
13-
body: [
14-
{ label: 'item test r1', values: [10, 16, 12] },
15-
{ label: 'item test r2', values: [12, 18, 15] },
16-
{ label: 'item test r3', values: [14, 12, 11] },
17-
],
18-
};
12+
// const data1 = {
13+
// body: [
14+
// { label: 'item test r1', values: [10, 16, 12] },
15+
// { label: 'item test r2', values: [12, 18, 15] },
16+
// { label: 'item test r3', values: [14, 12, 11] },
17+
// ],
18+
// };
1919

2020
const data2 = [
2121
[10, 16, 12],

src/helper/modify-chart-helper.ts

Lines changed: 90 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,106 @@ import {
88
ChartSeries,
99
} from '../types/chart-types';
1010

11-
export const setChartData = (data: ChartData) => (
12-
element: XMLDocument,
13-
chart: Document,
14-
workbook: Workbook,
15-
): void => {
16-
const slots = [] as ChartSlot[];
17-
data.series.forEach((series: ChartSeries, s: number) => {
18-
slots.push({
19-
index: s,
20-
series: series,
21-
targetCol: s + 1,
22-
type: 'defaultSeries',
11+
export default class ModifyChartHelper {
12+
/**
13+
* Set chart data to modify default chart types.
14+
* See `__tests__/modify-existing-chart.test.js`
15+
*/
16+
static setChartData = (data: ChartData) => (
17+
element: XMLDocument,
18+
chart: Document,
19+
workbook: Workbook,
20+
): void => {
21+
const slots = [] as ChartSlot[];
22+
data.series.forEach((series: ChartSeries, s: number) => {
23+
slots.push({
24+
index: s,
25+
series: series,
26+
targetCol: s + 1,
27+
type: 'defaultSeries',
28+
});
2329
});
24-
});
25-
26-
new ModifyChart(chart, workbook, data, slots).modify();
2730

28-
// XmlHelper.dump(chart)
29-
// XmlHelper.dump(workbook.table)
30-
};
31+
new ModifyChart(chart, workbook, data, slots).modify();
3132

32-
export const setChartVerticalLines = (data: ChartData) => (
33-
element: XMLDocument,
34-
chart: Document,
35-
workbook: Workbook,
36-
): void => {
37-
const slots = [] as ChartSlot[];
33+
// XmlHelper.dump(chart)
34+
// XmlHelper.dump(workbook.table)
35+
};
3836

39-
slots.push({
40-
label: `Y-Values`,
41-
mapData: (point: number, category: ChartCategory) => category.y,
42-
targetCol: 1,
43-
});
37+
/**
38+
* Set chart data to modify vertical line charts.
39+
* See `__tests__/modify-chart-vertical-lines.test.js`
40+
*/
41+
static setChartVerticalLines = (data: ChartData) => (
42+
element: XMLDocument,
43+
chart: Document,
44+
workbook: Workbook,
45+
): void => {
46+
const slots = [] as ChartSlot[];
4447

45-
data.series.forEach((series: ChartSeries, s: number) => {
4648
slots.push({
47-
index: s,
48-
series: series,
49-
targetCol: s + 2,
50-
type: 'xySeries',
49+
label: `Y-Values`,
50+
mapData: (point: number, category: ChartCategory) => category.y,
51+
targetCol: 1,
5152
});
52-
});
5353

54-
new ModifyChart(chart, workbook, data, slots).modify();
55-
};
54+
data.series.forEach((series: ChartSeries, s: number) => {
55+
slots.push({
56+
index: s,
57+
series: series,
58+
targetCol: s + 2,
59+
type: 'xySeries',
60+
});
61+
});
5662

57-
export const setChartBubbles = (data: ChartData) => (
58-
element: XMLDocument,
59-
chart: Document,
60-
workbook: Workbook,
61-
): void => {
62-
const slots = [] as ChartSlot[];
63+
new ModifyChart(chart, workbook, data, slots).modify();
64+
};
6365

64-
data.series.forEach((series: ChartSeries, s: number) => {
65-
const colId = s * 3;
66-
slots.push({
67-
index: s,
68-
series: series,
69-
targetCol: colId + 1,
70-
type: 'customSeries',
71-
tag: 'c:xVal',
72-
mapData: (point: ChartBubble): number => point.x,
73-
});
74-
slots.push({
75-
label: `${series.label}-Y-Value`,
76-
index: s,
77-
series: series,
78-
targetCol: colId + 2,
79-
type: 'customSeries',
80-
tag: 'c:yVal',
81-
mapData: (point: ChartBubble): number => point.y,
82-
isStrRef: false,
83-
});
84-
slots.push({
85-
label: `${series.label}-Size`,
86-
index: s,
87-
series: series,
88-
targetCol: colId + 3,
89-
type: 'customSeries',
90-
tag: 'c:bubbleSize',
91-
mapData: (point: ChartBubble): number => point.size,
92-
isStrRef: false,
66+
/**
67+
* Set chart data to modify bubble charts.
68+
* See `__tests__/modify-chart-bubbles.test.js`
69+
*/
70+
static setChartBubbles = (data: ChartData) => (
71+
element: XMLDocument,
72+
chart: Document,
73+
workbook: Workbook,
74+
): void => {
75+
const slots = [] as ChartSlot[];
76+
77+
data.series.forEach((series: ChartSeries, s: number) => {
78+
const colId = s * 3;
79+
slots.push({
80+
index: s,
81+
series: series,
82+
targetCol: colId + 1,
83+
type: 'customSeries',
84+
tag: 'c:xVal',
85+
mapData: (point: ChartBubble): number => point.x,
86+
});
87+
slots.push({
88+
label: `${series.label}-Y-Value`,
89+
index: s,
90+
series: series,
91+
targetCol: colId + 2,
92+
type: 'customSeries',
93+
tag: 'c:yVal',
94+
mapData: (point: ChartBubble): number => point.y,
95+
isStrRef: false,
96+
});
97+
slots.push({
98+
label: `${series.label}-Size`,
99+
index: s,
100+
series: series,
101+
targetCol: colId + 3,
102+
type: 'customSeries',
103+
tag: 'c:bubbleSize',
104+
mapData: (point: ChartBubble): number => point.size,
105+
isStrRef: false,
106+
});
93107
});
94-
});
95108

96-
new ModifyChart(chart, workbook, data, slots).modify();
109+
new ModifyChart(chart, workbook, data, slots).modify();
97110

98-
// XmlHelper.dump(chart)
99-
};
111+
// XmlHelper.dump(chart)
112+
};
113+
}

src/helper/modify-helper.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import { XmlHelper } from './xml-helper';
22

3+
export default class ModifyHelper {
4+
/**
5+
* Set value of an attribute.
6+
* @param tagName specify the tag name to search for
7+
* @param attribute name of target attribute
8+
* @param value the value to be set on the attribute
9+
* @param [count] specify if element index is different to zero
10+
*/
11+
static setAttribute = (
12+
tagName: string,
13+
attribute: string,
14+
value: string | number,
15+
count?: number,
16+
) => (element: XMLDocument): void => {
17+
element
18+
.getElementsByTagName(tagName)
19+
[count || 0].setAttribute(attribute, String(value));
20+
};
321

4-
export const setAttribute = (
5-
tagName: string,
6-
attribute: string,
7-
value: string | number,
8-
count?: number,
9-
) => (element: XMLDocument): void => {
10-
element
11-
.getElementsByTagName(tagName)
12-
[count || 0].setAttribute(attribute, String(value));
13-
};
14-
15-
export const dump = (element: XMLDocument | Document | Element): void => {
16-
XmlHelper.dump(element);
17-
};
22+
/**
23+
* Dump current element to console.
24+
*/
25+
static dump = (element: XMLDocument | Document | Element): void => {
26+
XmlHelper.dump(element);
27+
};
28+
}

src/helper/modify-shape-helper.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
import { ShapeCoordinates } from '../types/shape-types';
22

3-
export const setSolidFill = (element: XMLDocument): void => {
4-
element
5-
.getElementsByTagName('a:solidFill')[0]
6-
.getElementsByTagName('a:schemeClr')[0]
7-
.setAttribute('val', 'accent6');
8-
};
9-
10-
export const setText = (text: string) => (element: XMLDocument): void => {
11-
element.getElementsByTagName('a:t')[0].firstChild.textContent = text;
12-
};
3+
export default class ModifyShapeHelper {
4+
/**
5+
* Set solid fill of modified shape
6+
*/
7+
static setSolidFill = (element: XMLDocument): void => {
8+
element
9+
.getElementsByTagName('a:solidFill')[0]
10+
.getElementsByTagName('a:schemeClr')[0]
11+
.setAttribute('val', 'accent6');
12+
};
1313

14-
// e.g. setPosition({x: 8000000, h:5000000})
15-
export const setPosition = (pos: ShapeCoordinates) => (
16-
element: XMLDocument,
17-
): void => {
18-
const map = {
19-
x: { tag: 'a:off', attribute: 'x' },
20-
y: { tag: 'a:off', attribute: 'y' },
21-
w: { tag: 'a:ext', attribute: 'cx' },
22-
h: { tag: 'a:ext', attribute: 'cy' },
14+
/**
15+
* Set text content of modified shape
16+
*/
17+
static setText = (text: string) => (element: XMLDocument): void => {
18+
element.getElementsByTagName('a:t')[0].firstChild.textContent = text;
2319
};
2420

25-
const parent = 'a:xfrm';
21+
/**
22+
* Set position and size of modified shape.
23+
*/
24+
static setPosition = (pos: ShapeCoordinates) => (
25+
element: XMLDocument,
26+
): void => {
27+
const map = {
28+
x: { tag: 'a:off', attribute: 'x' },
29+
y: { tag: 'a:off', attribute: 'y' },
30+
w: { tag: 'a:ext', attribute: 'cx' },
31+
h: { tag: 'a:ext', attribute: 'cy' },
32+
};
2633

27-
Object.keys(pos).forEach((key) => {
28-
element
29-
.getElementsByTagName(parent)[0]
30-
.getElementsByTagName(map[key].tag)[0]
31-
.setAttribute(map[key].attribute, pos[key]);
32-
});
33-
};
34+
const parent = 'a:xfrm';
35+
36+
Object.keys(pos).forEach((key) => {
37+
element
38+
.getElementsByTagName(parent)[0]
39+
.getElementsByTagName(map[key].tag)[0]
40+
.setAttribute(map[key].attribute, pos[key]);
41+
});
42+
};
43+
}

0 commit comments

Comments
 (0)