JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.3.2
[ckeditor.git] / _source / plugins / pastetext / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @file Paste as plain text plugin\r
8  */\r
9 \r
10 (function()\r
11 {\r
12         // The pastetext command definition.\r
13         var pasteTextCmd =\r
14         {\r
15                 exec : function( editor )\r
16                 {\r
17                         var clipboardText = CKEDITOR.tools.tryThese(\r
18                                 function()\r
19                                 {\r
20                                         var clipboardText = window.clipboardData.getData( 'Text' );\r
21                                         if ( !clipboardText )\r
22                                                 throw 0;\r
23                                         return clipboardText;\r
24                                 }\r
25                                 // Any other approach that's working...\r
26                                 );\r
27 \r
28                         if ( !clipboardText )   // Clipboard access privilege is not granted.\r
29                         {\r
30                                 editor.openDialog( 'pastetext' );\r
31                                 return false;\r
32                         }\r
33                         else\r
34                                 editor.fire( 'paste', { 'text' : clipboardText } );\r
35 \r
36                         return true;\r
37                 }\r
38         };\r
39 \r
40         function doInsertText( doc, text )\r
41         {\r
42                 // Native text insertion.\r
43                 if ( CKEDITOR.env.ie )\r
44                 {\r
45                         var selection = doc.selection;\r
46                         if ( selection.type == 'Control' )\r
47                                 selection.clear();\r
48                         selection.createRange().pasteHTML( text );\r
49                 }\r
50                 else\r
51                         doc.execCommand( 'inserthtml', false, text );\r
52         }\r
53 \r
54         // Register the plugin.\r
55         CKEDITOR.plugins.add( 'pastetext',\r
56         {\r
57                 init : function( editor )\r
58                 {\r
59                         var commandName = 'pastetext',\r
60                                 command = editor.addCommand( commandName, pasteTextCmd );\r
61 \r
62                         editor.ui.addButton( 'PasteText',\r
63                                 {\r
64                                         label : editor.lang.pasteText.button,\r
65                                         command : commandName\r
66                                 });\r
67 \r
68                         CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );\r
69 \r
70                         if ( editor.config.forcePasteAsPlainText )\r
71                         {\r
72                                 // Intercept the default pasting process.\r
73                                 editor.on( 'beforeCommandExec', function ( evt )\r
74                                 {\r
75                                         if ( evt.data.name == 'paste' )\r
76                                         {\r
77                                                 editor.execCommand( 'pastetext' );\r
78                                                 evt.cancel();\r
79                                         }\r
80                                 }, null, null, 0 );\r
81                         }\r
82                 },\r
83 \r
84                 requires : [ 'clipboard' ]\r
85         });\r
86 \r
87         function doEnter( editor, mode, times, forceMode )\r
88         {\r
89                 while ( times-- )\r
90                 {\r
91                         CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ]\r
92                                         ( editor, mode, null, forceMode );\r
93                 }\r
94         }\r
95 \r
96         CKEDITOR.editor.prototype.insertText = function( text )\r
97         {\r
98                 this.focus();\r
99                 this.fire( 'saveSnapshot' );\r
100 \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
104                         self = this,\r
105                         line;\r
106 \r
107                 text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );\r
108 \r
109                 var startIndex = 0;\r
110                 text.replace( /\n+/g, function( match, lastIndex )\r
111                  {\r
112                         line = text.substring( startIndex, lastIndex );\r
113                         startIndex = lastIndex + match.length;\r
114                         line.length && doInsertText( doc, line );\r
115 \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
121 \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
125                  });\r
126 \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
130 \r
131                 this.fire( 'saveSnapshot' );\r
132         };\r
133 })();\r
134 \r
135 \r
136 /**\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
139  * text.\r
140  * @name CKEDITOR.config.forcePasteAsPlainText\r
141  * @type Boolean\r
142  * @default false\r
143  * @example\r
144  * config.forcePasteAsPlainText = true;\r
145  */\r