-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathplugin.js
More file actions
156 lines (153 loc) · 5.91 KB
/
Copy pathplugin.js
File metadata and controls
156 lines (153 loc) · 5.91 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
CKEDITOR.plugins.add( 'simage', {
icons: 'simage',
allowedContent: 'img[alt,!src,width,height,data-width,data-height]{border-style,border-width,float,height,margin,margin-bottom,margin-left,margin-right,margin-top,width}',
init: function( editor ) {
editor.addCommand('simage', {
exec: function (editor) {
a = document.createElement('input')
a.setAttribute('type','file')
a.setAttribute('accept', '.jpg,.jpeg,.png,.tif,.gif,.svg')
a.click()
a.onchange = function(){
file = a.files[0];
$(CKEDITOR.currentInstance).trigger('enableFormSubmit')
curr = CKEDITOR.currentInstance
if (file.size > 5000000){
b = document.createElement('div')
b.className = 'message alert alert-danger'
m = document.createElement('span')
m.innerHTML = "Image size exceeded! Please upload image of less than 5 MB."
b.appendChild(m)
c = document.createElement('span')
c.className = 'close'
c.innerHTML = 'X'
b.appendChild(c)
e = document.querySelector('.error-space')
e.appendChild(b)
setTimeout(function(){
alert = document.querySelector('.alert-danger')
alert.parentNode.removeChild(alert)
}, 20000)
c.onclick = function(){
b = document.querySelector('.alert-danger')
b.parentNode.removeChild(b)
}
$(CKEDITOR.instances[curr.name]).trigger('enableFormSubmit')
return
}else if (['jpeg','jpg','png','svg','gif','tif', 'svg+xml'].indexOf(file.type.split('/')[1]) === -1){
b = document.createElement('div')
b.className = 'message alert alert-danger'
m = document.createElement('span')
m.innerHTML = "The uploaded image format is not of acceptible format! Please upload an image!"
b.appendChild(m)
c = document.createElement('span')
c.className = 'close'
c.innerHTML = 'X'
b.appendChild(c)
e = document.querySelector('.error-space')
e.appendChild(b)
setTimeout(function(){
alert = document.querySelector('.alert-danger')
alert.parentNode.removeChild(alert)
}, 20000)
c.onclick = function(){
b = document.querySelector('.alert-danger')
b.parentNode.removeChild(b)
}
$(CKEDITOR.instances[curr.name]).trigger('enableFormSubmit')
return
}
img = new Image()
img.onload = function(){
inputWidth = this.width
inputHeight = this.height
}
img.src = window.URL.createObjectURL(file)
formData = new FormData;
formData.append('file', file);
loaderElem = new CKEDITOR.dom.element('loader-elem')
loaderHtmlStr = '<div style="position: relative; z-index: 100;width: 100%;height: 100%;text-align: center;background: white;opacity: 0.75;pointer-events:none">' + '<div style="width: 100%;height: 30px;margin-top: 100px;">Please wait while image is uploading...</div>' + '</div>'
loaderDomEle = CKEDITOR.dom.element.createFromHtml(loaderHtmlStr)
loaderElem.append(loaderDomEle)
editor.insertElement(loaderElem)
CKEDITOR.currentInstance.setReadOnly(true)
$.ajax({
url: editor.config.imageUploadURL,
type: 'POST',
data: formData,
processData: false,
contentType: false
}).success((function(_this) {
return function(data, textStatus, jqXHR) {
var isNew;
try {
data = JSON.parse(data)
} catch {}
if (jqXHR.status == 200) {
CKEDITOR.instances[curr.name].setReadOnly(false)
url = editor.config.dataParser(data)
elem = new CKEDITOR.dom.element( 'elem' )
maxWidth = Math.min(inputWidth, 600)
maxHeight = Math.min(inputHeight, 600)
if ((maxWidth/maxHeight) > (inputWidth/inputHeight)){
width = (maxWidth * inputWidth)/inputHeight
height = maxHeight
} else if ((maxWidth/maxHeight) < (inputWidth/inputHeight)){
width = maxWidth
height = (maxHeight * inputHeight)/inputWidth
} else{
width = maxWidth
height = maxHeight
}
newLine = CKEDITOR.dom.element.createFromHtml('<p><br></p>')
if (editor.config.srcSet){
srcSet = editor.config.srcSet(data)
imgElem = '<img src="' + url + '" class="image-editor" srcset="'+ srcSet +'" data-width="' + inputWidth + '" data-height="' + inputHeight + '" height="' + height + '" width="' + width + '">'
} else{
imgElem = '<img src="' + url + '" class="image-editor" data-width="' + inputWidth + '" data-height="' + inputHeight + '" height="' + height + '" width="' + width + '">'
}
imgDomElem = CKEDITOR.dom.element.createFromHtml(imgElem)
elem.append(imgDomElem)
editor.insertElement(newLine)
editor.insertElement(elem)
editor.insertElement(newLine)
loaderElem.remove()
$(CKEDITOR.instances[curr.name]).trigger('enableFormSubmit')
}
}
}(this))).error((function(_this){
return function(data, textStatus, jqXHR) {
CKEDITOR.instances[curr.name].setReadOnly(false)
b = document.createElement('div')
b.className = 'message alert alert-danger'
m = document.createElement('span')
m.innerHTML = "Image upload failed! Please try again!"
b.appendChild(m)
c = document.createElement('span')
c.className = 'close'
c.innerHTML = 'X'
b.appendChild(c)
e = document.querySelector('.error-space')
e.appendChild(b)
loaderElem.remove()
$(CKEDITOR.instances[curr.name]).trigger('enableFormSubmit')
setTimeout(function(){
alert = document.querySelector('.alert-danger')
alert.parentNode.removeChild(alert)
}, 20000)
c.onclick = function(){
b = document.querySelector('.alert-danger')
b.parentNode.removeChild(b)
}
}
}(this)))
}
}
});
editor.ui.addButton( 'SImage', {
label: 'Custom Image Uploader',
command: 'simage',
toolbar: 'insert'
});
}
});