X-Git-Url: https://jasonwoof.com/gitweb/?p=ckeditor.git;a=blobdiff_plain;f=_source%2Fplugins%2Fpastetext%2Fplugin.js;h=d391fd6691714be01f4b47c64a6cf2c47636cfa3;hp=3a1678d6d6746f20aa4258adf61a8d771b5d0392;hb=941b0a9ba4e673e292510d80a5a86806994b8ea6;hpb=7cd80714081a8ffdf4a1a8d2c72f120ed5ef3d6d diff --git a/_source/plugins/pastetext/plugin.js b/_source/plugins/pastetext/plugin.js index 3a1678d..d391fd6 100644 --- a/_source/plugins/pastetext/plugin.js +++ b/_source/plugins/pastetext/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -14,18 +14,60 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { exec : function( editor ) { - // We use getClipboardData just to test if the clipboard access has - // been granted by the user. - if ( CKEDITOR.getClipboardData() === false || !window.clipboardData ) + var clipboardText = CKEDITOR.tools.tryThese( + function() + { + var clipboardText = window.clipboardData.getData( 'Text' ); + if ( !clipboardText ) + throw 0; + return clipboardText; + }, + function() + { + window.netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" ); + + var clip = window.Components.classes[ "@mozilla.org/widget/clipboard;1" ] + .getService( window.Components.interfaces.nsIClipboard ); + var trans = window.Components.classes[ "@mozilla.org/widget/transferable;1" ] + .createInstance( window.Components.interfaces.nsITransferable ); + trans.addDataFlavor( "text/unicode" ); + clip.getData( trans, clip.kGlobalClipboard ); + + var str = {}, strLength = {}, clipboardText; + trans.getTransferData( "text/unicode", str, strLength ); + str = str.value.QueryInterface( window.Components.interfaces.nsISupportsString ); + clipboardText = str.data.substring( 0, strLength.value / 2 ); + return clipboardText; + } + // Any other approach that's working... + ); + + if ( !clipboardText ) // Clipboard access privilege is not granted. { editor.openDialog( 'pastetext' ); - return; + return false; } + else + editor.fire( 'paste', { 'text' : clipboardText } ); - editor.insertText( window.clipboardData.getData( 'Text' ) ); + return true; } }; + function doInsertText( doc, text ) + { + // Native text insertion. + if( CKEDITOR.env.ie ) + { + var selection = doc.selection; + if ( selection.type == 'Control' ) + selection.clear(); + selection.createRange().pasteHTML( text ); + } + else + doc.execCommand( 'inserthtml', false, text ); + } + // Register the plugin. CKEDITOR.plugins.add( 'pastetext', { @@ -44,99 +86,77 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( editor.config.forcePasteAsPlainText ) { - editor.on( 'beforePaste', function( event ) + // Intercept the default pasting process. + editor.on( 'beforeCommandExec', function ( evt ) + { + if ( evt.data.name == 'paste' ) { - if ( editor.mode == "wysiwyg" ) - { - setTimeout( function() { command.exec(); }, 0 ); - event.cancel(); - } - }, - null, null, 20 ); + editor.execCommand( 'pastetext' ); + evt.cancel(); + } + }, null, null, 0 ); } }, + requires : [ 'clipboard' ] }); - var clipboardDiv; - - CKEDITOR.getClipboardData = function() + function doEnter( editor, mode, times, forceMode ) { - if ( !CKEDITOR.env.ie ) - return false; - - var doc = CKEDITOR.document, - body = doc.getBody(); - - if ( !clipboardDiv ) + while ( times-- ) { - clipboardDiv = doc.createElement( 'div', - { - attributes : - { - id: 'cke_hiddenDiv' - }, - styles : - { - position : 'absolute', - visibility : 'hidden', - overflow : 'hidden', - width : '1px', - height : '1px' - } - }); - - clipboardDiv.setHtml( '' ); - - clipboardDiv.appendTo( body ); + CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ] + ( editor, mode, null, forceMode ); } + } - // The "enabled" flag is used to check whether the paste operation has - // been completed (the onpaste event has been fired). - var enabled = false; - var setEnabled = function() - { - enabled = true; - }; - - body.on( 'paste', setEnabled ); - - // Create a text range and move it inside the div. - var textRange = body.$.createTextRange(); - textRange.moveToElementText( clipboardDiv.$ ); - - // The execCommand in will fire the "onpaste", only if the - // security settings are enabled. - textRange.execCommand( 'Paste' ); - - // Get the DIV html and reset it. - var html = clipboardDiv.getHtml(); - clipboardDiv.setHtml( '' ); - - body.removeListener( 'paste', setEnabled ); - - // Return the HTML or false if not enabled. - return enabled && html; + CKEDITOR.editor.prototype.insertText = function( text ) + { + this.focus(); + this.fire( 'saveSnapshot' ); + + var mode = this.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : this.config.enterMode, + isEnterBrMode = mode == CKEDITOR.ENTER_BR, + doc = this.document.$, + self = this, + line; + + text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) ); + + var startIndex = 0; + text.replace( /\n+/g, function( match, lastIndex ) + { + line = text.substring( startIndex, lastIndex ); + startIndex = lastIndex + match.length; + line.length && doInsertText( doc, line ); + + var lineBreakNums = match.length, + // Duo consequence line-break as a enter block. + enterBlockTimes = isEnterBrMode ? 0 : Math.floor( lineBreakNums / 2 ), + // Per link-break as a enter br. + enterBrTimes = isEnterBrMode ? lineBreakNums : lineBreakNums % 2; + + // Line-breaks are converted to editor enter key strokes. + doEnter( self, mode, enterBlockTimes ); + doEnter( self, CKEDITOR.ENTER_BR, enterBrTimes, isEnterBrMode ? false : true ); + }); + + // Insert the last text line of text. + line = text.substring( startIndex, text.length ); + line.length && doInsertText( doc, line ); + + this.fire( 'saveSnapshot' ); }; })(); -CKEDITOR.editor.prototype.insertText = function( text ) -{ - text = CKEDITOR.tools.htmlEncode( text ); - - // TODO: Replace the following with fill line break processing (see V2). - text = text.replace( /(?:\r\n)|\n|\r/g, '
' ); - - this.insertHtml( text ); -}; /** * Whether to force all pasting operations to insert on plain text into the * editor, loosing any formatting information possibly available in the source * text. + * @name CKEDITOR.config.forcePasteAsPlainText * @type Boolean * @default false * @example * config.forcePasteAsPlainText = true; */ -CKEDITOR.config.forcePasteAsPlainText = false;