calculateNodeHeight.js 4.7 KB

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