calculateNodeHeight.js 4.6 KB

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