@native-html/heuristic-table-plugin
Version:
🔠A 100% native component using heuristics to render tables in react-native-render-html
101 lines (87 loc) • 2.75 kB
JavaScript
import pipe from 'ramda/src/pipe';
import prop from 'ramda/src/prop';
import slice from 'ramda/src/slice';
import sum from 'ramda/src/sum';
import map from 'ramda/src/map';
import max from 'ramda/src/max';
import reduce from 'ramda/src/reduce';
import partial from 'ramda/src/partial';
import flatten from 'ramda/src/flatten';
import sort from 'ramda/src/sort';
import filter from 'ramda/src/filter';
import makeRows from './makeRows';
const getRowGroupHeight = pipe(map(prop('lenY')), reduce(max, 0));
function groupCellsByVGroup(cellsByRow) {
const cellsByVGroup = [];
let rowHeight = 1;
for (let i = 0; i < cellsByRow.length; i += Math.max(rowHeight, 1)) {
const row = cellsByRow[i];
rowHeight = getRowGroupHeight(row);
cellsByVGroup.push(slice(i, i + rowHeight)(cellsByRow));
}
return cellsByVGroup;
}
function makeRowContainer(cells) {
return {
type: 'row-container',
children: cells
};
}
function makeColContainer(cells) {
return {
type: 'col-container',
children: makeRows(cells).map(makeRowContainer)
};
}
const splitToColumnContainers = pipe(flatten, function (cells) {
let breakpointsX = pipe(filter(cell => cell.lenY > 1), map(prop('x')))(cells);
let breakpointIndex = 0;
const cellsByRow = sort((a, b) => a.x - b.x)(cells);
let containers = [];
let colGroup = [];
for (const cell of cellsByRow) {
var _breakpointsX$breakpo;
if (cell.x < ((_breakpointsX$breakpo = breakpointsX[breakpointIndex]) !== null && _breakpointsX$breakpo !== void 0 ? _breakpointsX$breakpo : Infinity)) {
colGroup.push(cell);
} else {
colGroup.length && containers.push(colGroup);
containers.push([cell]);
colGroup = [];
breakpointIndex += 1;
}
}
colGroup.length && containers.push(colGroup);
return containers.map(makeColContainer);
});
function translateVGroups(virtualRowGroups) {
const flattenRows = [];
for (const rowGroup of virtualRowGroups) {
if (rowGroup.length === 1) {
flattenRows.push({
type: 'row-container',
children: rowGroup[0]
});
} else {
const container = {
type: 'row-container',
children: splitToColumnContainers(rowGroup)
};
flattenRows.push(container);
}
}
return flattenRows;
}
function makeCell(columnWidths, cell) {
return { ...cell,
type: 'cell',
width: pipe(slice(cell.x, cell.x + cell.lenX), sum)(columnWidths)
};
}
export default function createRenderTree(display, columnWidths) {
const children = pipe(map(partial(makeCell, [columnWidths])), makeRows, groupCellsByVGroup, translateVGroups)(display.cells);
return {
type: 'root',
children: children
};
}
//# sourceMappingURL=createRenderTree.js.map