X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fpastetext%2Fplugin.js;fp=_source%2Fplugins%2Fpastetext%2Fplugin.js;h=3a1678d6d6746f20aa4258adf61a8d771b5d0392;hb=ea7e3453c7b0f023b050aca6d9f83ab372860d91;hp=0000000000000000000000000000000000000000;hpb=b93873b6532ee7515fb0d6f8b73176c44fad28f7;p=ckeditor.git diff --git a/_source/plugins/pastetext/plugin.js b/_source/plugins/pastetext/plugin.js new file mode 100644 index 0000000..3a1678d --- /dev/null +++ b/_source/plugins/pastetext/plugin.js @@ -0,0 +1,142 @@ +/* +Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +For licensing, see LICENSE.html or http://ckeditor.com/license +*/ + +/** + * @file Paste as plain text plugin + */ + +(function() +{ + // The pastetext command definition. + var pasteTextCmd = + { + 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 ) + { + editor.openDialog( 'pastetext' ); + return; + } + + editor.insertText( window.clipboardData.getData( 'Text' ) ); + } + }; + + // Register the plugin. + CKEDITOR.plugins.add( 'pastetext', + { + init : function( editor ) + { + var commandName = 'pastetext', + command = editor.addCommand( commandName, pasteTextCmd ); + + editor.ui.addButton( 'PasteText', + { + label : editor.lang.pasteText.button, + command : commandName + }); + + CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) ); + + if ( editor.config.forcePasteAsPlainText ) + { + editor.on( 'beforePaste', function( event ) + { + if ( editor.mode == "wysiwyg" ) + { + setTimeout( function() { command.exec(); }, 0 ); + event.cancel(); + } + }, + null, null, 20 ); + } + }, + requires : [ 'clipboard' ] + }); + + var clipboardDiv; + + CKEDITOR.getClipboardData = function() + { + if ( !CKEDITOR.env.ie ) + return false; + + var doc = CKEDITOR.document, + body = doc.getBody(); + + if ( !clipboardDiv ) + { + clipboardDiv = doc.createElement( 'div', + { + attributes : + { + id: 'cke_hiddenDiv' + }, + styles : + { + position : 'absolute', + visibility : 'hidden', + overflow : 'hidden', + width : '1px', + height : '1px' + } + }); + + clipboardDiv.setHtml( '' ); + + clipboardDiv.appendTo( body ); + } + + // 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 ) +{ + 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. + * @type Boolean + * @default false + * @example + * config.forcePasteAsPlainText = true; + */ +CKEDITOR.config.forcePasteAsPlainText = false;