get-ellipsis-text.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import isString from './is-string';
  2. import toString from './to-string';
  3. import { Font, default as measureTextWidth } from './measure-text-width';
  4. /**
  5. * 获取文本的 ... 文本。
  6. * 算法(减少每次 measureText 的长度,measureText 的性能跟字符串时间相关):
  7. * 1. 先通过 STEP 逐步计算,找到最后一个小于 maxWidth 的字符串
  8. * 2. 然后对最后这个字符串二分计算
  9. * @param text 需要计算的文本, 由于历史原因 除了支持string,还支持空值,number和数组等
  10. * @param maxWidth 最大宽度
  11. * @param font 字体
  12. * @param str 要替换的文本
  13. */
  14. export default (text: string | number, maxWidth: number, font?: Font, str: string = '...') => {
  15. const STEP = 16; // 每次 16,调参工程师
  16. const PLACEHOLDER_WIDTH = measureTextWidth(str, font);
  17. let leftText = !isString(text) ? toString(text) : text;
  18. let leftWidth = maxWidth;
  19. const r = []; // 最终的分段字符串
  20. let currentText;
  21. let currentWidth;
  22. if (measureTextWidth(text, font) <= maxWidth) {
  23. return text;
  24. }
  25. // 首先通过 step 计算,找出最大的未超出长度的
  26. // eslint-disable-next-line no-constant-condition
  27. while (true) {
  28. // 更新字符串
  29. currentText = leftText.substr(0, STEP);
  30. // 计算宽度
  31. currentWidth = measureTextWidth(currentText, font);
  32. // 超出剩余宽度,则停止
  33. if (currentWidth + PLACEHOLDER_WIDTH > leftWidth) {
  34. if (currentWidth > leftWidth) {
  35. break;
  36. }
  37. }
  38. r.push(currentText);
  39. // 没有超出,则计算剩余宽度
  40. leftWidth -= currentWidth;
  41. leftText = leftText.substr(STEP);
  42. // 字符串整体没有超出
  43. if (!leftText) {
  44. return r.join('');
  45. }
  46. }
  47. // 最下的最后一个 STEP,使用 1 递增(用二分效果更高)
  48. // eslint-disable-next-line no-constant-condition
  49. while (true) {
  50. // 更新字符串
  51. currentText = leftText.substr(0, 1);
  52. // 计算宽度
  53. currentWidth = measureTextWidth(currentText, font);
  54. // 超出剩余宽度,则停止
  55. if (currentWidth + PLACEHOLDER_WIDTH > leftWidth) {
  56. break;
  57. }
  58. r.push(currentText);
  59. // 没有超出,则计算剩余宽度
  60. leftWidth -= currentWidth;
  61. leftText = leftText.substr(1);
  62. if (!leftText) {
  63. return r.join('');
  64. }
  65. }
  66. return `${r.join('')}${str}`;
  67. };