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