JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[ckeditor.git] / _source / plugins / pastefromword / 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 (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( 'paste', function( evt )\r
51                         {\r
52                                 var data = evt.data,\r
53                                         mswordHtml;\r
54 \r
55                                 // MS-WORD format sniffing.\r
56                                 if ( ( mswordHtml = data[ 'html' ] )\r
57                                          && ( forceFromWord || ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml ) ) )\r
58                                 {\r
59                                         var isLazyLoad = this.loadFilterRules( function()\r
60                                                 {\r
61                                                         // Event continuation with the original data.\r
62                                                         if ( isLazyLoad )\r
63                                                                 editor.fire( 'paste', data );\r
64                                                         else if ( !editor.config.pasteFromWordPromptCleanup\r
65                                                           || ( forceFromWord || confirm( editor.lang.pastefromword.confirmCleanup ) ) )\r
66                                                          {\r
67                                                                 data[ 'html' ] = CKEDITOR.cleanWord( mswordHtml, editor );\r
68                                                         }\r
69                                                 });\r
70 \r
71                                         // The cleanup rules are to be loaded, we should just cancel\r
72                                         // this event.\r
73                                         isLazyLoad && evt.cancel();\r
74                                 }\r
75                         }, this );\r
76                 },\r
77 \r
78                 loadFilterRules : function( callback )\r
79                 {\r
80 \r
81                         var isLoaded = CKEDITOR.cleanWord;\r
82 \r
83                         if ( isLoaded )\r
84                                 callback();\r
85                         else\r
86                         {\r
87                                 var filterFilePath = CKEDITOR.getUrl(\r
88                                                 CKEDITOR.config.pasteFromWordCleanupFile\r
89                                                 || ( this.path + 'filter/default.js' ) );\r
90 \r
91                                 // Load with busy indicator.\r
92                                 CKEDITOR.scriptLoader.load( filterFilePath, callback, null, false, true );\r
93                         }\r
94 \r
95                         return !isLoaded;\r
96                 }\r
97         });\r
98 })();\r
99 \r
100 /**\r
101  * Whether to prompt the user about the clean up of content being pasted from\r
102  * MS Word.\r
103  * @name CKEDITOR.config.pasteFromWordPromptCleanup\r
104  * @since 3.1\r
105  * @type Boolean\r
106  * @default undefined\r
107  * @example\r
108  * config.pasteFromWordPromptCleanup = true;\r
109  */\r
110 \r
111 /**\r
112  * The file that provides the MS Word cleanup function for pasting operations.\r
113  * Note: This is a global configuration shared by all editor instances present\r
114  * in the page.\r
115  * @name CKEDITOR.config.pasteFromWordCleanupFile\r
116  * @since 3.1\r
117  * @type String\r
118  * @default 'default'\r
119  * @example\r
120  * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file).\r
121  * CKEDITOR.config.pasteFromWordCleanupFile = 'custom';\r
122  */\r