JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.1
[ckeditor.git] / _source / plugins / clipboard / plugin.js
index d647058..342d708 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -61,7 +61,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        var cutCopyCmd = function( type )\r
        {\r
                this.type = type;\r
-               this.canUndo = ( this.type == 'cut' );          // We can't undo copy to clipboard.\r
+               this.canUndo = this.type == 'cut';              // We can't undo copy to clipboard.\r
+               this.startDisabled = true;\r
        };\r
 \r
        cutCopyCmd.prototype =\r
@@ -218,10 +219,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                evt.data.preventDefault();\r
                        }\r
                        else\r
-                       {\r
-                               doc.$.designMode = 'off';\r
                                pastebin.$.focus();\r
-                       }\r
                }\r
                else\r
                {\r
@@ -230,10 +228,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        range.select( true );\r
                }\r
 \r
+               var editor  = this;\r
                // Wait a while and grab the pasted contents\r
                window.setTimeout( function()\r
                {\r
-                       mode == 'text' && !CKEDITOR.env.ie && ( doc.$.designMode = 'on' );\r
+                       mode == 'text' && CKEDITOR.env.gecko && editor.focusGrabber.focus();\r
                        pastebin.remove();\r
 \r
                        // Grab the HTML contents.\r
@@ -281,6 +280,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        }\r
 \r
+       var depressBeforeEvent;\r
+       function stateFromNamedCommand( command, editor )\r
+       {\r
+               // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',\r
+               // guard to distinguish from the ordinary sources( either\r
+               // keyboard paste or execCommand ) (#4874).\r
+               CKEDITOR.env.ie && ( depressBeforeEvent = 1 );\r
+\r
+               var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
+               depressBeforeEvent = 0;\r
+               return retval;\r
+       }\r
+\r
+       var inReadOnly;\r
+       function setToolbarStates()\r
+       {\r
+               if ( this.mode != 'wysiwyg' )\r
+                       return;\r
+\r
+               this.getCommand( 'cut' ).setState( inReadOnly ? CKEDITOR.TRISTATE_DISABLED : stateFromNamedCommand( 'Cut', this ) );\r
+               this.getCommand( 'copy' ).setState( stateFromNamedCommand( 'Copy', this ) );\r
+               var pasteState = inReadOnly ? CKEDITOR.TRISTATE_DISABLED :\r
+                                               CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', this );\r
+               this.fire( 'pasteState', pasteState );\r
+       }\r
+\r
        // Register the plugin.\r
        CKEDITOR.plugins.add( 'clipboard',\r
                {\r
@@ -308,6 +333,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                }, 0 );\r
                                        });\r
 \r
+                               editor.on( 'pasteState', function( evt )\r
+                                       {\r
+                                               editor.getCommand( 'paste' ).setState( evt.data );\r
+                                       });\r
+\r
                                function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )\r
                                {\r
                                        var lang = editor.lang[ commandName ];\r
@@ -348,7 +378,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                editor.on( 'contentDom', function()\r
                                {\r
                                        var body = editor.document.getBody();\r
-                                       body.on( ( (mode == 'text' && CKEDITOR.env.ie ) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',\r
+                                       body.on( ( ( mode == 'text' && CKEDITOR.env.ie ) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',\r
                                                function( evt )\r
                                                {\r
                                                        if ( depressBeforeEvent )\r
@@ -368,31 +398,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                });\r
 \r
                                        body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );\r
+\r
+                                       body.on( 'mouseup', function(){ setTimeout( function(){ setToolbarStates.call( editor ); }, 0 ); }, editor );\r
+                                       body.on( 'keyup', setToolbarStates, editor );\r
+                               });\r
+\r
+                               // For improved performance, we're checking the readOnly state on selectionChange instead of hooking a key event for that.\r
+                               editor.on( 'selectionChange', function( evt )\r
+                               {\r
+                                       inReadOnly = evt.data.selection.getRanges()[ 0 ].checkReadOnly();\r
+                                       setToolbarStates.call( editor );\r
                                });\r
 \r
                                // If the "contextmenu" plugin is loaded, register the listeners.\r
                                if ( editor.contextMenu )\r
                                {\r
-                                       var depressBeforeEvent;\r
-                                       function stateFromNamedCommand( command )\r
-                                       {\r
-                                               // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',\r
-                                               // guard to distinguish from the ordinary sources( either\r
-                                               // keyboard paste or execCommand ) (#4874).\r
-                                               CKEDITOR.env.ie && ( depressBeforeEvent = 1 );\r
-\r
-                                               var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
-                                               depressBeforeEvent = 0;\r
-                                               return retval;\r
-                                       }\r
-\r
                                        editor.contextMenu.addListener( function( element, selection )\r
                                                {\r
-                                                       var readOnly = selection.getCommonAncestor().isReadOnly();\r
+                                                       var readOnly = selection.getRanges()[ 0 ].checkReadOnly();\r
                                                        return {\r
-                                                               cut : !readOnly && stateFromNamedCommand( 'Cut' ),\r
-                                                               copy : stateFromNamedCommand( 'Copy' ),\r
-                                                               paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' ) )\r
+                                                               cut : !readOnly && stateFromNamedCommand( 'Cut', editor ),\r
+                                                               copy : stateFromNamedCommand( 'Copy', editor ),\r
+                                                               paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', editor ) )\r
                                                        };\r
                                                });\r
                                }\r
@@ -410,3 +437,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
  * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined.\r
  * @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
  */\r
+\r
+/**\r
+ * Internal event to open the Paste dialog\r
+ * @name CKEDITOR.editor#pasteDialog\r
+ * @event\r
+ */\r