1 | /*
|
---|
2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
|
---|
3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
4 | */
|
---|
5 | (function()
|
---|
6 | {
|
---|
7 | CKEDITOR.plugins.add( 'pastefromword',
|
---|
8 | {
|
---|
9 | init : function( editor )
|
---|
10 | {
|
---|
11 |
|
---|
12 | // Flag indicate this command is actually been asked instead of a generic
|
---|
13 | // pasting.
|
---|
14 | var forceFromWord = 0;
|
---|
15 | var resetFromWord = function()
|
---|
16 | {
|
---|
17 | setTimeout( function() { forceFromWord = 0; }, 0 );
|
---|
18 | };
|
---|
19 |
|
---|
20 | // Features bring by this command beside the normal process:
|
---|
21 | // 1. No more bothering of user about the clean-up.
|
---|
22 | // 2. Perform the clean-up even if content is not from MS-Word.
|
---|
23 | // (e.g. from a MS-Word similar application.)
|
---|
24 | editor.addCommand( 'pastefromword',
|
---|
25 | {
|
---|
26 | canUndo : false,
|
---|
27 | exec : function()
|
---|
28 | {
|
---|
29 | forceFromWord = 1;
|
---|
30 | if ( editor.execCommand( 'paste' ) === false )
|
---|
31 | {
|
---|
32 | editor.on( 'dialogHide', function ( evt )
|
---|
33 | {
|
---|
34 | evt.removeListener();
|
---|
35 | resetFromWord();
|
---|
36 | });
|
---|
37 | }
|
---|
38 | }
|
---|
39 | });
|
---|
40 |
|
---|
41 | // Register the toolbar button.
|
---|
42 | editor.ui.addButton( 'PasteFromWord',
|
---|
43 | {
|
---|
44 | label : editor.lang.pastefromword.toolbar,
|
---|
45 | command : 'pastefromword'
|
---|
46 | });
|
---|
47 |
|
---|
48 | editor.on( 'paste', function( evt )
|
---|
49 | {
|
---|
50 | var data = evt.data,
|
---|
51 | mswordHtml;
|
---|
52 |
|
---|
53 | // MS-WORD format sniffing.
|
---|
54 | if ( ( mswordHtml = data[ 'html' ] )
|
---|
55 | && ( forceFromWord || ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml ) ) )
|
---|
56 | {
|
---|
57 | var isLazyLoad = this.loadFilterRules( function()
|
---|
58 | {
|
---|
59 | // Event continuation with the original data.
|
---|
60 | if ( isLazyLoad )
|
---|
61 | editor.fire( 'paste', data );
|
---|
62 | else if ( !editor.config.pasteFromWordPromptCleanup
|
---|
63 | || ( forceFromWord || confirm( editor.lang.pastefromword.confirmCleanup ) ) )
|
---|
64 | {
|
---|
65 | data[ 'html' ] = CKEDITOR.cleanWord( mswordHtml, editor );
|
---|
66 | }
|
---|
67 | });
|
---|
68 |
|
---|
69 | // The cleanup rules are to be loaded, we should just cancel
|
---|
70 | // this event.
|
---|
71 | isLazyLoad && evt.cancel();
|
---|
72 | }
|
---|
73 | }, this );
|
---|
74 | },
|
---|
75 |
|
---|
76 | loadFilterRules : function( callback )
|
---|
77 | {
|
---|
78 |
|
---|
79 | var isLoaded = CKEDITOR.cleanWord;
|
---|
80 |
|
---|
81 | if ( isLoaded )
|
---|
82 | callback();
|
---|
83 | else
|
---|
84 | {
|
---|
85 | var filterFilePath = CKEDITOR.getUrl(
|
---|
86 | CKEDITOR.config.pasteFromWordCleanupFile
|
---|
87 | || ( this.path + 'filter/default.js' ) );
|
---|
88 |
|
---|
89 | // Load with busy indicator.
|
---|
90 | CKEDITOR.scriptLoader.load( filterFilePath, callback, null, false, true );
|
---|
91 | }
|
---|
92 |
|
---|
93 | return !isLoaded;
|
---|
94 | }
|
---|
95 | });
|
---|
96 | })();
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Whether to prompt the user about the clean up of content being pasted from
|
---|
100 | * MS Word.
|
---|
101 | * @name CKEDITOR.config.pasteFromWordPromptCleanup
|
---|
102 | * @since 3.1
|
---|
103 | * @type Boolean
|
---|
104 | * @default undefined
|
---|
105 | * @example
|
---|
106 | * config.pasteFromWordPromptCleanup = true;
|
---|
107 | */
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * The file that provides the MS Word cleanup function for pasting operations.
|
---|
111 | * Note: This is a global configuration shared by all editor instances present
|
---|
112 | * in the page.
|
---|
113 | * @name CKEDITOR.config.pasteFromWordCleanupFile
|
---|
114 | * @since 3.1
|
---|
115 | * @type String
|
---|
116 | * @default 'default'
|
---|
117 | * @example
|
---|
118 | * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file).
|
---|
119 | * CKEDITOR.config.pasteFromWordCleanupFile = 'custom';
|
---|
120 | */
|
---|