summaryrefslogtreecommitdiff
path: root/plugins/pastefromlibreoffice/plugin.js
blob: 0cfecce50b71152053112215891bae7af5cf7801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
 * CKEditor 4 LTS ("Long Term Support") is available under the terms of the Extended Support Model.
 */

/**
 * @fileOverview This plugin handles pasting content from LibreOffice.
 */

(function() {
	'use strict';

	CKEDITOR.plugins.add('pastefromlibreoffice', {
		requires: 'pastetools',

		isSupportedEnvironment: function() {
			var isSafari = CKEDITOR.env.webkit && !CKEDITOR.env.chrome,
				isIE = CKEDITOR.env.ie && CKEDITOR.env.version <= 11;

			return !isSafari && !isIE;
		},

		init: function(editor) {
			if (!this.isSupportedEnvironment()) {
				return;
			}

			var pasteToolsPath = CKEDITOR.plugins.getPath('pastetools'),
				path = this.path;

			editor.pasteTools.register({
				priority: 100, // after PFW
				filters: [
					CKEDITOR.getUrl(pasteToolsPath + 'filter/common.js'),
					CKEDITOR.getUrl(pasteToolsPath + 'filter/image.js'),
					CKEDITOR.getUrl(path + 'filter/default.js')
				],

				canHandle: function(evt) {
					var data = evt.data,
						textHtml = data.dataTransfer.getData('text/html', true) || data.dataValue,
						generatorName;

					// Do not run the filter if there is no input data.
					if (!textHtml) {
						return false;
					}

					generatorName = CKEDITOR.plugins.pastetools.getContentGeneratorName(textHtml);

					return generatorName === 'libreoffice';
				},

				handle: function(evt, next) {
					var data = evt.data,
						clipboardHtml = data.dataValue || CKEDITOR.plugins.pastetools.getClipboardData(data, 'text/html');

					// Do not apply the paste filter to the data filtered by the LibreOffice filter (https://dev.ckeditor.com/ticket/13093).
					// TO DO it might be unnecessary!!!
					data.dontFilter = true;

					clipboardHtml = CKEDITOR.pasteFilters.image(clipboardHtml, editor, CKEDITOR.plugins.pastetools.getClipboardData(data, 'text/rtf'));

					data.dataValue = CKEDITOR.pasteFilters.libreoffice(clipboardHtml, editor);

					if (editor.config.forcePasteAsPlainText === true) {
						// If `config.forcePasteAsPlainText` is set to `true`, force plain text even on Libre Office content (#1013).
						data.type = 'text';
					}

					next();
				}
			});
		}
	});
})();