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 this.type == 'cut' && fixCut( editor );
\r
73 var success = tryToCutCopy( editor, this.type );
\r
76 alert( editor.lang.clipboard[ this.type + 'Error' ] ); // Show cutError or copyError.
\r
91 // Prevent IE from pasting at the begining of the document.
\r
94 if ( !editor.document.getBody().fire( 'beforepaste' )
\r
95 && !execIECommand( editor, 'paste' ) )
\r
97 editor.fire( 'pasteDialog' );
\r
106 if ( !editor.document.getBody().fire( 'beforepaste' )
\r
107 && !editor.document.$.execCommand( 'Paste', false, null ) )
\r
114 setTimeout( function()
\r
116 editor.fire( 'pasteDialog' );
\r
123 // Listens for some clipboard related keystrokes, so they get customized.
\r
124 var onKey = function( event )
\r
126 if ( this.mode != 'wysiwyg' )
\r
129 switch ( event.data.keyCode )
\r
132 case CKEDITOR.CTRL + 86 : // CTRL+V
\r
133 case CKEDITOR.SHIFT + 45 : // SHIFT+INS
\r
135 var body = this.document.getBody();
\r
137 // Simulate 'beforepaste' event for all none-IEs.
\r
138 if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )
\r
140 // Simulate 'paste' event for Opera/Firefox2.
\r
141 else if ( CKEDITOR.env.opera
\r
142 || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
\r
143 body.fire( 'paste' );
\r
147 case CKEDITOR.CTRL + 88 : // CTRL+X
\r
148 case CKEDITOR.SHIFT + 46 : // SHIFT+DEL
\r
150 // Save Undo snapshot.
\r
152 this.fire( 'saveSnapshot' ); // Save before paste
\r
153 setTimeout( function()
\r
155 editor.fire( 'saveSnapshot' ); // Save after paste
\r
160 // Allow to peek clipboard content by redirecting the
\r
161 // pasting content into a temporary bin and grab the content of it.
\r
162 function getClipboardData( evt, mode, callback )
\r
164 var doc = this.document;
\r
166 // Avoid recursions on 'paste' event for IE.
\r
167 if ( CKEDITOR.env.ie && doc.getById( 'cke_pastebin' ) )
\r
170 // If the browser supports it, get the data directly
\r
171 if (mode == 'text' && evt.data && evt.data.$.clipboardData)
\r
173 // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows.
\r
174 var plain = evt.data.$.clipboardData.getData( 'text/plain' );
\r
177 evt.data.preventDefault();
\r
183 var sel = this.getSelection(),
\r
184 range = new CKEDITOR.dom.range( doc );
\r
186 // Create container to paste into
\r
187 var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : CKEDITOR.env.webkit ? 'body' : 'div', doc );
\r
188 pastebin.setAttribute( 'id', 'cke_pastebin' );
\r
189 // Safari requires a filler node inside the div to have the content pasted into it. (#4882)
\r
190 CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );
\r
191 doc.getBody().append( pastebin );
\r
193 pastebin.setStyles(
\r
195 position : 'absolute',
\r
196 // Position the bin exactly at the position of the selected element
\r
197 // to avoid any subsequent document scroll.
\r
198 top : sel.getStartElement().getDocumentPosition().y + 'px',
\r
201 overflow : 'hidden'
\r
204 // It's definitely a better user experience if we make the paste-bin pretty unnoticed
\r
205 // by pulling it off the screen.
\r
206 pastebin.setStyle( this.config.contentsLangDirection == 'ltr' ? 'left' : 'right', '-1000px' );
\r
208 var bms = sel.createBookmarks();
\r
210 // Turn off design mode temporarily before give focus to the paste bin.
\r
211 if ( mode == 'text' )
\r
213 if ( CKEDITOR.env.ie )
\r
215 var ieRange = doc.getBody().$.createTextRange();
\r
216 ieRange.moveToElementText( pastebin.$ );
\r
217 ieRange.execCommand( 'Paste' );
\r
218 evt.data.preventDefault();
\r
222 doc.$.designMode = 'off';
\r
223 pastebin.$.focus();
\r
228 range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );
\r
229 range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );
\r
230 range.select( true );
\r
233 // Wait a while and grab the pasted contents
\r
234 window.setTimeout( function()
\r
236 mode == 'text' && !CKEDITOR.env.ie && ( doc.$.designMode = 'on' );
\r
239 // Grab the HTML contents.
\r
240 // We need to look for a apple style wrapper on webkit it also adds
\r
241 // a div wrapper if you copy/paste the body of the editor.
\r
242 // Remove hidden div and restore selection.
\r
244 pastebin = ( CKEDITOR.env.webkit
\r
245 && ( bogusSpan = pastebin.getFirst() )
\r
246 && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?
\r
247 bogusSpan : pastebin );
\r
249 sel.selectBookmarks( bms );
\r
250 callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );
\r
254 // Cutting off control type element in IE standards breaks the selection entirely. (#4881)
\r
255 function fixCut( editor )
\r
257 if ( !CKEDITOR.env.ie || editor.document.$.compatMode == 'BackCompat' )
\r
260 var sel = editor.getSelection();
\r
262 if( ( sel.getType() == CKEDITOR.SELECTION_ELEMENT ) && ( control = sel.getSelectedElement() ) )
\r
264 var range = sel.getRanges()[ 0 ];
\r
265 var dummy = editor.document.createText( '' );
\r
266 dummy.insertBefore( control );
\r
267 range.setStartBefore( dummy );
\r
268 range.setEndAfter( control );
\r
269 sel.selectRanges( [ range ] );
\r
271 // Clear up the fix if the paste wasn't succeeded.
\r
272 setTimeout( function()
\r
274 // Element still online?
\r
275 if ( control.getParent() )
\r
278 sel.selectElement( control );
\r
284 // Register the plugin.
\r
285 CKEDITOR.plugins.add( 'clipboard',
\r
287 requires : [ 'dialog', 'htmldataprocessor' ],
\r
288 init : function( editor )
\r
290 // Inserts processed data into the editor at the end of the
\r
292 editor.on( 'paste', function( evt )
\r
294 var data = evt.data;
\r
295 if ( data[ 'html' ] )
\r
296 editor.insertHtml( data[ 'html' ] );
\r
297 else if ( data[ 'text' ] )
\r
298 editor.insertText( data[ 'text' ] );
\r
300 }, null, null, 1000 );
\r
302 editor.on( 'pasteDialog', function( evt )
\r
304 setTimeout( function()
\r
306 // Open default paste dialog.
\r
307 editor.openDialog( 'paste' );
\r
311 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )
\r
313 var lang = editor.lang[ commandName ];
\r
315 editor.addCommand( commandName, command );
\r
316 editor.ui.addButton( buttonName,
\r
319 command : commandName
\r
322 // If the "menu" plugin is loaded, register the menu item.
\r
323 if ( editor.addMenuItems )
\r
325 editor.addMenuItem( commandName,
\r
328 command : commandName,
\r
329 group : 'clipboard',
\r
330 order : ctxMenuOrder
\r
335 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );
\r
336 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );
\r
337 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );
\r
339 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );
\r
341 editor.on( 'key', onKey, editor );
\r
343 var mode = editor.config.forcePasteAsPlainText ? 'text' : 'html';
\r
345 // We'll be catching all pasted content in one line, regardless of whether the
\r
346 // it's introduced by a document command execution (e.g. toolbar buttons) or
\r
347 // user paste behaviors. (e.g. Ctrl-V)
\r
348 editor.on( 'contentDom', function()
\r
350 var body = editor.document.getBody();
\r
351 body.on( ( (mode == 'text' && CKEDITOR.env.ie) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',
\r
354 if ( depressBeforeEvent )
\r
357 getClipboardData.call( editor, evt, mode, function ( data )
\r
359 // The very last guard to make sure the
\r
360 // paste has successfully happened.
\r
364 var dataTransfer = {};
\r
365 dataTransfer[ mode ] = data;
\r
366 editor.fire( 'paste', dataTransfer );
\r
370 body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );
\r
373 // If the "contextmenu" plugin is loaded, register the listeners.
\r
374 if ( editor.contextMenu )
\r
376 var depressBeforeEvent;
\r
377 function stateFromNamedCommand( command )
\r
379 // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',
\r
380 // guard to distinguish from the ordinary sources( either
\r
381 // keyboard paste or execCommand ) (#4874).
\r
382 CKEDITOR.env.ie && ( depressBeforeEvent = 1 );
\r
384 var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
\r
385 depressBeforeEvent = 0;
\r
389 editor.contextMenu.addListener( function( element, selection )
\r
391 var readOnly = selection.getCommonAncestor().isReadOnly();
\r
393 cut : !readOnly && stateFromNamedCommand( 'Cut' ),
\r
394 copy : stateFromNamedCommand( 'Copy' ),
\r
395 paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' ) )
\r
404 * Fired when a clipboard operation is about to be taken into the editor.
\r
405 * Listeners can manipulate the data to be pasted before having it effectively
\r
406 * inserted into the document.
\r
407 * @name CKEDITOR.editor#paste
\r
410 * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined.
\r
411 * @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