2 Copyright (c) 2003-2010, 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 var clipboardText = CKEDITOR.tools.tryThese(
\r
20 var clipboardText = window.clipboardData.getData( 'Text' );
\r
21 if ( !clipboardText )
\r
23 return clipboardText;
\r
27 window.netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" );
\r
29 var clip = window.Components.classes[ "@mozilla.org/widget/clipboard;1" ]
\r
30 .getService( window.Components.interfaces.nsIClipboard );
\r
31 var trans = window.Components.classes[ "@mozilla.org/widget/transferable;1" ]
\r
32 .createInstance( window.Components.interfaces.nsITransferable );
\r
33 trans.addDataFlavor( "text/unicode" );
\r
34 clip.getData( trans, clip.kGlobalClipboard );
\r
36 var str = {}, strLength = {}, clipboardText;
\r
37 trans.getTransferData( "text/unicode", str, strLength );
\r
38 str = str.value.QueryInterface( window.Components.interfaces.nsISupportsString );
\r
39 clipboardText = str.data.substring( 0, strLength.value / 2 );
\r
40 return clipboardText;
\r
42 // Any other approach that's working...
\r
45 if ( !clipboardText ) // Clipboard access privilege is not granted.
\r
47 editor.openDialog( 'pastetext' );
\r
51 editor.fire( 'paste', { 'text' : clipboardText } );
\r
57 function doInsertText( doc, text )
\r
59 // Native text insertion.
\r
60 if ( CKEDITOR.env.ie )
\r
62 var selection = doc.selection;
\r
63 if ( selection.type == 'Control' )
\r
65 selection.createRange().pasteHTML( text );
\r
68 doc.execCommand( 'inserthtml', false, text );
\r
71 // Register the plugin.
\r
72 CKEDITOR.plugins.add( 'pastetext',
\r
74 init : function( editor )
\r
76 var commandName = 'pastetext',
\r
77 command = editor.addCommand( commandName, pasteTextCmd );
\r
79 editor.ui.addButton( 'PasteText',
\r
81 label : editor.lang.pasteText.button,
\r
82 command : commandName
\r
85 CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );
\r
87 if ( editor.config.forcePasteAsPlainText )
\r
89 // Intercept the default pasting process.
\r
90 editor.on( 'beforeCommandExec', function ( evt )
\r
92 if ( evt.data.name == 'paste' )
\r
94 editor.execCommand( 'pastetext' );
\r
101 requires : [ 'clipboard' ]
\r
104 function doEnter( editor, mode, times, forceMode )
\r
108 CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ]
\r
109 ( editor, mode, null, forceMode );
\r
113 CKEDITOR.editor.prototype.insertText = function( text )
\r
116 this.fire( 'saveSnapshot' );
\r
118 var mode = this.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : this.config.enterMode,
\r
119 isEnterBrMode = mode == CKEDITOR.ENTER_BR,
\r
120 doc = this.document.$,
\r
124 text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
126 var startIndex = 0;
\r
127 text.replace( /\n+/g, function( match, lastIndex )
\r
129 line = text.substring( startIndex, lastIndex );
\r
130 startIndex = lastIndex + match.length;
\r
131 line.length && doInsertText( doc, line );
\r
133 var lineBreakNums = match.length,
\r
134 // Duo consequence line-break as a enter block.
\r
135 enterBlockTimes = isEnterBrMode ? 0 : Math.floor( lineBreakNums / 2 ),
\r
136 // Per link-break as a enter br.
\r
137 enterBrTimes = isEnterBrMode ? lineBreakNums : lineBreakNums % 2;
\r
139 // Line-breaks are converted to editor enter key strokes.
\r
140 doEnter( self, mode, enterBlockTimes );
\r
141 doEnter( self, CKEDITOR.ENTER_BR, enterBrTimes, isEnterBrMode ? false : true );
\r
144 // Insert the last text line of text.
\r
145 line = text.substring( startIndex, text.length );
\r
146 line.length && doInsertText( doc, line );
\r
148 this.fire( 'saveSnapshot' );
\r
154 * Whether to force all pasting operations to insert on plain text into the
\r
155 * editor, loosing any formatting information possibly available in the source
\r
157 * @name CKEDITOR.config.forcePasteAsPlainText
\r
161 * config.forcePasteAsPlainText = true;
\r