JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
a5a90d51074e1e386a44db3ae7a5d0b259cb3d9d
[ckeditor.git] / _source / plugins / pastefromword / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 (function()\r
6 {\r
7         CKEDITOR.plugins.add( 'pastefromword',\r
8         {\r
9                 init : function( editor )\r
10                 {\r
11 \r
12                         // Flag indicate this command is actually been asked instead of a generic\r
13                         // pasting.\r
14                         var forceFromWord = 0;\r
15                         var resetFromWord = function()\r
16                                 {\r
17                                         setTimeout( function() { forceFromWord = 0; }, 0 );\r
18                                 };\r
19 \r
20                         // Features bring by this command beside the normal process:\r
21                         // 1. No more bothering of user about the clean-up.\r
22                         // 2. Perform the clean-up even if content is not from MS-Word.\r
23                         // (e.g. from a MS-Word similar application.)\r
24                         editor.addCommand( 'pastefromword',\r
25                         {\r
26                                 canUndo : false,\r
27                                 exec : function()\r
28                                 {\r
29                                         forceFromWord = 1;\r
30                                         if ( editor.execCommand( 'paste' ) === false )\r
31                                         {\r
32                                                 editor.on( 'dialogHide', function ( evt )\r
33                                                         {\r
34                                                                 evt.removeListener();\r
35                                                                 resetFromWord();\r
36                                                         });\r
37                                         }\r
38                                         else\r
39                                                 resetFromWord();\r
40                                 }\r
41                         });\r
42 \r
43                         // Register the toolbar button.\r
44                         editor.ui.addButton( 'PasteFromWord',\r
45                                 {\r
46                                         label : editor.lang.pastefromword.toolbar,\r
47                                         command : 'pastefromword'\r
48                                 });\r
49 \r
50                         editor.on( 'pasteState', function( evt )\r
51                                 {\r
52                                         editor.getCommand( 'pastefromword' ).setState( evt.data );\r
53                                 });\r
54 \r
55                         editor.on( 'paste', function( evt )\r
56                         {\r
57                                 var data = evt.data,\r
58                                         mswordHtml;\r
59 \r
60                                 // MS-WORD format sniffing.\r
61                                 if ( ( mswordHtml = data[ 'html' ] )\r
62                                          && ( forceFromWord || ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml ) ) )\r
63                                 {\r
64                                         var isLazyLoad = this.loadFilterRules( function()\r
65                                                 {\r
66                                                         // Event continuation with the original data.\r
67                                                         if ( isLazyLoad )\r
68                                                                 editor.fire( 'paste', data );\r
69                                                         else if ( !editor.config.pasteFromWordPromptCleanup\r
70                                                           || ( forceFromWord || confirm( editor.lang.pastefromword.confirmCleanup ) ) )\r
71                                                          {\r
72                                                                 data[ 'html' ] = CKEDITOR.cleanWord( mswordHtml, editor );\r
73                                                         }\r
74                                                 });\r
75 \r
76                                         // The cleanup rules are to be loaded, we should just cancel\r
77                                         // this event.\r
78                                         isLazyLoad && evt.cancel();\r
79                                 }\r
80                         }, this );\r
81                 },\r
82 \r
83                 loadFilterRules : function( callback )\r
84                 {\r
85 \r
86                         var isLoaded = CKEDITOR.cleanWord;\r
87 \r
88                         if ( isLoaded )\r
89                                 callback();\r
90                         else\r
91                         {\r
92                                 var filterFilePath = CKEDITOR.getUrl(\r
93                                                 CKEDITOR.config.pasteFromWordCleanupFile\r
94                                                 || ( this.path + 'filter/default.js' ) );\r
95 \r
96                                 // Load with busy indicator.\r
97                                 CKEDITOR.scriptLoader.load( filterFilePath, callback, null, true );\r
98                         }\r
99 \r
100                         return !isLoaded;\r
101                 },\r
102 \r
103                 requires : [ 'clipboard' ]\r
104         });\r
105 })();\r
106 \r
107 /**\r
108  * Whether to prompt the user about the clean up of content being pasted from\r
109  * MS Word.\r
110  * @name CKEDITOR.config.pasteFromWordPromptCleanup\r
111  * @since 3.1\r
112  * @type Boolean\r
113  * @default undefined\r
114  * @example\r
115  * config.pasteFromWordPromptCleanup = true;\r
116  */\r
117 \r
118 /**\r
119  * The file that provides the MS Word cleanup function for pasting operations.\r
120  * Note: This is a global configuration shared by all editor instances present\r
121  * in the page.\r
122  * @name CKEDITOR.config.pasteFromWordCleanupFile\r
123  * @since 3.1\r
124  * @type String\r
125  * @default 'default'\r
126  * @example\r
127  * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file).\r
128  * CKEDITOR.config.pasteFromWordCleanupFile = 'custom';\r
129  */\r