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 // 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 );
\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
67 cutCopyCmd.prototype =
\r
69 exec : function( editor, data )
\r
71 var success = tryToCutCopy( editor, this.type );
\r
74 alert( editor.lang.clipboard[ this.type + 'Error' ] ); // Show cutError or copyError.
\r
89 // Prevent IE from pasting at the begining of the document.
\r
92 if ( !editor.document.getBody().fire( 'beforepaste' )
\r
93 && !execIECommand( editor, 'paste' ) )
\r
95 editor.fire( 'pasteDialog' );
\r
104 if ( !editor.document.getBody().fire( 'beforepaste' )
\r
105 && !editor.document.$.execCommand( 'Paste', false, null ) )
\r
112 setTimeout( function()
\r
114 editor.fire( 'pasteDialog' );
\r
121 // Listens for some clipboard related keystrokes, so they get customized.
\r
122 var onKey = function( event )
\r
124 if ( this.mode != 'wysiwyg' )
\r
127 switch ( event.data.keyCode )
\r
130 case CKEDITOR.CTRL + 86 : // CTRL+V
\r
131 case CKEDITOR.SHIFT + 45 : // SHIFT+INS
\r
133 var body = this.document.getBody();
\r
135 // Simulate 'beforepaste' event for all none-IEs.
\r
136 if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )
\r
138 // Simulate 'paste' event for Opera/Firefox2.
\r
139 else if ( CKEDITOR.env.opera
\r
140 || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
\r
141 body.fire( 'paste' );
\r
145 case CKEDITOR.CTRL + 88 : // CTRL+X
\r
146 case CKEDITOR.SHIFT + 46 : // SHIFT+DEL
\r
148 // Save Undo snapshot.
\r
150 this.fire( 'saveSnapshot' ); // Save before paste
\r
151 setTimeout( function()
\r
153 editor.fire( 'saveSnapshot' ); // Save after paste
\r
158 // Allow to peek clipboard content by redirecting the
\r
159 // pasting content into a temporary bin and grab the content of it.
\r
160 function getClipboardData( evt, mode, callback )
\r
162 var doc = this.document;
\r
164 // Avoid recursions on 'paste' event for IE.
\r
165 if ( CKEDITOR.env.ie && doc.getById( 'cke_pastebin' ) )
\r
168 // If the browser supports it, get the data directly
\r
169 if (mode == 'text' && evt.data && evt.data.$.clipboardData)
\r
171 // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows.
\r
172 var plain = evt.data.$.clipboardData.getData( 'text/plain' );
\r
175 evt.data.preventDefault();
\r
181 var sel = this.getSelection(),
\r
182 range = new CKEDITOR.dom.range( doc );
\r
184 // Create container to paste into
\r
185 var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : 'div', doc );
\r
186 pastebin.setAttribute( 'id', 'cke_pastebin' );
\r
187 // Safari requires a filler node inside the div to have the content pasted into it. (#4882)
\r
188 CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );
\r
189 doc.getBody().append( pastebin );
\r
191 // It's definitely a better user experience if we make the paste-bin pretty unnoticed
\r
192 // by pulling it off the screen.
\r
193 pastebin.setStyles(
\r
195 position : 'absolute',
\r
197 // Position the bin exactly at the position of the selected element
\r
198 // to avoid any subsequent document scroll.
\r
199 top : sel.getStartElement().getDocumentPosition().y + 'px',
\r
202 overflow : 'hidden'
\r
205 var bms = sel.createBookmarks();
\r
207 // Turn off design mode temporarily before give focus to the paste bin.
\r
208 if ( mode == 'text' )
\r
210 if ( CKEDITOR.env.ie )
\r
212 var ieRange = doc.getBody().$.createTextRange();
\r
213 ieRange.moveToElementText( pastebin.$ );
\r
214 ieRange.execCommand( 'Paste' );
\r
215 evt.data.preventDefault();
\r
219 doc.$.designMode = 'off';
\r
220 pastebin.$.focus();
\r
225 range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );
\r
226 range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );
\r
227 range.select( true );
\r
230 // Wait a while and grab the pasted contents
\r
231 window.setTimeout( function()
\r
233 mode == 'text' && !CKEDITOR.env.ie && ( doc.$.designMode = 'on' );
\r
236 // Grab the HTML contents.
\r
237 // We need to look for a apple style wrapper on webkit it also adds
\r
238 // a div wrapper if you copy/paste the body of the editor.
\r
239 // Remove hidden div and restore selection.
\r
241 pastebin = ( CKEDITOR.env.webkit
\r
242 && ( bogusSpan = pastebin.getFirst() )
\r
243 && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?
\r
244 bogusSpan : pastebin );
\r
246 sel.selectBookmarks( bms );
\r
247 callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );
\r
251 // Register the plugin.
\r
252 CKEDITOR.plugins.add( 'clipboard',
\r
254 requires : [ 'dialog', 'htmldataprocessor' ],
\r
255 init : function( editor )
\r
257 // Inserts processed data into the editor at the end of the
\r
259 editor.on( 'paste', function( evt )
\r
261 var data = evt.data;
\r
262 if ( data[ 'html' ] )
\r
263 editor.insertHtml( data[ 'html' ] );
\r
264 else if ( data[ 'text' ] )
\r
265 editor.insertText( data[ 'text' ] );
\r
267 }, null, null, 1000 );
\r
269 editor.on( 'pasteDialog', function( evt )
\r
271 setTimeout( function()
\r
273 // Open default paste dialog.
\r
274 editor.openDialog( 'paste' );
\r
278 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )
\r
280 var lang = editor.lang[ commandName ];
\r
282 editor.addCommand( commandName, command );
\r
283 editor.ui.addButton( buttonName,
\r
286 command : commandName
\r
289 // If the "menu" plugin is loaded, register the menu item.
\r
290 if ( editor.addMenuItems )
\r
292 editor.addMenuItem( commandName,
\r
295 command : commandName,
\r
296 group : 'clipboard',
\r
297 order : ctxMenuOrder
\r
302 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );
\r
303 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );
\r
304 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );
\r
306 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );
\r
308 editor.on( 'key', onKey, editor );
\r
310 var mode = editor.config.forcePasteAsPlainText ? 'text' : 'html';
\r
312 // We'll be catching all pasted content in one line, regardless of whether the
\r
313 // it's introduced by a document command execution (e.g. toolbar buttons) or
\r
314 // user paste behaviors. (e.g. Ctrl-V)
\r
315 editor.on( 'contentDom', function()
\r
317 var body = editor.document.getBody();
\r
318 body.on( ( (mode == 'text' && CKEDITOR.env.ie) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',
\r
321 if ( depressBeforePasteEvent )
\r
324 getClipboardData.call( editor, evt, mode, function ( data )
\r
326 // The very last guard to make sure the
\r
327 // paste has successfully happened.
\r
331 var dataTransfer = {};
\r
332 dataTransfer[ mode ] = data;
\r
333 editor.fire( 'paste', dataTransfer );
\r
339 // If the "contextmenu" plugin is loaded, register the listeners.
\r
340 if ( editor.contextMenu )
\r
342 var depressBeforePasteEvent;
\r
343 function stateFromNamedCommand( command )
\r
345 // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste',
\r
346 // guard to distinguish from the ordinary sources( either
\r
347 // keyboard paste or execCommand ) (#4874).
\r
348 CKEDITOR.env.ie && command == 'Paste'&& ( depressBeforePasteEvent = 1 );
\r
350 var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
\r
351 depressBeforePasteEvent = 0;
\r
355 editor.contextMenu.addListener( function()
\r
358 cut : stateFromNamedCommand( 'Cut' ),
\r
360 // Browser bug: 'Cut' has the correct states for both Copy and Cut.
\r
361 copy : stateFromNamedCommand( 'Cut' ),
\r
362 paste : CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' )
\r
371 * Fired when a clipboard operation is about to be taken into the editor.
\r
372 * Listeners can manipulate the data to be pasted before having it effectively
\r
373 * inserted into the document.
\r
374 * @name CKEDITOR.editor#paste
\r
377 * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined.
\r
378 * @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