JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / clipboard / plugin.js
index 23c58bf..3c53ab1 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
@@ -16,10 +16,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var doc = editor.document,\r
                        body = doc.getBody();\r
 \r
-               var     enabled = false;\r
+               var enabled = 0;\r
                var onExec = function()\r
                {\r
-                       enabled = true;\r
+                       enabled = 1;\r
                };\r
 \r
                // The following seems to be the only reliable way to detect that\r
@@ -49,7 +49,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                try\r
                                {\r
                                        // Other browsers throw an error if the command is disabled.\r
-                                       return editor.document.$.execCommand( type );\r
+                                       return editor.document.$.execCommand( type, false, null );\r
                                }\r
                                catch( e )\r
                                {\r
@@ -61,13 +61,16 @@ 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
        {\r
                exec : function( editor, data )\r
                {\r
+                       this.type == 'cut' && fixCut( editor );\r
+\r
                        var success = tryToCutCopy( editor, this.type );\r
 \r
                        if ( !success )\r
@@ -155,22 +158,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        };\r
 \r
+       function cancel( evt ) { evt.cancel(); }\r
+\r
        // Allow to peek clipboard content by redirecting the\r
        // pasting content into a temporary bin and grab the content of it.\r
        function getClipboardData( evt, mode, callback )\r
        {\r
                var doc = this.document;\r
 \r
-               // Avoid recursions on 'paste' event for IE.\r
-               if ( CKEDITOR.env.ie && doc.getById( 'cke_pastebin' ) )\r
+               // Avoid recursions on 'paste' event or consequent paste too fast. (#5730)\r
+               if ( doc.getById( 'cke_pastebin' ) )\r
                        return;\r
 \r
                // If the browser supports it, get the data directly\r
-               if (mode == 'text' && evt.data && evt.data.$.clipboardData)\r
+               if ( mode == 'text' && evt.data && evt.data.$.clipboardData )\r
                {\r
                        // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows.\r
                        var plain = evt.data.$.clipboardData.getData( 'text/plain' );\r
-                       if (plain)\r
+                       if ( plain )\r
                        {\r
                                evt.data.preventDefault();\r
                                callback( plain );\r
@@ -182,18 +187,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        range = new CKEDITOR.dom.range( doc );\r
 \r
                // Create container to paste into\r
-               var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : 'div', doc );\r
+               var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : CKEDITOR.env.webkit ? 'body' : 'div', doc );\r
                pastebin.setAttribute( 'id', 'cke_pastebin' );\r
                // Safari requires a filler node inside the div to have the content pasted into it. (#4882)\r
                CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );\r
                doc.getBody().append( pastebin );\r
 \r
-               // It's definitely a better user experience if we make the paste-bin pretty unnoticed\r
-               // by pulling it off the screen.\r
                pastebin.setStyles(\r
                        {\r
                                position : 'absolute',\r
-                               left : '-1000px',\r
                                // Position the bin exactly at the position of the selected element\r
                                // to avoid any subsequent document scroll.\r
                                top : sel.getStartElement().getDocumentPosition().y + 'px',\r
@@ -202,24 +204,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                overflow : 'hidden'\r
                        });\r
 \r
+               // It's definitely a better user experience if we make the paste-bin pretty unnoticed\r
+               // by pulling it off the screen.\r
+               pastebin.setStyle( this.config.contentsLangDirection == 'ltr' ? 'left' : 'right', '-1000px' );\r
+\r
                var bms = sel.createBookmarks();\r
 \r
+               this.on( 'selectionChange', cancel, null, null, 0 );\r
+\r
                // Turn off design mode temporarily before give focus to the paste bin.\r
                if ( mode == 'text' )\r
-               {\r
-                       if ( CKEDITOR.env.ie )\r
-                       {\r
-                               var ieRange = doc.getBody().$.createTextRange();\r
-                               ieRange.moveToElementText( pastebin.$ );\r
-                               ieRange.execCommand( 'Paste' );\r
-                               evt.data.preventDefault();\r
-                       }\r
-                       else\r
-                       {\r
-                               doc.$.designMode = 'off';\r
-                               pastebin.$.focus();\r
-                       }\r
-               }\r
+                       pastebin.$.focus();\r
                else\r
                {\r
                        range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );\r
@@ -227,11 +222,13 @@ 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
+                       editor.removeListener( 'selectionChange', cancel );\r
 \r
                        // Grab the HTML contents.\r
                        // We need to look for a apple style wrapper on webkit it also adds\r
@@ -248,6 +245,64 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }, 0 );\r
        }\r
 \r
