forked from uploadcare/uploadcare-ckeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
115 lines (103 loc) · 3.83 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
// Uploadcare CKeditor plugin
// Version: 2.0.0
CKEDITOR.plugins.add('uploadcare', {
hidpi: true,
icons: 'uploadcare',
init : function(editor) {
var config = editor.config.uploadcare || {};
// Check if Uploadcare is already loaded and load it if not.
if (typeof uploadcare === 'undefined') {
var version = config.widgetVersion || '1.3.1';
var widget_url = 'https://ucarecdn.com/widget/' + version +
'/uploadcare/uploadcare-' + version + '.min.js'
CKEDITOR.scriptLoader.load(widget_url);
}
// Apply default properties.
if ( ! ('crop' in config)) {
config.crop = '';
}
if ( ! ('autostore' in config)) {
config.autostore = true;
}
function searchSelectedElement(editor, needle) {
var sel = editor.getSelection();
var element = sel.getSelectedElement();
if (element && element.is(needle)) {
return element;
}
var widget;
if (editor.widgets && (widget = editor.widgets.selected[0])) {
if (widget.element.is(needle)) {
return widget.element;
}
}
var range = sel.getRanges()[0];
if (range) {
range.shrink(CKEDITOR.SHRINK_TEXT);
return editor.elementPath(range.getCommonAncestor()).contains(needle, 1);
}
}
editor.addCommand('showUploadcareDialog', {
allowedContent: 'img[!src,alt]{width,height};a[!href]',
requiredContent: 'img[src];a[href]',
exec : function() {
if (typeof uploadcare == 'undefined') {
return; // not loaded yet
}
uploadcare.plugin(function(uc) {
var settings, element, file;
if (element = searchSelectedElement(editor, 'img')) {
file = element.getAttribute('src');
} else if (element = searchSelectedElement(editor, 'a')) {
file = element.getAttribute('href');
}
if (file && uc.utils.splitCdnUrl(file)) {
settings = uc.settings.build(
uc.jQuery.extend({}, config, {multiple: false})
);
file = uploadcare.fileFrom('uploaded', file, settings);
} else {
settings = uc.settings.build(config)
file = null;
}
var dialog = uploadcare.openDialog(file, settings).done(function(selected) {
var files = settings.multiple ? selected.files() : [selected];
uc.jQuery.when.apply(null, files).done(function() {
uc.jQuery.each(arguments, function() {
var imageUrl = this.cdnUrl;
if (this.isImage && ! this.cdnUrlModifiers) {
imageUrl += '-/preview/';
}
if (element) {
var widget;
if (editor.widgets && (widget = editor.widgets.selected[0])
&& widget.element === element
) {
widget.setData('src', imageUrl).setData('height', null)
} else if (element.getName() == 'img') {
element.data('cke-saved-src', '');
element.setAttribute('src', imageUrl);
} else {
element.data('cke-saved-href', '');
element.setAttribute('href', this.cdnUrl);
}
} else {
if (this.isImage) {
editor.insertHtml('<img src="' + imageUrl + '" alt="" /><br>', 'unfiltered_html');
} else {
editor.insertHtml('<a href="' + this.cdnUrl + '">'+this.name+'</a> ', 'unfiltered_html');
}
}
});
});
});
});
}
});
editor.ui.addButton && editor.ui.addButton('Uploadcare', {
label : 'Uploadcare',
toolbar : 'insert',
command : 'showUploadcareDialog'
});
}
});