summaryrefslogtreecommitdiffstats
path: root/client/lib/pasteImage.js
blob: 195a781dfa4c5f9aa7d130ff517dd5736b07e680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* eslint-disable */

// ------------------------------------------------------------------------
// Created by STRd6
// MIT License
// https://github.com/distri/jquery-image_reader/blob/master/paste.coffee.md
//
// Raymond re-write it to javascript

(function($) {
  $.event.fix = (function(originalFix) {
    return function(event) {
      event = originalFix.apply(this, arguments);
      if (
        event.type.indexOf('copy') === 0 ||
        event.type.indexOf('paste') === 0
      ) {
        event.clipboardData = event.originalEvent.clipboardData;
      }
      return event;
    };
  })($.event.fix);

  const defaults = {
    callback: $.noop,
    matchType: /image.*/,
  };

  return ($.fn.pasteImageReader = function(options) {
    if (typeof options === 'function') {
      options = {
        callback: options,
      };
    }
    options = $.extend({}, defaults, options);
    return this.each(function() {
      const element = this;
      return $(element).on('paste', function(event) {
        const types = event.clipboardData.types;
        const items = event.clipboardData.items;
        for (let i = 0; i < types.length; i++) {
          if (
            types[i].match(options.matchType) ||
            items[i].type.match(options.matchType)
          ) {
            const f = items[i].getAsFile();
            const reader = new FileReader();
            reader.onload = function(evt) {
              return options.callback.call(element, {
                dataURL: evt.target.result,
                event: evt,
                file: f,
                name: f.name,
              });
            };
            reader.readAsDataURL(f);
            return;
          }
        }
      });
    });
  });
})(jQuery);