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