image.jsā¢6.55 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Image = 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 element_type_1 = require("../enums/element-type");
class Image 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';
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;
}
}
/*
* It is necessary to update existing rIds for all
* unmodified images on an added slide at first.
*/
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) {
return __awaiter(this, void 0, void 0, function* () {
yield this.prepare(targetTemplate, targetSlideNumber);
yield this.setTargetElement();
yield this.updateTargetElementRelId();
yield this.appendToSlideTree();
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;
});
}
/*
* Apply all ShapeModificationCallbacks to target element.
* Third argument this.createdRelation is necessery to directly
* manipulate relation Target and change the image.
*/
applyImageCallbacks() {
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('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);
});
}
/**
* ToDo: This will always append a new relation, and never replace an
* existing relation. At the end of creation process, unused relations will
* remain existing in the .xml.rels file. PowerPoint will not complain, but
* integrity checks will not be valid by this.
*/
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: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
Target: `../media/image${this.targetNumber}.${this.extension}`,
};
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