Ticket #7366: 7366_3.patch

File 7366_3.patch, 5.8 KB (added by Piotrek Koszuliński, 12 years ago)
  • _source/plugins/clipboard/plugin.js

     
    99
    1010(function()
    1111{
     12        // Safari doesn't like 'beforepaste' event - it sometimes doesn't
     13        // properly handles ctrl+c. Probably some race-condition between events.
     14        // Chrome and Firefox works well with both events, so better to use 'paste'
     15        // which will handle pasting from e.g. browsers' menu bars.
     16        // IE7/8 doesn't like 'paste' event for which it's throwing random errors.
     17        var mainPasteEvent = CKEDITOR.env.ie ? 'beforepaste' : 'paste';
     18
    1219        // Tries to execute any of the paste, cut or copy commands in IE. Returns a
    1320        // boolean indicating that the operation succeeded.
    1421        var execIECommand = function( editor, command )
     
    9299                                        // Prevent IE from pasting at the begining of the document.
    93100                                        editor.focus();
    94101
    95                                         if ( !editor.document.getBody().fire( 'beforepaste' )
     102                                        // Command will be handled by 'beforepaste', but as
     103                                        // execIECommand( 'paste' ) will fire also 'paste' event
     104                                        // we're canceling it.
     105                                        depressPasteEventNow();
     106
     107                                        if ( !editor.document.getBody().fire( mainPasteEvent )
    96108                                                 && !execIECommand( editor, 'paste' ) )
    97109                                        {
    98110                                                editor.fire( 'pasteDialog' );
     
    104116                                {
    105117                                        try
    106118                                        {
    107                                                 if ( !editor.document.getBody().fire( 'beforepaste' )
     119                                                if ( !editor.document.getBody().fire( mainPasteEvent )
    108120                                                         && !editor.document.$.execCommand( 'Paste', false, null ) )
    109121                                                {
    110122                                                        throw 0;
     
    134146                        case CKEDITOR.SHIFT + 45 :              // SHIFT+INS
    135147
    136148                                var body = this.document.getBody();
     149                                       
     150                                // Cancel 'paste' event because ctrl+v is for IE handled
     151                                // by 'beforepaste'.
     152                                depressPasteEventNow();
    137153
    138154                                // Simulate 'beforepaste' event for all none-IEs.
    139155                                if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )
     
    280296                }
    281297        }
    282298
    283         var depressBeforeEvent;
     299        var depressBeforePasteEvent;
    284300        function stateFromNamedCommand( command, editor )
    285301        {
    286302                // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',
    287303                // guard to distinguish from the ordinary sources( either
    288304                // keyboard paste or execCommand ) (#4874).
    289                 CKEDITOR.env.ie && ( depressBeforeEvent = 1 );
     305                CKEDITOR.env.ie && ( depressBeforePasteEvent = 1 );
    290306
    291307                var retval = CKEDITOR.TRISTATE_OFF;
    292308                try { retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED; }catch( er ){}
    293309
    294                 depressBeforeEvent = 0;
     310                depressBeforePasteEvent = 0;
    295311                return retval;
    296312        }
    297313
     
    308324                this.fire( 'pasteState', pasteState );
    309325        }
    310326
     327        var depressPasteEvent = 0;
     328        function depressPasteEventNow()
     329        {
     330                depressPasteEvent = 1;
     331                // For safety reason we should wait longer than 0/1ms.
     332                // We don't know how long execution of quite complex getClipboardData will take
     333                // and in for example 'paste' listner execCommand() (which fires 'paste') is called
     334                // after getClipboardData finishes.
     335                // Luckily, it's impossible to immediately fire another 'paste' event we want to handle,
     336                // because we only handle there native context menu and menu bar.
     337                setTimeout( function() { depressPasteEvent = 0; }, 100 );
     338        }
     339
    311340        // Register the plugin.
    312341        CKEDITOR.plugins.add( 'clipboard',
    313342                {
     
    380409                                editor.on( 'contentDom', function()
    381410                                {
    382411                                        var body = editor.document.getBody();
    383                                         body.on( CKEDITOR.env.webkit ? 'paste' : 'beforepaste', function( evt )
     412                                        body.on( mainPasteEvent, function( evt )
    384413                                                {
    385                                                         if ( depressBeforeEvent )
     414                                                        if ( CKEDITOR.env.ie && depressBeforePasteEvent )
    386415                                                                return;
    387416
    388417                                                        // Fire 'beforePaste' event so clipboard flavor get customized
     
    403432                                                        } );
    404433                                                });
    405434
     435                                        CKEDITOR.env.ie && body.on( 'paste', function( evt )
     436                                                {
     437                                                        if ( depressPasteEvent )
     438                                                                return;
     439
     440                                                        // Cancel next 'paste' event fired by execIECommand( 'paste' )
     441                                                        // at the end of this callback.
     442                                                        depressPasteEventNow();
     443
     444                                                        // Prevent native paste.
     445                                                        evt.data.preventDefault();
     446
     447                                                        // Fire 'beforePaste' event so clipboard flavor get customized
     448                                                        // by other plugins.
     449                                                        var eventData =  { mode : 'html' };
     450                                                        editor.fire( 'beforePaste', eventData );
     451
     452                                                        getClipboardData.call( editor, evt, eventData.mode, function ( data )
     453                                                        {
     454                                                                // The very last guard to make sure the
     455                                                                // paste has successfully happened.
     456                                                                if ( !( data = CKEDITOR.tools.trim( data.replace( /<span[^>]+data-cke-bookmark[^<]*?<\/span>/ig,'' ) ) ) )
     457                                                                        return;
     458
     459                                                                var dataTransfer = {};
     460                                                                dataTransfer[ eventData.mode ] = data;
     461                                                                editor.fire( 'paste', dataTransfer );
     462                                                        } );
     463
     464                                                        // Force IE to paste content into pastebin so pasteDataFromClipboard will work.
     465                                                        if ( !execIECommand( editor, 'paste' ) )
     466                                                                editor.openDialog( 'paste' );
     467                                                });
     468
    406469                                        // Dismiss the (wrong) 'beforepaste' event fired on context menu open. (#7953)
    407470                                        body.on( 'contextmenu', function()
    408471                                        {
    409                                                 depressBeforeEvent = 1;
    410                                                 setTimeout( function() { depressBeforeEvent = 0; }, 10 );
     472                                                depressBeforePasteEvent = 1;
     473                                                setTimeout( function() { depressBeforePasteEvent = 0; }, 10 );
    411474                                        });
    412475
    413                                         body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );
     476                                        body.on( 'beforecut', function() { !depressBeforePasteEvent && fixCut( editor ); } );
    414477
    415478                                        body.on( 'mouseup', function(){ setTimeout( function(){ setToolbarStates.call( editor ); }, 0 ); }, editor );
    416479                                        body.on( 'keyup', setToolbarStates, editor );
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy