UNPKG

@native-html/heuristic-table-plugin

Version:

🔠 A 100% native component using heuristics to render tables in react-native-render-html

121 lines (94 loc) • 4.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _pipe = _interopRequireDefault(require("ramda/src/pipe")); var _sum = _interopRequireDefault(require("ramda/src/sum")); var _map = _interopRequireDefault(require("ramda/src/map")); var _max = _interopRequireDefault(require("ramda/src/max")); var _prop = _interopRequireDefault(require("ramda/src/prop")); var _reduce = _interopRequireDefault(require("ramda/src/reduce")); var _measure = require("./measure"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } function getInitCellStatsForTnode(tnode) { return { blockWidth: 0, horizontalSpace: (0, _measure.getHorizontalSpacing)(tnode.styles.nativeBlockRet), textStats: [] }; } const getMaxWordSize = (0, _pipe.default)((0, _map.default)((0, _prop.default)('length')), (0, _reduce.default)(_max.default, 0)); 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", (0, _pipe.default)((0, _map.default)(ch => ch.characters * this.getTextCoeff(ch)), _sum.default)); _defineProperty(this, "geTextMinWidth", (0, _pipe.default)((0, _map.default)(ch => ch.maxWordLength * this.getTextCoeff(ch)), (0, _reduce.default)(_max.default, 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 = (0, _measure.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 }; } } exports.default = TCellConstraintsComputer; //# sourceMappingURL=TCellConstraintsComputer.js.map