]> git.immae.eu Git - perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git/blame - sources/plugins/pastefromword/plugin.js
Initial commit
[perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git] / sources / plugins / pastefromword / plugin.js
CommitLineData
7adcb81e
IB
1/**\r
2 * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.\r
3 * For licensing, see LICENSE.md or http://ckeditor.com/license\r
4 */\r
5\r
6( function() {\r
7 CKEDITOR.plugins.add( 'pastefromword', {\r
8 requires: 'clipboard',\r
9 // jscs:disable maximumLineLength\r
10 lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%\r
11 // jscs:enable maximumLineLength\r
12 icons: 'pastefromword,pastefromword-rtl', // %REMOVE_LINE_CORE%\r
13 hidpi: true, // %REMOVE_LINE_CORE%\r
14 init: function( editor ) {\r
15 var commandName = 'pastefromword',\r
16 // Flag indicate this command is actually been asked instead of a generic pasting.\r
17 forceFromWord = 0,\r
18 path = this.path;\r
19\r
20 editor.addCommand( commandName, {\r
21 // Snapshots are done manually by editable.insertXXX methods.\r
22 canUndo: false,\r
23 async: true,\r
24\r
25 exec: function( editor ) {\r
26 var cmd = this;\r
27\r
28 forceFromWord = 1;\r
29 // Force html mode for incomming paste events sequence.\r
30 editor.once( 'beforePaste', forceHtmlMode );\r
31\r
32 editor.getClipboardData( { title: editor.lang.pastefromword.title }, function( data ) {\r
33 // Do not use editor#paste, because it would start from beforePaste event.\r
34 data && editor.fire( 'paste', {\r
35 type: 'html',\r
36 dataValue: data.dataValue,\r
37 method: 'paste',\r
38 dataTransfer: CKEDITOR.plugins.clipboard.initPasteDataTransfer()\r
39 } );\r
40\r
41 editor.fire( 'afterCommandExec', {\r
42 name: commandName,\r
43 command: cmd,\r
44 returnValue: !!data\r
45 } );\r
46 } );\r
47 }\r
48 } );\r
49\r
50 // Register the toolbar button.\r
51 editor.ui.addButton && editor.ui.addButton( 'PasteFromWord', {\r
52 label: editor.lang.pastefromword.toolbar,\r
53 command: commandName,\r
54 toolbar: 'clipboard,50'\r
55 } );\r
56\r
57 editor.on( 'pasteState', function( evt ) {\r
58 editor.getCommand( commandName ).setState( evt.data );\r
59 } );\r
60\r
61 // Features bring by this command beside the normal process:\r
62 // 1. No more bothering of user about the clean-up.\r
63 // 2. Perform the clean-up even if content is not from MS-Word.\r
64 // (e.g. from a MS-Word similar application.)\r
65 // 3. Listen with high priority (3), so clean up is done before content\r
66 // type sniffing (priority = 6).\r
67 editor.on( 'paste', function( evt ) {\r
68 var data = evt.data,\r
69 mswordHtml = data.dataValue;\r
70\r
71 // MS-WORD format sniffing.\r
72 if ( mswordHtml && ( forceFromWord || ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml ) ) ) {\r
73 // Do not apply paste filter to data filtered by the Word filter (#13093).\r
74 data.dontFilter = true;\r
75\r
76 // If filter rules aren't loaded then cancel 'paste' event,\r
77 // load them and when they'll get loaded fire new paste event\r
78 // for which data will be filtered in second execution of\r
79 // this listener.\r
80 var isLazyLoad = loadFilterRules( editor, path, function() {\r
81 // Event continuation with the original data.\r
82 if ( isLazyLoad )\r
83 editor.fire( 'paste', data );\r
84 else if ( !editor.config.pasteFromWordPromptCleanup || ( forceFromWord || confirm( editor.lang.pastefromword.confirmCleanup ) ) ) // jshint ignore:line\r
85 data.dataValue = CKEDITOR.cleanWord( mswordHtml, editor );\r
86\r
87 // Reset forceFromWord.\r
88 forceFromWord = 0;\r
89 } );\r
90\r
91 // The cleanup rules are to be loaded, we should just cancel\r
92 // this event.\r
93 isLazyLoad && evt.cancel();\r
94 }\r
95 }, null, null, 3 );\r
96 }\r
97\r
98 } );\r
99\r
100 function loadFilterRules( editor, path, callback ) {\r
101 var isLoaded = CKEDITOR.cleanWord;\r
102\r
103 if ( isLoaded )\r
104 callback();\r
105 else {\r
106 var filterFilePath = CKEDITOR.getUrl( editor.config.pasteFromWordCleanupFile || ( path + 'filter/default.js' ) );\r
107\r
108 // Load with busy indicator.\r
109 CKEDITOR.scriptLoader.load( filterFilePath, callback, null, true );\r
110 }\r
111\r
112 return !isLoaded;\r
113 }\r
114\r
115 function forceHtmlMode( evt ) {\r
116 evt.data.type = 'html';\r
117 }\r
118} )();\r
119\r
120\r
121/**\r
122 * Whether to prompt the user about the clean up of content being pasted from MS Word.\r
123 *\r
124 * config.pasteFromWordPromptCleanup = true;\r
125 *\r
126 * @since 3.1\r
127 * @cfg {Boolean} [pasteFromWordPromptCleanup=false]\r
128 * @member CKEDITOR.config\r
129 */\r
130\r
131/**\r
132 * The file that provides the MS Word cleanup function for pasting operations.\r
133 *\r
134 * **Note:** This is a global configuration shared by all editor instances present\r
135 * in the page.\r
136 *\r
137 * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file) using path relative to CKEditor installation folder.\r
138 * CKEDITOR.config.pasteFromWordCleanupFile = 'plugins/pastefromword/filter/custom.js';\r
139 *\r
140 * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file) using full path (including CKEditor installation folder).\r
141 * CKEDITOR.config.pasteFromWordCleanupFile = '/ckeditor/plugins/pastefromword/filter/custom.js';\r
142 *\r
143 * // Load custom.js file from 'customFilerts' folder (located in server's root) using full URL.\r
144 * CKEDITOR.config.pasteFromWordCleanupFile = 'http://my.example.com/customFilerts/custom.js';\r
145 *\r
146 * @since 3.1\r
147 * @cfg {String} [pasteFromWordCleanupFile=<plugin path> + 'filter/default.js']\r
148 * @member CKEDITOR.config\r
149 */\r