JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[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                                 function()\r
26                                 {\r
27                                         window.netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" );\r
28 \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
35 \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
41                                 }\r
42                                 // Any other approach that's working...\r
43                                 );\r
44 \r
45                         if ( !clipboardText )   // Clipboard access privilege is not granted.\r
46                         {\r
47                                 editor.openDialog( 'pastetext' );\r
48                                 return false;\r
49                         }\r
50                         else\r
51                                 editor.fire( 'paste', { 'text' : clipboardText } );\r
52 \r
53                         return true;\r
54                 }\r
55         };\r
56 \r
57         function doInsertText( doc, text )\r
58         {\r
59                 // Native text insertion.\r
60                 if( CKEDITOR.env.ie )\r
61                 {\r
62                         var selection = doc.selection;\r
63                         if ( selection.type == 'Control' )\r
64                                 selection.clear();\r
65                         selection.createRange().pasteHTML( text );\r
66                 }\r
67                 else\r
68                         doc.execCommand( 'inserthtml', false, text );\r
69         }\r
70 \r
71         // Register the plugin.\r
72         CKEDITOR.plugins.add( 'pastetext',\r
73         {\r
74                 init : function( editor )\r
75                 {\r
76                         var commandName = 'pastetext',\r
77                                 command = editor.addCommand( commandName, pasteTextCmd );\r
78 \r
79                         editor.ui.addButton( 'PasteText',\r
80                                 {\r
81                                         label : editor.lang.pasteText.button,\r
82                                         command : commandName\r
83                                 });\r
84 \r
85                         CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );\r
86 \r
87                         if ( editor.config.forcePasteAsPlainText )\r
88                         {\r
89                                 // Intercept the default pasting process.\r
90                                 editor.on( 'beforeCommandExec', function ( evt )\r
91                                 {\r
92                                         if ( evt.data.name == 'paste' )\r
93                                         {\r
94                                                 editor.execCommand( 'pastetext' );\r
95                                                 evt.cancel();\r
96                                         }\r
97                                 }, null, null, 0 );\r
98                         }\r
99                 },\r
100 \r
101                 requires : [ 'clipboard' ]\r
102         });\r
103 \r
104         function doEnter( editor, mode, times, forceMode )\r
105         {\r
106                 while ( times-- )\r
107                 {\r
108                         CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ]\r
109                                         ( editor, mode, null, forceMode );\r
110                 }\r
111         }\r
112 \r
113         CKEDITOR.editor.prototype.insertText = function( text )\r
114         {\r
115                 this.focus();\r
116                 this.fire( 'saveSnapshot' );\r
117 \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
121                         self = this,\r
122                         line;\r
123 \r
124                 text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );\r
125 \r
126                 var startIndex = 0;\r
127                 text.replace( /\n+/g, function( match, lastIndex )\r
128                  {\r
129                         line = text.substring( startIndex, lastIndex );\r
130                         startIndex = lastIndex + match.length;\r
131                         line.length && doInsertText( doc, line );\r
132 \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
138 \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
142                  });\r
143 \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
147 \r
148                 this.fire( 'saveSnapshot' );\r
149         };\r
150 })();\r
151 \r
152 \r
153 /**\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
156  * text.\r
157  * @name CKEDITOR.config.forcePasteAsPlainText\r
158  * @type Boolean\r
159  * @default false\r
160  * @example\r
161  * config.forcePasteAsPlainText = true;\r
162  */\r