1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Plugin } from 'ckeditor5/src/core.js';
|
9 | import { ClipboardPipeline } from 'ckeditor5/src/clipboard.js';
|
10 | import MSWordNormalizer from './normalizers/mswordnormalizer.js';
|
11 | import GoogleDocsNormalizer from './normalizers/googledocsnormalizer.js';
|
12 | import GoogleSheetsNormalizer from './normalizers/googlesheetsnormalizer.js';
|
13 | import { parseHtml } from './filters/parse.js';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export default class PasteFromOffice extends Plugin {
|
28 | |
29 |
|
30 |
|
31 | static get pluginName() {
|
32 | return 'PasteFromOffice';
|
33 | }
|
34 | |
35 |
|
36 |
|
37 | static get isOfficialPlugin() {
|
38 | return true;
|
39 | }
|
40 | |
41 |
|
42 |
|
43 | static get requires() {
|
44 | return [ClipboardPipeline];
|
45 | }
|
46 | |
47 |
|
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 | }
|