123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.calculateNodeStyling = calculateNodeStyling;
- exports['default'] = calculateNodeHeight;
- // Thanks to https://github.com/andreypopp/react-textarea-autosize/
- /**
- * calculateNodeHeight(uiTextNode, useCache = false)
- */
- 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';
- 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'];
- var computedStyleCache = {};
- var hiddenTextarea = void 0;
- function calculateNodeStyling(node) {
- var useCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
- var nodeRef = node.getAttribute('id') || node.getAttribute('data-reactid') || node.getAttribute('name');
- if (useCache && computedStyleCache[nodeRef]) {
- return computedStyleCache[nodeRef];
- }
- var style = window.getComputedStyle(node);
- var boxSizing = style.getPropertyValue('box-sizing') || style.getPropertyValue('-moz-box-sizing') || style.getPropertyValue('-webkit-box-sizing');
- var paddingSize = parseFloat(style.getPropertyValue('padding-bottom')) + parseFloat(style.getPropertyValue('padding-top'));
- var borderSize = parseFloat(style.getPropertyValue('border-bottom-width')) + parseFloat(style.getPropertyValue('border-top-width'));
- var sizingStyle = SIZING_STYLE.map(function (name) {
- return name + ':' + style.getPropertyValue(name);
- }).join(';');
- var nodeInfo = {
- sizingStyle: sizingStyle,
- paddingSize: paddingSize,
- borderSize: borderSize,
- boxSizing: boxSizing
- };
- if (useCache && nodeRef) {
- computedStyleCache[nodeRef] = nodeInfo;
- }
- return nodeInfo;
- }
- function calculateNodeHeight(uiTextNode) {
- var useCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
- var minRows = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var maxRows = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
- if (!hiddenTextarea) {
- hiddenTextarea = document.createElement('textarea');
- document.body.appendChild(hiddenTextarea);
- }
- // Fix wrap="off" issue
- // https://github.com/ant-design/ant-design/issues/6577
- if (uiTextNode.getAttribute('wrap')) {
- hiddenTextarea.setAttribute('wrap', uiTextNode.getAttribute('wrap'));
- } else {
- hiddenTextarea.removeAttribute('wrap');
- }
- // Copy all CSS properties that have an impact on the height of the content in
- // the textbox
- var _calculateNodeStyling = calculateNodeStyling(uiTextNode, useCache),
- paddingSize = _calculateNodeStyling.paddingSize,
- borderSize = _calculateNodeStyling.borderSize,
- boxSizing = _calculateNodeStyling.boxSizing,
- sizingStyle = _calculateNodeStyling.sizingStyle;
- // Need to have the overflow attribute to hide the scrollbar otherwise
- // text-lines will not calculated properly as the shadow will technically be
- // narrower for content
- hiddenTextarea.setAttribute('style', sizingStyle + ';' + HIDDEN_TEXTAREA_STYLE);
- hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || '';
- var minHeight = Number.MIN_SAFE_INTEGER;
- var maxHeight = Number.MAX_SAFE_INTEGER;
- var height = hiddenTextarea.scrollHeight;
- var overflowY = void 0;
- if (boxSizing === 'border-box') {
- // border-box: add border, since height = content + padding + border
- height += borderSize;
- } else if (boxSizing === 'content-box') {
- // remove padding, since height = content
- height -= paddingSize;
- }
- if (minRows !== null || maxRows !== null) {
- // measure height of a textarea with a single row
- hiddenTextarea.value = ' ';
- var singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
- if (minRows !== null) {
- minHeight = singleRowHeight * minRows;
- if (boxSizing === 'border-box') {
- minHeight = minHeight + paddingSize + borderSize;
- }
- height = Math.max(minHeight, height);
- }
- if (maxRows !== null) {
- maxHeight = singleRowHeight * maxRows;
- if (boxSizing === 'border-box') {
- maxHeight = maxHeight + paddingSize + borderSize;
- }
- overflowY = height > maxHeight ? '' : 'hidden';
- height = Math.min(maxHeight, height);
- }
- }
- return {
- height: height + 'px',
- minHeight: minHeight + 'px',
- maxHeight: maxHeight + 'px',
- overflowY: overflowY
- };
- }
|