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