|
7 | 7 |
|
8 | 8 | function assign(ta) { |
9 | 9 | if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || assignedElements.has(ta)) return; |
10 | | - var heightOffset = null; |
11 | | - var clientWidth = null; |
12 | | - var cachedHeight = null; |
13 | | - |
14 | | - function init() { |
15 | | - var style = window.getComputedStyle(ta); |
16 | | - |
17 | | - if (style.resize === 'vertical') { |
18 | | - ta.style.resize = 'none'; |
19 | | - } else if (style.resize === 'both') { |
20 | | - ta.style.resize = 'horizontal'; |
21 | | - } |
22 | | - |
23 | | - if (style.boxSizing === 'content-box') { |
24 | | - heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom)); |
25 | | - } else { |
26 | | - heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth); |
27 | | - } // Fix when a textarea is not on document body and heightOffset is Not a Number |
28 | | - |
29 | | - |
30 | | - if (isNaN(heightOffset)) { |
31 | | - heightOffset = 0; |
32 | | - } |
33 | | - |
34 | | - update(); |
35 | | - } |
36 | | - |
37 | | - function changeOverflow(value) { |
38 | | - { |
39 | | - // Chrome/Safari-specific fix: |
40 | | - // When the textarea y-overflow is hidden, Chrome/Safari do not reflow the text to account for the space |
41 | | - // made available by removing the scrollbar. The following forces the necessary text reflow. |
42 | | - var width = ta.style.width; |
43 | | - ta.style.width = '0px'; // Force reflow: |
44 | | - /* jshint ignore:end */ |
45 | | - |
46 | | - ta.style.width = width; |
47 | | - } |
48 | | - ta.style.overflowY = value; |
49 | | - } |
| 10 | + var previousHeight = null; |
50 | 11 |
|
51 | 12 | function cacheScrollTops(el) { |
52 | 13 | var arr = []; |
|
70 | 31 | }; |
71 | 32 | } |
72 | 33 |
|
73 | | - function resize() { |
| 34 | + var computed = window.getComputedStyle(ta); |
| 35 | + |
| 36 | + function update(cachedTextAlign) { |
| 37 | + if (cachedTextAlign === void 0) { |
| 38 | + cachedTextAlign = null; |
| 39 | + } |
| 40 | + |
| 41 | + var initialOverflowY = computed.overflowY; |
| 42 | + |
74 | 43 | if (ta.scrollHeight === 0) { |
75 | 44 | // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM. |
76 | 45 | return; |
77 | 46 | } // ensure the scrollTop values of parent elements are not modified as a consequence of calculating the textarea height |
78 | 47 |
|
79 | 48 |
|
80 | 49 | var restoreScrollTops = cacheScrollTops(ta); |
81 | | - ta.style.height = ''; // this is necessary for getting an accurate scrollHeight on the next line |
| 50 | + ta.style.height = ''; // this is necessary for to scrollHeight to accurately reflect situations where the textarea should shrink |
| 51 | + // disallow vertical resizing |
82 | 52 |
|
83 | | - ta.style.height = ta.scrollHeight + heightOffset + 'px'; // used to check if an update is actually necessary on window.resize |
| 53 | + if (computed.resize === 'vertical') { |
| 54 | + ta.style.resize = 'none'; |
| 55 | + } else if (computed.resize === 'both') { |
| 56 | + ta.style.resize = 'horizontal'; |
| 57 | + } |
84 | 58 |
|
85 | | - clientWidth = ta.clientWidth; |
86 | | - restoreScrollTops(); |
87 | | - } |
| 59 | + var newHeight; |
88 | 60 |
|
89 | | - function update() { |
90 | | - resize(); |
91 | | - var styleHeight = parseFloat(ta.style.height); |
92 | | - var computed = window.getComputedStyle(ta); |
93 | | - var actualHeight = parseFloat(computed.height); // The actual height not matching the style height (set via the resize method) indicates that |
94 | | - // the max-height has been exceeded, in which case the overflow should be allowed. |
| 61 | + if (computed.boxSizing === 'content-box') { |
| 62 | + newHeight = ta.scrollHeight - (parseFloat(computed.paddingTop) + parseFloat(computed.paddingBottom)); |
| 63 | + } else { |
| 64 | + newHeight = ta.scrollHeight + parseFloat(computed.borderTopWidth) + parseFloat(computed.borderBottomWidth); |
| 65 | + } |
95 | 66 |
|
96 | | - if (actualHeight < styleHeight) { |
| 67 | + if (computed.maxHeight !== 'none' && newHeight > parseFloat(computed.maxHeight)) { |
97 | 68 | if (computed.overflowY === 'hidden') { |
98 | | - changeOverflow('scroll'); |
99 | | - resize(); |
100 | | - actualHeight = parseFloat(window.getComputedStyle(ta).height); |
101 | | - } |
102 | | - } else { |
103 | | - // Normally keep overflow set to hidden, to avoid flash of scrollbar as the textarea expands. |
104 | | - if (computed.overflowY !== 'hidden') { |
105 | | - changeOverflow('hidden'); |
106 | | - resize(); |
107 | | - actualHeight = parseFloat(window.getComputedStyle(ta).height); |
| 69 | + ta.style.overflow = 'scroll'; |
108 | 70 | } |
| 71 | + |
| 72 | + newHeight = parseFloat(computed.maxHeight); |
| 73 | + } else if (computed.overflowY !== 'hidden') { |
| 74 | + ta.style.overflow = 'hidden'; |
| 75 | + } |
| 76 | + |
| 77 | + ta.style.height = newHeight + 'px'; |
| 78 | + |
| 79 | + if (cachedTextAlign) { |
| 80 | + ta.style.textAlign = cachedTextAlign; |
109 | 81 | } |
110 | 82 |
|
111 | | - if (cachedHeight !== actualHeight) { |
112 | | - cachedHeight = actualHeight; |
| 83 | + restoreScrollTops(); |
| 84 | + |
| 85 | + if (previousHeight !== newHeight) { |
113 | 86 | ta.dispatchEvent(new Event('autosize:resized', { |
114 | 87 | bubbles: true |
115 | 88 | })); |
| 89 | + previousHeight = newHeight; |
116 | 90 | } |
117 | | - } |
118 | 91 |
|
119 | | - var pageResize = function pageResize() { |
120 | | - if (ta.clientWidth !== clientWidth) { |
121 | | - update(); |
| 92 | + if (initialOverflowY !== computed.overflow && !cachedTextAlign) { |
| 93 | + var textAlign = computed.textAlign; |
| 94 | + |
| 95 | + if (computed.overflow === 'hidden') { |
| 96 | + // Webkit fails to reflow text after overflow is hidden, |
| 97 | + // even if hiding overflow would allow text to fit more compactly. |
| 98 | + // The following is intended to force the necessary text reflow. |
| 99 | + ta.style.textAlign = textAlign === 'start' ? 'end' : 'start'; |
| 100 | + } |
| 101 | + |
| 102 | + update(textAlign); |
122 | 103 | } |
123 | | - }; |
| 104 | + } |
124 | 105 |
|
125 | 106 | var destroy = function (style) { |
126 | | - window.removeEventListener('resize', pageResize, false); |
| 107 | + window.removeEventListener('resize', update, false); |
127 | 108 | ta.removeEventListener('input', update, false); |
128 | 109 | ta.removeEventListener('keyup', update, false); |
129 | 110 | ta.removeEventListener('autosize:destroy', destroy, false); |
130 | 111 | ta.removeEventListener('autosize:update', update, false); |
131 | 112 | Object.keys(style).forEach(function (key) { |
132 | | - ta.style[key] = style[key]; |
| 113 | + return ta.style[key] = style[key]; |
133 | 114 | }); |
134 | 115 | assignedElements["delete"](ta); |
135 | 116 | }.bind(ta, { |
136 | 117 | height: ta.style.height, |
137 | 118 | resize: ta.style.resize, |
| 119 | + textAlign: ta.style.textAlign, |
138 | 120 | overflowY: ta.style.overflowY, |
139 | 121 | overflowX: ta.style.overflowX, |
140 | 122 | wordWrap: ta.style.wordWrap |
141 | 123 | }); |
142 | 124 |
|
143 | 125 | ta.addEventListener('autosize:destroy', destroy, false); |
144 | | - window.addEventListener('resize', pageResize, false); |
| 126 | + window.addEventListener('resize', update, false); |
145 | 127 | ta.addEventListener('input', update, false); |
146 | 128 | ta.addEventListener('autosize:update', update, false); |
147 | 129 | ta.style.overflowX = 'hidden'; |
|
150 | 132 | destroy: destroy, |
151 | 133 | update: update |
152 | 134 | }); |
153 | | - init(); |
| 135 | + update(); |
154 | 136 | } |
155 | 137 |
|
156 | 138 | function destroy(ta) { |
|
0 commit comments