UNPKG

nsn-comp

Version:

NSN核心组件

56 lines (47 loc) 1.59 kB
import { NConst } from 'nsn-const'; import { bisection } from './bisection'; var isSupportLineClamp = document.body.style.webkitLineClamp !== undefined; var computeLine = function computeLine(comp, elements) { var lines = comp.props.lines; var normal = elements.normal, shadow = elements.shadow, contentContainer = elements.contentContainer, content = elements.content; if (lines && !isSupportLineClamp) { var _ref = normal || {}, innerText = _ref.innerText, textContent = _ref.textContent, _ref$offsetHeight = _ref.offsetHeight, totalHeight = _ref$offsetHeight === void 0 ? 0 : _ref$offsetHeight; var text = innerText || textContent || NConst.EMPTY; var textLength = text.length; var lineHeight = contentContainer ? parseInt(getComputedStyle(contentContainer).lineHeight, 10) : 10; var targetHeight = lines * lineHeight; if (content) { content.style.height = "".concat(targetHeight, "px"); } if (totalHeight <= targetHeight) { comp.setState({ text: text, targetCount: textLength }); return; } // bisection var _ref2 = shadow || {}, firstElementChild = _ref2.firstElementChild; var shadowNode = firstElementChild; var targetCount = bisection({ targetHeight: targetHeight, text: text, shadowNode: shadowNode, midIndex: Math.ceil(textLength / 2), beginIndex: 0, endIndex: textLength }); comp.setState({ text: text, targetCount: targetCount }); } }; export { computeLine };