2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @file Clipboard support
\r
12 // Tries to execute any of the paste, cut or copy commands in IE. Returns a
\r
13 // boolean indicating that the operation succeeded.
\r
14 var execIECommand = function( editor, command )
\r
16 var doc = editor.document,
\r
17 body = doc.getBody();
\r
20 var onExec = function()
\r
25 // The following seems to be the only reliable way to detect that
\r
26 // clipboard commands are enabled in IE. It will fire the
\r
27 // onpaste/oncut/oncopy events only if the security settings allowed
\r
28 // the command to execute.
\r
29 body.on( command, onExec );
\r
31 // IE6/7: document.execCommand has problem to paste into positioned element.
\r
32 ( CKEDITOR.env.version > 7 ? doc.$ : doc.$.selection.createRange() ) [ 'execCommand' ]( command );
\r
34 body.removeListener( command, onExec );
\r
39 // Attempts to execute the Cut and Copy operations.
\r
42 function( editor, type )
\r
44 return execIECommand( editor, type );
\r
47 function( editor, type )
\r
51 // Other browsers throw an error if the command is disabled.
\r
52 return editor.document.$.execCommand( type, false, null );
\r
60 // A class that represents one of the cut or copy commands.
\r
61 var cutCopyCmd = function( type )
\r
64 this.canUndo = this.type == 'cut'; // We can't undo copy to clipboard.
\r
65 this.startDisabled = true;
\r
68 cutCopyCmd.prototype =
\r
70 exec : function( editor, data )
\r
72 this.type == 'cut' && fixCut( editor );
\r
74 var success = tryToCutCopy( editor, this.type );
\r
77 alert( editor.lang.clipboard[ this.type + 'Error' ] ); // Show cutError or copyError.
\r
92 // Prevent IE from pasting at the begining of the document.
\r
95 if ( !editor.document.getBody().fire( 'beforepaste' )
\r
96 && !execIECommand( editor, 'paste' ) )
\r
98 editor.fire( 'pasteDialog' );
\r
107 if ( !editor.document.getBody().fire( 'beforepaste' )
\r
108 && !editor.document.$.execCommand( 'Paste', false, null ) )
\r
115 setTimeout( function()
\r
117 editor.fire( 'pasteDialog' );
\r
124 // Listens for some clipboard related keystrokes, so they get customized.
\r
125 var onKey = function( event )
\r
127 if ( this.mode != 'wysiwyg' )
\r
130 switch ( event.data.keyCode )
\r
133 case CKEDITOR.CTRL + 86 : // CTRL+V
\r
134 case CKEDITOR.SHIFT + 45 : // SHIFT+INS
\r
136 var body = this.document.getBody();
\r
138 // Simulate 'beforepaste' event for all none-IEs.
\r
139 if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )
\r
141 // Simulate 'paste' event for Opera/Firefox2.
\r
142 else if ( CKEDITOR.env.opera
\r
143 || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
\r
144 body.fire( 'paste' );
\r
148 case CKEDITOR.CTRL + 88 : // CTRL+X
\r
149 case CKEDITOR.SHIFT + 46 : // SHIFT+DEL
\r
151 // Save Undo snapshot.
\r
153 this.fire( 'saveSnapshot' ); // Save before paste
\r
154 setTimeout( function()
\r
156 editor.fire( 'saveSnapshot' ); // Save after paste
\r
161 function cancel( evt ) { evt.cancel(); }
\r
163 // Allow to peek clipboard content by redirecting the
\r
164 // pasting content into a temporary bin and grab the content of it.
\r
165 function getClipboardData( evt, mode, callback )
\r
167 var doc = this.document;
\r
169 // Avoid recursions on 'paste' event or consequent paste too fast. (#5730)
\r
170 if ( doc.getById( 'cke_pastebin' ) )
\r
173 // If the browser supports it, get the data directly
\r
174 if ( mode == 'text' && evt.data && evt.data.$.clipboardData )
\r
176 // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows.
\r
177 var plain = evt.data.$.clipboardData.getData( 'text/plain' );
\r
180 evt.data.preventDefault();
\r
186 var sel = this.getSelection(),
\r
187 range = new CKEDITOR.dom.range( doc );
\r
189 // Create container to paste into
\r
190 var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : CKEDITOR.env.webkit ? 'body' : 'div', doc );
\r
191 pastebin.setAttribute( 'id', 'cke_pastebin' );
\r
192 // Safari requires a filler node inside the div to have the content pasted into it. (#4882)
\r
193 CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );
\r
194 doc.getBody().append( pastebin );
\r
196 pastebin.setStyles(
\r
198 position : 'absolute',
\r
199 // Position the bin exactly at the position of the selected element
\r
200 // to avoid any subsequent document scroll.
\r
201 top : sel.getStartElement().getDocumentPosition().y + 'px',
\r
204 overflow : 'hidden'
\r
207 // It's definitely a better user experience if we make the paste-bin pretty unnoticed
\r
208 // by pulling it off the screen.
\r
209 pastebin.setStyle( this.config.contentsLangDirection == 'ltr' ? 'left' : 'right', '-1000px' );
\r
211 var bms = sel.createBookmarks();
\r
213 this.on( 'selectionChange', cancel, null, null, 0 );
\r
215 // Turn off design mode temporarily before give focus to the paste bin.
\r
216 if ( mode == 'text' )
\r
217 pastebin.$.focus();
\r
220 range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );
\r
221 range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );
\r
222 range.select( true );
\r
226 // Wait a while and grab the pasted contents
\r
227 window.setTimeout( function()
\r
229 mode == 'text' && CKEDITOR.env.gecko && editor.focusGrabber.focus();
\r
231 editor.removeListener( 'selectionChange', cancel );
\r
233 // Grab the HTML contents.
\r
234 // We need to look for a apple style wrapper on webkit it also adds
\r
235 // a div wrapper if you copy/paste the body of the editor.
\r
236 // Remove hidden div and restore selection.
\r
238 pastebin = ( CKEDITOR.env.webkit
\r
239 && ( bogusSpan = pastebin.getFirst() )
\r
240 && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?
\r
241 bogusSpan : pastebin );
\r
243 sel.selectBookmarks( bms );
\r
244 callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );
\r
248 // Cutting off control type element in IE standards breaks the selection entirely. (#4881)
\r
249 function fixCut( editor )
\r
251 if ( !CKEDITOR.env.ie || CKEDITOR.env.quirks )
\r
254 var sel = editor.getSelection();
\r
256 if( ( sel.getType() == CKEDITOR.SELECTION_ELEMENT ) && ( control = sel.getSelectedElement() ) )
\r
258 var range = sel.getRanges()[ 0 ];
\r
259 var dummy = editor.document.createText( '' );
\r
260 dummy.insertBefore( control );
\r
261 range.setStartBefore( dummy );
\r
262 range.setEndAfter( control );
\r
263 sel.selectRanges( [ range ] );
\r
265 // Clear up the fix if the paste wasn't succeeded.
\r
266 setTimeout( function()
\r
268 // Element still online?
\r
269 if ( control.getParent() )
\r
272 sel.selectElement( control );
\r
278 var depressBeforeEvent;
\r
279 function stateFromNamedCommand( command, editor )
\r
281 // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',
\r
282 // guard to distinguish from the ordinary sources( either
\r
283 // keyboard paste or execCommand ) (#4874).
\r
284 CKEDITOR.env.ie && ( depressBeforeEvent = 1 );
\r
286 var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
\r
287 depressBeforeEvent = 0;
\r
292 function setToolbarStates()
\r
294 if ( this.mode != 'wysiwyg' )
\r
297 this.getCommand( 'cut' ).setState( inReadOnly ? CKEDITOR.TRISTATE_DISABLED : stateFromNamedCommand( 'Cut', this ) );
\r
298 this.getCommand( 'copy' ).setState( stateFromNamedCommand( 'Copy', this ) );
\r
299 var pasteState = inReadOnly ? CKEDITOR.TRISTATE_DISABLED :
\r
300 CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', this );
\r
301 this.fire( 'pasteState', pasteState );
\r
304 // Register the plugin.
\r
305 CKEDITOR.plugins.add( 'clipboard',
\r
307 requires : [ 'dialog', 'htmldataprocessor' ],
\r
308 init : function( editor )
\r
310 // Inserts processed data into the editor at the end of the
\r
312 editor.on( 'paste', function( evt )
\r
314 var data = evt.data;
\r
315 if ( data[ 'html' ] )
\r
316 editor.insertHtml( data[ 'html' ] );
\r
317 else if ( data[ 'text' ] )
\r
318 editor.insertText( data[ 'text' ] );
\r
320 setTimeout( function () { editor.fire( 'afterPaste' ); }, 0 );
\r
322 }, null, null, 1000 );
\r
324 editor.on( 'pasteDialog', function( evt )
\r
326 setTimeout( function()
\r
328 // Open default paste dialog.
\r
329 editor.openDialog( 'paste' );
\r
333 editor.on( 'pasteState', function( evt )
\r
335 editor.getCommand( 'paste' ).setState( evt.data );
\r
338 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )
\r
340 var lang = editor.lang[ commandName ];
\r
342 editor.addCommand( commandName, command );
\r
343 editor.ui.addButton( buttonName,
\r
346 command : commandName
\r
349 // If the "menu" plugin is loaded, register the menu item.
\r
350 if ( editor.addMenuItems )
\r
352 editor.addMenuItem( commandName,
\r
355 command : commandName,
\r
356 group : 'clipboard',
\r
357 order : ctxMenuOrder
\r
362 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );
\r
363 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );
\r
364 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );
\r
366 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );
\r
368 editor.on( 'key', onKey, editor );
\r
370 // We'll be catching all pasted content in one line, regardless of whether the
\r
371 // it's introduced by a document command execution (e.g. toolbar buttons) or
\r
372 // user paste behaviors. (e.g. Ctrl-V)
\r
373 editor.on( 'contentDom', function()
\r
375 var body = editor.document.getBody();
\r
376 body.on( CKEDITOR.env.webkit ? 'paste' : 'beforepaste', function( evt )
\r
378 if ( depressBeforeEvent )
\r
381 // Fire 'beforePaste' event so clipboard flavor get customized
\r
382 // by other plugins.
\r
383 var eventData = { mode : 'html' };
\r
384 editor.fire( 'beforePaste', eventData );
\r
386 getClipboardData.call( editor, evt, eventData.mode, function ( data )
\r
388 // The very last guard to make sure the
\r
389 // paste has successfully happened.
\r
390 if ( !( data = CKEDITOR.tools.trim( data.replace( /<span[^>]+data-cke-bookmark[^<]*?<\/span>/ig,'' ) ) ) )
\r
393 var dataTransfer = {};
\r
394 dataTransfer[ eventData.mode ] = data;
\r
395 editor.fire( 'paste', dataTransfer );
\r
399 body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );
\r
401 body.on( 'mouseup', function(){ setTimeout( function(){ setToolbarStates.call( editor ); }, 0 ); }, editor );
\r
402 body.on( 'keyup', setToolbarStates, editor );
\r
405 // For improved performance, we're checking the readOnly state on selectionChange instead of hooking a key event for that.
\r
406 editor.on( 'selectionChange', function( evt )
\r
408 inReadOnly = evt.data.selection.getRanges()[ 0 ].checkReadOnly();
\r
409 setToolbarStates.call( editor );
\r
412 // If the "contextmenu" plugin is loaded, register the listeners.
\r
413 if ( editor.contextMenu )
\r
415 editor.contextMenu.addListener( function( element, selection )
\r
417 var readOnly = selection.getRanges()[ 0 ].checkReadOnly();
\r
419 cut : !readOnly && stateFromNamedCommand( 'Cut', editor ),
\r
420 copy : stateFromNamedCommand( 'Copy', editor ),
\r
421 paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', editor ) )
\r
430 * Fired when a clipboard operation is about to be taken into the editor.
\r
431 * Listeners can manipulate the data to be pasted before having it effectively
\r
432 * inserted into the document.
\r
433 * @name CKEDITOR.editor#paste
\r
436 * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined.
\r
437 * @param {String} [data.text] The plain text data to be pasted, available when plain text operations are to used. If not available, e.data.html will be defined.
\r
441 * Internal event to open the Paste dialog
\r
442 * @name CKEDITOR.editor#pasteDialog
\r