diff --git a/src/components/ContentNode.vue b/src/components/ContentNode.vue index 05c41b40f..c8a6d9ed3 100644 --- a/src/components/ContentNode.vue +++ b/src/components/ContentNode.vue @@ -96,6 +96,13 @@ const TableHeaderStyle = { row: 'row', }; +const TableColumnAlignments = { + left: 'left', + right: 'right', + center: 'center', + unset: 'unset', +}; + // The point after which a TabNavigator turns to vertical mode. const TabNavigatorVerticalThreshold = 5; @@ -115,17 +122,22 @@ function renderNode(createElement, references) { )); const renderTableCell = ( - element, attrs, data, cellIndex, rowIndex, extendedData, + element, attrs, data, cellIndex, rowIndex, extendedData, alignments, ) => { const { colspan, rowspan } = extendedData[`${rowIndex}_${cellIndex}`] || {}; // if either is `0`, then its spanned over and should not be rendered if (colspan === 0 || rowspan === 0) return null; - return createElement(element, { attrs: { ...attrs, colspan, rowspan } }, ( + const align = alignments[cellIndex] || TableColumnAlignments.unset; + let classes = null; + if (align !== TableColumnAlignments.unset) classes = `${align}-cell`; + return createElement(element, { attrs: { ...attrs, colspan, rowspan }, class: classes }, ( renderChildren(data) )); }; - const renderTableChildren = (rows, headerStyle = TableHeaderStyle.none, extendedData = {}) => { + const renderTableChildren = ( + rows, headerStyle = TableHeaderStyle.none, extendedData = {}, alignments = [], + ) => { // build the matrix for the array switch (headerStyle) { // thead with first row and th for each first row cell @@ -135,14 +147,16 @@ function renderNode(createElement, references) { return [ createElement('thead', {}, [ createElement('tr', {}, firstRow.map((cell, cellIndex) => ( - renderTableCell('th', { scope: 'col' }, cell, cellIndex, 0, extendedData) + renderTableCell('th', { scope: 'col' }, cell, cellIndex, 0, extendedData, alignments) ))), ]), createElement('tbody', {}, otherRows.map(([firstCell, ...otherCells], rowIndex) => ( createElement('tr', {}, [ - renderTableCell('th', { scope: 'row' }, firstCell, 0, rowIndex + 1, extendedData), + renderTableCell( + 'th', { scope: 'row' }, firstCell, 0, rowIndex + 1, extendedData, alignments, + ), ...otherCells.map((cell, cellIndex) => ( - renderTableCell('td', {}, cell, cellIndex + 1, rowIndex + 1, extendedData) + renderTableCell('td', {}, cell, cellIndex + 1, rowIndex + 1, extendedData, alignments) )), ]) ))), @@ -153,9 +167,11 @@ function renderNode(createElement, references) { return [ createElement('tbody', {}, rows.map(([firstCell, ...otherCells], rowIndex) => ( createElement('tr', {}, [ - renderTableCell('th', { scope: 'row' }, firstCell, 0, rowIndex, extendedData), + renderTableCell( + 'th', { scope: 'row' }, firstCell, 0, rowIndex, extendedData, alignments, + ), ...otherCells.map((cell, cellIndex) => ( - renderTableCell('td', {}, cell, cellIndex + 1, rowIndex, extendedData) + renderTableCell('td', {}, cell, cellIndex + 1, rowIndex, extendedData, alignments) )), ]) ))), @@ -167,12 +183,12 @@ function renderNode(createElement, references) { return [ createElement('thead', {}, [ createElement('tr', {}, firstRow.map((cell, cellIndex) => renderTableCell( - 'th', { scope: 'col' }, cell, cellIndex, 0, extendedData, + 'th', { scope: 'col' }, cell, cellIndex, 0, extendedData, alignments, ))), ]), createElement('tbody', {}, otherRows.map((row, rowIndex) => ( createElement('tr', {}, row.map((cell, cellIndex) => ( - renderTableCell('td', {}, cell, cellIndex, rowIndex + 1, extendedData) + renderTableCell('td', {}, cell, cellIndex, rowIndex + 1, extendedData, alignments) ))) ))), ]; @@ -184,7 +200,7 @@ function renderNode(createElement, references) { rows.map((row, rowIndex) => ( createElement('tr', {}, ( row.map((cell, cellIndex) => ( - renderTableCell('td', {}, cell, cellIndex, rowIndex, extendedData) + renderTableCell('td', {}, cell, cellIndex, rowIndex, extendedData, alignments) )) )) )) @@ -269,7 +285,7 @@ function renderNode(createElement, references) { spanned: !!node.extendedData, }, }, ( - renderTableChildren(node.rows, node.header, node.extendedData) + renderTableChildren(node.rows, node.header, node.extendedData, node.alignments) )); case BlockType.termList: return createElement('dl', {}, node.items.map(({ term, definition }) => [ @@ -417,7 +433,7 @@ function renderNode(createElement, references) { export default { name: 'ContentNode', - constants: { TableHeaderStyle }, + constants: { TableHeaderStyle, TableColumnAlignments }, render: function render(createElement) { // Dynamically map each content item and any children to their // corresponding components, and wrap the whole tree in a