Skip to content

Commit 9eb5bb4

Browse files
author
Dobromir Hristov
authored
Add table spanning support (#420)
closes rdar://98017748
1 parent a71033c commit 9eb5bb4

File tree

4 files changed

+292
-103
lines changed

4 files changed

+292
-103
lines changed

src/components/ContentNode.vue

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,35 @@ function renderNode(createElement, references) {
114114
))
115115
));
116116
117-
const renderTableChildren = (rows, headerStyle = TableHeaderStyle.none) => {
117+
const renderTableCell = (
118+
element, attrs, data, cellIndex, rowIndex, extendedData,
119+
) => {
120+
const { colspan, rowspan } = extendedData[`${rowIndex}_${cellIndex}`] || {};
121+
// if either is `0`, then its spanned over and should not be rendered
122+
if (colspan === 0 || rowspan === 0) return null;
123+
return createElement(element, { attrs: { ...attrs, colspan, rowspan } }, (
124+
renderChildren(data)
125+
));
126+
};
127+
128+
const renderTableChildren = (rows, headerStyle = TableHeaderStyle.none, extendedData = {}) => {
129+
// build the matrix for the array
118130
switch (headerStyle) {
119131
// thead with first row and th for each first row cell
120132
// tbody with rows where first cell in each row is th, others are td
121133
case TableHeaderStyle.both: {
122134
const [firstRow, ...otherRows] = rows;
123135
return [
124136
createElement('thead', {}, [
125-
createElement('tr', {}, firstRow.map(cell => (
126-
createElement('th', { attrs: { scope: 'col' } }, (
127-
renderChildren(cell)
128-
))
137+
createElement('tr', {}, firstRow.map((cell, cellIndex) => (
138+
renderTableCell('th', { scope: 'col' }, cell, cellIndex, 0, extendedData)
129139
))),
130140
]),
131-
createElement('tbody', {}, otherRows.map(([firstCell, ...otherCells]) => (
141+
createElement('tbody', {}, otherRows.map(([firstCell, ...otherCells], rowIndex) => (
132142
createElement('tr', {}, [
133-
createElement('th', { attrs: { scope: 'row' } }, (
134-
renderChildren(firstCell)
135-
)),
136-
...otherCells.map(cell => (
137-
createElement('td', {}, (
138-
renderChildren(cell)
139-
))
143+
renderTableCell('th', { scope: 'row' }, firstCell, 0, rowIndex + 1, extendedData),
144+
...otherCells.map((cell, cellIndex) => (
145+
renderTableCell('td', {}, cell, cellIndex + 1, rowIndex + 1, extendedData)
140146
)),
141147
])
142148
))),
@@ -145,15 +151,11 @@ function renderNode(createElement, references) {
145151
// tbody with rows, th for first cell of each row, td for other cells
146152
case TableHeaderStyle.column:
147153
return [
148-
createElement('tbody', {}, rows.map(([firstCell, ...otherCells]) => (
154+
createElement('tbody', {}, rows.map(([firstCell, ...otherCells], rowIndex) => (
149155
createElement('tr', {}, [
150-
createElement('th', { attrs: { scope: 'row' } }, (
151-
renderChildren(firstCell)
152-
)),
153-
...otherCells.map(cell => (
154-
createElement('td', {}, (
155-
renderChildren(cell)
156-
))
156+
renderTableCell('th', { scope: 'row' }, firstCell, 0, rowIndex, extendedData),
157+
...otherCells.map((cell, cellIndex) => (
158+
renderTableCell('td', {}, cell, cellIndex + 1, rowIndex, extendedData)
157159
)),
158160
])
159161
))),
@@ -164,17 +166,13 @@ function renderNode(createElement, references) {
164166
const [firstRow, ...otherRows] = rows;
165167
return [
166168
createElement('thead', {}, [
167-
createElement('tr', {}, firstRow.map(cell => (
168-
createElement('th', { attrs: { scope: 'col' } }, (
169-
renderChildren(cell)
170-
))
169+
createElement('tr', {}, firstRow.map((cell, cellIndex) => renderTableCell(
170+
'th', { scope: 'col' }, cell, cellIndex, 0, extendedData,
171171
))),
172172
]),
173-
createElement('tbody', {}, otherRows.map(row => (
174-
createElement('tr', {}, row.map(cell => (
175-
createElement('td', {}, (
176-
renderChildren(cell)
177-
))
173+
createElement('tbody', {}, otherRows.map((row, rowIndex) => (
174+
createElement('tr', {}, row.map((cell, cellIndex) => (
175+
renderTableCell('td', {}, cell, cellIndex, rowIndex + 1, extendedData)
178176
)))
179177
))),
180178
];
@@ -183,12 +181,10 @@ function renderNode(createElement, references) {
183181
// tbody with all rows and every cell is td
184182
return [
185183
createElement('tbody', {}, (
186-
rows.map(row => (
184+
rows.map((row, rowIndex) => (
187185
createElement('tr', {}, (
188-
row.map(cell => (
189-
createElement('td', {}, (
190-
renderChildren(cell)
191-
))
186+
row.map((cell, cellIndex) => (
187+
renderTableCell('td', {}, cell, cellIndex, rowIndex, extendedData)
192188
))
193189
))
194190
))
@@ -268,8 +264,12 @@ function renderNode(createElement, references) {
268264
return renderFigure(node);
269265
}
270266
271-
return createElement(Table, {}, (
272-
renderTableChildren(node.rows, node.header)
267+
return createElement(Table, {
268+
props: {
269+
spanned: !!node.extendedData,
270+
},
271+
}, (
272+
renderTableChildren(node.rows, node.header, node.extendedData)
273273
));
274274
case BlockType.termList:
275275
return createElement('dl', {}, node.items.map(({ term, definition }) => [

src/components/ContentNode/Table.vue

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@
99
-->
1010

1111
<template>
12-
<div class="table-wrapper"><table><slot /></table></div>
12+
<div class="table-wrapper">
13+
<table :class="{ spanned }">
14+
<slot />
15+
</table>
16+
</div>
1317
</template>
1418

1519
<script>
16-
export default { name: 'Table' };
20+
export default {
21+
name: 'Table',
22+
props: {
23+
spanned: {
24+
type: Boolean,
25+
default: false,
26+
},
27+
},
28+
};
1729
</script>
1830

1931
<style lang="scss">
@@ -35,6 +47,10 @@ export default { name: 'Table' };
3547
-webkit-overflow-scrolling: touch;
3648
}
3749
50+
table {
51+
border-style: hidden;
52+
}
53+
3854
/deep/ {
3955
th {
4056
font-weight: $font-weight-semibold;
@@ -44,7 +60,7 @@ export default { name: 'Table' };
4460
th {
4561
border-color: var(--color-fill-gray-tertiary);
4662
border-style: solid;
47-
border-width: 1px 0;
63+
border-width: var(--table-border-width, 1px 1px);
4864
padding: rem(10px);
4965
}
5066
}

0 commit comments

Comments
 (0)