1 | /***************************************************************************************** |
---|
2 | |
---|
3 | This plugin opens a little popup after pasting an image. |
---|
4 | You can set several options that will be set to your upload URL. |
---|
5 | |
---|
6 | The upload URL |
---|
7 | config.imageUploadUrl = 'browser/upload.php?pasted=newfile'; |
---|
8 | is extended to |
---|
9 | browser/upload.php?pasted=newfile&filepath=' + YourPath + '&imgwidth=' + YourWidth + '&imgheight=' + YourHeight; |
---|
10 | |
---|
11 | The filepath, imgwidth and imgheight variables are only examples. Create inputs by your needs. |
---|
12 | |
---|
13 | Example upload.php |
---|
14 | <?PHP |
---|
15 | if($_GET['pasted'] == 'newfile') { |
---|
16 | $filename = rawurldecode($_GET['filename']); |
---|
17 | $filepath = rawurldecode($_GET['filepath']); |
---|
18 | $imgwidth = $_GET['imgwidth']; |
---|
19 | $imgheight = $_GET['imgheight']; |
---|
20 | // Your file upload progress |
---|
21 | echo json_encode( |
---|
22 | array( |
---|
23 | 'uploaded' => 1, |
---|
24 | 'fileName' => $filename, |
---|
25 | 'url' => 'images/'.$filepath.'/'.$filename, |
---|
26 | 'attributes' => array( |
---|
27 | 'width' => $imgwidth, |
---|
28 | 'height' => $imgheight |
---|
29 | ) |
---|
30 | ) |
---|
31 | ); |
---|
32 | } |
---|
33 | ?> |
---|
34 | |
---|
35 | *****************************************************************************************/ |
---|
36 | |
---|
37 | 'use strict'; |
---|
38 | |
---|
39 | (function() |
---|
40 | { |
---|
41 | CKEDITOR.plugins.add( 'pastedupload', |
---|
42 | { |
---|
43 | requires: 'uploadwidget', |
---|
44 | |
---|
45 | onLoad : function() |
---|
46 | { |
---|
47 | CKEDITOR.addCss( |
---|
48 | '.cke_upload_loaded, .cke_upload_uploading {' + |
---|
49 | 'position: static;' + |
---|
50 | 'max-width: 100%;' + |
---|
51 | '}' + |
---|
52 | '.cke_upload_loaded img, .cke_upload_uploading img {' + |
---|
53 | 'max-width: 100%;' + |
---|
54 | 'opacity: 0.3;' + |
---|
55 | '}' + |
---|
56 | '.cke_upload_background_cover {' + |
---|
57 | 'position: fixed;' + |
---|
58 | 'z-index: 9000;' + |
---|
59 | 'top: 0px;' + |
---|
60 | 'left: 0px;' + |
---|
61 | 'background-color: white;' + |
---|
62 | 'opacity: 0.5;' + |
---|
63 | 'width: 100%;' + |
---|
64 | 'height: 100%;' + |
---|
65 | '}' + |
---|
66 | '.cke_pastedupload_options {' + |
---|
67 | 'position: fixed;' + |
---|
68 | 'z-index: 9010;' + |
---|
69 | 'top: 50%;' + |
---|
70 | 'left: 50%;' + |
---|
71 | 'transform: translate(-50%, -50%);' + |
---|
72 | 'border: 1px solid #bebebe;' + |
---|
73 | 'background: #f4f4f4;' + |
---|
74 | 'padding: 20px;' + |
---|
75 | '}' + |
---|
76 | '.cke_pastedupload_options input {' + |
---|
77 | 'border: 1px solid #bebebe;' + |
---|
78 | 'background: #fff;' + |
---|
79 | '}' + |
---|
80 | '.cke_pastedupload_options div {' + |
---|
81 | 'margin: 4px;' + |
---|
82 | '}' + |
---|
83 | '#cke_pastedupload_button {' + |
---|
84 | 'width: 100%;' + |
---|
85 | 'background: #bebebe;' + |
---|
86 | 'text-align: center;' + |
---|
87 | '}' |
---|
88 | ); |
---|
89 | }, |
---|
90 | |
---|
91 | init: function( editor ) |
---|
92 | { |
---|
93 | if ( !CKEDITOR.plugins.clipboard.isFileApiSupported ) { |
---|
94 | return; |
---|
95 | } |
---|
96 | |
---|
97 | var popupSource = '<div class="cke_upload_background_cover"></div>' + |
---|
98 | '<div class="cke_pastedupload_options">' + |
---|
99 | '<div>Path: <input type="text" id="cke_pastedupload_path" /> px</div>' + |
---|
100 | '<div>Width: <input type="text" id="cke_pastedupload_width" /> px</div>' + |
---|
101 | '<div>Height: <input type="text" id="cke_pastedupload_height" /> px</div>' + |
---|
102 | '<div><input type="button" id="cke_pastedupload_button" value="upload" /></div>' + |
---|
103 | '</div>'; |
---|
104 | |
---|
105 | var loadingImage = 'data:image/gif;base64,R0lGODlhDgAOAIAAAAAAAP///yH5BAAAAAAALAAAAAAOAA4AAAIMhI+py+0Po5y02qsKADs='; |
---|
106 | var optionsPopup; |
---|
107 | var imageAttributes = {}; |
---|
108 | var fileUrl; |
---|
109 | var fileTools = CKEDITOR.fileTools; |
---|
110 | var uploadUrl = fileTools.getUploadUrl( editor.config, 'image' ); |
---|
111 | var loaders = {}; |
---|
112 | |
---|
113 | if ( !uploadUrl ) { |
---|
114 | window.console && window.console.log( |
---|
115 | 'Error: Upload URL for the Upload Image feature was not defined. ' + |
---|
116 | 'For more information see: http://docs.ckeditor.com/#!/guide/dev_file_upload' |
---|
117 | ); |
---|
118 | return; |
---|
119 | } |
---|
120 | |
---|
121 | fileTools.addUploadWidget( editor, 'imagepasted', { |
---|
122 | loadMethod: 'load', |
---|
123 | parts: { |
---|
124 | img: 'img' |
---|
125 | }, |
---|
126 | onLoaded: function( upload ) { |
---|
127 | this.parts.img.setAttribute( 'src', upload.data ); |
---|
128 | }, |
---|
129 | onUploaded: function( upload ) { |
---|
130 | var imgSource = '<img src="' + upload.url + '" '; |
---|
131 | for(var key in imageAttributes) { |
---|
132 | if(key == 'width' || key == 'height') { |
---|
133 | if(editor.config.image_prefillDimensions) { |
---|
134 | imgSource += key + '="' + imageAttributes[key] + '" '; |
---|
135 | } |
---|
136 | } |
---|
137 | else { |
---|
138 | imgSource += key + '="' + imageAttributes[key] + '" '; |
---|
139 | } |
---|
140 | } |
---|
141 | imgSource += '/>'; |
---|
142 | |
---|
143 | this.replaceWith( imgSource ); |
---|
144 | } |
---|
145 | }); |
---|
146 | |
---|
147 | editor.on( 'paste', function( evt ) { |
---|
148 | var data = evt.data, |
---|
149 | dataTransfer = data.dataTransfer, |
---|
150 | filesCount = dataTransfer.getFilesCount(); |
---|
151 | |
---|
152 | if ( data.dataValue || !filesCount ) { |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|
156 | for ( var i = 0; i < filesCount; i++ ) { |
---|
157 | var file = dataTransfer.getFile( i ); |
---|
158 | |
---|
159 | if ( fileTools.isTypeSupported( file, /image\/(jpeg|png|gif)/ ) ) { |
---|
160 | var el = new CKEDITOR.dom.element( 'img' ); |
---|
161 | el.setAttribute( 'src', loadingImage ); |
---|
162 | loaders[i] = editor.uploadRepository.create( file ); |
---|
163 | |
---|
164 | if ( el ) { |
---|
165 | loaders[i].load(); |
---|
166 | fileTools.markElement( el, 'imagepasted', loaders[i].id ); |
---|
167 | |
---|
168 | data.dataValue += el.getOuterHtml(); |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | if ( !data.dataValue.match( /<img[\s\S]+data:/i ) ) { |
---|
174 | return; |
---|
175 | } |
---|
176 | |
---|
177 | optionsPopup = new CKEDITOR.dom.element( 'div' ); |
---|
178 | optionsPopup.setAttributes({ 'class': 'cke_reset_all cke_upload_options' }); |
---|
179 | optionsPopup.setHtml( popupSource ); |
---|
180 | |
---|
181 | CKEDITOR.document.getBody().append( optionsPopup ); |
---|
182 | |
---|
183 | document.getElementById('cke_pastedupload_button').onclick = function() { |
---|
184 | editor.widgets.registered.imagepasted.loadMethod = 'upload'; |
---|
185 | |
---|
186 | var filepath = document.getElementById('cke_pastedupload_path').value; |
---|
187 | var imgwidth = document.getElementById('cke_pastedupload_width').value; |
---|
188 | var imgheight = document.getElementById('cke_pastedupload_height').value; |
---|
189 | |
---|
190 | fileUrl = '&filepath=' + encodeURIComponent(filepath); |
---|
191 | fileUrl += '&imgwidth=' + parseInt(imgwidth); |
---|
192 | fileUrl += '&imgheight=' + parseInt(imgheight); |
---|
193 | |
---|
194 | optionsPopup.remove(); |
---|
195 | |
---|
196 | for ( var key in loaders ) { |
---|
197 | var filename = loaders[key].fileName; |
---|
198 | fileUrl += '&filename=' + encodeURIComponent(filename); |
---|
199 | |
---|
200 | loaders[key].upload( uploadUrl + fileUrl ); |
---|
201 | |
---|
202 | fileTools.bindNotifications( editor, loaders[key] ); |
---|
203 | } |
---|
204 | } |
---|
205 | }, null, null, 11 ); |
---|
206 | |
---|
207 | editor.on( 'fileUploadRequest', function( evt ) { |
---|
208 | var fileLoader = evt.data.fileLoader; |
---|
209 | var xhr = fileLoader.xhr; |
---|
210 | |
---|
211 | xhr.open( 'POST', fileLoader.uploadUrl, true ); |
---|
212 | xhr.setRequestHeader( 'Cache-Control', 'no-cache' ); |
---|
213 | xhr.setRequestHeader( 'X-File-Name', fileLoader.fileName ); |
---|
214 | xhr.setRequestHeader( 'X-File-Size', fileLoader.total ); |
---|
215 | |
---|
216 | xhr.send( fileLoader.file ); |
---|
217 | |
---|
218 | evt.stop(); |
---|
219 | }, null, null, 1 ); |
---|
220 | |
---|
221 | editor.on( 'fileUploadResponse', function( evt ) { |
---|
222 | evt.stop(); |
---|
223 | |
---|
224 | var data = evt.data, |
---|
225 | xhr = data.fileLoader.xhr; |
---|
226 | |
---|
227 | try { |
---|
228 | var response = JSON.parse(xhr.responseText); |
---|
229 | |
---|
230 | if ( response.uploaded == 1 ) { |
---|
231 | imageAttributes = response.attributes; |
---|
232 | data.url = response.url; |
---|
233 | if(response.error) |
---|
234 | alert(response.error.message); |
---|
235 | } |
---|
236 | else { |
---|
237 | if(response.error) |
---|
238 | data.message = response.error.message; |
---|
239 | evt.cancel(); |
---|
240 | } |
---|
241 | } catch ( err ) { |
---|
242 | data.message = fileLoader.lang.filetools.responseError; |
---|
243 | window.console && window.console.log( xhr.responseText ); |
---|
244 | |
---|
245 | evt.cancel(); |
---|
246 | } |
---|
247 | }, null, null, 1 ); |
---|
248 | } |
---|
249 | }); |
---|
250 | })(); |
---|
251 | |
---|