UNPKG

3.1 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 isOfficialPlugin() {
38 return true;
39 }
40 /**
41 * @inheritDoc
42 */
43 static get requires() {
44 return [ClipboardPipeline];
45 }
46 /**
47 * @inheritDoc
48 */
49 init() {
50 const editor = this.editor;
51 const clipboardPipeline = editor.plugins.get('ClipboardPipeline');
52 const viewDocument = editor.editing.view.document;
53 const normalizers = [];
54 const hasMultiLevelListPlugin = this.editor.plugins.has('MultiLevelList');
55 normalizers.push(new MSWordNormalizer(viewDocument, hasMultiLevelListPlugin));
56 normalizers.push(new GoogleDocsNormalizer(viewDocument));
57 normalizers.push(new GoogleSheetsNormalizer(viewDocument));
58 clipboardPipeline.on('inputTransformation', (evt, data) => {
59 if (data._isTransformedWithPasteFromOffice) {
60 return;
61 }
62 const codeBlock = editor.model.document.selection.getFirstPosition().parent;
63 if (codeBlock.is('element', 'codeBlock')) {
64 return;
65 }
66 const htmlString = data.dataTransfer.getData('text/html');
67 const activeNormalizer = normalizers.find(normalizer => normalizer.isActive(htmlString));
68 if (activeNormalizer) {
69 if (!data._parsedData) {
70 data._parsedData = parseHtml(htmlString, viewDocument.stylesProcessor);
71 }
72 activeNormalizer.execute(data);
73 data._isTransformedWithPasteFromOffice = true;
74 }
75 }, { priority: 'high' });
76 }
77}