@native-html/heuristic-table-plugin
Version:
🔠A 100% native component using heuristics to render tables in react-native-render-html
103 lines (87 loc) • 3.8 kB
JavaScript
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import pipe from 'ramda/src/pipe';
import sum from 'ramda/src/sum';
import map from 'ramda/src/map';
import max from 'ramda/src/max';
import prop from 'ramda/src/prop';
import reduce from 'ramda/src/reduce';
import { getHorizontalMargins, getHorizontalSpacing } from './measure';
function getInitCellStatsForTnode(tnode) {
return {
blockWidth: 0,
horizontalSpace: getHorizontalSpacing(tnode.styles.nativeBlockRet),
textStats: []
};
}
const getMaxWordSize = pipe(map(prop('length')), reduce(max, 0));
export default class TCellConstraintsComputer {
constructor({
baseFontCoeff,
fallbackFontSize
}) {
_defineProperty(this, "baseFontCoeff", void 0);
_defineProperty(this, "fallbackFontSize", void 0);
_defineProperty(this, "fontWeightCoeffs", {
'100': 0.8,
'200': 0.85,
'300': 0.9,
'400': 1,
'500': 1.1,
'600': 1.2,
'700': 1.3,
'800': 1.4,
'900': 1.5,
bold: 1.3,
normal: 1
});
_defineProperty(this, "getContentDensity", pipe(map(ch => ch.characters * this.getTextCoeff(ch)), sum));
_defineProperty(this, "geTextMinWidth", pipe(map(ch => ch.maxWordLength * this.getTextCoeff(ch)), reduce(max, 0)));
this.baseFontCoeff = baseFontCoeff !== null && baseFontCoeff !== void 0 ? baseFontCoeff : 0.65;
this.fallbackFontSize = fallbackFontSize !== null && fallbackFontSize !== void 0 ? fallbackFontSize : 14;
}
getTextCoeff(ch) {
return ch.fontFamilyCoeff * ch.fontSize * this.baseFontCoeff * ch.fontWeightCoeff;
}
assembleCellStats(tnode, stats = getInitCellStatsForTnode(tnode)) {
if (tnode.type === 'text') {
var _tnode$styles$nativeT, _tnode$styles$nativeT2, _this$fontWeightCoeff;
const fontSize = (_tnode$styles$nativeT = tnode.styles.nativeTextFlow.fontSize) !== null && _tnode$styles$nativeT !== void 0 ? _tnode$styles$nativeT : this.fallbackFontSize;
const fontWeight = (_tnode$styles$nativeT2 = tnode.styles.nativeTextFlow.fontWeight) !== null && _tnode$styles$nativeT2 !== void 0 ? _tnode$styles$nativeT2 : 'normal';
const fontWeightCoeff = (_this$fontWeightCoeff = this.fontWeightCoeffs[fontWeight]) !== null && _this$fontWeightCoeff !== void 0 ? _this$fontWeightCoeff : 1;
stats.textStats.push({
characters: tnode.data.length,
maxWordLength: getMaxWordSize(tnode.data.split(/\s+/)),
fontFamilyCoeff: 1,
fontSize,
fontWeightCoeff
});
} else {
if (tnode.type === 'block') {
const blockStyle = tnode.styles.nativeBlockRet;
const width = typeof blockStyle.width === 'number' ? blockStyle.width : typeof blockStyle.minWidth === 'number' ? blockStyle.minWidth : 0;
const margins = getHorizontalMargins(tnode.styles.nativeBlockRet);
stats.blockWidth = Math.max(stats.blockWidth, width + margins);
}
tnode.children.forEach(n => this.assembleCellStats(n, stats));
}
return stats;
}
computeTextConstraints(chunks) {
const minWidth = this.geTextMinWidth(chunks);
const contentDensity = this.getContentDensity(chunks);
return {
minWidth,
contentDensity
};
}
computeCellConstraints(tnode) {
const stats = this.assembleCellStats(tnode);
const blockWidth = stats.blockWidth;
const textConstrains = this.computeTextConstraints(stats.textStats);
return {
minWidth: Math.max(blockWidth, textConstrains.minWidth) + stats.horizontalSpace,
contentDensity: textConstrains.contentDensity
};
}
}
//# sourceMappingURL=TCellConstraintsComputer.js.map