99
1010import Plugin from '@ckeditor/ckeditor5-core/src/plugin' ;
1111
12- import { parseHtml } from './filters/parse ' ;
13- import { transformListItemLikeElementsIntoLists } from './filters/list ' ;
14- import { replaceImagesSourceWithBase64 } from './filters/image ' ;
12+ import GoogleDocsNormalizer from './normalizers/googledocsnormalizer ' ;
13+ import MSWordNormalizer from './normalizers/mswordnormalizer ' ;
14+ import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard ' ;
1515
1616/**
1717 * The Paste from Office plugin.
1818 *
19- * This plugin handles content pasted from Office apps (for now only Word) and transforms it (if necessary)
19+ * This plugin handles content pasted from Office apps and transforms it (if necessary)
2020 * to a valid structure which can then be understood by the editor features.
2121 *
22+ * Transformation is made by a set of predefined {@link module:paste-from-office/normalizer~Normalizer normalizers}.
23+ * This plugin includes following normalizers:
24+ * * {@link module:paste-from-office/normalizer/mswordnormalizer~MSWordNormalizer Microsoft Word normalizer}
25+ * * {@link module:paste-from-office/normalizer/googledocsnormalizer~GoogleDocsNormalizer Google Docs normalizer}
26+ *
2227 * For more information about this feature check the {@glink api/paste-from-office package page}.
2328 *
2429 * @extends module:core/plugin~Plugin
@@ -31,49 +36,40 @@ export default class PasteFromOffice extends Plugin {
3136 return 'PasteFromOffice' ;
3237 }
3338
39+ /**
40+ * @inheritDoc
41+ */
42+ static get requires ( ) {
43+ return [ Clipboard ] ;
44+ }
45+
3446 /**
3547 * @inheritDoc
3648 */
3749 init ( ) {
3850 const editor = this . editor ;
51+ const normalizers = [ ] ;
3952
40- this . listenTo ( editor . plugins . get ( 'Clipboard' ) , 'inputTransformation' , ( evt , data ) => {
41- const html = data . dataTransfer . getData ( 'text/html' ) ;
53+ normalizers . push ( new MSWordNormalizer ( ) ) ;
54+ normalizers . push ( new GoogleDocsNormalizer ( ) ) ;
4255
43- if ( data . pasteFromOfficeProcessed !== true && isWordInput ( html ) ) {
44- data . content = this . _normalizeWordInput ( html , data . dataTransfer ) ;
56+ editor . plugins . get ( 'Clipboard' ) . on (
57+ 'inputTransformation' ,
58+ ( evt , data ) => {
59+ if ( data . isTransformedWithPasteFromOffice ) {
60+ return ;
61+ }
4562
46- // Set the flag so if `inputTransformation` is re-fired, PFO will not process it again (#44).
47- data . pasteFromOfficeProcessed = true ;
48- }
49- } , { priority : 'high' } ) ;
50- }
51-
52- /**
53- * Normalizes input pasted from Word to format suitable for editor {@link module:engine/model/model~Model}.
54- *
55- * **Note**: this function was exposed mainly for testing purposes and should not be called directly.
56- *
57- * @protected
58- * @param {String } input Word input.
59- * @param {module:clipboard/datatransfer~DataTransfer } dataTransfer Data transfer instance.
60- * @returns {module:engine/view/documentfragment~DocumentFragment } Normalized input.
61- */
62- _normalizeWordInput ( input , dataTransfer ) {
63- const { body, stylesString } = parseHtml ( input ) ;
63+ const htmlString = data . dataTransfer . getData ( 'text/html' ) ;
64+ const activeNormalizer = normalizers . find ( normalizer => normalizer . isActive ( htmlString ) ) ;
6465
65- transformListItemLikeElementsIntoLists ( body , stylesString ) ;
66- replaceImagesSourceWithBase64 ( body , dataTransfer . getData ( 'text/rtf' ) ) ;
66+ if ( activeNormalizer ) {
67+ activeNormalizer . execute ( data ) ;
6768
68- return body ;
69+ data . isTransformedWithPasteFromOffice = true ;
70+ }
71+ } ,
72+ { priority : 'high' }
73+ ) ;
6974 }
7075}
71-
72- // Checks if given HTML string is a result of pasting content from Word.
73- //
74- // @param {String } html HTML string to test.
75- // @returns {Boolean } True if given HTML string is a Word HTML.
76- function isWordInput ( html ) {
77- return ! ! ( html && ( html . match ( / < m e t a \s * n a m e = " ? g e n e r a t o r " ? \s * c o n t e n t = " ? m i c r o s o f t \s * w o r d \s * \d + " ? \/ ? > / gi ) ||
78- html . match ( / x m l n s : o = " u r n : s c h e m a s - m i c r o s o f t - c o m / gi ) ) ) ;
79- }
0 commit comments