mp70-pptx-automizer
Version:
A template based pptx generator
136 lines • 6.18 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Image = void 0;
const mediaBase_1 = require("./mediaBase");
const element_type_1 = require("../../enums/element-type");
const xml_helper_1 = require("../../helper/xml-helper");
const file_helper_1 = require("../../helper/file-helper");
class Image extends mediaBase_1.MediaBase {
constructor(shape, targetType) {
super(shape, targetType);
this.mediaType = this.getMediaType();
this.relRootTag = this.getRelRootTag();
this.relAttribute = 'r:embed';
switch (this.extension) {
case 'svg':
this.relRootTag = 'asvg:svgBlip';
this.relParent = (element) => element.parentNode;
break;
default:
this.relRootTag = 'a:blip';
this.relParent = (element) => element.parentNode.parentNode;
break;
}
// Ensure mediaType is set correctly
this.mediaType = this.getMediaType();
}
getMediaType() {
const imageTypes = {
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'svg': 'image/svg+xml',
'wmf': 'image/wmf',
};
return imageTypes[this.extension.toLowerCase()] || 'image/png';
}
getRelRootTag() {
return this.extension.toLowerCase() === 'svg' ? 'asvg:svgBlip' : 'a:blip';
}
getRelationshipType() {
return 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image';
}
modifyOnAddedSlide(targetTemplate, targetSlideNumber) {
return __awaiter(this, void 0, void 0, function* () {
yield this.prepare(targetTemplate, targetSlideNumber);
yield this.updateElementsRelId();
return this;
});
}
modify(targetTemplate, targetSlideNumber) {
return __awaiter(this, void 0, void 0, function* () {
yield this.prepare(targetTemplate, targetSlideNumber);
yield this.setTargetElement();
yield this.updateTargetElementRelId();
this.applyImageCallbacks();
yield this.replaceIntoSlideTree();
return this;
});
}
append(targetTemplate, targetSlideNumber) {
const _super = Object.create(null, {
append: { get: () => super.append }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.append.call(this, targetTemplate, targetSlideNumber);
this.applyImageCallbacks();
if (this.hasSvgRelation()) {
const relsPath = `ppt/slides/_rels/slide${this.sourceSlideNumber}.xml.rels`;
const target = yield xml_helper_1.XmlHelper.getTargetByRelId(this.sourceArchive, relsPath, this.targetElement, 'image:svg');
yield new Image({
mode: 'append',
target,
sourceArchive: this.sourceArchive,
sourceSlideNumber: this.sourceSlideNumber,
type: element_type_1.ElementType.Image,
}, this.targetType).modify(targetTemplate, targetSlideNumber);
}
return this;
});
}
applyImageCallbacks() {
this.applyCallbacks(this.callbacks, this.targetElement, this.createdRelation);
}
prepare(targetTemplate, targetSlideNumber) {
return __awaiter(this, void 0, void 0, function* () {
yield this.setTarget(targetTemplate, targetSlideNumber);
this.targetNumber = this.targetTemplate.incrementCounter('images');
this.targetFile = 'image' + this.targetNumber + '.' + this.extension;
yield this.copyFiles();
yield this.appendTypes();
yield this.appendToSlideRels();
});
}
copyFiles() {
return __awaiter(this, void 0, void 0, function* () {
yield file_helper_1.FileHelper.zipCopy(this.sourceArchive, `ppt/media/${this.sourceFile}`, this.targetArchive, `ppt/media/${this.targetFile}`);
});
}
appendTypes() {
return __awaiter(this, void 0, void 0, function* () {
yield this.appendImageExtensionToContentType(this.extension);
});
}
appendToSlideRels() {
return __awaiter(this, void 0, void 0, function* () {
const targetRelFile = `ppt/${this.targetType}s/_rels/${this.targetType}${this.targetSlideNumber}.xml.rels`;
this.createdRid = yield xml_helper_1.XmlHelper.getNextRelId(this.targetArchive, targetRelFile);
const attributes = {
Id: this.createdRid,
Type: this.getRelationshipType(),
Target: `../media/${this.targetFile}`,
};
this.createdRelation = yield xml_helper_1.XmlHelper.append(xml_helper_1.XmlHelper.createRelationshipChild(this.targetArchive, targetRelFile, attributes));
});
}
hasSvgRelation() {
return this.targetElement.getElementsByTagName('asvg:svgBlip').length > 0;
}
static getAllOnSlide(archive, relsPath) {
return __awaiter(this, void 0, void 0, function* () {
return yield xml_helper_1.XmlHelper.getTargetsByRelationshipType(archive, relsPath, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image');
});
}
}
exports.Image = Image;
//# sourceMappingURL=image.js.map