Skip to content

Commit 9bf6111

Browse files
committed
refactor(image): unify Xml types; apply modifyElement to images
1 parent 44487df commit 9bf6111

File tree

12 files changed

+137
-65
lines changed

12 files changed

+137
-65
lines changed

src/automizer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { IPresentationProps } from './interfaces/ipresentation-props';
1010
import { PresTemplate } from './interfaces/pres-template';
1111
import { RootPresTemplate } from './interfaces/root-pres-template';
1212
import { Template } from './classes/template';
13-
import { ModifyXmlCallback, TemplateInfo } from './types/xml-types';
13+
import { ModifyXmlCallback, TemplateInfo, XmlElement } from './types/xml-types';
1414
import { GeneralHelper, vd } from './helper/general-helper';
1515
import { Master } from './classes/master';
1616
import path from 'path';
@@ -21,6 +21,7 @@ import { ContentTracker } from './helper/content-tracker';
2121
import JSZip from 'jszip';
2222
import { ISlide } from './interfaces/islide';
2323
import { IMaster } from './interfaces/imaster';
24+
import { ContentTypeExtension } from './enums/content-type-map';
2425

2526
/**
2627
* Automizer
@@ -203,7 +204,9 @@ export default class Automizer implements IPresentationProps {
203204
files.forEach((file) => {
204205
const directory = dir || this.params.mediaDir;
205206
const filepath = path.join(directory, file);
206-
const extension = path.extname(file).replace('.', '');
207+
const extension = path
208+
.extname(file)
209+
.replace('.', '') as ContentTypeExtension;
207210
try {
208211
fs.accessSync(filepath, fs.constants.F_OK);
209212
} catch (e) {

src/classes/has-shapes.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IPresentationProps } from '../interfaces/ipresentation-props';
66
import {
77
AnalyzedElementType,
88
AutomizerParams,
9+
ElementOnSlide,
910
FindElementSelector,
1011
FindElementStrategy,
1112
ImportedElement,
@@ -191,13 +192,15 @@ export default class HasShapes {
191192
const presName = this.sourceTemplate.name;
192193
const slideNumber = this.sourceNumber;
193194

194-
return this.addElementToModificationsList(
195+
this.addElementToModificationsList(
195196
presName,
196197
slideNumber,
197198
selector,
198199
'modify',
199200
callback,
200201
);
202+
203+
return this;
201204
}
202205

203206
/**
@@ -214,13 +217,15 @@ export default class HasShapes {
214217
selector: FindElementSelector,
215218
callback?: ShapeModificationCallback | ShapeModificationCallback[],
216219
): this {
217-
return this.addElementToModificationsList(
220+
this.addElementToModificationsList(
218221
presName,
219222
slideNumber,
220223
selector,
221224
'append',
222225
callback,
223226
);
227+
228+
return this;
224229
}
225230

226231
/**
@@ -231,13 +236,15 @@ export default class HasShapes {
231236
const presName = this.sourceTemplate.name;
232237
const slideNumber = this.sourceNumber;
233238

234-
return this.addElementToModificationsList(
239+
this.addElementToModificationsList(
235240
presName,
236241
slideNumber,
237242
selector,
238243
'remove',
239244
undefined,
240245
);
246+
247+
return this;
241248
}
242249

243250
/**
@@ -256,16 +263,14 @@ export default class HasShapes {
256263
selector: FindElementSelector,
257264
mode: string,
258265
callback?: ShapeModificationCallback | ShapeModificationCallback[],
259-
): this {
266+
): void {
260267
this.importElements.push({
261268
presName,
262269
slideNumber,
263270
selector,
264271
mode,
265272
callback,
266273
});
267-
268-
return this;
269274
}
270275

271276
/**
@@ -416,11 +421,7 @@ export default class HasShapes {
416421
sourceArchive: IArchive,
417422
sourcePath: string,
418423
useCreationIds: boolean,
419-
): Promise<{
420-
sourceElement: XmlDocument;
421-
selector: string;
422-
mode?: 'findByElementCreationId' | 'findByElementName';
423-
}> {
424+
): Promise<ElementOnSlide> {
424425
const strategies: FindElementStrategy[] = [];
425426
if (typeof selector === 'string') {
426427
if (useCreationIds) {
@@ -725,7 +726,7 @@ export default class HasShapes {
725726
* @returns element
726727
*/
727728
async analyzeElement(
728-
sourceElement: XmlDocument,
729+
sourceElement: XmlElement,
729730
sourceArchive: IArchive,
730731
slideNumber: number,
731732
): Promise<AnalyzedElementType> {

src/classes/shape.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
} from '../types/types';
1010
import { RootPresTemplate } from '../interfaces/root-pres-template';
1111
import { HelperElement, XmlDocument, XmlElement } from '../types/xml-types';
12-
import { ImageTypeMap } from '../enums/image-type-map';
12+
import {
13+
ContentTypeExtension,
14+
ContentTypeMap,
15+
} from '../enums/content-type-map';
1316
import { ElementSubtype } from '../enums/element-type';
1417
import IArchive from '../interfaces/iarchive';
1518

