Skip to content

Commit 593f03a

Browse files
committed
chore(xml): add info keys to Target; fix return value for fileExistsInArchive
1 parent 69754c4 commit 593f03a

File tree

7 files changed

+73
-49
lines changed

7 files changed

+73
-49
lines changed

src/constants/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ export const TargetByRelIdMap = {
1515
relRootTag: 'a:blip',
1616
relAttribute: 'r:embed',
1717
prefix: '../media/image',
18-
expression: /\..+?$/,
1918
} as TargetByRelIdMapParam,
2019
'image:svg': {
2120
relRootTag: 'asvg:svgBlip',
2221
relAttribute: 'r:embed',
2322
prefix: '../media/image',
24-
expression: /\..+?$/,
2523
} as TargetByRelIdMapParam,
2624
};

src/dev.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,31 @@ const automizer = new Automizer({
1515
});
1616

1717
const run = async () => {
18-
const ppt = automizer
18+
// const pres = automizer
19+
// .loadRoot(`RootTemplate.pptx`)
20+
// .load(`SlideWithCharts.pptx`, 'charts');
21+
//
22+
// const result = await pres
23+
// .addSlide('charts', 2, (slide) => {
24+
// slide.modifyElement('ColumnChart', [
25+
// modify.setChartData(<ChartData>{
26+
// series: [
27+
// { label: 'series 1' },
28+
// { label: 'series 2' },
29+
// { label: 'series 3' },
30+
// ],
31+
// categories: [
32+
// { label: 'cat 2-1', values: [50, 50, 20] },
33+
// { label: 'cat 2-2', values: [14, 50, 20] },
34+
// { label: 'cat 2-3', values: [15, 50, 20] },
35+
// { label: 'cat 2-4', values: [26, 50, 20] },
36+
// ],
37+
// }),
38+
// ]);
39+
// })
40+
// .write(`modify-existing-chart.test.pptx`);
41+
42+
const pres = automizer
1943
.loadRoot(`RootTemplate.pptx`)
2044
.load(`EmptySlide.pptx`, 'EmptySlide')
2145
.load(`ChartWaterfall.pptx`, 'ChartWaterfall')

src/helper/file-helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class FileHelper {
3333
if (archive === undefined || archive.files[file] === undefined) {
3434
return false;
3535
}
36+
return true;
3637
}
3738

3839
static extractFileContent(file: Buffer): Promise<JSZip> {

src/helper/xml-helper.ts

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -138,40 +138,10 @@ export class XmlHelper {
138138
return max;
139139
}
140140

141-
static pushRelTargets(element: Element, prefix: string, targets: Target[]) {
142-
const type = element.getAttribute('Type');
143-
const target = element.getAttribute('Target');
144-
const rId = element.getAttribute('Id');
145-
146-
const subtype = _.last(prefix.split('/'));
147-
const relType = _.last(type.split('/'));
148-
149-
const matchNumber = target.match(/(\d+)/);
150-
const stripNumber =
151-
matchNumber && matchNumber[1] ? Number(matchNumber[1]) : 0;
152-
153-
if (XmlHelper.targetMatchesRelationship(relType, subtype, target, prefix)) {
154-
targets.push({
155-
file: target,
156-
rId: rId,
157-
number: stripNumber,
158-
type: type,
159-
subtype: subtype,
160-
} as Target);
161-
}
162-
}
163-
164-
static targetMatchesRelationship(relType, subtype, target, prefix) {
165-
if (relType === 'package') return true;
166-
167-
return relType === subtype && target.indexOf(prefix) === 0;
168-
}
169-
170141
static async getTargetsFromRelationships(
171142
archive: JSZip,
172143
path: string,
173144
prefix: string | string[],
174-
suffix?: string | RegExp,
175145
): Promise<Target[]> {
176146
const prefixes = typeof prefix === 'string' ? [prefix] : prefix;
177147

@@ -186,6 +156,45 @@ export class XmlHelper {
186156
);
187157
}
188158

159+
static pushRelTargets(element: Element, prefix: string, targets: Target[]) {
160+
const type = element.getAttribute('Type');
161+
const file = element.getAttribute('Target');
162+
const rId = element.getAttribute('Id');
163+
164+
const subtype = _.last(prefix.split('/'));
165+
const relType = _.last(type.split('/'));
166+
const filename = _.last(file.split('/'));
167+
const filenameExt = _.last(filename.split('.'));
168+
const filenameMatch = filename
169+
.replace('.' + filenameExt, '')
170+
.match(/^(.+?)(\d+)*$/);
171+
172+
const number =
173+
filenameMatch && filenameMatch[2] ? Number(filenameMatch[2]) : 0;
174+
const filenameBase =
175+
filenameMatch && filenameMatch[1] ? filenameMatch[1] : filename;
176+
177+
if (XmlHelper.targetMatchesRelationship(relType, subtype, file, prefix)) {
178+
targets.push({
179+
file,
180+
rId,
181+
number,
182+
type,
183+
subtype,
184+
prefix,
185+
filename,
186+
filenameExt,
187+
filenameBase,
188+
} as Target);
189+
}
190+
}
191+
192+
static targetMatchesRelationship(relType, subtype, target, prefix) {
193+
if (relType === 'package') return true;
194+
195+
return relType === subtype && target.indexOf(prefix) === 0;
196+
}
197+
189198
static async getTargetsByRelationshipType(
190199
archive: JSZip,
191200
path: string,
@@ -278,7 +287,6 @@ export class XmlHelper {
278287
archive,
279288
relsPath,
280289
params.prefix,
281-
params.expression,
282290
);
283291
const target = imageRels.find((rel) => rel.rId === sourceRid);
284292

src/modify/modify-chart.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ export class ModifyChart {
4747

4848
this.chart = new ModifyXmlHelper(chart);
4949
this.workbook = new ModifyXmlHelper(workbook.sheet);
50-
51-
if (workbook.table) {
52-
this.workbookTable = new ModifyXmlHelper(workbook.table);
53-
}
50+
this.workbookTable = workbook.table
51+
? new ModifyXmlHelper(workbook.table)
52+
: null;
5453

5554
this.sharedStrings = workbook.sharedStrings;
5655

@@ -65,7 +64,6 @@ export class ModifyChart {
6564
this.setSeriesDataLabels();
6665
this.setPointStyles();
6766
this.sliceChartSpace();
68-
6967
this.modifyWorkbook();
7068

7169
// XmlHelper.dump(this.chart.root as XMLDocument)
@@ -75,7 +73,6 @@ export class ModifyChart {
7573
this.setExtData();
7674
this.setExtSeries();
7775
this.sliceExtChartSpace();
78-
7976
this.modifyWorkbook();
8077
}
8178

src/shapes/chart.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ export class Chart extends Shape implements IChart {
199199
this.sourceArchive,
200200
this.wbRelsPath,
201201
`${this.wbEmbeddingsPath}${this.worksheetFilePrefix}`,
202-
this.wbExtension,
203202
);
204203

205204
const worksheet = worksheets[0];
@@ -216,16 +215,9 @@ export class Chart extends Shape implements IChart {
216215
this.sourceArchive,
217216
targetRelFile,
218217
this.wbEmbeddingsPath,
219-
this.wbExtension,
220218
);
221219

222-
const wbPath = relationTargets[0].file
223-
.replace(this.wbEmbeddingsPath, '')
224-
.replace(this.wbExtension, '');
225-
226-
const wbTitle = wbPath.match(/^(.+?)(\d+)*$/);
227-
228-
return wbTitle[0];
220+
return relationTargets[0].filenameBase;
229221
}
230222

231223
async appendTypes(): Promise<void> {

src/types/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export type Target = {
7171
rId?: string;
7272
prefix?: string;
7373
subtype?: ElementSubtype;
74+
type: string;
75+
filename: string;
76+
filenameExt: string;
77+
filenameBase: string;
7478
};
7579
export type ImportElement = {
7680
presName: string;

0 commit comments

Comments
 (0)