nsn-comp
Version:
NSN核心组件
74 lines (58 loc) • 1.67 kB
JavaScript
import { NStr } from 'nsn-util';
var bisection = function bisection(props) {
var targetHeight = props.targetHeight,
midIndex = props.midIndex,
beginIndex = props.beginIndex,
endIndex = props.endIndex,
text = props.text,
shadowNode = props.shadowNode,
tail = props.tail;
var suffix = tail || '...';
var begin = beginIndex;
var mid = midIndex;
var end = endIndex;
if (shadowNode) {
shadowNode.innerHTML = text.substring(0, mid) + suffix;
var shadowNodeHeight = shadowNode.offsetHeight; //
if (shadowNodeHeight <= targetHeight) {
shadowNode.innerHTML = text.substring(0, mid + 1) + suffix;
shadowNodeHeight = shadowNode.offsetHeight;
if (shadowNodeHeight > targetHeight || NStr.isEqual(mid, begin)) {
return mid;
} //
begin = mid;
if (end - begin === 1) {
mid = 1 + begin;
} else {
mid = Math.floor((end - begin) / 2) + begin;
}
return bisection({
targetHeight: targetHeight,
midIndex: mid,
beginIndex: begin,
endIndex: end,
text: text,
shadowNode: shadowNode
});
} //
if (mid - 1 < 0) {
return mid;
} //
shadowNode.innerHTML = text.substring(0, mid - 1) + suffix;
shadowNodeHeight = shadowNode.offsetHeight;
if (shadowNodeHeight <= targetHeight) {
return mid - 1;
}
} //
end = mid;
mid = Math.floor((end - begin) / 2) + begin;
return bisection({
targetHeight: targetHeight,
midIndex: mid,
beginIndex: begin,
endIndex: end,
text: text,
shadowNode: shadowNode
});
};
export { bisection };