-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.js
115 lines (110 loc) · 3.6 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* {[The file is published on the basis of YetiForce Public License 3.0 that can be found in the following directory: licenses/LicenseEN.txt or yetiforce.com]} */
function initPasteEvent(editorInstance) {
if (editorInstance.addFeature) {
editorInstance.addFeature({
allowedContent: 'img[alt,id,!src]{width,height};'
});
}
editorInstance.on('contentDom', function() {
var editableElement = editorInstance.editable
? editorInstance.editable()
: editorInstance.document;
editableElement.on('paste', onPaste, null, { editor: editorInstance });
});
}
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
}
});
setTimeout(function() {
editor.insertElement(element);
}, 10);
};
reader.readAsDataURL(file);
}
CKEDITOR.plugins.add('ckeditor-image-to-base', {
requires: 'dialog',
icons: 'ckeditor-image-to-base',
hidpi: true,
init: function(editorInstance) {
initPasteEvent(editorInstance);
var pluginName = 'ckeditor-image-to-base-dialog';
editorInstance.ui.addToolbarGroup('ckeditor-image-to-base', 'insert');
editorInstance.ui.addButton('ckeditor-image-to-base', {
label: editorInstance.lang.common.image,
command: pluginName,
toolbar: 'insert'
});
CKEDITOR.dialog.add(
pluginName,
this.path + 'dialogs/ckeditor-image-to-base.js'
);
editorInstance.addCommand(
pluginName,
new CKEDITOR.dialogCommand(pluginName, {
allowedContent:
'img[alt,!src]{border-style,border-width,float,height,margin,margin-bottom,margin-left,margin-right,margin-top,width}',
requiredContent: 'img[alt,src]',
contentTransformations: [
['img{width}: sizeToStyle', 'img[width]: sizeToAttribute'],
['img{float}: alignmentToStyle', 'img[align]: alignmentToAttribute']
]
})
);
editorInstance.on('doubleclick', function(evt) {
if (
evt.data.element &&
!evt.data.element.isReadOnly() &&
evt.data.element.getName() === 'img'
) {
evt.data.dialog = pluginName;
editorInstance.getSelection().selectElement(evt.data.element);
}
});
if (editorInstance.addMenuItem) {
editorInstance.addMenuGroup('imageToBase64Group');
editorInstance.addMenuItem('imageToBase64Item', {
label: editorInstance.lang.common.image,
icon: this.path + 'icons/ckeditor-image-to-base.png',
command: pluginName,
group: 'imageToBase64Group'
});
}
if (editorInstance.contextMenu) {
editorInstance.contextMenu.addListener(function(element, selection) {
if (element && element.getName() === 'img') {
editorInstance.getSelection().selectElement(element);
return { imageToBase64Item: CKEDITOR.TRISTATE_ON };
}
return null;
});
}
}
});