@@ -23,7 +26,7 @@ export class Shape {
2326
sourceNumber: number;
2427
sourceFile: string;
2528
sourceRid: string;
26-
sourceElement: XmlDocument;
29+
sourceElement: XmlElement;
2730

2831
targetFile: string;
2932
targetArchive: IArchive;
@@ -39,13 +42,13 @@ export class Shape {
3942
relAttribute: string;
4043
relParent: (element: XmlElement) => XmlElement;
4144

42-
targetElement: XmlDocument;
45+
targetElement: XmlElement;
4346
targetType: ShapeTargetType;
4447
target: Target;
4548

4649
callbacks: ShapeModificationCallback[];
4750
hasCreationId: boolean;
48-
contentTypeMap: typeof ImageTypeMap;
51+
contentTypeMap: typeof ContentTypeMap;
4952
subtype: ElementSubtype;
5053

5154
constructor(shape: ImportedElement, targetType: ShapeTargetType) {
@@ -60,7 +63,7 @@ export class Shape {
6063
this.hasCreationId = shape.hasCreationId;
6164

6265
this.callbacks = GeneralHelper.arrayify(shape.callback);
63-
this.contentTypeMap = ImageTypeMap;
66+
this.contentTypeMap = ContentTypeMap;
6467

6568
if (shape.target) {
6669
this.sourceNumber = shape.target.number;
@@ -84,7 +87,7 @@ export class Shape {
8487
}
8588

8689
async setTargetElement(): Promise<void> {
87-
this.targetElement = this.sourceElement.cloneNode(true) as XmlDocument;
90+
this.targetElement = this.sourceElement.cloneNode(true) as XmlElement;
8891
}
8992

9093
async appendToSlideTree(): Promise<void> {
@@ -128,7 +131,7 @@ export class Shape {
128131
this.name,
129132
);
130133

131-
if (!sourceElementOnTargetSlide) {
134+
if (!sourceElementOnTargetSlide?.parentNode) {
132135
console.error(`Can't modify slide tree for ${this.name}`);
133136
return;
134137
}
@@ -195,8 +198,8 @@ export class Shape {
195198

196199
applyCallbacks(
197200
callbacks: ShapeModificationCallback[],
198-
element: XmlDocument,
199-
arg1?: XmlDocument,
201+
element: XmlElement | XmlDocument,
202+
arg1?: XmlElement | XmlDocument,
200203
arg2?: Workbook,
201204
): void {
202205
callbacks.forEach((callback) => {
@@ -210,7 +213,9 @@ export class Shape {
210213
});
211214
}
212215

213-
appendImageExtensionToContentType(extension): Promise<XmlElement | boolean> {
216+
appendImageExtensionToContentType(
217+
extension: ContentTypeExtension,
218+
): Promise<XmlElement | boolean> {
214219
return XmlHelper.appendImageExtensionToContentType(
215220
this.targetArchive,
216221
extension,

src/dev.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const run = async () => {
1515
.load(`SlideWithImages.pptx`, 'images');
1616

1717
await pres
18-
.addSlide('shapes', 1, (slide) => {
19-
slide.addElement('images', 2, 'imagePNGduotone', [
18+
.addSlide('images', 2, (slide) => {
19+
slide.modifyElement('imagePNG', [
2020
ModifyShapeHelper.setPosition({
2121
w: CmToDxa(5),
2222
h: CmToDxa(5),
@@ -30,7 +30,38 @@ const run = async () => {
3030
},
3131
}),
3232
]);
33+
34+
slide.modifyElement('Textfeld 1', [
35+
ModifyShapeHelper.setPosition({
36+
w: CmToDxa(5),
37+
h: CmToDxa(5),
38+
}),
39+
// ModifyImageHelper.setRelationTarget('feather.png'),
40+
// ModifyImageHelper.setDuotoneFill({
41+
// tint: 100000,
42+
// color: {
43+
// type: 'srgbClr',
44+
// value: 'ff850c',
45+
// },
46+
// }),
47+
]);
3348
})
49+
// .addSlide('shapes', 1, (slide) => {
50+
// slide.addElement('images', 2, 'imagePNGduotone', [
51+
// ModifyShapeHelper.setPosition({
52+
// w: CmToDxa(5),
53+
// h: CmToDxa(5),
54+
// }),
55+
// ModifyImageHelper.setRelationTarget('feather.png'),
56+
// ModifyImageHelper.setDuotoneFill({
57+
// tint: 100000,
58+
// color: {
59+
// type: 'srgbClr',
60+
// value: 'ff850c',
61+
// },
62+
// }),
63+
// ]);
64+
// })
3465
.write(`modify-shapes.test.pptx`);
3566
};
3667

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export enum ImageTypeMap {
1+
export enum ContentTypeMap {
22
jpg = 'image/jpeg',
33
jpeg = 'image/jpeg',
44
png = 'image/png',
@@ -14,3 +14,5 @@ export enum ImageTypeMap {
1414
bin = 'application/vnd.openxmlformats-officedocument.oleObject',
1515
vml = 'application/vnd.openxmlformats-officedocument.vmlDrawing',
1616
}
17+
18+
export type ContentTypeExtension = keyof typeof ContentTypeMap;

src/helper/file-helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import IArchive, {
1010
import ArchiveJszip from './archive/archive-jszip';
1111
import ArchiveFs from './archive/archive-fs';
1212
import { vd } from './general-helper';
13+
import { ContentTypeExtension } from '../enums/content-type-map';
1314

1415
export class FileHelper {
1516
static importArchive(location: string, params: ArchiveParams): IArchive {
@@ -42,8 +43,8 @@ export class FileHelper {
4243
return removed;
4344
}
4445

45-
static getFileExtension(filename: string): string {
46-
return path.extname(filename).replace('.', '');
46+
static getFileExtension(filename: string): ContentTypeExtension {
47+
return path.extname(filename).replace('.', '') as ContentTypeExtension;
4748
}
4849

4950
static getFileInfo(filename: string): FileInfo {

src/helper/modify-shape-helper.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class ModifyShapeHelper {
2525
/**
2626
* Set solid fill of modified shape
2727
*/
28-
static setSolidFill = (element: XmlDocument | XmlElement): void => {
28+
static setSolidFill = (element: XmlElement): void => {
2929
element
3030
.getElementsByTagName('a:solidFill')[0]
3131
.getElementsByTagName('a:schemeClr')[0]
@@ -37,7 +37,7 @@ export default class ModifyShapeHelper {
3737
*/
3838
static setText =
3939
(text: string) =>
40-
(element: XmlDocument | XmlElement): void => {
40+
(element: XmlElement): void => {
4141
ModifyTextHelper.setText(text)(element as XmlElement);
4242
};
4343

@@ -46,7 +46,7 @@ export default class ModifyShapeHelper {
4646
*/
4747
static replaceText =
4848
(replaceText: ReplaceText | ReplaceText[], options?: ReplaceTextOptions) =>
49-
(element: XmlDocument | XmlElement): void => {
49+
(element: XmlElement): void => {
5050
const replaceTexts = GeneralHelper.arrayify(replaceText);
5151

5252
new TextReplaceHelper(options, element as XmlElement)
@@ -59,7 +59,7 @@ export default class ModifyShapeHelper {
5959
*/
6060
static setPosition =
6161
(pos: ShapeCoordinates) =>
62-
(element: XmlDocument | XmlElement): void => {
62+
(element: XmlElement): void => {
6363
const aOff = element.getElementsByTagName('a:off');
6464
if (!aOff?.item(0)) {
6565
return;
@@ -83,7 +83,7 @@ export default class ModifyShapeHelper {
8383
*/
8484
static updatePosition =
8585
(pos: ShapeCoordinates) =>
86-
(element: XmlDocument | XmlElement): void => {
86+
(element: XmlElement): void => {
8787
const xfrm = element.getElementsByTagName('a:off')[0]
8888
.parentNode as XmlElement;
8989

@@ -119,5 +119,4 @@ export default class ModifyShapeHelper {
119119
xfrm.setAttribute('rot', String(Math.round(degrees * 60000)));
120120
}
121121
};
122-
123122
}

0 commit comments

Comments
 (0)