2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @file Paste as plain text plugin
\r
12 // The pastetext command definition.
\r
15 exec : function( editor )
\r
17 // We use getClipboardData just to test if the clipboard access has
\r
18 // been granted by the user.
\r
19 if ( CKEDITOR.getClipboardData() === false || !window.clipboardData )
\r
21 editor.openDialog( 'pastetext' );
\r
25 editor.insertText( window.clipboardData.getData( 'Text' ) );
\r
29 // Register the plugin.
\r
30 CKEDITOR.plugins.add( 'pastetext',
\r
32 init : function( editor )
\r
34 var commandName = 'pastetext',
\r
35 command = editor.addCommand( commandName, pasteTextCmd );
\r
37 editor.ui.addButton( 'PasteText',
\r
39 label : editor.lang.pasteText.button,
\r
40 command : commandName
\r
43 CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );
\r
45 if ( editor.config.forcePasteAsPlainText )
\r
47 editor.on( 'beforePaste', function( event )
\r
49 if ( editor.mode == "wysiwyg" )
\r
51 setTimeout( function() { command.exec(); }, 0 );
\r
58 requires : [ 'clipboard' ]
\r
63 CKEDITOR.getClipboardData = function()
\r
65 if ( !CKEDITOR.env.ie )
\r
68 var doc = CKEDITOR.document,
\r
69 body = doc.getBody();
\r
71 if ( !clipboardDiv )
\r
73 clipboardDiv = doc.createElement( 'div',
\r
81 position : 'absolute',
\r
82 visibility : 'hidden',
\r
83 overflow : 'hidden',
\r
89 clipboardDiv.setHtml( '' );
\r
91 clipboardDiv.appendTo( body );
\r
94 // The "enabled" flag is used to check whether the paste operation has
\r
95 // been completed (the onpaste event has been fired).
\r
96 var enabled = false;
\r
97 var setEnabled = function()
\r
102 body.on( 'paste', setEnabled );
\r
104 // Create a text range and move it inside the div.
\r
105 var textRange = body.$.createTextRange();
\r
106 textRange.moveToElementText( clipboardDiv.$ );
\r
108 // The execCommand in will fire the "onpaste", only if the
\r
109 // security settings are enabled.
\r
110 textRange.execCommand( 'Paste' );
\r
112 // Get the DIV html and reset it.
\r
113 var html = clipboardDiv.getHtml();
\r
114 clipboardDiv.setHtml( '' );
\r
116 body.removeListener( 'paste', setEnabled );
\r
118 // Return the HTML or false if not enabled.
\r
119 return enabled && html;
\r
123 CKEDITOR.editor.prototype.insertText = function( text )
\r
125 text = CKEDITOR.tools.htmlEncode( text );
\r
127 // TODO: Replace the following with fill line break processing (see V2).
\r
128 text = text.replace( /(?:\r\n)|\n|\r/g, '<br>' );
\r
130 this.insertHtml( text );
\r
134 * Whether to force all pasting operations to insert on plain text into the
\r
135 * editor, loosing any formatting information possibly available in the source
\r
140 * config.forcePasteAsPlainText = true;
\r
142 CKEDITOR.config.forcePasteAsPlainText = false;
\r