util.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.whiteSpace = 'normal';
  58. ellipsisContainer.style.webkitLineClamp = 'none';
  59. // Render fake container
  60. ReactDOM.render(
  61. <></>,
  62. ellipsisContainer
  63. );
  64. // Check if ellipsis in measure div is height enough for content
  65. function inRange() {
  66. // console.log('inrange?', ellipsisContainer.scrollHeight, ellipsisContainer.scrollHeight < maxHeight)
  67. return ellipsisContainer.scrollHeight < maxHeight;
  68. }
  69. // ========================= Find match ellipsis content =========================
  70. // Create origin content holder
  71. const ellipsisContentHolder = document.createElement('span');
  72. const ellipsisTextNode = document.createTextNode(suffix);
  73. ellipsisContentHolder.appendChild(ellipsisTextNode);
  74. ellipsisContainer.appendChild(ellipsisContentHolder);
  75. fixedContent.map((node: Node) => node && ellipsisContainer.appendChild(node.cloneNode(true)));
  76. // Append before fixed nodes
  77. function appendChildNode(node: ChildNode) {
  78. ellipsisContentHolder.insertBefore(node, ellipsisTextNode);
  79. }
  80. function getCurrentText(text: string, pos: number) {
  81. const end = text.length;
  82. if (!pos) {
  83. return ellipsisStr;
  84. }
  85. if (ellipsisPos === 'end' || pos > end - pos) {
  86. return text.slice(0, pos) + ellipsisStr;
  87. }
  88. return text.slice(0, pos) + ellipsisStr + text.slice(end - pos, end);
  89. }
  90. // Get maximum text
  91. function measureText(
  92. textNode: Text,
  93. fullText: string,
  94. startLoc = 0,
  95. endLoc = fullText.length,
  96. lastSuccessLoc = 0
  97. ): string {
  98. const midLoc = Math.floor((startLoc + endLoc) / 2);
  99. const currentText = getCurrentText(fullText, midLoc);
  100. textNode.textContent = currentText;
  101. // console.log('calculating....', currentText);
  102. if (startLoc >= endLoc - 1 && endLoc > 0) { // Loop when step is small
  103. for (let step = endLoc; step >= startLoc; step -= 1) {
  104. const currentStepText = getCurrentText(fullText, step);
  105. textNode.textContent = currentStepText;
  106. if (inRange() || !currentStepText) {
  107. return step === fullText.length ? fullText : currentStepText;
  108. }
  109. }
  110. } else if (endLoc === 0) {
  111. return ellipsisStr;
  112. }
  113. if (inRange()) {
  114. return measureText(textNode, fullText, midLoc, endLoc, midLoc);
  115. }
  116. return measureText(textNode, fullText, startLoc, midLoc, lastSuccessLoc);
  117. }
  118. const textNode = document.createTextNode(content);
  119. appendChildNode(textNode);
  120. const resText = measureText(textNode, content);
  121. ellipsisContainer.innerHTML = '';
  122. return resText;
  123. };
  124. export default getRenderText;