c45836a02950dda09f2e64e6aa13250ae36020514d74142e3250b4aae2ef79d4fa58abbaba140dc54153c0aa4b7fb43c35ed40fef09457e29e00d713336b70 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var browser = require('../../../utils/browser.js');
  4. var types = require('../../../utils/types.js');
  5. let hiddenTextarea = void 0;
  6. const HIDDEN_STYLE = {
  7. height: "0",
  8. visibility: "hidden",
  9. overflow: browser.isFirefox() ? "" : "hidden",
  10. position: "absolute",
  11. "z-index": "-1000",
  12. top: "0",
  13. right: "0"
  14. };
  15. const CONTEXT_STYLE = [
  16. "letter-spacing",
  17. "line-height",
  18. "padding-top",
  19. "padding-bottom",
  20. "font-family",
  21. "font-weight",
  22. "font-size",
  23. "text-rendering",
  24. "text-transform",
  25. "width",
  26. "text-indent",
  27. "padding-left",
  28. "padding-right",
  29. "border-width",
  30. "box-sizing",
  31. "word-break"
  32. ];
  33. function calculateNodeStyling(targetElement) {
  34. const style = window.getComputedStyle(targetElement);
  35. const boxSizing = style.getPropertyValue("box-sizing");
  36. const paddingSize = Number.parseFloat(style.getPropertyValue("padding-bottom")) + Number.parseFloat(style.getPropertyValue("padding-top"));
  37. const borderSize = Number.parseFloat(style.getPropertyValue("border-bottom-width")) + Number.parseFloat(style.getPropertyValue("border-top-width"));
  38. const contextStyle = CONTEXT_STYLE.map((name) => [
  39. name,
  40. style.getPropertyValue(name)
  41. ]);
  42. return { contextStyle, paddingSize, borderSize, boxSizing };
  43. }
  44. function calcTextareaHeight(targetElement, minRows = 1, maxRows) {
  45. var _a, _b;
  46. if (!hiddenTextarea) {
  47. hiddenTextarea = document.createElement("textarea");
  48. ((_a = targetElement.parentNode) != null ? _a : document.body).appendChild(hiddenTextarea);
  49. }
  50. const { paddingSize, borderSize, boxSizing, contextStyle } = calculateNodeStyling(targetElement);
  51. contextStyle.forEach(([key, value]) => hiddenTextarea == null ? void 0 : hiddenTextarea.style.setProperty(key, value));
  52. Object.entries(HIDDEN_STYLE).forEach(([key, value]) => hiddenTextarea == null ? void 0 : hiddenTextarea.style.setProperty(key, value, "important"));
  53. hiddenTextarea.value = targetElement.value || targetElement.placeholder || "";
  54. let height = hiddenTextarea.scrollHeight;
  55. const result = {};
  56. if (boxSizing === "border-box") {
  57. height = height + borderSize;
  58. } else if (boxSizing === "content-box") {
  59. height = height - paddingSize;
  60. }
  61. hiddenTextarea.value = "";
  62. const singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
  63. if (types.isNumber(minRows)) {
  64. let minHeight = singleRowHeight * minRows;
  65. if (boxSizing === "border-box") {
  66. minHeight = minHeight + paddingSize + borderSize;
  67. }
  68. height = Math.max(minHeight, height);
  69. result.minHeight = `${minHeight}px`;
  70. }
  71. if (types.isNumber(maxRows)) {
  72. let maxHeight = singleRowHeight * maxRows;
  73. if (boxSizing === "border-box") {
  74. maxHeight = maxHeight + paddingSize + borderSize;
  75. }
  76. height = Math.min(maxHeight, height);
  77. }
  78. result.height = `${height}px`;
  79. (_b = hiddenTextarea.parentNode) == null ? void 0 : _b.removeChild(hiddenTextarea);
  80. hiddenTextarea = void 0;
  81. return result;
  82. }
  83. exports.calcTextareaHeight = calcTextareaHeight;
  84. //# sourceMappingURL=utils.js.map