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