-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
364 lines (281 loc) · 10.1 KB
/
plugin.js
File metadata and controls
364 lines (281 loc) · 10.1 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* @version 0.1.0
*/
(function($) {
// Global config
var pConfig = {
images_only : true // Paste images only? (Currently not supported)
};
// Extend config
/*if( window.ScreenshotPasteConfig )
$.extend( true, pConfig, window.ScreenshotPasteConfig );*/
// Something like local globals
var pEditor = null; // Any editor instance
var pDocument = document; // The document of the parent window
var pWaitingForInput = null; // The currently active/focused contenteditable to take the pasted screenshots from
/**
* Check whether the two given buttons are equal
* Since we might run with a jQuery version <1.6 we can't rely on $.is()
*
* @param pButton1
* @param pButton2
* @return (bool)
*/
function buttonsEqual( pButton1, pButton2 ) {
return pButton1.attr("id")==pButton2.attr("id");
}
/**
* Set the given buttons text
*
* @param sText The text to be set
* @param pButton The button to set the text to. Defaults to pWaitingForInput
*/
function setText( sText, pButton, sAppend ) {
if( !pButton )
pButton = pWaitingForInput;
if( !pButton )
return;
pButton
.removeClass( 'paste-error' )
.removeClass( 'active' )
.text('');
if( sText=='unknown_file_type' || sText=='missing_image' || sText=='use_real_browser' )
pButton
.addClass( 'paste-error' );
else if( sText=='press_ctrl_v' )
pButton
.addClass( 'active' );
sText = pEditor.lang.screenshot_paste[sText] + (sAppend?sAppend:'');
$('<span class="cke_button_label"></span>')
.text( sText )
.appendTo( pButton );
}
/**
* Don't let anyone know about the mousedown event
*
* @param e The jQuery event object
* @return FALSE
*/
function mousedown(e) {
e.stopPropagation();
e.preventDefault();
return false;
}
/**
* Don't let anyone know about the mouseup event
*
* @param e The jQuery event object
* @return FALSE
*/
function mouseup(e) {
e.stopPropagation();
e.preventDefault();
return false;
}
/**
* Handle contenteditable click event
* We simply focus the content
*
* @param e The jQuery event object
* @return FALSE
*/
function click(e) {
pNewInput = $(e.target);
if( !pNewInput.hasClass('cke_button__screenshotpaste') )
pNewInput = pNewInput.parents( '.cke_button__screenshotpaste' );
if( pWaitingForInput && !buttonsEqual( pWaitingForInput, pNewInput ) )
setText( 'click_here' );
pWaitingForInput = pNewInput;
pWaitingForInput.focus();
e.stopPropagation();
e.preventDefault();
return false;
}
/**
* Handle conenteditable blur event
* Here we change back the text and unset the global pointing to us
*
* @param e The jQuery event object
* @return FALSE
*/
function blur(e) {
setText( 'click_here' );
if( pWaitingForInput && buttonsEqual( pWaitingForInput, $(e.target) ) )
pWaitingForInput = null;
}
/**
* Handle conenteditable focus event
* Change text to "PASTE NOW" and select whole text to make sure we don't paste our own translations instead...
*
* @param e The jQuery event object
* @return FALSE
*/
function focus(e) {
if( pWaitingForInput && !buttonsEqual( pWaitingForInput, $(e.target) ) )
setText( 'click_here' );
pWaitingForInput = $(e.target);
setText( 'press_ctrl_v' );
//pWaitingForInput.focus();
if( pWaitingForInput[0].execCommand )
pWaitingForInput[0].execCommand( 'selectAll', false, null );
//e.stopPropagation();
//e.preventDefault();
//return false;
}
/**
* Handle the global paste event
*
* @param event The jQuery event object
* @return FALSE
*/
function onPaste(event) {
// No input selected... make sure that we don't accidentally display a wrong button text
if( !pWaitingForInput ) {
$(pDocument)
.find('div.cke_button__screenshotpaste')
.each( function() {
setText( 'click_here', $(this) );
} );
return;
}
// Chrome: We have access to the files from the clipboard. So why not inserting them?!
if( event.originalEvent.clipboardData ) {
var items = event.originalEvent.clipboardData.items;
if( !items )
items = event.originalEvent.clipboardData.files;
if( !items.length ) {
setText( 'missing_image' );
pWaitingForInput = false;
return;
}
var sType = items[0].type;
var bImage = sType.substr(0,6)=="image/";
var bText = sType.substr(0,5)=="text/";
if( !items[0] || (!bImage && (pConfig.images_only || !bText)) ) {
if( items[0] ) {
if( pWaitingForInput )
setText( 'unknown_file_type', null, items[0].type );
}
pWaitingForInput = false;
return;
}
// Text doesn't need a reader. Just paste it
if( bText ) {
items[0].getAsString( function(data) {
if( sType=="text/html" )
paste( data );
else
paste( null, null, data );
} );
return;
}
// Read blob file, then paste the data url as img-src
var blob = items[0].getAsFile();
var reader = new FileReader();
reader.onload = function( event ) {
paste( null, event.target.result );
};
reader.readAsDataURL( blob );
return false;
}
else {
// Since firefox hasn't added the screenshot to the contenteditable yet, we wait a milisecond before pasting it
setTimeout( function() {
var html = pWaitingForInput.html();
if( html.indexOf( '<img' )<0 ) {
setText( 'missing_image' );
pWaitingForInput = false;
}
else
paste( html );
}, 1 );
// Firefox would skip pasting it, if we returned false here...
//return false;
}
}
$( pDocument ).bind( "paste", onPaste );
/**
* Paste the given data into the CKEDITOR
*
* @param html The HTML to paste
* @param image The image SRC to be added. Will overwrite any given HTML
* @param text The plain text to be added. Will overwrite both html and image
* @return NULL
*/
function paste( html, image, text ) {
var sID = pWaitingForInput.parents( '.cke' ).attr( 'id' ).substr( 4 );
setText( 'click_here' );
if( text ) {
CKEDITOR.instances[ sID ].insertText( text );
}
else {
if( image )
html = "<img src=\""+image+"\" />";
var object = CKEDITOR.dom.element.createFromHtml( html );
CKEDITOR.instances[ sID ].insertElement( object );
}
pWaitingForInput = null;
}
// Global function to transform toolbar buttons into contenteditables
window._____loadScreenshotPasteButton = function() {
var pButton = $(pDocument).find('a.cke_button__screenshotpaste');
var pEditable = $('<div />');
setText( 'click_here', pEditable );
pButton.each( function() {
var pSelf = $(this);
var sID = pSelf.parents( '.cke' ).attr( 'id' )+'-screenshot-paste';
var pInstance = pEditable
.attr( "id", sID )
.clone()
.addClass( pSelf.attr('class') )
.attr( "contenteditable", "" )
.mousedown( mousedown )
.click( click )
.mouseup( mouseup )
.focus( focus )
.blur( blur );
pButton
.replaceWith( pInstance );
} );
};
var pGlobalInited = false;
// Add our plugin
CKEDITOR.plugins.add( 'screenshot_paste', {
icons : 'click', // Should never be displayed
lang : 'en,de', // Languages we support
init : function( editor ) {
// As taken from http://stackoverflow.com/a/9851769
var isFirefox = 'MozBoxSizing' in document.documentElement.style;
var isChrome = !!(window.chrome && chrome.webstore && chrome.webstore.install);
// No IE/Opera/Safari etc.
if( !isFirefox && !isChrome )
return;
// Config given? Support it.
/*if( editor.config.screenshot_paste )
$.extend( true, pConfig, editor.config.screenshot_paste );*/
// For the case we need any editor instance somewhere (like when using translations)
pEditor = editor;
// Hollow command
editor.addCommand( 'pasteScreenshot', {
exec : function( editor )
{
}
} );
/*var add = editor.ui.addButton;
add.apply( editor.ui, [ 'ScreenshotPaste', {*/
editor.ui.addButton( 'ScreenshotPaste', {
label: editor.lang.screenshot_paste.click_here,
command: 'pasteScreenshot',
icon: this.path + 'click.png'
} );
// Replace toolbar buttons
setTimeout( '_____loadScreenshotPasteButton();', 1 );
// Require only once...
if( !pGlobalInited ) {
// Add fancy stylesheet
CKEDITOR.document.appendStyleSheet( CKEDITOR.getUrl( this.path + 'plugin.css' ) );
pGlobalInited = true;
}
}
} );
})(jQuery);