import { RefObject, useLayoutEffect } from "react"; import { BorderCell, BorderSide, BorderWidths, resolveCollapsedBorders, } from "../../collapsed-borders"; const SIDES: BorderSide[] = ["top", "right", "bottom", "left"]; const widthOf = (style: CSSStyleDeclaration, side: BorderSide) => parseFloat( style.getPropertyValue(`border-${side}-width`) || "0" ) || 0; const widthsOf = (style: CSSStyleDeclaration): BorderWidths => ({ top: widthOf(style, "top"), right: widthOf(style, "right"), bottom: widthOf(style, "bottom"), left: widthOf(style, "left"), }); const borderOf = (style: CSSStyleDeclaration, side: BorderSide) => [ style.getPropertyValue(`border-${side}-width`), style.getPropertyValue(`border-${side}-style`), style.getPropertyValue(`border-${side}-color`), ].join(" "); /** * The CSS that draws a table with separate borders the way the frontend does * when its cells round their corners (see TableRenderer's * `settleSeparateBorders`): a row's own border, which only a collapsed table * draws on its ``, moves onto the row's cells, and of two borders where * cells meet only one is kept. */ function resolveSeparateBordersCss(table: HTMLTableElement): string { const view = table.ownerDocument.defaultView; const body = table.tBodies[0]; if (!view || !body) { return ""; } const rows = Array.from(body.rows); const occupied: boolean[][] = rows.map(() => []); const cells: BorderCell[] = []; let cols = 0; rows.forEach((row, rowIndex) => { let col = 0; Array.from(row.cells).forEach(element => { while (occupied[rowIndex][col]) { col++; } const rowSpan = Math.max(1, element.rowSpan); const colSpan = Math.max(1, element.colSpan); for (let r = rowIndex; r < rowIndex + rowSpan && r < rows.length; r++) { for (let c = col; c < col + colSpan; c++) { occupied[r][c] = true; } } // Every cell block has its `block-` id to aim at. if (element.id) { cells.push({ id: element.id, row: rowIndex, col, rowSpan, colSpan, widths: widthsOf(view.getComputedStyle(element)), }); } col += colSpan; cols = Math.max(cols, col); }); }); const rules: string[] = []; const lastRowOf = (cell: BorderCell) => cell.row + cell.rowSpan - 1; cells.forEach(cell => { const rowStyleAt = (rowIndex: number) => view.getComputedStyle(rows[rowIndex]); const fromRow: Partial> = { top: rowStyleAt(cell.row), bottom: rowStyleAt(lastRowOf(cell)), }; if (cell.col === 0) { fromRow.left = rowStyleAt(cell.row); } if (cell.col + cell.colSpan >= cols) { fromRow.right = rowStyleAt(cell.row); } SIDES.forEach(side => { const rowStyle = fromRow[side]; if (!rowStyle) { return; } const rowWidth = widthOf(rowStyle, side); if (rowWidth > cell.widths[side]) { cell.widths[side] = rowWidth; rules.push( `#${cell.id} { border-${side}: ${borderOf( rowStyle, side )} !important; }` ); } }); }); const tableWidths = widthsOf(view.getComputedStyle(table)); resolveCollapsedBorders(cells, rows.length, cols, tableWidths).forEach( (sides, id) => { sides.forEach(side => { rules.push(`#${id} { border-${side}: none !important; }`); }); } ); return rules.join("\n"); } /** * Keeps `resolveSeparateBordersCss` in the given `