calculateNodeHeight.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * calculateNodeHeight(uiTextNode, useCache = false)
  3. */
  4. const HIDDEN_TEXTAREA_STYLE = `
  5. min-height:0 !important;
  6. max-height:none !important;
  7. height:0 !important;
  8. visibility:hidden !important;
  9. overflow:hidden !important;
  10. position:absolute !important;
  11. z-index:-1000 !important;
  12. top:0 !important;
  13. right:0 !important;
  14. pointer-events: none !important;
  15. `;
  16. const SIZING_STYLE = ['letter-spacing', 'line-height', 'padding-top', 'padding-bottom', 'font-family', 'font-weight', 'font-size', 'font-variant', 'text-rendering', 'text-transform', 'width', 'text-indent', 'padding-left', 'padding-right', 'border-width', 'box-sizing', 'word-break', 'white-space'];
  17. const computedStyleCache = {};
  18. let hiddenTextarea;
  19. export function calculateNodeStyling(node) {
  20. let useCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  21. const nodeRef = node.getAttribute('id') || node.getAttribute('data-reactid') || node.getAttribute('name');
  22. if (useCache && computedStyleCache[nodeRef]) {
  23. return computedStyleCache[nodeRef];
  24. }
  25. const style = window.getComputedStyle(node);
  26. const boxSizing = style.getPropertyValue('box-sizing') || style.getPropertyValue('-moz-box-sizing') || style.getPropertyValue('-webkit-box-sizing');
  27. const paddingSize = parseFloat(style.getPropertyValue('padding-bottom')) + parseFloat(style.getPropertyValue('padding-top'));
  28. const borderSize = parseFloat(style.getPropertyValue('border-bottom-width')) + parseFloat(style.getPropertyValue('border-top-width'));
  29. const sizingStyle = SIZING_STYLE.map(name => `${name}:${style.getPropertyValue(name)}`).join(';');
  30. const nodeInfo = {
  31. sizingStyle,
  32. paddingSize,
  33. borderSize,
  34. boxSizing
  35. };
  36. if (useCache && nodeRef) {
  37. computedStyleCache[nodeRef] = nodeInfo;
  38. }
  39. return nodeInfo;
  40. }
  41. export default function calculateAutoSizeStyle(uiTextNode) {
  42. let useCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  43. let minRows = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  44. let maxRows = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
  45. if (!hiddenTextarea) {
  46. hiddenTextarea = document.createElement('textarea');
  47. hiddenTextarea.setAttribute('tab-index', '-1');
  48. hiddenTextarea.setAttribute('aria-hidden', 'true');
  49. document.body.appendChild(hiddenTextarea);
  50. }
  51. // Fix wrap="off" issue
  52. // https://github.com/ant-design/ant-design/issues/6577
  53. if (uiTextNode.getAttribute('wrap')) {
  54. hiddenTextarea.setAttribute('wrap', uiTextNode.getAttribute('wrap'));
  55. } else {
  56. hiddenTextarea.removeAttribute('wrap');
  57. }
  58. // Copy all CSS properties that have an impact on the height of the content in
  59. // the textbox
  60. const {
  61. paddingSize,
  62. borderSize,
  63. boxSizing,
  64. sizingStyle
  65. } = calculateNodeStyling(uiTextNode, useCache);
  66. // Need to have the overflow attribute to hide the scrollbar otherwise
  67. // text-lines will not calculated properly as the shadow will technically be
  68. // narrower for content
  69. hiddenTextarea.setAttribute('style', `${sizingStyle};${HIDDEN_TEXTAREA_STYLE}`);
  70. hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || '';
  71. let minHeight = undefined;
  72. let maxHeight = undefined;
  73. let overflowY;
  74. let height = hiddenTextarea.scrollHeight;
  75. if (boxSizing === 'border-box') {
  76. // border-box: add border, since height = content + padding + border
  77. height += borderSize;
  78. } else if (boxSizing === 'content-box') {
  79. // remove padding, since height = content
  80. height -= paddingSize;
  81. }
  82. if (minRows !== null || maxRows !== null) {
  83. // measure height of a textarea with a single row
  84. hiddenTextarea.value = ' ';
  85. const singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
  86. if (minRows !== null) {
  87. minHeight = singleRowHeight * minRows;
  88. if (boxSizing === 'border-box') {
  89. minHeight = minHeight + paddingSize + borderSize;
  90. }
  91. height = Math.max(minHeight, height);
  92. }
  93. if (maxRows !== null) {
  94. maxHeight = singleRowHeight * maxRows;
  95. if (boxSizing === 'border-box') {
  96. maxHeight = maxHeight + paddingSize + borderSize;
  97. }
  98. overflowY = height > maxHeight ? '' : 'hidden';
  99. height = Math.min(maxHeight, height);
  100. }
  101. }
  102. const style = {
  103. height: `${height}px`,
  104. overflowY,
  105. resize: 'none'
  106. };
  107. if (minHeight) {
  108. style.minHeight = `${minHeight}px`;
  109. }
  110. if (maxHeight) {
  111. style.maxHeight = `${maxHeight}px`;
  112. }
  113. return style;
  114. }