-
Notifications
You must be signed in to change notification settings - Fork 10
/
plugin.js
executable file
·70 lines (56 loc) · 1.95 KB
/
plugin.js
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
64
65
66
67
68
69
70
(function () {
'use strict';
CKEDITOR.plugins.add('pastebase64', {
init: init
});
function init(editor) {
if (editor.addFeature) {
editor.addFeature({
allowedContent: 'img[alt,id,!src]{width,height};'
});
}
editor.on("contentDom", function () {
var editableElement = editor.editable ? editor.editable() : editor.document;
editableElement.on("paste", onPaste, null, {editor: editor});
});
}
function onPaste(event) {
var editor = event.listenerData && event.listenerData.editor;
var $event = event.data.$;
var clipboardData = $event.clipboardData;
var found = false;
var imageType = /^image/;
if (!clipboardData) {
return;
}
return Array.prototype.forEach.call(clipboardData.types, function (type, i) {
if (found) {
return;
}
if (type.match(imageType) || clipboardData.items[i].type.match(imageType)) {
readImageAsBase64(clipboardData.items[i], editor);
return found = true;
}
});
}
function readImageAsBase64(item, editor) {
if (!item || typeof item.getAsFile !== 'function') {
return;
}
var file = item.getAsFile();
var reader = new FileReader();
reader.onload = function (evt) {
var element = editor.document.createElement('img', {
attributes: {
src: evt.target.result
}
});
// We use a timeout callback to prevent a bug where insertElement inserts at first caret
// position
setTimeout(function () {
editor.insertElement(element);
}, 10);
};
reader.readAsDataURL(file);
}
})();