mirror of
https://github.com/cosmocode/dokuwiki-plugin-imgpaste.git
synced 2025-07-22 00:34:39 +00:00
65 lines
2.2 KiB
JavaScript
65 lines
2.2 KiB
JavaScript
/**
|
|
* Image paste Event handler
|
|
*
|
|
* @author STRd6
|
|
* @license MIT License
|
|
* @link http://strd6.com/2011/09/html5-javascript-pasting-image-data-in-chrome/
|
|
*/
|
|
(function ($) {
|
|
var defaults;
|
|
$.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);
|
|
|
|
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 () {
|
|
var $this, element;
|
|
element = this;
|
|
$this = $(this);
|
|
return $this.bind('paste', function (event) {
|
|
var clipboardData, found;
|
|
found = false;
|
|
clipboardData = event.clipboardData;
|
|
if(typeof clipboardData === 'undefined') return;
|
|
return Array.prototype.forEach.call(clipboardData.types, function (type, i) {
|
|
var file, reader;
|
|
if (found) {
|
|
return;
|
|
}
|
|
if (type.match(options.matchType) || clipboardData.items[i].type.match(options.matchType)) {
|
|
file = clipboardData.items[i].getAsFile();
|
|
reader = new FileReader();
|
|
reader.onload = function (evt) {
|
|
return options.callback.call(element, {
|
|
dataURL: evt.target.result,
|
|
event: evt,
|
|
file: file,
|
|
name: file.name
|
|
});
|
|
};
|
|
reader.readAsDataURL(file);
|
|
return found = true;
|
|
}
|
|
});
|
|
});
|
|
});
|
|
};
|
|
})(jQuery);
|