JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / pastetext / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, 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                                         var mode = evt.data.commandData;\r
62                                         // Do NOT overwrite if HTML format is explicitly requested.\r
63                                         if ( evt.data.name == 'paste' && mode != 'html' )\r
64                                         {\r
65                                                 editor.execCommand( 'pastetext' );\r
66                                                 evt.cancel();\r
67                                         }\r
68                                 }, null, null, 0 );\r
69 \r
70                                 editor.on( 'beforePaste', function( evt )\r
71                                 {\r
72                                         evt.data.mode = 'text';\r
73                                 });\r
74                         }\r
75 \r
76                         editor.on( 'pasteState', function( evt )\r
77                                 {\r
78                                         editor.getCommand( 'pastetext' ).setState( evt.data );\r
79                                 });\r
80                 },\r
81 \r
82                 requires : [ 'clipboard' ]\r
83         });\r
84 \r
85 })();\r
86 \r
87 \r
88 /**\r
89  * Whether to force all pasting operations to insert on plain text into the\r
90  * editor, loosing any formatting information possibly available in the source\r
91  * text.\r
92  * <strong>Note:</strong> paste from word is not affected by this configuration.\r
93  * @name CKEDITOR.config.forcePasteAsPlainText\r
94  * @type Boolean\r
95  * @default false\r
96  * @example\r
97  * config.forcePasteAsPlainText = true;\r
98  */\r