1 /* 2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. 3 For licensing, see LICENSE.html or http://ckeditor.com/license 4 */ 5 6 /** 7 * @file Paste as plain text plugin 8 */ 9 10 (function() 11 { 12 // The pastetext command definition. 13 var pasteTextCmd = 14 { 15 exec : function( editor ) 16 { 17 // We use getClipboardData just to test if the clipboard access has 18 // been granted by the user. 19 if ( CKEDITOR.getClipboardData() === false || !window.clipboardData ) 20 { 21 editor.openDialog( 'pastetext' ); 22 return; 23 } 24 25 editor.insertText( window.clipboardData.getData( 'Text' ) ); 26 } 27 }; 28 29 // Register the plugin. 30 CKEDITOR.plugins.add( 'pastetext', 31 { 32 init : function( editor ) 33 { 34 var commandName = 'pastetext', 35 command = editor.addCommand( commandName, pasteTextCmd ); 36 37 editor.ui.addButton( 'PasteText', 38 { 39 label : editor.lang.pasteText.button, 40 command : commandName 41 }); 42 43 CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) ); 44 45 if ( editor.config.forcePasteAsPlainText ) 46 { 47 editor.on( 'beforePaste', function( event ) 48 { 49 setTimeout( function() { command.exec(); }, 0 ); 50 event.cancel(); 51 }, 52 null, null, 20 ); 53 } 54 }, 55 requires : [ 'clipboard' ] 56 }); 57 58 var clipboardDiv; 59 60 CKEDITOR.getClipboardData = function() 61 { 62 if ( !CKEDITOR.env.ie ) 63 return false; 64 65 var doc = CKEDITOR.document, 66 body = doc.getBody(); 67 68 if ( !clipboardDiv ) 69 { 70 clipboardDiv = doc.createElement( 'div', 71 { 72 attributes : 73 { 74 id: 'cke_hiddenDiv' 75 }, 76 styles : 77 { 78 position : 'absolute', 79 visibility : 'hidden', 80 overflow : 'hidden', 81 width : '1px', 82 height : '1px' 83 } 84 }); 85 86 clipboardDiv.setHtml( '' ); 87 88 clipboardDiv.appendTo( body ); 89 } 90 91 // The "enabled" flag is used to check whether the paste operation has 92 // been completed (the onpaste event has been fired). 93 var enabled = false; 94 var setEnabled = function() 95 { 96 enabled = true; 97 }; 98 99 body.on( 'paste', setEnabled ); 100 101 // Create a text range and move it inside the div. 102 var textRange = body.$.createTextRange(); 103 textRange.moveToElementText( clipboardDiv.$ ); 104 105 // The execCommand in will fire the "onpaste", only if the 106 // security settings are enabled. 107 textRange.execCommand( 'Paste' ); 108 109 // Get the DIV html and reset it. 110 var html = clipboardDiv.getHtml(); 111 clipboardDiv.setHtml( '' ); 112 113 body.removeListener( 'paste', setEnabled ); 114 115 // Return the HTML or false if not enabled. 116 return enabled && html; 117 }; 118 })(); 119 120 CKEDITOR.editor.prototype.insertText = function( text ) 121 { 122 text = CKEDITOR.tools.htmlEncode( text ); 123 124 // TODO: Replace the following with fill line break processing (see V2). 125 text = text.replace( /(?:\r\n)|\n|\r/g, '<br>' ); 126 127 this.insertHtml( text ); 128 }; 129 130 CKEDITOR.config.forcePasteAsPlainText = false; 131