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