UNPKG

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