UNPKG

2.5 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5
6/**
7 * @module paste-from-office/pastefromoffice
8 */
9
10import { Plugin } from 'ckeditor5/src/core';
11import { ClipboardPipeline } from 'ckeditor5/src/clipboard';
12
13import GoogleDocsNormalizer from './normalizers/googledocsnormalizer';
14import MSWordNormalizer from './normalizers/mswordnormalizer';
15
16import { parseHtml } from './filters/parse';
17
18/**
19 * The Paste from Office plugin.
20 *
21 * This plugin handles content pasted from Office apps and transforms it (if necessary)
22 * to a valid structure which can then be understood by the editor features.
23 *
24 * Transformation is made by a set of predefined {@link module:paste-from-office/normalizer~Normalizer normalizers}.
25 * This plugin includes following normalizers:
26 * * {@link module:paste-from-office/normalizers/mswordnormalizer~MSWordNormalizer Microsoft Word normalizer}
27 * * {@link module:paste-from-office/normalizers/googledocsnormalizer~GoogleDocsNormalizer Google Docs normalizer}
28 *
29 * For more information about this feature check the {@glink api/paste-from-office package page}.
30 *
31 * @extends module:core/plugin~Plugin
32 */
33export default class PasteFromOffice extends Plugin {
34 /**
35 * @inheritDoc
36 */
37 static get pluginName() {
38 return 'PasteFromOffice';
39 }
40
41 /**
42 * @inheritDoc
43 */
44 static get requires() {
45 return [ ClipboardPipeline ];
46 }
47
48 /**
49 * @inheritDoc
50 */
51 init() {
52 const editor = this.editor;
53 const viewDocument = editor.editing.view.document;
54 const normalizers = [];
55
56 normalizers.push( new MSWordNormalizer( viewDocument ) );
57 normalizers.push( new GoogleDocsNormalizer( viewDocument ) );
58
59 editor.plugins.get( 'ClipboardPipeline' ).on(
60 'inputTransformation',
61 ( evt, data ) => {
62 if ( data._isTransformedWithPasteFromOffice ) {
63 return;
64 }
65
66 const codeBlock = editor.model.document.selection.getFirstPosition().parent;
67
68 if ( codeBlock.is( 'element', 'codeBlock' ) ) {
69 return;
70 }
71
72 const htmlString = data.dataTransfer.getData( 'text/html' );
73 const activeNormalizer = normalizers.find( normalizer => normalizer.isActive( htmlString ) );
74
75 if ( activeNormalizer ) {
76 data._parsedData = parseHtml( htmlString, viewDocument.stylesProcessor );
77
78 activeNormalizer.execute( data );
79
80 data._isTransformedWithPasteFromOffice = true;
81 }
82 },
83 { priority: 'high' }
84 );
85 }
86}