pptx-automizer
Version:
A template based pptx generator
346 lines • 13.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModifyTable = void 0;
const xml_helper_1 = require("../helper/xml-helper");
const modify_xml_helper_1 = __importDefault(require("../helper/modify-xml-helper"));
const modify_text_helper_1 = __importDefault(require("../helper/modify-text-helper"));
const index_1 = require("../index");
const general_helper_1 = require("../helper/general-helper");
class ModifyTable {
constructor(table, data) {
var _a;
this.maxCols = 0;
this.row = (index, children) => {
return {
'a:tr': {
forceCreate: true,
index: index,
children: children,
},
};
};
this.column = (index, children) => {
var _a;
return {
'a:tc': {
index: index,
children: children,
fromPrevious: !!((_a = this.params) === null || _a === void 0 ? void 0 : _a.expand),
},
};
};
this.cell = (value, style) => {
return {
'a:txBody': {
children: {
'a:t': {
modify: modify_text_helper_1.default.content(value),
},
'a:rPr': {
modify: modify_text_helper_1.default.style(style),
},
'a:r': {
collection: (collection) => {
xml_helper_1.XmlHelper.sliceCollection(collection, 1);
},
},
},
},
'a:tcPr': Object.assign({}, this.setCellStyle(style)),
};
};
this.expandRows = (count, rowId) => {
const tplRow = this.xml.getElementsByTagName('a:tr').item(rowId);
for (let r = 1; r <= count; r++) {
const newRow = tplRow.cloneNode(true);
xml_helper_1.XmlHelper.insertAfter(newRow, tplRow);
this.updateId(newRow, 'a16:rowId', r);
}
};
this.expandSpanColumns = (count, colId, gridSpan) => {
for (let cs = 1; cs <= count; cs++) {
const rows = this.xml.getElementsByTagName('a:tr');
for (let r = 0; r < rows.length; r++) {
const row = rows.item(r);
const columns = row.getElementsByTagName('a:tc');
const maxC = colId + gridSpan;
for (let c = colId; c < maxC; c++) {
const sourceCell = columns.item(c);
const insertAfter = columns.item(c + gridSpan - 1);
const clone = sourceCell.cloneNode(true);
xml_helper_1.XmlHelper.insertAfter(clone, insertAfter);
}
}
}
this.expandGrid(count, colId, gridSpan);
};
this.expandColumns = (count, colId) => {
for (let cs = 1; cs <= count; cs++) {
const rows = this.xml.getElementsByTagName('a:tr');
for (let r = 0; r < rows.length; r++) {
const row = rows.item(r);
const columns = row.getElementsByTagName('a:tc');
const sourceCell = columns.item(colId);
const newCell = this.getExpandCellClone(columns, sourceCell, colId);
xml_helper_1.XmlHelper.insertAfter(newCell, sourceCell);
}
}
this.expandGrid(count, colId, 1);
};
this.expandGrid = (count, colId, gridSpan) => {
const tblGrid = this.xml.getElementsByTagName('a:tblGrid').item(0);
for (let cs = 1; cs <= count; cs++) {
const maxC = colId + gridSpan;
for (let c = colId; c < maxC; c++) {
const sourceTblGridCol = tblGrid
.getElementsByTagName('a:gridCol')
.item(c);
const newCol = sourceTblGridCol.cloneNode(true);
xml_helper_1.XmlHelper.insertAfter(newCol, sourceTblGridCol);
this.updateId(newCol, 'a16:colId', c * (cs + 1) * colId * 1000);
}
}
};
this.updateId = (element, tag, id) => {
const idElement = element.getElementsByTagName(tag).item(0);
const previousId = Number(idElement.getAttribute('val'));
idElement.setAttribute('val', String(previousId + id));
};
this.data = data;
this.table = new modify_xml_helper_1.default(table);
this.xml = table;
(_a = this.data) === null || _a === void 0 ? void 0 : _a.body.forEach((row) => {
this.maxCols =
row.values.length > this.maxCols ? row.values.length : this.maxCols;
});
}
modify(params) {
this.params = params;
this.setRows();
this.setGridCols();
this.sliceRows();
this.sliceCols();
return this;
}
setRows() {
var _a, _b;
const alreadyExpanded = (_b = (_a = this.params) === null || _a === void 0 ? void 0 : _a.expand) === null || _b === void 0 ? void 0 : _b.find((expand) => expand.mode === 'column');
this.data.body.forEach((row, r) => {
row.values.forEach((cell, c) => {
var _a;
const rowStyles = row.styles && row.styles[c] ? row.styles[c] : {};
this.table.modify(this.row(r, this.column(c, this.cell(cell, rowStyles))));
this.table.modify({
'a16:rowId': {
index: r,
modify: modify_xml_helper_1.default.attribute('val', r),
},
});
if (((_a = this.params) === null || _a === void 0 ? void 0 : _a.expand) && !alreadyExpanded) {
this.expandOtherMergedCellsInColumn(c, r);
}
});
});
}
expandOtherMergedCellsInColumn(c, r) {
const rows = this.xml.getElementsByTagName('a:tr');
for (let rs = 0; rs < rows.length; rs++) {
// Skip current row
if (r !== rs) {
const row = rows.item(r);
const columns = row.getElementsByTagName('a:tc');
const sourceCell = columns.item(c);
this.expandGridSpan(sourceCell);
}
}
}
setGridCols() {
for (let c = 0; c <= this.maxCols; c++) {
this.table.modify({
'a:gridCol': {
index: c,
},
'a16:colId': {
index: c,
modify: modify_xml_helper_1.default.attribute('val', c),
},
});
}
}
sliceRows() {
this.table.modify({
'a:tbl': this.slice('a:tr', this.data.body.length),
});
}
sliceCols() {
this.table.modify({
'a:tblGrid': this.slice('a:gridCol', this.maxCols),
});
}
setCellStyle(style) {
const cellProps = {
modify: [],
children: {},
};
if (style.background) {
cellProps.modify.push(index_1.ModifyColorHelper.solidFill(style.background, 'last'));
}
if (style.border) {
cellProps.children = this.setCellBorder(style);
}
return cellProps;
}
setCellBorder(style) {
const borders = general_helper_1.GeneralHelper.arrayify(style.border);
const sortBorderTags = ['lnB', 'lnT', 'lnR', 'lnL'];
const modifications = {};
borders
.sort((b1, b2) => sortBorderTags.indexOf(b1.tag) < sortBorderTags.indexOf(b2.tag)
? -1
: 1)
.forEach((border) => {
const tag = 'a:' + border.tag;
const modifyCell = [];
if (border.color) {
modifyCell.push(index_1.ModifyColorHelper.solidFill(border.color));
}
if (border.weight) {
modifyCell.push(modify_xml_helper_1.default.attribute('w', border.weight));
}
modifications[tag] = {
modify: modifyCell,
};
if (border.type) {
modifications[tag].children = {
'a:prstDash': {
modify: modify_xml_helper_1.default.attribute('val', border.type),
},
};
}
});
return modifications;
}
slice(tag, length) {
return {
children: {
[tag]: {
collection: (collection) => {
xml_helper_1.XmlHelper.sliceCollection(collection, length);
},
},
},
};
}
adjustHeight() {
const tableHeight = this.getTableSize('cy');
const rowHeight = tableHeight / this.data.body.length;
this.data.body.forEach((row, r) => {
this.table.modify({
'a:tr': {
index: r,
modify: modify_xml_helper_1.default.attribute('h', Math.round(rowHeight)),
},
});
});
return this;
}
adjustWidth() {
var _a, _b, _c;
const tableWidth = this.getTableSize('cx');
const rowWidth = tableWidth / ((_b = (_a = this.data.body[0]) === null || _a === void 0 ? void 0 : _a.values) === null || _b === void 0 ? void 0 : _b.length) || 1;
(_c = this.data.body[0]) === null || _c === void 0 ? void 0 : _c.values.forEach((cell, c) => {
this.table.modify({
'a:gridCol': {
index: c,
modify: modify_xml_helper_1.default.attribute('w', Math.round(rowWidth)),
},
});
});
return this;
}
updateColumnWidth(c, size) {
const tableWidth = this.getTableSize('cx');
const targetSize = Math.round(size);
let currentSize = 0;
this.table.modify({
'a:gridCol': {
index: c,
modify: [
(ele) => {
currentSize = Number(ele.getAttribute('w'));
},
modify_xml_helper_1.default.attribute('w', targetSize),
],
},
});
const diff = currentSize - targetSize;
const targetWidth = tableWidth - diff;
this.setSize('cx', targetWidth);
return this;
}
updateRowHeight(r, size) {
const tableSize = this.getTableSize('cy');
const targetSize = Math.round(size);
let currentSize = 0;
this.table.modify({
'a:tr': {
index: r,
modify: [
(ele) => {
currentSize = Number(ele.getAttribute('h'));
},
modify_xml_helper_1.default.attribute('h', targetSize),
],
},
});
const diff = currentSize - targetSize;
const targetTableSize = tableSize - diff;
this.setSize('cy', targetTableSize);
return this;
}
setSize(orientation, size) {
const sizeElement = this.xml
.getElementsByTagName('p:xfrm')[0]
.getElementsByTagName('a:ext')[0];
sizeElement.setAttribute(orientation, String(size));
}
getTableSize(orientation) {
return Number(this.xml
.getElementsByTagName('p:xfrm')[0]
.getElementsByTagName('a:ext')[0]
.getAttribute(orientation));
}
getExpandCellClone(columns, sourceCell, colId) {
const hasGridSpan = this.expandGridSpan(sourceCell);
if (hasGridSpan) {
return columns.item(colId + 1).cloneNode(true);
}
const hMerge = sourceCell.getAttribute('hMerge');
if (hMerge) {
for (let findCol = colId - 1; colId >= 0; colId--) {
const previousSibling = columns.item(findCol);
if (!previousSibling) {
break;
}
const siblingHasSpan = this.expandGridSpan(previousSibling);
if (siblingHasSpan) {
break;
}
}
}
return sourceCell.cloneNode(true);
}
expandGridSpan(sourceCell) {
const gridSpan = sourceCell.getAttribute('gridSpan');
if (gridSpan) {
const incrementGridSpan = Number(gridSpan) + 1;
sourceCell.setAttribute('gridSpan', String(incrementGridSpan));
return true;
}
}
}
exports.ModifyTable = ModifyTable;
//# sourceMappingURL=modify-table.js.map