+       // Cutting off control type element in IE standards breaks the selection entirely. (#4881)\r
+       function fixCut( editor )\r
+       {\r
+               if ( !CKEDITOR.env.ie || CKEDITOR.env.quirks )\r
+                       return;\r
+\r
+               var sel = editor.getSelection();\r
+               var control;\r
+               if( ( sel.getType() == CKEDITOR.SELECTION_ELEMENT ) && ( control = sel.getSelectedElement() ) )\r
+               {\r
+                       var range = sel.getRanges()[ 0 ];\r
+                       var dummy = editor.document.createText( '' );\r
+                       dummy.insertBefore( control );\r
+                       range.setStartBefore( dummy );\r
+                       range.setEndAfter( control );\r
+                       sel.selectRanges( [ range ] );\r
+\r
+                       // Clear up the fix if the paste wasn't succeeded.\r
+                       setTimeout( function()\r
+                       {\r
+                               // Element still online?\r
+                               if ( control.getParent() )\r
+                               {\r
+                                       dummy.remove();\r
+                                       sel.selectElement( control );\r
+                               }\r
+                       }, 0 );\r
+               }\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 = CKEDITOR.TRISTATE_OFF;\r
+               try { retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED; }catch( er ){}\r
+\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
@@ -264,6 +319,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                else if ( data[ 'text' ] )\r
                                                        editor.insertText( data[ 'text' ] );\r
 \r
+                                               setTimeout( function () { editor.fire( 'afterPaste' ); }, 0 );\r
+\r
                                        }, null, null, 1000 );\r
 \r
                                editor.on( 'pasteDialog', function( evt )\r
@@ -275,6 +332,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
@@ -307,59 +369,65 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                editor.on( 'key', onKey, editor );\r
 \r
-                               var mode = editor.config.forcePasteAsPlainText ? 'text' : 'html';\r
-\r
                                // We'll be catching all pasted content in one line, regardless of whether the\r
                                // it's introduced by a document command execution (e.g. toolbar buttons) or\r
                                // user paste behaviors. (e.g. Ctrl-V)\r
                                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
-                                               function( evt )\r
+                                       body.on( CKEDITOR.env.webkit ? 'paste' : 'beforepaste', function( evt )\r
                                                {\r
-                                                       if ( depressBeforePasteEvent )\r
+                                                       if ( depressBeforeEvent )\r
                                                                return;\r
 \r
-                                                       getClipboardData.call( editor, evt, mode, function ( data )\r
+                                                       // Fire 'beforePaste' event so clipboard flavor get customized\r
+                                                       // by other plugins.\r
+                                                       var eventData =  { mode : 'html' };\r
+                                                       editor.fire( 'beforePaste', eventData );\r
+\r
+                                                       getClipboardData.call( editor, evt, eventData.mode, function ( data )\r
                                                        {\r
                                                                // The very last guard to make sure the\r
                                                                // paste has successfully happened.\r
-                                                               if ( !data )\r
+                                                               if ( !( data = CKEDITOR.tools.trim( data.replace( /<span[^>]+data-cke-bookmark[^<]*?<\/span>/ig,'' ) ) ) )\r
                                                                        return;\r
 \r
                                                                var dataTransfer = {};\r
-                                                               dataTransfer[ mode ] = data;\r
+                                                               dataTransfer[ eventData.mode ] = data;\r
                                                                editor.fire( 'paste', dataTransfer );\r
                                                        } );\r
                                                });\r
 \r
+                                       // Dismiss the (wrong) 'beforepaste' event fired on context menu open. (#7953)\r
+                                       body.on( 'contextmenu', function()\r
+                                       {\r
+                                               depressBeforeEvent = 1;\r
+                                               setTimeout( function() { depressBeforeEvent = 0; }, 10 );\r
+                                       });\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 depressBeforePasteEvent;\r
-                                       function stateFromNamedCommand( command )\r
-                                       {\r
-                                               // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste',\r
-                                               // guard to distinguish from the ordinary sources( either\r
-                                               // keyboard paste or execCommand ) (#4874).\r
-                                               CKEDITOR.env.ie && command == 'Paste'&& ( depressBeforePasteEvent = 1 );\r
-\r
-                                               var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
-                                               depressBeforePasteEvent = 0;\r
-                                               return retval;\r
-                                       }\r
-\r
-                                       editor.contextMenu.addListener( function()\r
+                                       editor.contextMenu.addListener( function( element, selection )\r
                                                {\r
+                                                       var readOnly = selection.getRanges()[ 0 ].checkReadOnly();\r
                                                        return {\r
-                                                               cut : stateFromNamedCommand( 'Cut' ),\r
-\r
-                                                               // Browser bug: 'Cut' has the correct states for both Copy and Cut.\r
-                                                               copy : stateFromNamedCommand( 'Cut' ),\r
-                                                               paste : 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
@@ -377,3 +445,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