utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. ];
  32. function calculateNodeStyling(targetElement) {
  33. const style = window.getComputedStyle(targetElement);
  34. const boxSizing = style.getPropertyValue("box-sizing");
  35. const paddingSize = Number.parseFloat(style.getPropertyValue("padding-bottom")) + Number.parseFloat(style.getPropertyValue("padding-top"));
  36. const borderSize = Number.parseFloat(style.getPropertyValue("border-bottom-width")) + Number.parseFloat(style.getPropertyValue("border-top-width"));
  37. const contextStyle = CONTEXT_STYLE.map((name) => [
  38. name,
  39. style.getPropertyValue(name)
  40. ]);
  41. return { contextStyle, paddingSize, borderSize, boxSizing };
  42. }
  43. function calcTextareaHeight(targetElement, minRows = 1, maxRows) {
  44. var _a;
  45. if (!hiddenTextarea) {
  46. hiddenTextarea = document.createElement("textarea");
  47. document.body.appendChild(hiddenTextarea);
  48. }
  49. const { paddingSize, borderSize, boxSizing, contextStyle } = calculateNodeStyling(targetElement);
  50. contextStyle.forEach(([key, value]) => hiddenTextarea == null ? void 0 : hiddenTextarea.style.setProperty(key, value));
  51. Object.entries(HIDDEN_STYLE).forEach(([key, value]) => hiddenTextarea == null ? void 0 : hiddenTextarea.style.setProperty(key, value, "important"));
  52. hiddenTextarea.value = targetElement.value || targetElement.placeholder || "";
  53. let height = hiddenTextarea.scrollHeight;
  54. const result = {};
  55. if (boxSizing === "border-box") {
  56. height = height + borderSize;
  57. } else if (boxSizing === "content-box") {
  58. height = height - paddingSize;
  59. }
  60. hiddenTextarea.value = "";
  61. const singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
  62. if (types.isNumber(minRows)) {
  63. let minHeight = singleRowHeight * minRows;
  64. if (boxSizing === "border-box") {
  65. minHeight = minHeight + paddingSize + borderSize;
  66. }
  67. height = Math.max(minHeight, height);
  68. result.minHeight = `${minHeight}px`;
  69. }
  70. if (types.isNumber(maxRows)) {
  71. let maxHeight = singleRowHeight * maxRows;
  72. if (boxSizing === "border-box") {
  73. maxHeight = maxHeight + paddingSize + borderSize;
  74. }
  75. height = Math.min(maxHeight, height);
  76. }
  77. result.height = `${height}px`;
  78. (_a = hiddenTextarea.parentNode) == null ? void 0 : _a.removeChild(hiddenTextarea);
  79. hiddenTextarea = void 0;
  80. return result;
  81. }
  82. exports.calcTextareaHeight = calcTextareaHeight;
  83. //# sourceMappingURL=utils.js.map