autosize.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*!
  2. Autosize 3.0.17
  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. var set = typeof Set === 'function' ? new Set() : (function () {
  21. var list = [];
  22. return {
  23. has: function has(key) {
  24. return Boolean(list.indexOf(key) > -1);
  25. },
  26. add: function add(key) {
  27. list.push(key);
  28. },
  29. 'delete': function _delete(key) {
  30. list.splice(list.indexOf(key), 1);
  31. } };
  32. })();
  33. var createEvent = function createEvent(name) {
  34. return new Event(name);
  35. };
  36. try {
  37. new Event('test');
  38. } catch (e) {
  39. // IE does not support `new Event()`
  40. createEvent = function (name) {
  41. var evt = document.createEvent('Event');
  42. evt.initEvent(name, true, false);
  43. return evt;
  44. };
  45. }
  46. function assign(ta) {
  47. if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || set.has(ta)) return;
  48. var heightOffset = null;
  49. var clientWidth = ta.clientWidth;
  50. var cachedHeight = null;
  51. function init() {
  52. var style = window.getComputedStyle(ta, null);
  53. if (style.resize === 'vertical') {
  54. ta.style.resize = 'none';
  55. } else if (style.resize === 'both') {
  56. ta.style.resize = 'horizontal';
  57. }
  58. if (style.boxSizing === 'content-box') {
  59. heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom));
  60. } else {
  61. heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
  62. }
  63. // Fix when a textarea is not on document body and heightOffset is Not a Number
  64. if (isNaN(heightOffset)) {
  65. heightOffset = 0;
  66. }
  67. update();
  68. }
  69. function changeOverflow(value) {
  70. {
  71. // Chrome/Safari-specific fix:
  72. // When the textarea y-overflow is hidden, Chrome/Safari do not reflow the text to account for the space
  73. // made available by removing the scrollbar. The following forces the necessary text reflow.
  74. var width = ta.style.width;
  75. ta.style.width = '0px';
  76. // Force reflow:
  77. /* jshint ignore:start */
  78. ta.offsetWidth;
  79. /* jshint ignore:end */
  80. ta.style.width = width;
  81. }
  82. ta.style.overflowY = value;
  83. resize();
  84. }
  85. function getParentOverflows(el) {
  86. var arr = [];
  87. while (el && el.parentNode && el.parentNode instanceof Element) {
  88. if (el.parentNode.scrollTop) {
  89. arr.push({
  90. node: el.parentNode,
  91. scrollTop: el.parentNode.scrollTop });
  92. }
  93. el = el.parentNode;
  94. }
  95. return arr;
  96. }
  97. function resize() {
  98. var originalHeight = ta.style.height;
  99. var overflows = getParentOverflows(ta);
  100. var docTop = document.documentElement && document.documentElement.scrollTop; // Needed for Mobile IE (ticket #240)
  101. ta.style.height = 'auto';
  102. var endHeight = ta.scrollHeight + heightOffset;
  103. if (ta.scrollHeight === 0) {
  104. // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM.
  105. ta.style.height = originalHeight;
  106. return;
  107. }
  108. ta.style.height = endHeight + 'px';
  109. // used to check if an update is actually necessary on window.resize
  110. clientWidth = ta.clientWidth;
  111. // prevents scroll-position jumping
  112. overflows.forEach(function (el) {
  113. el.node.scrollTop = el.scrollTop;
  114. });
  115. if (docTop) {
  116. document.documentElement.scrollTop = docTop;
  117. }
  118. }
  119. function update() {
  120. resize();
  121. var computed = window.getComputedStyle(ta, null);
  122. var computedHeight = Math.round(parseFloat(computed.height));
  123. var styleHeight = Math.round(parseFloat(ta.style.height));
  124. // The computed height not matching the height set via resize indicates that
  125. // the max-height has been exceeded, in which case the overflow should be set to visible.
  126. if (computedHeight !== styleHeight) {
  127. if (computed.overflowY !== 'visible') {
  128. changeOverflow('visible');
  129. }
  130. } else {
  131. // Normally keep overflow set to hidden, to avoid flash of scrollbar as the textarea expands.
  132. if (computed.overflowY !== 'hidden') {
  133. changeOverflow('hidden');
  134. }
  135. }
  136. if (cachedHeight !== computedHeight) {
  137. cachedHeight = computedHeight;
  138. var evt = createEvent('autosize:resized');
  139. ta.dispatchEvent(evt);
  140. }
  141. }
  142. var pageResize = function pageResize() {
  143. if (ta.clientWidth !== clientWidth) {
  144. update();
  145. }
  146. };
  147. var destroy = (function (style) {
  148. window.removeEventListener('resize', pageResize, false);
  149. ta.removeEventListener('input', update, false);
  150. ta.removeEventListener('keyup', update, false);
  151. ta.removeEventListener('autosize:destroy', destroy, false);
  152. ta.removeEventListener('autosize:update', update, false);
  153. set['delete'](ta);
  154. Object.keys(style).forEach(function (key) {
  155. ta.style[key] = style[key];
  156. });
  157. }).bind(ta, {
  158. height: ta.style.height,
  159. resize: ta.style.resize,
  160. overflowY: ta.style.overflowY,
  161. overflowX: ta.style.overflowX,
  162. wordWrap: ta.style.wordWrap });
  163. ta.addEventListener('autosize:destroy', destroy, false);
  164. // IE9 does not fire onpropertychange or oninput for deletions,
  165. // so binding to onkeyup to catch most of those events.
  166. // There is no way that I know of to detect something like 'cut' in IE9.
  167. if ('onpropertychange' in ta && 'oninput' in ta) {
  168. ta.addEventListener('keyup', update, false);
  169. }
  170. window.addEventListener('resize', pageResize, false);
  171. ta.addEventListener('input', update, false);
  172. ta.addEventListener('autosize:update', update, false);
  173. set.add(ta);
  174. ta.style.overflowX = 'hidden';
  175. ta.style.wordWrap = 'break-word';
  176. init();
  177. }
  178. function destroy(ta) {
  179. if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
  180. var evt = createEvent('autosize:destroy');
  181. ta.dispatchEvent(evt);
  182. }
  183. function update(ta) {
  184. if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
  185. var evt = createEvent('autosize:update');
  186. ta.dispatchEvent(evt);
  187. }
  188. var autosize = null;
  189. // Do nothing in Node.js environment and IE8 (or lower)
  190. if (typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') {
  191. autosize = function (el) {
  192. return el;
  193. };
  194. autosize.destroy = function (el) {
  195. return el;
  196. };
  197. autosize.update = function (el) {
  198. return el;
  199. };
  200. } else {
  201. autosize = function (el, options) {
  202. if (el) {
  203. Array.prototype.forEach.call(el.length ? el : [el], function (x) {
  204. return assign(x, options);
  205. });
  206. }
  207. return el;
  208. };
  209. autosize.destroy = function (el) {
  210. if (el) {
  211. Array.prototype.forEach.call(el.length ? el : [el], destroy);
  212. }
  213. return el;
  214. };
  215. autosize.update = function (el) {
  216. if (el) {
  217. Array.prototype.forEach.call(el.length ? el : [el], update);
  218. }
  219. return el;
  220. };
  221. }
  222. module.exports = autosize;
  223. });