06b9af599c6645156992ebb55a67186e8a3662638ef87f2ef3be9608526dbeed4d407b8c42e9298819ee611767b9f65cc66be582167a95c1b3d13d5e77e23d 2.9 KB

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