UNPKG

iep-ui

Version:

An enterprise-class UI design language and Vue-based implementation

235 lines (204 loc) 7.13 kB
import Vue from 'vue'; var TEXT_NODE = 3; var COMMENT_NODE = 8; var ellipsisContainer = void 0; var wrapperStyle = { padding: 0, margin: 0, display: 'inline', lineHeight: 'inherit' }; export function pxToNumber(value) { if (!value) { return 0; } var match = value.match(/^\d*(\.\d*)?/); return match ? Number(match[0]) : 0; } export function styleToString(style) { var styleNames = Array.prototype.slice.apply(style); return styleNames.map(function (name) { return name + ': ' + style.getPropertyValue(name) + ';'; }).join(''); } export function mergeChildren(children) { var childList = []; children.forEach(function (child) { var prevChild = childList[childList.length - 1]; if (typeof child === 'string' && typeof prevChild === 'string') { childList[childList.length - 1] += child; } else { childList.push(child); } }); return childList; } export var resetDomStyles = function resetDomStyles(target, origin) { target.setAttribute('aria-hidden', 'true'); var originStyle = window.getComputedStyle(origin); var originCSS = styleToString(originStyle); // Set shadow target.setAttribute('style', originCSS); target.style.position = 'fixed'; target.style.left = '0'; target.style.height = 'auto'; target.style.minHeight = 'auto'; target.style.maxHeight = 'auto'; target.style.top = '-999999px'; target.style.zIndex = '-1000'; // clean up css overflow target.style.textOverflow = 'clip'; target.style.whiteSpace = 'normal'; target.style.webkitLineClamp = 'none'; }; export function getRealLineHeight(originElement) { var heightContainer = document.createElement('div'); resetDomStyles(heightContainer, originElement); heightContainer.appendChild(document.createTextNode('text')); document.body.appendChild(heightContainer); var offsetHeight = heightContainer.offsetHeight; var lineHeight = pxToNumber(window.getComputedStyle(originElement).lineHeight); document.body.removeChild(heightContainer); return offsetHeight > lineHeight ? offsetHeight : lineHeight; } export default (function (originElement, option, content, fixedContent, ellipsisStr) { if (!ellipsisContainer) { ellipsisContainer = document.createElement('div'); ellipsisContainer.setAttribute('aria-hidden', 'true'); } if (!ellipsisContainer.parentNode) { document.body.appendChild(ellipsisContainer); } var rows = option.rows, _option$suffix = option.suffix, suffix = _option$suffix === undefined ? '' : _option$suffix; // Get origin style var originStyle = window.getComputedStyle(originElement); var lineHeight = getRealLineHeight(originElement); var maxHeight = Math.floor(lineHeight) * (rows + 1) + pxToNumber(originStyle.paddingTop) + pxToNumber(originStyle.paddingBottom); resetDomStyles(ellipsisContainer, originElement); var vm = new Vue({ render: function render() { var h = arguments[0]; return h( 'div', { style: wrapperStyle }, [h( 'span', { style: wrapperStyle }, [content, suffix] ), h( 'span', { style: wrapperStyle }, [fixedContent] )] ); } }); vm.$mount(ellipsisContainer); function inRange() { return Math.ceil(ellipsisContainer.getBoundingClientRect().height) < maxHeight; } if (inRange()) { vm.$destroy(); return { content: content, text: ellipsisContainer.innerHTML, ellipsis: false }; } var childNodes = Array.prototype.slice.apply(ellipsisContainer.childNodes[0].childNodes[0].cloneNode(true).childNodes).filter(function (_ref) { var nodeType = _ref.nodeType, data = _ref.data; return nodeType !== COMMENT_NODE && data !== ''; }); var fixedNodes = Array.prototype.slice.apply(ellipsisContainer.childNodes[0].childNodes[1].cloneNode(true).childNodes); vm.$destroy(); // ========================= Find match ellipsis content ========================= var ellipsisChildren = []; ellipsisContainer.innerHTML = ''; // Create origin content holder var ellipsisContentHolder = document.createElement('span'); ellipsisContainer.appendChild(ellipsisContentHolder); var ellipsisTextNode = document.createTextNode(ellipsisStr + suffix); ellipsisContentHolder.appendChild(ellipsisTextNode); fixedNodes.forEach(function (childNode) { ellipsisContainer.appendChild(childNode); }); // Append before fixed nodes function appendChildNode(node) { ellipsisContentHolder.insertBefore(node, ellipsisTextNode); } // Get maximum text function measureText(textNode, fullText) { var startLoc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; var endLoc = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : fullText.length; var lastSuccessLoc = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0; var midLoc = Math.floor((startLoc + endLoc) / 2); var currentText = fullText.slice(0, midLoc); textNode.textContent = currentText; if (startLoc >= endLoc - 1) { // Loop when step is small for (var step = endLoc; step >= startLoc; step -= 1) { var currentStepText = fullText.slice(0, step); textNode.textContent = currentStepText; if (inRange() || !currentStepText) { return step === fullText.length ? { finished: false, reactNode: fullText } : { finished: true, reactNode: currentStepText }; } } } if (inRange()) { return measureText(textNode, fullText, midLoc, endLoc, midLoc); } return measureText(textNode, fullText, startLoc, midLoc, lastSuccessLoc); } function measureNode(childNode) { var type = childNode.nodeType; // if (type === ELEMENT_NODE) { // // We don't split element, it will keep if whole element can be displayed. // appendChildNode(childNode); // if (inRange()) { // return { // finished: false, // reactNode: contentList[index], // }; // } // // // Clean up if can not pull in // ellipsisContentHolder.removeChild(childNode); // return { // finished: true, // reactNode: null, // }; // } if (type === TEXT_NODE) { var fullText = childNode.textContent || ''; var textNode = document.createTextNode(fullText); appendChildNode(textNode); return measureText(textNode, fullText); } // Not handle other type of content // PS: This code should not be attached after react 16 /* istanbul ignore next */ return { finished: false, reactNode: null }; } childNodes.some(function (childNode, index) { var _measureNode = measureNode(childNode, index), finished = _measureNode.finished, reactNode = _measureNode.reactNode; if (reactNode) { ellipsisChildren.push(reactNode); } return finished; }); return { content: ellipsisChildren, text: ellipsisContainer.innerHTML, ellipsis: true }; });