1 line
6.5 KiB
Plaintext
1 line
6.5 KiB
Plaintext
|
|
{"version":3,"file":"utils.mjs","sources":["../../../../../../packages/components/input/src/utils.ts"],"sourcesContent":["import { isFirefox, isNumber } from '@element-plus/utils'\n\nlet hiddenTextarea: HTMLTextAreaElement | undefined = undefined\n\nconst HIDDEN_STYLE = {\n height: '0',\n visibility: 'hidden',\n overflow: isFirefox() ? '' : 'hidden',\n position: 'absolute',\n 'z-index': '-1000',\n top: '0',\n right: '0',\n}\n\nconst CONTEXT_STYLE = [\n 'letter-spacing',\n 'line-height',\n 'padding-top',\n 'padding-bottom',\n 'font-family',\n 'font-weight',\n 'font-size',\n 'text-rendering',\n 'text-transform',\n 'width',\n 'text-indent',\n 'padding-left',\n 'padding-right',\n 'border-width',\n 'box-sizing',\n 'word-break',\n]\n\ntype NodeStyle = {\n contextStyle: string[][]\n boxSizing: string\n paddingSize: number\n borderSize: number\n}\n\ntype TextAreaHeight = {\n height: string\n minHeight?: string\n}\n\nfunction calculateNodeStyling(targetElement: Element): NodeStyle {\n const style = window.getComputedStyle(targetElement)\n\n const boxSizing = style.getPropertyValue('box-sizing')\n\n const paddingSize =\n Number.parseFloat(style.getPropertyValue('padding-bottom')) +\n Number.parseFloat(style.getPropertyValue('padding-top'))\n\n const borderSize =\n Number.parseFloat(style.getPropertyValue('border-bottom-width')) +\n Number.parseFloat(style.getPropertyValue('border-top-width'))\n\n const contextStyle = CONTEXT_STYLE.map((name) => [\n name,\n style.getPropertyValue(name),\n ])\n\n return { contextStyle, paddingSize, borderSize, boxSizing }\n}\n\nexport function calcTextareaHeight(\n targetElement: HTMLTextAreaElement,\n minRows = 1,\n maxRows?: number\n): TextAreaHeight {\n if (!hiddenTextarea) {\n hiddenTextarea = document.createElement('textarea')\n ;(targetElement.parentNode ?? document.body).appendChild(hiddenTextarea)\n }\n\n const { paddingSize, borderSize, boxSizing, contextStyle } =\n calculateNodeStyling(targetElement)\n\n contextStyle.forEach(([key, value]) =>\n hiddenTextarea?.style.setProperty(key, value)\n )\n\n Object.entries(HIDDEN_STYLE).forEach(([key, value]) =>\n hiddenTextarea?.style.setProperty(key, value, 'important')\n )\n\n hiddenTextarea.value = targetElement.value || targetElement.placeholder || ''\n\n let height = hiddenTextarea.scrollHeight\n const result = {} as TextAreaHeight\n\n if (boxSizing === 'border-box') {\n height = height + borderSize\n } else if (boxSizing === 'content-box') {\n height = height - paddingSize\n }\n\n hiddenTextarea.value = ''\n const singleRowHeight = hiddenTextarea.scrollHeight - paddingSize\n\n if (isNumber(minRows)) {\n let minHeight = singleRowHeight * minRows\n if (boxSizing === 'border-box') {\n minHeight = minHeight + paddingSize + borderSize\n }\n height = Math.max(minHeight, height)\n result.minHeight = `${minHeight}px`\n }\n if (isNumber(maxRows)) {\n let maxHeight = singleRowHeight * maxRows\n if (boxSizing === 'border-box') {\n maxHeight = maxHeight + paddingSize + borderSize\n }\n height = Math.min(maxHeight, height)\n }\n result.height = `${height}px`\n hiddenTextarea.parentNode?.removeChild(hiddenTextarea)\n hiddenTextarea = undefined\n\n return result\n}\n"],"names":[],"mappings":";;;AACA,IAAI,cAAc,GAAG,KAAK,CAAC,CAAC;AAC5B,MAAM,YAAY,GAAG;AACrB,EAAE,MAAM,EAAE,GAAG;AACb,EAAE,UAAU,EAAE,QAAQ;AACtB,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,QAAQ;AACvC,EAAE,QAAQ,EAAE,UAAU;AACtB,EAAE,SAAS,EAAE,OAAO;AACpB,EAAE,GAAG,EAAE,GAAG;AACV,EAAE,KAAK,EAAE,GAAG;AACZ,CAAC,CAAC;AACF,MAAM,aAAa,GAAG;AACtB,EAAE,gBAAgB;AAClB,EAAE,aAAa;AACf,EAAE,aAAa;AACf,EAAE,gBAAgB;AAClB,EAAE,aAAa;AACf,EAAE,aAAa;AACf,EAAE,WAAW;AACb,EAAE,gBAAgB;AAClB,EAAE,gBAAgB;AAClB,EAAE,OAAO;AACT,EAAE,aAAa;AACf,EAAE,cAAc;AAChB,EAAE,eAAe;AACjB,EAAE,cAAc;AAChB,EAAE,YAAY;AACd,EAAE,YAAY;AACd,CAAC,CAAC;AACF,SAAS,oBAAoB,CAAC,aAAa,EAAE;AAC7C,EAAE,MAAM,KAAK,GAAG,MAAM,CAAC,gBAAgB,CAAC,aAAa,CAAC,CAAC;AACvD,EAAE,MAAM,SAAS,GAAG,KAAK,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAC;AACzD,EAAE,MAAM,
|