Commit bbd2cf6f by Maxime Richard Committed by Edgar HIPP

Code quality

parent 9cc5a1f3
......@@ -14,11 +14,12 @@ module.exports = class ImgManager {
}
getRelsFile(fileName) {
let relsFilePath;
let relsFileName = this.getRelsFileName(fileName);
let fileType = this.getFileType(fileName);
if (fileType == "ppt") {
const relsFileName = this.getRelsFileName(fileName);
const fileType = this.getFileType(fileName);
if (fileType === "ppt") {
relsFilePath = "ppt/slides/_rels/" + relsFileName;
} else {
}
else {
relsFilePath = "word/_rels/" + relsFileName;
}
return relsFilePath;
......@@ -100,8 +101,9 @@ module.exports = class ImgManager {
newTag.setAttribute("Id", `rId${maxRid}`);
newTag.setAttribute("Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
if (this.fileType === "ppt") {
newTag.setAttribute("Target", `../media/${realImageName}`)
} else {
newTag.setAttribute("Target", `../media/${realImageName}`);
}
else {
newTag.setAttribute("Target", `media/${realImageName}`);
}
relationships.appendChild(newTag);
......
......@@ -10,29 +10,23 @@ function isNaN(number) {
const ImgManager = require("./imgManager");
const moduleName = "open-xml-templating/docxtemplater-image-module";
function getInner({part, left, right, postparsed, index}) {
let xmlString = postparsed.slice(left + 1, right).reduce(function (concat, item) {
function getInner({part, left, right, postparsed}) {
const xmlString = postparsed.slice(left + 1, right).reduce(function (concat, item) {
return concat + item.value;
}, "");
part.off = {};
part.ext = {};
var xmlDoc = new DOMParser().parseFromString("<xml>" + xmlString + "</xml>");
var off = xmlDoc.getElementsByTagName("a:off");
part.off = {x: 0, y: 0};
part.ext = {cx: 0, cy: 0};
const xmlDoc = new DOMParser().parseFromString("<xml>" + xmlString + "</xml>");
const off = xmlDoc.getElementsByTagName("a:off");
if (off.length > 0) {
part.off.x = off[0].getAttribute("x");
part.off.y = off[0].getAttribute("y");
}
var ext = xmlDoc.getElementsByTagName("a:ext");
const ext = xmlDoc.getElementsByTagName("a:ext");
if (ext.length > 0) {
part.ext.cx = ext[0].getAttribute("cx");
part.ext.cy = ext[0].getAttribute("cy");
}
if (part.off.x == null || part.off.y == null || part.ext.cx == null || part.ext.cy == null) {
part.off.x = 0;
part.off.y = 0;
part.ext.cx = 0;
part.ext.cy = 0;
}
return part;
}
......@@ -76,9 +70,10 @@ class ImageModule {
}
postparse(parsed) {
let expandTo;
if (this.options.fileType == "pptx") {
if (this.options.fileType === "pptx") {
expandTo = "p:sp";
} else {
}
else {
expandTo = this.options.centered ? "w:p" : "w:t";
}
return DocUtils.traits.expandToOne(parsed, {moduleName, getInner, expandTo});
......@@ -88,43 +83,48 @@ class ImageModule {
if (!part.type === "placeholder" || part.module !== moduleName) {
return null;
}
try {
const tagValue = options.scopeManager.getValue(part.value);
const tagXml = this.fileTypeConfig.tagTextXml;
if (tagValue == null) {
return {value: tagXml};
if (!tagValue) {
throw new Error("tagValue is empty");
}
let imgBuffer;
try {
imgBuffer = this.options.getImage(tagValue, part.value);
const imgBuffer = this.options.getImage(tagValue, part.value);
const rId = this.imgManager.addImageRels(this.getNextImageName(), imgBuffer);
const sizePixel = this.options.getSize(imgBuffer, tagValue, part.value);
return this.getRenderedPart(part, rId, sizePixel);
}
catch (e) {
return {value: tagXml};
return {value: this.fileTypeConfig.tagTextXml};
}
const rId = this.imgManager.addImageRels(this.getNextImageName(), imgBuffer);
const sizePixel = this.options.getSize(imgBuffer, tagValue, part.value);
}
getRenderedPart(part, rId, sizePixel) {
const size = [this.convertPixelsToEmus(sizePixel[0]), this.convertPixelsToEmus(sizePixel[1])];
const centered = (this.options.centered || part.centered);
let newText;
if (this.options.fileType === "pptx") {
newText = this.getPptRender(part, rId, size, centered);
}
else {
newText = this.getDocxRender(part, rId, size, centered);
}
return {value: newText};
}
getPptRender(part, rId, size, centered) {
const offset = {x: parseInt(part.off.x, 10), y: parseInt(part.off.y, 10)};
const cellCX = parseInt(part.ext.cx, 10) || 1;
const cellCY = parseInt(part.ext.cy, 10) || 1;
const imgW = parseInt(size[0], 10) || 1;
const imgH = parseInt(size[1], 10) || 1;
if (this.options.fileType == "pptx") {
let offset = {x: parseInt(part.off.x, 10), y: parseInt(part.off.y, 10)};
let cellCX = parseInt(part.ext.cx, 10) || 1;
let cellCY = parseInt(part.ext.cy, 10) || 1;
let imgW = parseInt(size[0], 10) || 1;
let imgH = parseInt(size[1], 10) || 1;
if (this.options.centered || part.centered) {
if (centered) {
offset.x = offset.x + (cellCX / 2) - (imgW / 2);
offset.y = offset.y + (cellCY / 2) - (imgH / 2);
}
newText = this.getPPTImageXml(rId, [imgW, imgH], offset);
} else {
newText = (this.options.centered || part.centered) ? this.getImageXmlCentered(rId, size) : this.getImageXml(rId, size);
return this.getPptImageXml(rId, [imgW, imgH], offset);
}
return {value: newText};
getDocxRender(part, rId, size, centered) {
return (centered) ? this.getImageXmlCentered(rId, size) : this.getImageXml(rId, size);
}
getNextImageName() {
const name = `image_generated_${this.imageNumber}.png`;
......@@ -242,7 +242,7 @@ class ImageModule {
</w:p>
`.replace(/\t|\n/g, "");
}
getPPTImageXml(rId, size, off) {
getPptImageXml(rId, size, off) {
if (isNaN(rId)) {
throw new Error("rId is NaN, aborting");
}
......
......@@ -21,7 +21,7 @@ const fileNames = [
"withoutRels.docx",
"expectedWithoutRels.docx",
"tagImage.pptx",
"expectedTagImage.pptx"
"expectedTagImage.pptx",
];
beforeEach(function () {
......@@ -32,12 +32,12 @@ beforeEach(function () {
getSize: function () {
return [150, 150];
},
centered: false
centered: false,
};
this.loadAndRender = function () {
var fileType = (testutils.pptX[this.name]) ? 'pptx' : 'docx';
var file = (fileType == 'pptx') ? testutils.pptX[this.name] : testutils.docX[this.name];
const fileType = (testutils.pptX[this.name]) ? "pptx" : "docx";
const file = (fileType === "pptx") ? testutils.pptX[this.name] : testutils.docX[this.name];
this.doc = new Docxtemplater();
this.doc.setOptions({fileType});
this.opts.fileType = fileType;
......@@ -57,7 +57,7 @@ function testStart() {
this.name = "imageExample.docx";
this.expectedName = "expectedOneImage.docx";
this.data = {image: "examples/image.png"};
this.fileType = 'docx';
this.fileType = "docx";
this.loadAndRender();
});
......@@ -110,9 +110,10 @@ function testStart() {
testutils.setExamplesDirectory(path.resolve(__dirname, "..", "examples"));
testutils.setStartFunction(testStart);
fileNames.forEach(function (filename) {
if (filename.endsWith('pptx')) {
if (filename.indexOf(".pptx") === filename.length - 5) {
testutils.loadFile(filename, testutils.loadPptx);
} else {
}
else {
testutils.loadFile(filename, testutils.loadDocx);
}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment