mp70-pptx-automizer
Version:
A template based pptx generator
129 lines • 6.12 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaBase = void 0;
const file_helper_1 = require("../../helper/file-helper");
const xml_helper_1 = require("../../helper/xml-helper");
const shape_1 = require("../../classes/shape");
const path_1 = __importDefault(require("path"));
class MediaBase extends shape_1.Shape {
constructor(shape, targetType) {
super(shape, targetType);
this.sourceFile = shape.target.file.replace('../media/', '');
this.extension = file_helper_1.FileHelper.getFileExtension(this.sourceFile);
this.relAttribute = 'r:embed';
// Define relParent as a property that returns a function
this.relParent = (element) => element.parentNode.parentNode;
}
getMediaType() {
return this.mediaType;
}
getRelRootTag() {
return this.relRootTag;
}
getRelationshipType() {
return this.relationshipType;
}
getCounterType() {
return this.counterType;
}
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.applyMediaCallbacks();
yield this.replaceIntoSlideTree();
return this;
});
}
append(targetTemplate, targetSlideNumber) {
return __awaiter(this, void 0, void 0, function* () {
yield this.prepare(targetTemplate, targetSlideNumber);
yield this.setTargetElement();
yield this.updateTargetElementRelId();
yield this.appendToSlideTree();
this.applyMediaCallbacks();
return this;
});
}
applyMediaCallbacks() {
this.applyCallbacks(this.callbacks, this.targetElement, this.createdRelation);
}
remove(targetTemplate, targetSlideNumber) {
return __awaiter(this, void 0, void 0, function* () {
yield this.prepare(targetTemplate, targetSlideNumber);
yield this.removeFromSlideTree();
return this;
});
}
prepare(targetTemplate, targetSlideNumber) {
return __awaiter(this, void 0, void 0, function* () {
yield this.setTarget(targetTemplate, targetSlideNumber);
this.targetNumber = this.targetTemplate.incrementCounter(this.getCounterType());
this.targetFile = `media${this.targetNumber}${path_1.default.extname(this.sourceFile)}`;
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* () {
const contentTypesPath = '[Content_Types].xml';
const contentTypesXml = yield xml_helper_1.XmlHelper.getXmlFromArchive(this.targetArchive, contentTypesPath);
const types = contentTypesXml.getElementsByTagName('Types')[0];
const partName = `/ppt/media/${this.targetFile}`;
const contentType = this.mediaType;
const newOverride = contentTypesXml.createElement('Override');
newOverride.setAttribute('PartName', partName);
newOverride.setAttribute('ContentType', contentType);
types.appendChild(newOverride);
console.log(`Content Type for ${this.targetFile}: ${contentType}`);
yield xml_helper_1.XmlHelper.writeXmlToArchive(this.targetArchive, contentTypesPath, contentTypesXml);
});
}
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 relationshipType = this.getRelationshipType();
const attributes = {
Id: this.createdRid,
Type: relationshipType,
Target: `../media/${this.targetFile}`,
};
this.createdRelation = yield xml_helper_1.XmlHelper.append(xml_helper_1.XmlHelper.createRelationshipChild(this.targetArchive, targetRelFile, attributes));
});
}
static getAllOnSlide(archive, relsPath) {
return __awaiter(this, void 0, void 0, function* () {
console.log("GET", MediaBase.prototype.getRelationshipType());
return xml_helper_1.XmlHelper.getTargetsByRelationshipType(archive, relsPath, MediaBase.prototype.getRelationshipType());
});
}
}
exports.MediaBase = MediaBase;
//# sourceMappingURL=mediaBase.js.map