You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
9.5 KiB

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