* Show ellipsis if overflow. * * @public * @param {string} text * @param {string} containerWidth * @param {string} font * @param {number} [ellipsis='...'] * @param {Object} [options] * @param {number} [options.maxIterations=3] * @param {number} [options.minChar=0] If truncate result
(text, containerWidth, font, ellipsis, options)
| 7844 | * @return {string} |
| 7845 | */ |
| 7846 | function truncateText(text, containerWidth, font, ellipsis, options) { |
| 7847 | if (!containerWidth) { |
| 7848 | return ''; |
| 7849 | } |
| 7850 | |
| 7851 | var textLines = (text + '').split('\n'); |
| 7852 | options = prepareTruncateOptions(containerWidth, font, ellipsis, options); |
| 7853 | |
| 7854 | // FIXME |
| 7855 | // It is not appropriate that every line has '...' when truncate multiple lines. |
| 7856 | for (var i = 0, len = textLines.length; i < len; i++) { |
| 7857 | textLines[i] = truncateSingleLine(textLines[i], options); |
| 7858 | } |
| 7859 | |
| 7860 | return textLines.join('\n'); |
| 7861 | } |
| 7862 | |
| 7863 | function prepareTruncateOptions(containerWidth, font, ellipsis, options) { |
| 7864 | options = extend({}, options); |
no test coverage detected