2 Copyright (c) 2003-2010, 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
19 var enabled = false;
\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 doc.$.execCommand( command );
\r
33 body.removeListener( command, onExec );
\r
38 // Attempts to execute the Cut and Copy operations.
\r
41 function( editor, type )
\r
43 return execIECommand( editor, type );
\r
46 function( editor, type )
\r
50 // Other browsers throw an error if the command is disabled.
\r
51 return editor.document.$.execCommand( type );
\r
59 // A class that represents one of the cut or copy commands.
\r
60 var cutCopyCmd = function( type )
\r
63 this.canUndo = ( this.type == 'cut' ); // We can't undo copy to clipboard.
\r
66 cutCopyCmd.prototype =
\r
68 exec : function( editor, data )
\r
70 var success = tryToCutCopy( editor, this.type );
\r
73 alert( editor.lang.clipboard[ this.type + 'Error' ] ); // Show cutError or copyError.
\r
88 // Prevent IE from pasting at the begining of the document.
\r
91 if ( !editor.document.getBody().fire( 'beforepaste' )
\r
92 && !execIECommand( editor, 'paste' ) )
\r
94 editor.fire( 'pasteDialog' );
\r
103 if ( !editor.document.getBody().fire( 'beforepaste' )
\r
104 && !editor.document.$.execCommand( 'Paste', false, null ) )
\r
111 setTimeout( function()
\r
113 editor.fire( 'pasteDialog' );
\r
120 // Listens for some clipboard related keystrokes, so they get customized.
\r
121 var onKey = function( event )
\r
123 if ( this.mode != 'wysiwyg' )
\r
126 switch ( event.data.keyCode )
\r
129 case CKEDITOR.CTRL + 86 : // CTRL+V
\r
130 case CKEDITOR.SHIFT + 45 : // SHIFT+INS
\r
132 var body = this.document.getBody();
\r
134 // Simulate 'beforepaste' event for all none-IEs.
\r
135 if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )
\r
137 // Simulate 'paste' event for Opera/Firefox2.
\r
138 else if ( CKEDITOR.env.opera
\r
139 || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
\r
140 body.fire( 'paste' );
\r
144 case CKEDITOR.CTRL + 88 : // CTRL+X
\r
145 case CKEDITOR.SHIFT + 46 : // SHIFT+DEL
\r
147 // Save Undo snapshot.
\r
149 this.fire( 'saveSnapshot' ); // Save before paste
\r
150 setTimeout( function()
\r
152 editor.fire( 'saveSnapshot' ); // Save after paste
\r
157 // Allow to peek clipboard content by redirecting the
\r
158 // pasting content into a temporary bin and grab the content of it.
\r
159 function getClipboardData( evt, mode, callback )
\r
161 var doc = this.document;
\r
163 // Avoid recursions on 'paste' event for IE.
\r
164 if ( CKEDITOR.env.ie && doc.getById( 'cke_pastebin' ) )
\r
167 var sel = this.getSelection(),
\r
168 range = new CKEDITOR.dom.range( doc );
\r
170 // Create container to paste into
\r
171 var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : 'div', doc );
\r
172 pastebin.setAttribute( 'id', 'cke_pastebin' );
\r
173 // Safari requires a filler node inside the div to have the content pasted into it. (#4882)
\r
174 CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );
\r
175 doc.getBody().append( pastebin );
\r
177 // It's definitely a better user experience if we make the paste-bin pretty unnoticed
\r
178 // by pulling it off the screen, while this hack will make the paste-bin a control type element
\r
179 // and that become a selection plain later.
\r
180 if ( !CKEDITOR.env.ie && mode != 'html' )
\r
182 pastebin.setStyles(
\r
184 position : 'absolute',
\r
186 // Position the bin exactly at the position of the selected element
\r
187 // to avoid any subsequent document scroll.
\r
188 top : sel.getStartElement().getDocumentPosition().y + 'px',
\r
191 overflow : 'hidden'
\r
195 var bms = sel.createBookmarks();
\r
197 // Turn off design mode temporarily before give focus to the paste bin.
\r
198 if ( mode == 'text' )
\r
200 if ( CKEDITOR.env.ie )
\r
202 var ieRange = doc.getBody().$.createTextRange();
\r
203 ieRange.moveToElementText( pastebin.$ );
\r
204 ieRange.execCommand( 'Paste' );
\r
205 evt.data.preventDefault();
\r
209 doc.$.designMode = 'off';
\r
210 pastebin.$.focus();
\r
215 range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );
\r
216 range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );
\r
217 range.select( true );
\r
220 // Wait a while and grab the pasted contents
\r
221 window.setTimeout( function()
\r
223 mode == 'text' && !CKEDITOR.env.ie && ( doc.$.designMode = 'on' );
\r
226 // Grab the HTML contents.
\r
227 // We need to look for a apple style wrapper on webkit it also adds
\r
228 // a div wrapper if you copy/paste the body of the editor.
\r
229 // Remove hidden div and restore selection.
\r
231 pastebin = ( CKEDITOR.env.webkit
\r
232 && ( bogusSpan = pastebin.getFirst() )
\r
233 && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?
\r
234 bogusSpan : pastebin );
\r
236 sel.selectBookmarks( bms );
\r
237 callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );
\r
241 // Register the plugin.
\r
242 CKEDITOR.plugins.add( 'clipboard',
\r
244 requires : [ 'htmldataprocessor' ],
\r
245 init : function( editor )
\r
247 // Inserts processed data into the editor at the end of the
\r
249 editor.on( 'paste', function( evt )
\r
251 var data = evt.data;
\r
252 if ( data[ 'html' ] )
\r
253 editor.insertHtml( data[ 'html' ] );
\r
254 else if ( data[ 'text' ] )
\r
255 editor.insertText( data[ 'text' ] );
\r
257 }, null, null, 1000 );
\r
259 editor.on( 'pasteDialog', function( evt )
\r
261 setTimeout( function()
\r
263 // Open default paste dialog.
\r
264 editor.openDialog( 'paste' );
\r
268 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )
\r
270 var lang = editor.lang[ commandName ];
\r
272 editor.addCommand( commandName, command );
\r
273 editor.ui.addButton( buttonName,
\r
276 command : commandName
\r
279 // If the "menu" plugin is loaded, register the menu item.
\r
280 if ( editor.addMenuItems )
\r
282 editor.addMenuItem( commandName,
\r
285 command : commandName,
\r
286 group : 'clipboard',
\r
287 order : ctxMenuOrder
\r
292 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );
\r
293 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );
\r
294 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );
\r
296 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );
\r
298 editor.on( 'key', onKey, editor );
\r
300 var mode = editor.config.forcePasteAsPlainText ? 'text' : 'html';
\r
302 // We'll be catching all pasted content in one line, regardless of whether the
\r
303 // it's introduced by a document command execution (e.g. toolbar buttons) or
\r
304 // user paste behaviors. (e.g. Ctrl-V)
\r
305 editor.on( 'contentDom', function()
\r
307 var body = editor.document.getBody();
\r
308 body.on( ( mode == 'text' && CKEDITOR.env.ie ) ? 'paste' : 'beforepaste',
\r
311 if( depressBeforePasteEvent )
\r
314 getClipboardData.call( editor, evt, mode, function ( data )
\r
316 // The very last guard to make sure the
\r
317 // paste has successfully happened.
\r
321 var dataTransfer = {};
\r
322 dataTransfer[ mode ] = data;
\r
323 editor.fire( 'paste', dataTransfer );
\r
329 // If the "contextmenu" plugin is loaded, register the listeners.
\r
330 if ( editor.contextMenu )
\r
332 var depressBeforePasteEvent;
\r
333 function stateFromNamedCommand( command )
\r
335 // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste',
\r
336 // guard to distinguish from the ordinary sources( either
\r
337 // keyboard paste or execCommand ) (#4874).
\r
338 CKEDITOR.env.ie && command == 'Paste'&& ( depressBeforePasteEvent = 1 );
\r
340 var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
\r
341 depressBeforePasteEvent = 0;
\r
345 editor.contextMenu.addListener( function()
\r
348 cut : stateFromNamedCommand( 'Cut' ),
\r
350 // Browser bug: 'Cut' has the correct states for both Copy and Cut.
\r
351 copy : stateFromNamedCommand( 'Cut' ),
\r
352 paste : CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' )
\r