JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5
[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         // Register the plugin.\r
41         CKEDITOR.plugins.add( 'pastetext',\r
42         {\r
43                 init : function( editor )\r
44                 {\r
45                         var commandName = 'pastetext',\r
46                                 command = editor.addCommand( commandName, pasteTextCmd );\r
47 \r
48                         editor.ui.addButton( 'PasteText',\r
49                                 {\r
50                                         label : editor.lang.pasteText.button,\r
51                                         command : commandName\r
52                                 });\r
53 \r
54                         CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );\r
55 \r
56                         if ( editor.config.forcePasteAsPlainText )\r
57                         {\r
58                                 // Intercept the default pasting process.\r
59                                 editor.on( 'beforeCommandExec', function ( evt )\r
60                                 {\r
61                                         if ( evt.data.name == 'paste' )\r
62                                         {\r
63                                                 editor.execCommand( 'pastetext' );\r
64                                                 evt.cancel();\r
65                                         }\r
66                                 }, null, null, 0 );\r
67                         }\r
68                 },\r
69 \r
70                 requires : [ 'clipboard' ]\r
71         });\r
72 \r
73 })();\r
74 \r
75 \r
76 /**\r
77  * Whether to force all pasting operations to insert on plain text into the\r
78  * editor, loosing any formatting information possibly available in the source\r
79  * text.\r
80  * @name CKEDITOR.config.forcePasteAsPlainText\r
81  * @type Boolean\r
82  * @default false\r
83  * @example\r
84  * config.forcePasteAsPlainText = true;\r
85  */\r