2 Copyright (c) 2003-2009, 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
83 exec : function( editor, data )
\r
85 // Prevent IE from pasting at the begining of the document.
\r
88 if ( !editor.fire( 'beforePaste' )
\r
89 && !execIECommand( editor, 'paste' ) )
\r
91 editor.openDialog( 'paste' );
\r
97 exec : function( editor )
\r
101 if ( !editor.fire( 'beforePaste' )
\r
102 && !editor.document.$.execCommand( 'Paste', false, null ) )
\r
109 // Open the paste dialog.
\r
110 editor.openDialog( 'paste' );
\r
115 // Listens for some clipboard related keystrokes, so they get customized.
\r
116 var onKey = function( event )
\r
118 switch ( event.data.keyCode )
\r
121 case CKEDITOR.CTRL + 86 : // CTRL+V
\r
122 case CKEDITOR.SHIFT + 45 : // SHIFT+INS
\r
125 editor.fire( 'saveSnapshot' ); // Save before paste
\r
127 if ( editor.fire( 'beforePaste' ) )
\r
130 setTimeout( function()
\r
132 editor.fire( 'saveSnapshot' ); // Save after paste
\r
137 case CKEDITOR.CTRL + 88 : // CTRL+X
\r
138 case CKEDITOR.SHIFT + 46 : // SHIFT+DEL
\r
140 // Save Undo snapshot.
\r
142 editor.fire( 'saveSnapshot' ); // Save before paste
\r
143 setTimeout( function()
\r
145 editor.fire( 'saveSnapshot' ); // Save after paste
\r
150 // Register the plugin.
\r
151 CKEDITOR.plugins.add( 'clipboard',
\r
153 init : function( editor )
\r
155 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )
\r
157 var lang = editor.lang[ commandName ];
\r
159 editor.addCommand( commandName, command );
\r
160 editor.ui.addButton( buttonName,
\r
163 command : commandName
\r
166 // If the "menu" plugin is loaded, register the menu item.
\r
167 if ( editor.addMenuItems )
\r
169 editor.addMenuItem( commandName,
\r
172 command : commandName,
\r
173 group : 'clipboard',
\r
174 order : ctxMenuOrder
\r
179 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );
\r
180 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );
\r
181 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );
\r
183 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );
\r
185 editor.on( 'key', onKey, editor );
\r
187 // If the "contextmenu" plugin is loaded, register the listeners.
\r
188 if ( editor.contextMenu )
\r
190 function stateFromNamedCommand( command )
\r
192 return editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
\r
195 editor.contextMenu.addListener( function()
\r
198 cut : stateFromNamedCommand( 'Cut' ),
\r
200 // Browser bug: 'Cut' has the correct states for both Copy and Cut.
\r
201 copy : stateFromNamedCommand( 'Cut' ),
\r
202 paste : CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' )
\r