@awesome-fe/translate
Version:
Translation utils
49 lines • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TinyTableRenderer = void 0;
const base_tiny_node_renderer_1 = require("./base-tiny-node-renderer");
function renderAttributes(attributes) {
return Object.entries(attributes).map(([key, value]) => `data-${key}="${value}"`).join(' ');
}
function renderCellSpans(cell) {
const colSpan = cell.getColumnSpan();
const rowSpan = cell.getRowSpan();
const result = [colSpan && `data-colspan=${colSpan}`, rowSpan && `data-rowspan=${rowSpan}`].filter(it => !!it).join(' ');
return result && `${result} `;
}
class TinyTableRenderer extends base_tiny_node_renderer_1.BaseTinyNodeRenderer {
tagName = 'table';
getContent(node) {
return [
this.renderCaption(node),
this.renderColGroups(node.getColumns()),
this.renderHeads(node.getHeadRows()),
this.renderBody(node.getBodyRows()),
this.renderFoots(node.getFootRows()),
].filter(it => !!it).join('');
}
renderCaption(node) {
return node.getCaption().toString() ? `<caption>${node.getCaption()}</caption>` : '';
}
renderHeads(rows) {
const result = rows.map(row => `<tr>${this.renderRow(row, 'th')}</tr>`).join('');
return rows.length > 0 ? `<thead>${result}</thead>` : '';
}
renderBody(rows) {
const result = rows.map(row => `<tr>${this.renderRow(row, 'td')}</tr>`).join('');
return rows.length > 0 ? `<tbody>${result}</tbody>` : '';
}
renderFoots(rows) {
const result = rows.map(row => `<tr>${this.renderRow(row, 'td')}</tr>`).join('');
return rows.length > 0 ? `<tfoot>${result}</tfoot>` : '';
}
renderColGroups(columns) {
const result = columns.map(column => `<col ${renderAttributes(column.getAttributes())}/>`).join('');
return `<colgroup>${result}</colgroup>`;
}
renderRow(row, tagName) {
return row.map(cell => `<${tagName} ${renderCellSpans(cell)}${renderAttributes(cell.getAttributes())}>${cell.getContent()}</${tagName}>`).join('');
}
}
exports.TinyTableRenderer = TinyTableRenderer;
//# sourceMappingURL=tiny-table-renderer.js.map