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

paste.js 9.3 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Generated by CoffeeScript 1.9.0
  2. /*
  3. paste.js is an interface to read data ( text / image ) from clipboard in different browsers. It also contains several hacks.
  4. https://github.com/layerssss/paste.js
  5. */
  6. (function() {
  7. var $, Paste, createHiddenEditable, dataURLtoBlob;
  8. $ = window.jQuery;
  9. $.paste = function(pasteContainer) {
  10. var pm;
  11. if (typeof console !== "undefined" && console !== null) {
  12. console.log("DEPRECATED: This method is deprecated. Please use $.fn.pastableNonInputable() instead.");
  13. }
  14. pm = Paste.mountNonInputable(pasteContainer);
  15. return pm._container;
  16. };
  17. $.fn.pastableNonInputable = function() {
  18. var el, _i, _len;
  19. for (_i = 0, _len = this.length; _i < _len; _i++) {
  20. el = this[_i];
  21. Paste.mountNonInputable(el);
  22. }
  23. return this;
  24. };
  25. $.fn.pastableTextarea = function() {
  26. var el, _i, _len;
  27. for (_i = 0, _len = this.length; _i < _len; _i++) {
  28. el = this[_i];
  29. Paste.mountTextarea(el);
  30. }
  31. return this;
  32. };
  33. $.fn.pastableContenteditable = function() {
  34. var el, _i, _len;
  35. for (_i = 0, _len = this.length; _i < _len; _i++) {
  36. el = this[_i];
  37. Paste.mountContenteditable(el);
  38. }
  39. return this;
  40. };
  41. dataURLtoBlob = function(dataURL, sliceSize) {
  42. var b64Data, byteArray, byteArrays, byteCharacters, byteNumbers, contentType, i, m, offset, slice, _ref;
  43. if (sliceSize == null) {
  44. sliceSize = 512;
  45. }
  46. if (!(m = dataURL.match(/^data\:([^\;]+)\;base64\,(.+)$/))) {
  47. return null;
  48. }
  49. _ref = m, m = _ref[0], contentType = _ref[1], b64Data = _ref[2];
  50. byteCharacters = atob(b64Data);
  51. byteArrays = [];
  52. offset = 0;
  53. while (offset < byteCharacters.length) {
  54. slice = byteCharacters.slice(offset, offset + sliceSize);
  55. byteNumbers = new Array(slice.length);
  56. i = 0;
  57. while (i < slice.length) {
  58. byteNumbers[i] = slice.charCodeAt(i);
  59. i++;
  60. }
  61. byteArray = new Uint8Array(byteNumbers);
  62. byteArrays.push(byteArray);
  63. offset += sliceSize;
  64. }
  65. return new Blob(byteArrays, {
  66. type: contentType
  67. });
  68. };
  69. createHiddenEditable = function() {
  70. return $(document.createElement('div')).attr('contenteditable', true).css({
  71. width: 1,
  72. height: 1,
  73. position: 'fixed',
  74. left: -100,
  75. overflow: 'hidden'
  76. });
  77. };
  78. Paste = (function() {
  79. Paste.prototype._target = null;
  80. Paste.prototype._container = null;
  81. Paste.mountNonInputable = function(nonInputable) {
  82. var paste;
  83. paste = new Paste(createHiddenEditable().appendTo(nonInputable), nonInputable);
  84. $(nonInputable).on('click', (function(_this) {
  85. return function() {
  86. return paste._container.focus();
  87. };
  88. })(this));
  89. paste._container.on('focus', (function(_this) {
  90. return function() {
  91. return $(nonInputable).addClass('pastable-focus');
  92. };
  93. })(this));
  94. return paste._container.on('blur', (function(_this) {
  95. return function() {
  96. return $(nonInputable).removeClass('pastable-focus');
  97. };
  98. })(this));
  99. };
  100. Paste.mountTextarea = function(textarea) {
  101. var ctlDown, paste;
  102. if (-1 !== navigator.userAgent.toLowerCase().indexOf('chrome')) {
  103. return this.mountContenteditable(textarea);
  104. }
  105. paste = new Paste(createHiddenEditable().insertBefore(textarea), textarea);
  106. ctlDown = false;
  107. $(textarea).on('keyup', function(ev) {
  108. var _ref;
  109. if ((_ref = ev.keyCode) === 17 || _ref === 224) {
  110. return ctlDown = false;
  111. }
  112. });
  113. $(textarea).on('keydown', function(ev) {
  114. var _ref;
  115. if ((_ref = ev.keyCode) === 17 || _ref === 224) {
  116. ctlDown = true;
  117. }
  118. if (ctlDown && ev.keyCode === 86) {
  119. return paste._container.focus();
  120. }
  121. });
  122. $(paste._target).on('pasteImage', (function(_this) {
  123. return function() {
  124. return $(textarea).focus();
  125. };
  126. })(this));
  127. $(paste._target).on('pasteText', (function(_this) {
  128. return function() {
  129. return $(textarea).focus();
  130. };
  131. })(this));
  132. $(textarea).on('focus', (function(_this) {
  133. return function() {
  134. return $(textarea).addClass('pastable-focus');
  135. };
  136. })(this));
  137. return $(textarea).on('blur', (function(_this) {
  138. return function() {
  139. return $(textarea).removeClass('pastable-focus');
  140. };
  141. })(this));
  142. };
  143. Paste.mountContenteditable = function(contenteditable) {
  144. var paste;
  145. paste = new Paste(contenteditable, contenteditable);
  146. $(contenteditable).on('focus', (function(_this) {
  147. return function() {
  148. return $(contenteditable).addClass('pastable-focus');
  149. };
  150. })(this));
  151. return $(contenteditable).on('blur', (function(_this) {
  152. return function() {
  153. return $(contenteditable).removeClass('pastable-focus');
  154. };
  155. })(this));
  156. };
  157. function Paste(_at__container, _at__target) {
  158. this._container = _at__container;
  159. this._target = _at__target;
  160. this._container = $(this._container);
  161. this._target = $(this._target).addClass('pastable');
  162. this._container.on('paste', (function(_this) {
  163. return function(ev) {
  164. var clipboardData, file, item, reader, text, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
  165. if (((_ref = ev.originalEvent) != null ? _ref.clipboardData : void 0) != null) {
  166. clipboardData = ev.originalEvent.clipboardData;
  167. if (clipboardData.items) {
  168. _ref1 = clipboardData.items;
  169. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  170. item = _ref1[_i];
  171. if (item.type.match(/^image\//)) {
  172. reader = new FileReader();
  173. reader.onload = function(event) {
  174. return _this._handleImage(event.target.result);
  175. };
  176. reader.readAsDataURL(item.getAsFile());
  177. }
  178. if (item.type === 'text/plain') {
  179. item.getAsString(function(string) {
  180. return _this._target.trigger('pasteText', {
  181. text: string
  182. });
  183. });
  184. }
  185. }
  186. } else {
  187. if (-1 !== Array.prototype.indexOf.call(clipboardData.types, 'text/plain')) {
  188. text = clipboardData.getData('Text');
  189. _this._target.trigger('pasteText', {
  190. text: text
  191. });
  192. }
  193. _this._checkImagesInContainer(function(src) {
  194. return _this._handleImage(src);
  195. });
  196. }
  197. }
  198. if (clipboardData = window.clipboardData) {
  199. if ((_ref2 = (text = clipboardData.getData('Text'))) != null ? _ref2.length : void 0) {
  200. return _this._target.trigger('pasteText', {
  201. text: text
  202. });
  203. } else {
  204. _ref3 = clipboardData.files;
  205. _results = [];
  206. for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
  207. file = _ref3[_j];
  208. _this._handleImage(URL.createObjectURL(file));
  209. _results.push(_this._checkImagesInContainer(function() {}));
  210. }
  211. return _results;
  212. }
  213. }
  214. };
  215. })(this));
  216. }
  217. Paste.prototype._handleImage = function(src) {
  218. var loader;
  219. loader = new Image();
  220. loader.onload = (function(_this) {
  221. return function() {
  222. var blob, canvas, ctx, dataURL;
  223. canvas = document.createElement('canvas');
  224. canvas.width = loader.width;
  225. canvas.height = loader.height;
  226. ctx = canvas.getContext('2d');
  227. ctx.drawImage(loader, 0, 0, canvas.width, canvas.height);
  228. dataURL = null;
  229. try {
  230. dataURL = canvas.toDataURL('image/png');
  231. blob = dataURLtoBlob(dataURL);
  232. } catch (_error) {}
  233. if (dataURL) {
  234. return _this._target.trigger('pasteImage', {
  235. blob: blob,
  236. dataURL: dataURL,
  237. width: loader.width,
  238. height: loader.height
  239. });
  240. }
  241. };
  242. })(this);
  243. return loader.src = src;
  244. };
  245. Paste.prototype._checkImagesInContainer = function(cb) {
  246. var img, timespan, _i, _len, _ref;
  247. timespan = Math.floor(1000 * Math.random());
  248. _ref = this._container.find('img');
  249. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  250. img = _ref[_i];
  251. img["_paste_marked_" + timespan] = true;
  252. }
  253. return setTimeout((function(_this) {
  254. return function() {
  255. var _j, _len1, _ref1, _results;
  256. _ref1 = _this._container.find('img');
  257. _results = [];
  258. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  259. img = _ref1[_j];
  260. if (!img["_paste_marked_" + timespan]) {
  261. cb(img.src);
  262. }
  263. _results.push($(img).remove());
  264. }
  265. return _results;
  266. };
  267. })(this), 1);
  268. };
  269. return Paste;
  270. })();
  271. }).call(this);

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...