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
25 // Any other approach that's working...
\r
28 if ( !clipboardText ) // Clipboard access privilege is not granted.
\r
30 editor.openDialog( 'pastetext' );
\r
34 editor.fire( 'paste', { 'text' : clipboardText } );
\r
40 function doInsertText( doc, text )
\r
42 // Native text insertion.
\r
43 if ( CKEDITOR.env.ie )
\r
45 var selection = doc.selection;
\r
46 if ( selection.type == 'Control' )
\r
48 selection.createRange().pasteHTML( text );
\r
51 doc.execCommand( 'inserthtml', false, text );
\r
54 // Register the plugin.
\r
55 CKEDITOR.plugins.add( 'pastetext',
\r
57 init : function( editor )
\r
59 var commandName = 'pastetext',
\r
60 command = editor.addCommand( commandName, pasteTextCmd );
\r
62 editor.ui.addButton( 'PasteText',
\r
64 label : editor.lang.pasteText.button,
\r
65 command : commandName
\r
68 CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );
\r
70 if ( editor.config.forcePasteAsPlainText )
\r
72 // Intercept the default pasting process.
\r
73 editor.on( 'beforeCommandExec', function ( evt )
\r
75 if ( evt.data.name == 'paste' )
\r
77 editor.execCommand( 'pastetext' );
\r
84 requires : [ 'clipboard' ]
\r
87 function doEnter( editor, mode, times, forceMode )
\r
91 CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ]
\r
92 ( editor, mode, null, forceMode );
\r
96 CKEDITOR.editor.prototype.insertText = function( text )
\r
99 this.fire( 'saveSnapshot' );
\r
101 var mode = this.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : this.config.enterMode,
\r
102 isEnterBrMode = mode == CKEDITOR.ENTER_BR,
\r
103 doc = this.document.$,
\r
107 text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
109 var startIndex = 0;
\r
110 text.replace( /\n+/g, function( match, lastIndex )
\r
112 line = text.substring( startIndex, lastIndex );
\r
113 startIndex = lastIndex + match.length;
\r
114 line.length && doInsertText( doc, line );
\r
116 var lineBreakNums = match.length,
\r
117 // Duo consequence line-break as a enter block.
\r
118 enterBlockTimes = isEnterBrMode ? 0 : Math.floor( lineBreakNums / 2 ),
\r
119 // Per link-break as a enter br.
\r
120 enterBrTimes = isEnterBrMode ? lineBreakNums : lineBreakNums % 2;
\r
122 // Line-breaks are converted to editor enter key strokes.
\r
123 doEnter( self, mode, enterBlockTimes );
\r
124 doEnter( self, CKEDITOR.ENTER_BR, enterBrTimes, isEnterBrMode ? false : true );
\r
127 // Insert the last text line of text.
\r
128 line = text.substring( startIndex, text.length );
\r
129 line.length && doInsertText( doc, line );
\r
131 this.fire( 'saveSnapshot' );
\r
137 * Whether to force all pasting operations to insert on plain text into the
\r
138 * editor, loosing any formatting information possibly available in the source
\r
140 * @name CKEDITOR.config.forcePasteAsPlainText
\r
144 * config.forcePasteAsPlainText = true;
\r