JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / clipboard / plugin.js
index d215221..7a1c971 100644 (file)
@@ -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
@@ -28,7 +28,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                // the command to execute.\r
                body.on( command, onExec );\r
 \r
-               doc.$.execCommand( command );\r
+               // IE6/7: document.execCommand has problem to paste into positioned element.\r
+               ( CKEDITOR.env.version > 7 ? doc.$ : doc.$.selection.createRange() ) [ 'execCommand' ]( command );\r
 \r
                body.removeListener( command, onExec );\r
 \r
@@ -67,6 +68,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        {\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
@@ -160,37 +163,47 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        {\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
+               {\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
+                       {\r
+                               evt.data.preventDefault();\r
+                               callback( plain );\r
+                               return;\r
+                       }\r
+               }\r
+\r
                var sel = this.getSelection(),\r
                        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
+               pastebin.setStyles(\r
+                       {\r
+                               position : 'absolute',\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
+                               width : '1px',\r
+                               height : '1px',\r
+                               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, while this hack will make the paste-bin a control type element\r
-               // and that become a selection plain later.\r
-               if ( !CKEDITOR.env.ie && mode != 'html' )\r
-               {\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
-                                       width : '1px',\r
-                                       height : '1px',\r
-                                       overflow : 'hidden'\r
-                               });\r
-               }\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
@@ -238,10 +251,40 @@ 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
        // Register the plugin.\r
        CKEDITOR.plugins.add( 'clipboard',\r
                {\r
-                       requires : [ 'htmldataprocessor' ],\r
+                       requires : [ 'dialog', 'htmldataprocessor' ],\r
                        init : function( editor )\r
                        {\r
                                // Inserts processed data into the editor at the end of the\r
@@ -305,10 +348,10 @@ 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 ) ? 'paste' : 'beforepaste',\r
+                                       body.on( ( (mode == 'text' && CKEDITOR.env.ie ) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',\r
                                                function( evt )\r
                                                {\r
-                                                       if( depressBeforePasteEvent )\r
+                                                       if ( depressBeforeEvent )\r
                                                                return;\r
 \r
                                                        getClipboardData.call( editor, evt, mode, function ( data )\r
@@ -324,35 +367,46 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        } );\r
                                                });\r
 \r
+                                       body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );\r
                                });\r
 \r
                                // If the "contextmenu" plugin is loaded, register the listeners.\r
                                if ( editor.contextMenu )\r
                                {\r
-                                       var depressBeforePasteEvent;\r
+                                       var depressBeforeEvent;\r
                                        function stateFromNamedCommand( command )\r
                                        {\r
-                                               // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste',\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 && command == 'Paste'&& ( depressBeforePasteEvent = 1 );\r
+                                               CKEDITOR.env.ie && ( depressBeforeEvent = 1 );\r
 \r
                                                var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
-                                               depressBeforePasteEvent = 0;\r
+                                               depressBeforeEvent = 0;\r
                                                return retval;\r
                                        }\r
 \r
-                                       editor.contextMenu.addListener( function()\r
+                                       editor.contextMenu.addListener( function( element, selection )\r
                                                {\r
+                                                       var readOnly = selection.getCommonAncestor().isReadOnly();\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' ),\r
+                                                               copy : stateFromNamedCommand( 'Copy' ),\r
+                                                               paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' ) )\r
                                                        };\r
                                                });\r
                                }\r
                        }\r
                });\r
 })();\r
+\r
+/**\r
+ * Fired when a clipboard operation is about to be taken into the editor.\r
+ * Listeners can manipulate the data to be pasted before having it effectively\r
+ * inserted into the document.\r
+ * @name CKEDITOR.editor#paste\r
+ * @since 3.1\r
+ * @event\r
+ * @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