JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
3a1678d6d6746f20aa4258adf61a8d771b5d0392
[ckeditor.git] / _source / plugins / pastetext / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, 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                         // 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
20                         {\r
21                                 editor.openDialog( 'pastetext' );\r
22                                 return;\r
23                         }\r
24 \r
25                         editor.insertText( window.clipboardData.getData( 'Text' ) );\r
26                 }\r
27         };\r
28 \r
29         // Register the plugin.\r
30         CKEDITOR.plugins.add( 'pastetext',\r
31         {\r
32                 init : function( editor )\r
33                 {\r
34                         var commandName = 'pastetext',\r
35                                 command = editor.addCommand( commandName, pasteTextCmd );\r
36 \r
37                         editor.ui.addButton( 'PasteText',\r
38                                 {\r
39                                         label : editor.lang.pasteText.button,\r
40                                         command : commandName\r
41                                 });\r
42 \r
43                         CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );\r
44 \r
45                         if ( editor.config.forcePasteAsPlainText )\r
46                         {\r
47                                 editor.on( 'beforePaste', function( event )\r
48                                         {\r
49                                                 if ( editor.mode == "wysiwyg" )\r
50                                                 {\r
51                                                         setTimeout( function() { command.exec(); }, 0 );\r
52                                                         event.cancel();\r
53                                                 }\r
54                                         },\r
55                                         null, null, 20 );\r
56                         }\r
57                 },\r
58                 requires : [ 'clipboard' ]\r
59         });\r
60 \r
61         var clipboardDiv;\r
62 \r
63         CKEDITOR.getClipboardData = function()\r
64         {\r
65                 if ( !CKEDITOR.env.ie )\r
66                         return false;\r
67 \r
68                 var doc = CKEDITOR.document,\r
69                         body = doc.getBody();\r
70 \r
71                 if ( !clipboardDiv )\r
72                 {\r
73                         clipboardDiv = doc.createElement( 'div',\r
74                                 {\r
75                                         attributes :\r
76                                                 {\r
77                                                         id: 'cke_hiddenDiv'\r
78                                                 },\r
79                                         styles :\r
80                                                 {\r
81                                                         position : 'absolute',\r
82                                                         visibility : 'hidden',\r
83                                                         overflow : 'hidden',\r
84                                                         width : '1px',\r
85                                                         height : '1px'\r
86                                                 }\r
87                                 });\r
88 \r
89                         clipboardDiv.setHtml( '' );\r
90 \r
91                         clipboardDiv.appendTo( body );\r
92                 }\r
93 \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
98                 {\r
99                         enabled = true;\r
100                 };\r
101 \r
102                 body.on( 'paste', setEnabled );\r
103 \r
104                 // Create a text range and move it inside the div.\r
105                 var textRange = body.$.createTextRange();\r
106                 textRange.moveToElementText( clipboardDiv.$ );\r
107 \r
108                 // The execCommand in will fire the "onpaste", only if the\r
109                 // security settings are enabled.\r
110                 textRange.execCommand( 'Paste' );\r
111 \r
112                 // Get the DIV html and reset it.\r
113                 var html = clipboardDiv.getHtml();\r
114                 clipboardDiv.setHtml( '' );\r
115 \r
116                 body.removeListener( 'paste', setEnabled );\r
117 \r
118                 // Return the HTML or false if not enabled.\r
119                 return enabled && html;\r
120         };\r
121 })();\r
122 \r
123 CKEDITOR.editor.prototype.insertText = function( text )\r
124 {\r
125         text = CKEDITOR.tools.htmlEncode( text );\r
126 \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
129 \r
130         this.insertHtml( text );\r
131 };\r
132 \r
133 /**\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
136  * text.\r
137  * @type Boolean\r
138  * @default false\r
139  * @example\r
140  * config.forcePasteAsPlainText = true;\r
141  */\r
142 CKEDITOR.config.forcePasteAsPlainText = false;\r