Administrator
2023-04-21 195945efc5db921a4c9eb8cf9421c172273293f5
提交 | 用户 | 时间
58d006 1 /*!
A 2     Autosize 3.0.14
3     license: MIT
4     http://www.jacklmoore.com/autosize
5 */
6 (function (global, factory) {
7     if (typeof define === 'function' && define.amd) {
8         define(['exports', 'module'], factory);
9     } else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
10         factory(exports, module);
11     } else {
12         var mod = {
13             exports: {}
14         };
15         factory(mod.exports, mod);
16         global.autosize = mod.exports;
17     }
18 })(this, function (exports, module) {
19     'use strict';
20
21     var set = typeof Set === 'function' ? new Set() : (function () {
22         var list = [];
23
24         return {
25             has: function has(key) {
26                 return Boolean(list.indexOf(key) > -1);
27             },
28             add: function add(key) {
29                 list.push(key);
30             },
31             'delete': function _delete(key) {
32                 list.splice(list.indexOf(key), 1);
33             } };
34     })();
35
36     function assign(ta) {
37         var _ref = arguments[1] === undefined ? {} : arguments[1];
38
39         var _ref$setOverflowX = _ref.setOverflowX;
40         var setOverflowX = _ref$setOverflowX === undefined ? true : _ref$setOverflowX;
41         var _ref$setOverflowY = _ref.setOverflowY;
42         var setOverflowY = _ref$setOverflowY === undefined ? true : _ref$setOverflowY;
43
44         if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || set.has(ta)) return;
45
46         var heightOffset = null;
47         var overflowY = null;
48         var clientWidth = ta.clientWidth;
49
50         function init() {
51             var style = window.getComputedStyle(ta, null);
52
53             overflowY = style.overflowY;
54
55             if (style.resize === 'vertical') {
56                 ta.style.resize = 'none';
57             } else if (style.resize === 'both') {
58                 ta.style.resize = 'horizontal';
59             }
60
61             if (style.boxSizing === 'content-box') {
62                 heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom));
63             } else {
64                 heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
65             }
66             // Fix when a textarea is not on document body and heightOffset is Not a Number
67             if (isNaN(heightOffset)) {
68                 heightOffset = 0;
69             }
70
71             update();
72         }
73
74         function changeOverflow(value) {
75             {
76                 // Chrome/Safari-specific fix:
77                 // When the textarea y-overflow is hidden, Chrome/Safari do not reflow the text to account for the space
78                 // made available by removing the scrollbar. The following forces the necessary text reflow.
79                 var width = ta.style.width;
80                 ta.style.width = '0px';
81                 // Force reflow:
82                 /* jshint ignore:start */
83                 ta.offsetWidth;
84                 /* jshint ignore:end */
85                 ta.style.width = width;
86             }
87
88             overflowY = value;
89
90             if (setOverflowY) {
91                 ta.style.overflowY = value;
92             }
93
94             resize();
95         }
96
97         function resize() {
98             var htmlTop = window.pageYOffset;
99             var bodyTop = document.body.scrollTop;
100             var originalHeight = ta.style.height;
101
102             ta.style.height = 'auto';
103
104             var endHeight = ta.scrollHeight + heightOffset;
105
106             if (ta.scrollHeight === 0) {
107                 // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM.
108                 ta.style.height = originalHeight;
109                 return;
110             }
111
112             ta.style.height = endHeight + 'px';
113
114             // used to check if an update is actually necessary on window.resize
115             clientWidth = ta.clientWidth;
116
117             // prevents scroll-position jumping
118             document.documentElement.scrollTop = htmlTop;
119             document.body.scrollTop = bodyTop;
120         }
121
122         function update() {
123             var startHeight = ta.style.height;
124
125             resize();
126
127             var style = window.getComputedStyle(ta, null);
128
129             if (style.height !== ta.style.height) {
130                 if (overflowY !== 'visible') {
131                     changeOverflow('visible');
132                 }
133             } else {
134                 if (overflowY !== 'hidden') {
135                     changeOverflow('hidden');
136                 }
137             }
138
139             if (startHeight !== ta.style.height) {
140                 var evt = document.createEvent('Event');
141                 evt.initEvent('autosize:resized', true, false);
142                 ta.dispatchEvent(evt);
143             }
144         }
145
146         var pageResize = function pageResize() {
147             if (ta.clientWidth !== clientWidth) {
148                 update();
149             }
150         };
151
152         var destroy = (function (style) {
153             window.removeEventListener('resize', pageResize, false);
154             ta.removeEventListener('input', update, false);
155             ta.removeEventListener('keyup', update, false);
156             ta.removeEventListener('autosize:destroy', destroy, false);
157             ta.removeEventListener('autosize:update', update, false);
158             set['delete'](ta);
159
160             Object.keys(style).forEach(function (key) {
161                 ta.style[key] = style[key];
162             });
163         }).bind(ta, {
164             height: ta.style.height,
165             resize: ta.style.resize,
166             overflowY: ta.style.overflowY,
167             overflowX: ta.style.overflowX,
168             wordWrap: ta.style.wordWrap });
169
170         ta.addEventListener('autosize:destroy', destroy, false);
171
172         // IE9 does not fire onpropertychange or oninput for deletions,
173         // so binding to onkeyup to catch most of those events.
174         // There is no way that I know of to detect something like 'cut' in IE9.
175         if ('onpropertychange' in ta && 'oninput' in ta) {
176             ta.addEventListener('keyup', update, false);
177         }
178
179         window.addEventListener('resize', pageResize, false);
180         ta.addEventListener('input', update, false);
181         ta.addEventListener('autosize:update', update, false);
182         set.add(ta);
183
184         if (setOverflowX) {
185             ta.style.overflowX = 'hidden';
186             ta.style.wordWrap = 'break-word';
187         }
188
189         init();
190     }
191
192     function destroy(ta) {
193         if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
194         var evt = document.createEvent('Event');
195         evt.initEvent('autosize:destroy', true, false);
196         ta.dispatchEvent(evt);
197     }
198
199     function update(ta) {
200         if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
201         var evt = document.createEvent('Event');
202         evt.initEvent('autosize:update', true, false);
203         ta.dispatchEvent(evt);
204     }
205
206     var autosize = null;
207
208     // Do nothing in Node.js environment and IE8 (or lower)
209     if (typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') {
210         autosize = function (el) {
211             return el;
212         };
213         autosize.destroy = function (el) {
214             return el;
215         };
216         autosize.update = function (el) {
217             return el;
218         };
219     } else {
220         autosize = function (el, options) {
221             if (el) {
222                 Array.prototype.forEach.call(el.length ? el : [el], function (x) {
223                     return assign(x, options);
224                 });
225             }
226             return el;
227         };
228         autosize.destroy = function (el) {
229             if (el) {
230                 Array.prototype.forEach.call(el.length ? el : [el], destroy);
231             }
232             return el;
233         };
234         autosize.update = function (el) {
235             if (el) {
236                 Array.prototype.forEach.call(el.length ? el : [el], update);
237             }
238             return el;
239         };
240     }
241
242     module.exports = autosize;
243 });