UNPKG

mp70-pptx-automizer

Version:
174 lines 8.97 kB
"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.OLEObject = 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 OLEObject extends shape_1.Shape { constructor(shape, targetType, sourceArchive) { var _a; super(shape, targetType); this.sourceArchive = sourceArchive; this.oleObjectPath = `ppt/embeddings/${this.sourceRid}${this.getFileExtension((_a = shape.target) === null || _a === void 0 ? void 0 : _a.file)}`; this.relRootTag = 'p:oleObj'; this.relAttribute = 'r:id'; } getFileExtension(file) { if (!file) return '.bin'; const ext = path_1.default.extname(file).toLowerCase(); return ['.bin', '.xls', '.xlsx', '.doc', '.docx', '.ppt', '.pptx'].includes(ext) ? ext : '.bin'; } // NOTE: modify is not currently implemented // async modify( // targetTemplate: RootPresTemplate, // targetSlideNumber: number, // ): Promise<OLEObject> { // await this.prepare(targetTemplate, targetSlideNumber); // await this.replaceIntoSlideTree(); // await this.editTargetOleObjectRel(); // return this; // } // NOTE: append is not currently implemented // async append( // targetTemplate: RootPresTemplate, // targetSlideNumber: number, // ): Promise<OLEObject> { // await this.prepare(targetTemplate, targetSlideNumber); // await this.appendToSlideTree(); // return this; // } // TODO: remove is not currently properly implemented, suggest we delete the file from the archive as well as removing the relationship. remove(targetTemplate, targetSlideNumber) { return __awaiter(this, void 0, void 0, function* () { yield this.prepare(targetTemplate, targetSlideNumber); yield this.removeFromSlideTree(); return this; }); } prepare(targetTemplate, targetSlideNumber, oleObjects) { return __awaiter(this, void 0, void 0, function* () { yield this.setTarget(targetTemplate, targetSlideNumber); this.targetNumber = this.targetTemplate.incrementCounter('oleObjects'); const allOleObjects = oleObjects || (yield OLEObject.getAllOnSlide(this.sourceArchive, this.targetSlideRelFile)); const oleObject = allOleObjects.find(obj => obj.rId === this.sourceRid); if (!oleObject) { throw new Error(`OLE object with rId ${this.sourceRid} not found.`); } const sourceFilePath = `ppt/embeddings/${oleObject.file.split('/').pop()}`; yield this.copyFiles(sourceFilePath); yield this.appendTypes(); yield this.appendToSlideRels(); }); } copyFiles(sourceFilePath) { return __awaiter(this, void 0, void 0, function* () { const fileExtension = this.getFileExtension(sourceFilePath); const targetFileName = `ppt/embeddings/oleObject${this.targetNumber}${fileExtension}`; try { yield file_helper_1.FileHelper.zipCopy(this.sourceArchive, sourceFilePath, this.targetArchive, targetFileName); } catch (error) { console.error("Error copying OLE object file:", error); throw error; } }); } appendTypes() { return __awaiter(this, void 0, void 0, function* () { yield this.appendOleObjectToContentType(); }); } 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 fileExtension = this.getFileExtension(this.oleObjectPath); const attributes = { Id: this.createdRid, Type: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject', Target: `../embeddings/oleObject${this.targetNumber}${fileExtension}`, }; yield xml_helper_1.XmlHelper.append(xml_helper_1.XmlHelper.createRelationshipChild(this.targetArchive, targetRelFile, attributes)); }); } updateSlideXml() { return __awaiter(this, void 0, void 0, function* () { const slideXmlPath = `ppt/slides/slide${this.targetSlideNumber}.xml`; const slideXml = yield xml_helper_1.XmlHelper.getXmlFromArchive(this.targetArchive, slideXmlPath); const oleObjs = slideXml.getElementsByTagName('p:oleObj'); Array.from(oleObjs).forEach((oleObj) => { if (oleObj.getAttribute('r:id') === this.sourceRid) { oleObj.setAttribute('r:id', this.createdRid); } }); yield xml_helper_1.XmlHelper.writeXmlToArchive(this.targetArchive, slideXmlPath, slideXml); }); } appendOleObjectToContentType() { 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 fileExtension = this.getFileExtension(this.oleObjectPath); const partName = `/ppt/embeddings/oleObject${this.targetNumber}${fileExtension}`; const existingOverride = Array.from(types.getElementsByTagName('Override')).find((override) => override.getAttribute('PartName') === partName); if (!existingOverride) { const newOverride = contentTypesXml.createElement('Override'); newOverride.setAttribute('PartName', partName); newOverride.setAttribute('ContentType', this.getContentType(fileExtension)); types.appendChild(newOverride); yield xml_helper_1.XmlHelper.writeXmlToArchive(this.targetArchive, contentTypesPath, contentTypesXml); } }); } getContentType(fileExtension) { const contentTypes = { '.bin': 'application/vnd.openxmlformats-officedocument.oleObject', '.xls': 'application/vnd.ms-excel', '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', '.doc': 'application/msword', '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', '.ppt': 'application/vnd.ms-powerpoint', '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', }; return contentTypes[fileExtension.toLowerCase()] || 'application/vnd.openxmlformats-officedocument.oleObject'; } static getAllOnSlide(archive, relsPath) { return __awaiter(this, void 0, void 0, function* () { const oleObjectType = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject'; return xml_helper_1.XmlHelper.getRelationshipItems(archive, relsPath, (element, rels) => { const type = element.getAttribute('Type'); if (type === oleObjectType) { rels.push({ rId: element.getAttribute('Id'), type: element.getAttribute('Type'), file: element.getAttribute('Target'), element: element, }); } }); }); } modifyOnAddedSlide(targetTemplate, targetSlideNumber, oleObjects) { return __awaiter(this, void 0, void 0, function* () { yield this.prepare(targetTemplate, targetSlideNumber, oleObjects); yield this.updateSlideXml(); }); } } exports.OLEObject = OLEObject; //# sourceMappingURL=ole%20copy.js.map