X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fclipboard%2Fplugin.js;h=7a1c971c1391d39092a0ac6258b05e1b0802d7ff;hb=039a051ccf3901311661022a30afd60fc38130c9;hp=d215221c73f420d4d4d5001e4dfe93caf8faaeaf;hpb=941b0a9ba4e673e292510d80a5a86806994b8ea6;p=ckeditor.git diff --git a/_source/plugins/clipboard/plugin.js b/_source/plugins/clipboard/plugin.js index d215221..7a1c971 100644 --- a/_source/plugins/clipboard/plugin.js +++ b/_source/plugins/clipboard/plugin.js @@ -16,10 +16,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var doc = editor.document, body = doc.getBody(); - var enabled = false; + var enabled = 0; var onExec = function() { - enabled = true; + enabled = 1; }; // The following seems to be the only reliable way to detect that @@ -28,7 +28,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // the command to execute. body.on( command, onExec ); - doc.$.execCommand( command ); + // IE6/7: document.execCommand has problem to paste into positioned element. + ( CKEDITOR.env.version > 7 ? doc.$ : doc.$.selection.createRange() ) [ 'execCommand' ]( command ); body.removeListener( command, onExec ); @@ -67,6 +68,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { exec : function( editor, data ) { + this.type == 'cut' && fixCut( editor ); + var success = tryToCutCopy( editor, this.type ); if ( !success ) @@ -160,37 +163,47 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { var doc = this.document; - // Avoid recursions on 'paste' event for IE. - if ( CKEDITOR.env.ie && doc.getById( 'cke_pastebin' ) ) + // Avoid recursions on 'paste' event or consequent paste too fast. (#5730) + if ( doc.getById( 'cke_pastebin' ) ) return; + // If the browser supports it, get the data directly + if ( mode == 'text' && evt.data && evt.data.$.clipboardData ) + { + // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows. + var plain = evt.data.$.clipboardData.getData( 'text/plain' ); + if ( plain ) + { + evt.data.preventDefault(); + callback( plain ); + return; + } + } + var sel = this.getSelection(), range = new CKEDITOR.dom.range( doc ); // Create container to paste into - var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : 'div', doc ); + var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : CKEDITOR.env.webkit ? 'body' : 'div', doc ); pastebin.setAttribute( 'id', 'cke_pastebin' ); // Safari requires a filler node inside the div to have the content pasted into it. (#4882) CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) ); doc.getBody().append( pastebin ); + pastebin.setStyles( + { + position : 'absolute', + // Position the bin exactly at the position of the selected element + // to avoid any subsequent document scroll. + top : sel.getStartElement().getDocumentPosition().y + 'px', + width : '1px', + height : '1px', + overflow : 'hidden' + }); + // It's definitely a better user experience if we make the paste-bin pretty unnoticed - // by pulling it off the screen, while this hack will make the paste-bin a control type element - // and that become a selection plain later. - if ( !CKEDITOR.env.ie && mode != 'html' ) - { - pastebin.setStyles( - { - position : 'absolute', - left : '-1000px', - // Position the bin exactly at the position of the selected element - // to avoid any subsequent document scroll. - top : sel.getStartElement().getDocumentPosition().y + 'px', - width : '1px', - height : '1px', - overflow : 'hidden' - }); - } + // by pulling it off the screen. + pastebin.setStyle( this.config.contentsLangDirection == 'ltr' ? 'left' : 'right', '-1000px' ); var bms = sel.createBookmarks(); @@ -238,10 +251,40 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, 0 ); } + // Cutting off control type element in IE standards breaks the selection entirely. (#4881) + function fixCut( editor ) + { + if ( !CKEDITOR.env.ie || CKEDITOR.env.quirks ) + return; + + var sel = editor.getSelection(); + var control; + if( ( sel.getType() == CKEDITOR.SELECTION_ELEMENT ) && ( control = sel.getSelectedElement() ) ) + { + var range = sel.getRanges()[ 0 ]; + var dummy = editor.document.createText( '' ); + dummy.insertBefore( control ); + range.setStartBefore( dummy ); + range.setEndAfter( control ); + sel.selectRanges( [ range ] ); + + // Clear up the fix if the paste wasn't succeeded. + setTimeout( function() + { + // Element still online? + if ( control.getParent() ) + { + dummy.remove(); + sel.selectElement( control ); + } + }, 0 ); + } + } + // Register the plugin. CKEDITOR.plugins.add( 'clipboard', { - requires : [ 'htmldataprocessor' ], + requires : [ 'dialog', 'htmldataprocessor' ], init : function( editor ) { // Inserts processed data into the editor at the end of the @@ -305,10 +348,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license editor.on( 'contentDom', function() { var body = editor.document.getBody(); - body.on( ( mode == 'text' && CKEDITOR.env.ie ) ? 'paste' : 'beforepaste', + body.on( ( (mode == 'text' && CKEDITOR.env.ie ) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste', function( evt ) { - if( depressBeforePasteEvent ) + if ( depressBeforeEvent ) return; getClipboardData.call( editor, evt, mode, function ( data ) @@ -324,35 +367,46 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } ); }); + body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } ); }); // If the "contextmenu" plugin is loaded, register the listeners. if ( editor.contextMenu ) { - var depressBeforePasteEvent; + var depressBeforeEvent; function stateFromNamedCommand( command ) { - // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste', + // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)', // guard to distinguish from the ordinary sources( either // keyboard paste or execCommand ) (#4874). - CKEDITOR.env.ie && command == 'Paste'&& ( depressBeforePasteEvent = 1 ); + CKEDITOR.env.ie && ( depressBeforeEvent = 1 ); var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED; - depressBeforePasteEvent = 0; + depressBeforeEvent = 0; return retval; } - editor.contextMenu.addListener( function() + editor.contextMenu.addListener( function( element, selection ) { + var readOnly = selection.getCommonAncestor().isReadOnly(); return { - cut : stateFromNamedCommand( 'Cut' ), - - // Browser bug: 'Cut' has the correct states for both Copy and Cut. - copy : stateFromNamedCommand( 'Cut' ), - paste : CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' ) + cut : !readOnly && stateFromNamedCommand( 'Cut' ), + copy : stateFromNamedCommand( 'Copy' ), + paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' ) ) }; }); } } }); })(); + +/** + * Fired when a clipboard operation is about to be taken into the editor. + * Listeners can manipulate the data to be pasted before having it effectively + * inserted into the document. + * @name CKEDITOR.editor#paste + * @since 3.1 + * @event + * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined. + * @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. + */