util.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import ReactDOM from 'react-dom';
  2. import React from 'react';
  3. /**
  4. * The logic of JS for text truncation is referenced from antd typography
  5. * https://github.com/ant-design/ant-design/blob/master/components/typography/util.tsx
  6. *
  7. * For more thinking and analysis about this function, please refer to Feishu document
  8. * https://bytedance.feishu.cn/docs/doccnqovjjyoKm2U5O13bj30aTh
  9. */
  10. let ellipsisContainer: HTMLElement;
  11. function pxToNumber(value: string) {
  12. if (!value) {
  13. return 0;
  14. }
  15. const match = value.match(/^\d*(\.\d*)?/);
  16. return match ? Number(match[0]) : 0;
  17. }
  18. function styleToString(style: CSSStyleDeclaration): string {
  19. // There are some different behavior between Firefox & Chrome.
  20. // We have to handle this ourself.
  21. const styleNames = Array.prototype.slice.apply(style);
  22. return styleNames.map((name: string) => `${name}: ${style.getPropertyValue(name)};`).join('');
  23. }
  24. const getRenderText = (
  25. originEle: HTMLElement,
  26. rows: number,
  27. content = '',
  28. fixedContent: any[],
  29. ellipsisStr: string,
  30. suffix: string,
  31. ellipsisPos: string
  32. // eslint-disable-next-line max-params
  33. ) => {
  34. if (!ellipsisContainer) {
  35. ellipsisContainer = document.createElement('div');
  36. ellipsisContainer.setAttribute('aria-hidden', 'true');
  37. document.body.appendChild(ellipsisContainer);
  38. }
  39. // Get origin style
  40. const originStyle = window.getComputedStyle(originEle);
  41. const originCSS = styleToString(originStyle);
  42. const lineHeight = pxToNumber(originStyle.lineHeight);
  43. const maxHeight = Math.round(
  44. lineHeight * (rows + 1) +
  45. pxToNumber(originStyle.paddingTop) +
  46. pxToNumber(originStyle.paddingBottom)
  47. );
  48. // Set shadow
  49. ellipsisContainer.setAttribute('style', originCSS);
  50. ellipsisContainer.style.position = 'fixed';
  51. ellipsisContainer.style.left = '0';
  52. ellipsisContainer.style.height = 'auto';
  53. ellipsisContainer.style.top = '-999999px';
  54. ellipsisContainer.style.zIndex = '-1000';
  55. // clean up css overflow
  56. ellipsisContainer.style.textOverflow = 'clip';
  57. ellipsisContainer.style.webkitLineClamp = 'none';
  58. // Render fake container
  59. ReactDOM.render(
  60. <></>,
  61. ellipsisContainer
  62. );
  63. // Check if ellipsis in measure div is height enough for content
  64. function inRange() {
  65. // console.log('inrange?', ellipsisContainer.scrollHeight, ellipsisContainer.scrollHeight < maxHeight)
  66. return ellipsisContainer.scrollHeight < maxHeight;
  67. }
  68. // ========================= Find match ellipsis content =========================
  69. // Create origin content holder
  70. const ellipsisContentHolder = document.createElement('span');
  71. const ellipsisTextNode = document.createTextNode(suffix);
  72. ellipsisContentHolder.appendChild(ellipsisTextNode);
  73. ellipsisContainer.appendChild(ellipsisContentHolder);
  74. fixedContent.map((node: Node) => node && ellipsisContainer.appendChild(node.cloneNode(true)));
  75. // Append before fixed nodes
  76. function appendChildNode(node: ChildNode) {
  77. ellipsisContentHolder.insertBefore(node, ellipsisTextNode);
  78. }
  79. function getCurrentText(text: string, pos: number) {
  80. const end = text.length;
  81. if (!pos) {
  82. return ellipsisStr;
  83. }
  84. if (ellipsisPos === 'end' || pos > end - pos) {
  85. return text.slice(0, pos) + ellipsisStr;
  86. }
  87. return text.slice(0, pos) + ellipsisStr + text.slice(end - pos, end);
  88. }
  89. // Get maximum text
  90. function measureText(
  91. textNode: Text,
  92. fullText: string,
  93. startLoc = 0,
  94. endLoc = fullText.length,
  95. lastSuccessLoc = 0
  96. ): string {
  97. const midLoc = Math.floor((startLoc + endLoc) / 2);
  98. const currentText = getCurrentText(fullText, midLoc);
  99. textNode.textContent = currentText;
  100. // console.log('calculating....', currentText);
  101. if (startLoc >= endLoc - 1 && endLoc > 0) { // Loop when step is small
  102. for (let step = endLoc; step >= startLoc; step -= 1) {
  103. const currentStepText = getCurrentText(fullText, step);
  104. textNode.textContent = currentStepText;
  105. if (inRange() || !currentStepText) {
  106. return step === fullText.length ? fullText : currentStepText;
  107. }
  108. }
  109. } else if (endLoc === 0) {
  110. return ellipsisStr;
  111. }
  112. if (inRange()) {
  113. return measureText(textNode, fullText, midLoc, endLoc, midLoc);
  114. }
  115. return measureText(textNode, fullText, startLoc, midLoc, lastSuccessLoc);
  116. }
  117. const textNode = document.createTextNode(content);
  118. appendChildNode(textNode);
  119. const resText = measureText(textNode, content);
  120. ellipsisContainer.innerHTML = '';
  121. return resText;
  122. };
  123. export default getRenderText;