JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / plugins / clipboard / plugin.js
index c063b41..d215221 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -78,59 +78,66 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        // Paste command.\r
        var pasteCmd =\r
-               CKEDITOR.env.ie ?\r
-                       {\r
-                               exec : function( editor, data )\r
+       {\r
+               canUndo : false,\r
+\r
+               exec :\r
+                       CKEDITOR.env.ie ?\r
+                               function( editor )\r
                                {\r
                                        // Prevent IE from pasting at the begining of the document.\r
                                        editor.focus();\r
 \r
-                                       if ( !editor.fire( 'beforePaste' )\r
-                                               && !execIECommand( editor, 'paste' ) )\r
+                                       if ( !editor.document.getBody().fire( 'beforepaste' )\r
+                                                && !execIECommand( editor, 'paste' ) )\r
                                        {\r
-                                                       editor.openDialog( 'paste' );\r
+                                               editor.fire( 'pasteDialog' );\r
+                                               return false;\r
                                        }\r
                                }\r
-                       }\r
-               :\r
-                       {\r
-                               exec : function( editor )\r
+                       :\r
+                               function( editor )\r
                                {\r
                                        try\r
                                        {\r
-                                               if ( !editor.fire( 'beforePaste' )\r
-                                                       && !editor.document.$.execCommand( 'Paste', false, null ) )\r
+                                               if ( !editor.document.getBody().fire( 'beforepaste' )\r
+                                                        && !editor.document.$.execCommand( 'Paste', false, null ) )\r
                                                {\r
                                                        throw 0;\r
                                                }\r
                                        }\r
                                        catch ( e )\r
                                        {\r
-                                               // Open the paste dialog.\r
-                                               editor.openDialog( 'paste' );\r
+                                               setTimeout( function()\r
+                                                       {\r
+                                                               editor.fire( 'pasteDialog' );\r
+                                                       }, 0 );\r
+                                               return false;\r
                                        }\r
                                }\r
-                       };\r
+       };\r
 \r
        // Listens for some clipboard related keystrokes, so they get customized.\r
        var onKey = function( event )\r
        {\r
+               if ( this.mode != 'wysiwyg' )\r
+                       return;\r
+\r
                switch ( event.data.keyCode )\r
                {\r
                        // Paste\r
                        case CKEDITOR.CTRL + 86 :               // CTRL+V\r
                        case CKEDITOR.SHIFT + 45 :              // SHIFT+INS\r
 \r
-                               var editor = this;\r
-                               editor.fire( 'saveSnapshot' );          // Save before paste\r
+                               var body = this.document.getBody();\r
 \r
-                               if ( editor.fire( 'beforePaste' ) )\r
+                               // Simulate 'beforepaste' event for all none-IEs.\r
+                               if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )\r
                                        event.cancel();\r
-\r
-                               setTimeout( function()\r
-                                       {\r
-                                               editor.fire( 'saveSnapshot' );          // Save after paste\r
-                                       }, 0 );\r
+                               // Simulate 'paste' event for Opera/Firefox2.\r
+                               else if ( CKEDITOR.env.opera\r
+                                                || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
+                                       body.fire( 'paste' );\r
                                return;\r
 \r
                        // Cut\r
@@ -138,8 +145,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        case CKEDITOR.SHIFT + 46 :              // SHIFT+DEL\r
 \r
                                // Save Undo snapshot.\r
-                               editor = this;\r
-                               editor.fire( 'saveSnapshot' );          // Save before paste\r
+                               var editor = this;\r
+                               this.fire( 'saveSnapshot' );            // Save before paste\r
                                setTimeout( function()\r
                                        {\r
                                                editor.fire( 'saveSnapshot' );          // Save after paste\r
@@ -147,11 +154,117 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        };\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
+                       return;\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
+               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, 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
+\r
+               var bms = sel.createBookmarks();\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
+               else\r
+               {\r
+                       range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );\r
+                       range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );\r
+                       range.select( true );\r
+               }\r
+\r
+               // Wait a while and grab the pasted contents\r
+               window.setTimeout( function()\r
+               {\r
+                       mode == 'text' && !CKEDITOR.env.ie && ( doc.$.designMode = 'on' );\r
+                       pastebin.remove();\r
+\r
+                       // Grab the HTML contents.\r
+                       // We need to look for a apple style wrapper on webkit it also adds\r
+                       // a div wrapper if you copy/paste the body of the editor.\r
+                       // Remove hidden div and restore selection.\r
+                       var bogusSpan;\r
+                       pastebin = ( CKEDITOR.env.webkit\r
+                                                && ( bogusSpan = pastebin.getFirst() )\r
+                                                && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?\r
+                                                       bogusSpan : pastebin );\r
+\r
+                       sel.selectBookmarks( bms );\r
+                       callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );\r
+               }, 0 );\r
+       }\r
+\r
        // Register the plugin.\r
        CKEDITOR.plugins.add( 'clipboard',\r
                {\r
+                       requires : [ 'htmldataprocessor' ],\r
                        init : function( editor )\r
                        {\r
+                               // Inserts processed data into the editor at the end of the\r
+                               // events chain.\r
+                               editor.on( 'paste', function( evt )\r
+                                       {\r
+                                               var data = evt.data;\r
+                                               if ( data[ 'html' ] )\r
+                                                       editor.insertHtml( data[ 'html' ] );\r
+                                               else if ( data[ 'text' ] )\r
+                                                       editor.insertText( data[ 'text' ] );\r
+\r
+                                       }, null, null, 1000 );\r
+\r
+                               editor.on( 'pasteDialog', function( evt )\r
+                                       {\r
+                                               setTimeout( function()\r
+                                               {\r
+                                                       // Open default paste dialog.\r
+                                                       editor.openDialog( 'paste' );\r
+                                               }, 0 );\r
+                                       });\r
+\r
                                function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )\r
                                {\r
                                        var lang = editor.lang[ commandName ];\r
@@ -184,12 +297,49 @@ 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 ) ? 'paste' : 'beforepaste',\r
+                                               function( evt )\r
+                                               {\r
+                                                       if( depressBeforePasteEvent )\r
+                                                               return;\r
+\r
+                                                       getClipboardData.call( editor, evt, mode, function ( data )\r
+                                                       {\r
+                                                               // The very last guard to make sure the\r
+                                                               // paste has successfully happened.\r
+                                                               if ( !data )\r
+                                                                       return;\r
+\r
+                                                               var dataTransfer = {};\r
+                                                               dataTransfer[ mode ] = data;\r
+                                                               editor.fire( 'paste', dataTransfer );\r
+                                                       } );\r
+                                               });\r
+\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
-                                               return editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\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