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