Skip to content

Commit 0ad4f80

Browse files
committed
refactor(modify-helper): seperated by element type
1 parent 7075a97 commit 0ad4f80

File tree

8 files changed

+208
-161
lines changed

8 files changed

+208
-161
lines changed

src/automizer.ts

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { PresTemplate } from './interfaces/pres-template';
66
import { RootPresTemplate } from './interfaces/root-pres-template';
77
import { Template } from './classes/template';
88

9+
/**
10+
* Automizer
11+
*
12+
* The basic class for `pptx-automizer` package.
13+
* This class will be exported as `Automizer` by `index.ts`.
14+
*/
915
export default class Automizer implements IPresentationProps {
1016
rootTemplate: RootPresTemplate;
1117
templates: PresTemplate[];
@@ -15,8 +21,8 @@ export default class Automizer implements IPresentationProps {
1521
params: AutomizerParams;
1622

1723
/**
18-
* Parameters for Automizer constructor.
19-
* @param params
24+
* Creates an instance of `pptx-automizer`.
25+
* @param [params]
2026
*/
2127
constructor(params?: AutomizerParams) {
2228
this.templates = [];
@@ -30,25 +36,32 @@ export default class Automizer implements IPresentationProps {
3036

3137
/**
3238
* Load a pptx file and set it as root template.
33-
* @param {string} location - Filename or path to the template. Will be prefixed with 'templateDir'
34-
* @return {Automizer} Instance of Automizer
39+
* @param location - Filename or path to the template. Will be prefixed with 'templateDir'
40+
* @returns Instance of Automizer
3541
*/
3642
public loadRoot(location: string): this {
3743
return this.loadTemplate(location);
3844
}
3945

4046
/**
4147
* Load a template pptx file.
42-
* @param {string} location - Filename or path to the template. Will be prefixed with 'templateDir'
43-
* @param {string} name - Optional: A short name for the template. If skipped, the template will be named by its location.
44-
* @return {Automizer} Instance of Automizer
48+
* @param location - Filename or path to the template. Will be prefixed with 'templateDir'
49+
* @param name - Optional: A short name for the template. If skipped, the template will be named by its location.
50+
* @returns Instance of Automizer
4551
*/
4652
public load(location: string, name?: string): this {
4753
name = name === undefined ? location : name;
4854
return this.loadTemplate(location, name);
4955
}
5056

51-
public loadTemplate(location: string, name?: string): this {
57+
/**
58+
* Loads a pptx file either as a root template as a template file.
59+
* A name can be specified to give templates an alias.
60+
* @param location
61+
* @param [name]
62+
* @returns template
63+
*/
64+
private loadTemplate(location: string, name?: string): this {
5265
location = this.getLocation(location, 'template');
5366

5467
const newTemplate = Template.import(location, name);
@@ -62,18 +75,23 @@ export default class Automizer implements IPresentationProps {
6275
return this;
6376
}
6477

65-
isPresTemplate(
78+
/**
79+
* Determines whether template is root or default template.
80+
* @param template
81+
* @returns pres template
82+
*/
83+
private isPresTemplate(
6684
template: PresTemplate | RootPresTemplate,
6785
): template is PresTemplate {
6886
return 'name' in template;
6987
}
7088

7189
/**
72-
* Find imported template by given name and return a certain slide by number.
73-
* @param {string} name - Name of template; must be imported by Automizer.importTemplate()
74-
* @param {number} slideNumber - Number of slide in template presentation
75-
* @param callback - Executed when slide is created. Passes the newly created slide
76-
* @return {Automizer} Instance of Automizer
90+
* Find a slide in by number in one of the imported templates.
91+
* @param name - Name or alias of the template; must have been loaded with `Automizer.load()`
92+
* @param slideNumber - Number of slide in template presentation
93+
* @param callback - Executed after slide was added. The newly created slide will be passed to the callback as first argument.
94+
* @returns Instance of Automizer
7795
*/
7896
public addSlide(
7997
name: string,
@@ -102,7 +120,12 @@ export default class Automizer implements IPresentationProps {
102120
return this;
103121
}
104122

105-
public getTemplate(name: string): PresTemplate {
123+
/**
124+
* Searches this.templates to find template by given name.
125+
* @param name Alias name if given to loaded template.
126+
* @returns template
127+
*/
128+
private getTemplate(name: string): PresTemplate {
106129
const template = this.templates.find((t) => t.name === name);
107130
if (template === undefined) {
108131
throw new Error(`Template not found: ${name}`);
@@ -112,9 +135,10 @@ export default class Automizer implements IPresentationProps {
112135

113136
/**
114137
* Write all imports and modifications to a file.
115-
* @param {string} location - Filename or path for the file. Will be prefixed with 'outputDir'
138+
* @param location - Filename or path for the file. Will be prefixed with 'outputDir'
139+
* @returns summary object.
116140
*/
117-
async write(location: string): Promise<AutomizerSummary> {
141+
public async write(location: string): Promise<AutomizerSummary> {
118142
const rootArchive = await this.rootTemplate.archive;
119143

120144
for (const slide of this.rootTemplate.slides) {
@@ -130,6 +154,12 @@ export default class Automizer implements IPresentationProps {
130154
);
131155
}
132156

157+
/**
158+
* Applies path prefix to given location string.
159+
* @param location path and/or filename
160+
* @param [type] template or output
161+
* @returns location
162+
*/
133163
private getLocation(location: string, type?: string): string {
134164
switch (type) {
135165
case 'template':

src/helper/modify-chart-helper.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { ModifyChart } from '../modify/modify-chart';
2+
import { Workbook } from '../types/types';
3+
import {
4+
ChartData,
5+
ChartBubble,
6+
ChartSlot,
7+
ChartCategory,
8+
ChartSeries,
9+
} from '../types/chart-types';
10+
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',
23+
});
24+
});
25+
26+
new ModifyChart(chart, workbook, data, slots).modify();
27+
28+
// XmlHelper.dump(chart)
29+
// XmlHelper.dump(workbook.table)
30+
};
31+
32+
export const setChartVerticalLines = (data: ChartData) => (
33+
element: XMLDocument,
34+
chart: Document,
35+
workbook: Workbook,
36+
): void => {
37+
const slots = [] as ChartSlot[];
38+
39+
slots.push({
40+
label: `Y-Values`,
41+
mapData: (point: number, category: ChartCategory) => category.y,
42+
targetCol: 1,
43+
});
44+
45+
data.series.forEach((series: ChartSeries, s: number) => {
46+
slots.push({
47+
index: s,
48+
series: series,
49+
targetCol: s + 2,
50+
type: 'xySeries',
51+
});
52+
});
53+
54+
new ModifyChart(chart, workbook, data, slots).modify();
55+
};
56+
57+
export const setChartBubbles = (data: ChartData) => (
58+
element: XMLDocument,
59+
chart: Document,
60+
workbook: Workbook,
61+
): void => {
62+
const slots = [] as ChartSlot[];
63+
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,
93+
});
94+
});
95+
96+
new ModifyChart(chart, workbook, data, slots).modify();
97+
98+
// XmlHelper.dump(chart)
99+
};

src/helper/modify-helper.ts

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,5 @@
11
import { XmlHelper } from './xml-helper';
2-
import { ModifyChart } from '../modify/modify-chart';
3-
import { Workbook } from '../types/types';
4-
import {
5-
ChartData,
6-
ChartBubble,
7-
ChartSlot,
8-
ChartCategory,
9-
ChartSeries,
10-
} from '../types/chart-types';
11-
import { TableData } from '../types/table-types';
12-
import { ShapeCoordinates } from '../types/shape-types';
132

14-
export const setSolidFill = (element: XMLDocument): void => {
15-
element
16-
.getElementsByTagName('a:solidFill')[0]
17-
.getElementsByTagName('a:schemeClr')[0]
18-
.setAttribute('val', 'accent6');
19-
};
20-
21-
export const setText = (text: string) => (element: XMLDocument): void => {
22-
element.getElementsByTagName('a:t')[0].firstChild.textContent = text;
23-
};
24-
25-
// e.g. setPosition({x: 8000000, h:5000000})
26-
export const setPosition = (pos: ShapeCoordinates) => (
27-
element: XMLDocument,
28-
): void => {
29-
const map = {
30-
x: { tag: 'a:off', attribute: 'x' },
31-
y: { tag: 'a:off', attribute: 'y' },
32-
w: { tag: 'a:ext', attribute: 'cx' },
33-
h: { tag: 'a:ext', attribute: 'cy' },
34-
};
35-
36-
const parent = 'a:xfrm';
37-
38-
Object.keys(pos).forEach((key) => {
39-
element
40-
.getElementsByTagName(parent)[0]
41-
.getElementsByTagName(map[key].tag)[0]
42-
.setAttribute(map[key].attribute, pos[key]);
43-
});
44-
};
453

464
export const setAttribute = (
475
tagName: string,
@@ -54,102 +12,6 @@ export const setAttribute = (
5412
[count || 0].setAttribute(attribute, String(value));
5513
};
5614

57-
export const setChartData = (data: ChartData) => (
58-
element: XMLDocument,
59-
chart: Document,
60-
workbook: Workbook,
61-
): void => {
62-
const slots = [] as ChartSlot[];
63-
data.series.forEach((series: ChartSeries, s: number) => {
64-
slots.push({
65-
index: s,
66-
series: series,
67-
targetCol: s + 1,
68-
type: 'defaultSeries',
69-
});
70-
});
71-
72-
new ModifyChart(chart, workbook, data, slots).modify();
73-
74-
// XmlHelper.dump(chart)
75-
// XmlHelper.dump(workbook.table)
76-
};
77-
78-
export const setChartVerticalLines = (data: ChartData) => (
79-
element: XMLDocument,
80-
chart: Document,
81-
workbook: Workbook,
82-
): void => {
83-
const slots = [] as ChartSlot[];
84-
85-
slots.push({
86-
label: `Y-Values`,
87-
mapData: (point: number, category: ChartCategory) => category.y,
88-
targetCol: 1,
89-
});
90-
91-
data.series.forEach((series: ChartSeries, s: number) => {
92-
slots.push({
93-
index: s,
94-
series: series,
95-
targetCol: s + 2,
96-
type: 'xySeries',
97-
});
98-
});
99-
100-
new ModifyChart(chart, workbook, data, slots).modify();
101-
};
102-
103-
export const setChartBubbles = (data: ChartData) => (
104-
element: XMLDocument,
105-
chart: Document,
106-
workbook: Workbook,
107-
): void => {
108-
const slots = [] as ChartSlot[];
109-
110-
data.series.forEach((series: ChartSeries, s: number) => {
111-
const colId = s * 3;
112-
slots.push({
113-
index: s,
114-
series: series,
115-
targetCol: colId + 1,
116-
type: 'customSeries',
117-
tag: 'c:xVal',
118-
mapData: (point: ChartBubble): number => point.x,
119-
});
120-
slots.push({
121-
label: `${series.label}-Y-Value`,
122-
index: s,
123-
series: series,
124-
targetCol: colId + 2,
125-
type: 'customSeries',
126-
tag: 'c:yVal',
127-
mapData: (point: ChartBubble): number => point.y,
128-
isStrRef: false,
129-
});
130-
slots.push({
131-
label: `${series.label}-Size`,
132-
index: s,
133-
series: series,
134-
targetCol: colId + 3,
135-
type: 'customSeries',
136-
tag: 'c:bubbleSize',
137-
mapData: (point: ChartBubble): number => point.size,
138-
isStrRef: false,
139-
});
140-
});
141-
142-
new ModifyChart(chart, workbook, data, slots).modify();
143-
144-
// XmlHelper.dump(chart)
145-
};
146-
147-
export const setTableData = (data: TableData) => (
148-
element: XMLDocument | Document | Element,
149-
): void => {
150-
XmlHelper.dump(element);
151-
};
152-
15315
export const dump = (element: XMLDocument | Document | Element): void => {
15416
XmlHelper.dump(element);
15517
};

0 commit comments

Comments
 (0)