@ckeditor/ckeditor5-clipboard 0.0.0-nightly-next-20260105.0 → 0.0.0-nightly-next-20260106.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,287 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module clipboard/clipboardpipeline
7
- */
8
- import { Plugin } from '@ckeditor/ckeditor5-core';
9
- import { EventInfo } from '@ckeditor/ckeditor5-utils';
10
- import { ClipboardObserver } from './clipboardobserver.js';
11
- import { plainTextToHtml } from './utils/plaintexttohtml.js';
12
- import { normalizeClipboardData } from './utils/normalizeclipboarddata.js';
13
- import { viewToPlainText } from './utils/viewtoplaintext.js';
14
- import { ClipboardMarkersUtils } from './clipboardmarkersutils.js';
15
- // Input pipeline events overview:
16
- //
17
- // ┌──────────────────────┐ ┌──────────────────────┐
18
- // │ view.Document │ │ view.Document │
19
- // │ paste │ │ drop │
20
- // └───────────┬──────────┘ └───────────┬──────────┘
21
- // │ │
22
- // └────────────────┌────────────────┘
23
- // │
24
- // ┌─────────V────────┐
25
- // │ view.Document │ Retrieves text/html or text/plain from data.dataTransfer
26
- // │ clipboardInput │ and processes it to view.DocumentFragment.
27
- // └─────────┬────────┘
28
- // │
29
- // ┌───────────V───────────┐
30
- // │ ClipboardPipeline │ Converts view.DocumentFragment to model.DocumentFragment.
31
- // │ inputTransformation │
32
- // └───────────┬───────────┘
33
- // │
34
- // ┌──────────V──────────┐
35
- // │ ClipboardPipeline │ Calls model.insertContent().
36
- // │ contentInsertion │
37
- // └─────────────────────┘
38
- //
39
- //
40
- // Output pipeline events overview:
41
- //
42
- // ┌──────────────────────┐ ┌──────────────────────┐
43
- // │ view.Document │ │ view.Document │ Retrieves the selected model.DocumentFragment
44
- // │ copy │ │ cut │ and fires the `outputTransformation` event.
45
- // └───────────┬──────────┘ └───────────┬──────────┘
46
- // │ │
47
- // └────────────────┌────────────────┘
48
- // │
49
- // ┌───────────V───────────┐
50
- // │ ClipboardPipeline │ Processes model.DocumentFragment and converts it to
51
- // │ outputTransformation │ view.DocumentFragment.
52
- // └───────────┬───────────┘
53
- // │
54
- // ┌─────────V────────┐
55
- // │ view.Document │ Processes view.DocumentFragment to text/html and text/plain
56
- // │ clipboardOutput │ and stores the results in data.dataTransfer.
57
- // └──────────────────┘
58
- //
59
- /**
60
- * The clipboard pipeline feature. It is responsible for intercepting the `paste` and `drop` events and
61
- * passing the pasted content through a series of events in order to insert it into the editor's content.
62
- * It also handles the `cut` and `copy` events to fill the native clipboard with the serialized editor's data.
63
- *
64
- * # Input pipeline
65
- *
66
- * The behavior of the default handlers (all at a `low` priority):
67
- *
68
- * ## Event: `paste` or `drop`
69
- *
70
- * 1. Translates the event data.
71
- * 2. Fires the {@link module:engine/view/document~ViewDocument#event:clipboardInput `view.Document#clipboardInput`} event.
72
- *
73
- * ## Event: `view.Document#clipboardInput`
74
- *
75
- * 1. If the `data.content` event field is already set (by some listener on a higher priority), it takes this content and fires the event
76
- * from the last point.
77
- * 2. Otherwise, it retrieves `text/html` or `text/plain` from `data.dataTransfer`.
78
- * 3. Normalizes the raw data by applying simple filters on string data.
79
- * 4. Processes the raw data to {@link module:engine/view/documentfragment~ViewDocumentFragment `view.DocumentFragment`} with the
80
- * {@link module:engine/controller/datacontroller~DataController#htmlProcessor `DataController#htmlProcessor`}.
81
- * 5. Fires the {@link module:clipboard/clipboardpipeline~ClipboardPipeline#event:inputTransformation
82
- * `ClipboardPipeline#inputTransformation`} event with the view document fragment in the `data.content` event field.
83
- *
84
- * ## Event: `ClipboardPipeline#inputTransformation`
85
- *
86
- * 1. Converts {@link module:engine/view/documentfragment~ViewDocumentFragment `view.DocumentFragment`} from the `data.content` field to
87
- * {@link module:engine/model/documentfragment~ModelDocumentFragment `model.DocumentFragment`}.
88
- * 2. Fires the {@link module:clipboard/clipboardpipeline~ClipboardPipeline#event:contentInsertion `ClipboardPipeline#contentInsertion`}
89
- * event with the model document fragment in the `data.content` event field.
90
- * **Note**: The `ClipboardPipeline#contentInsertion` event is fired within a model change block to allow other handlers
91
- * to run in the same block without post-fixers called in between (i.e., the selection post-fixer).
92
- *
93
- * ## Event: `ClipboardPipeline#contentInsertion`
94
- *
95
- * 1. Calls {@link module:engine/model/model~Model#insertContent `model.insertContent()`} to insert `data.content`
96
- * at the current selection position.
97
- *
98
- * # Output pipeline
99
- *
100
- * The behavior of the default handlers (all at a `low` priority):
101
- *
102
- * ## Event: `copy`, `cut` or `dragstart`
103
- *
104
- * 1. Retrieves the selected {@link module:engine/model/documentfragment~ModelDocumentFragment `model.DocumentFragment`} by calling
105
- * {@link module:engine/model/model~Model#getSelectedContent `model#getSelectedContent()`}.
106
- * 2. Converts the model document fragment to {@link module:engine/view/documentfragment~ViewDocumentFragment `view.DocumentFragment`}.
107
- * 3. Fires the {@link module:engine/view/document~ViewDocument#event:clipboardOutput `view.Document#clipboardOutput`} event
108
- * with the view document fragment in the `data.content` event field.
109
- *
110
- * ## Event: `view.Document#clipboardOutput`
111
- *
112
- * 1. Processes `data.content` to HTML and plain text with the
113
- * {@link module:engine/controller/datacontroller~DataController#htmlProcessor `DataController#htmlProcessor`}.
114
- * 2. Updates the `data.dataTransfer` data for `text/html` and `text/plain` with the processed data.
115
- * 3. For the `cut` method, calls {@link module:engine/model/model~Model#deleteContent `model.deleteContent()`}
116
- * on the current selection.
117
- *
118
- * Read more about the clipboard integration in the {@glink framework/deep-dive/clipboard clipboard deep-dive} guide.
119
- */
120
- export class ClipboardPipeline extends Plugin {
121
- /**
122
- * @inheritDoc
123
- */
124
- static get pluginName() {
125
- return 'ClipboardPipeline';
126
- }
127
- /**
128
- * @inheritDoc
129
- */
130
- static get isOfficialPlugin() {
131
- return true;
132
- }
133
- /**
134
- * @inheritDoc
135
- */
136
- static get requires() {
137
- return [ClipboardMarkersUtils];
138
- }
139
- /**
140
- * @inheritDoc
141
- */
142
- init() {
143
- const editor = this.editor;
144
- const view = editor.editing.view;
145
- view.addObserver(ClipboardObserver);
146
- this._setupPasteDrop();
147
- this._setupCopyCut();
148
- }
149
- /**
150
- * Fires Clipboard `'outputTransformation'` event for given parameters.
151
- *
152
- * @internal
153
- */
154
- _fireOutputTransformationEvent(dataTransfer, selection, method) {
155
- const clipboardMarkersUtils = this.editor.plugins.get('ClipboardMarkersUtils');
156
- this.editor.model.enqueueChange({ isUndoable: method === 'cut' }, () => {
157
- const documentFragment = clipboardMarkersUtils._copySelectedFragmentWithMarkers(method, selection);
158
- this.fire('outputTransformation', {
159
- dataTransfer,
160
- content: documentFragment,
161
- method
162
- });
163
- });
164
- }
165
- /**
166
- * The clipboard paste pipeline.
167
- */
168
- _setupPasteDrop() {
169
- const editor = this.editor;
170
- const model = editor.model;
171
- const view = editor.editing.view;
172
- const viewDocument = view.document;
173
- const clipboardMarkersUtils = this.editor.plugins.get('ClipboardMarkersUtils');
174
- // Pasting is disabled when selection is in non-editable place.
175
- // Dropping is disabled in drag and drop handler.
176
- this.listenTo(viewDocument, 'clipboardInput', (evt, data) => {
177
- if (data.method == 'paste' && !editor.model.canEditAt(editor.model.document.selection)) {
178
- evt.stop();
179
- }
180
- }, { priority: 'highest' });
181
- this.listenTo(viewDocument, 'clipboardInput', (evt, data) => {
182
- const dataTransfer = data.dataTransfer;
183
- let content;
184
- // Some feature could already inject content in the higher priority event handler (i.e., codeBlock).
185
- if (data.content) {
186
- content = data.content;
187
- }
188
- else {
189
- let contentData = '';
190
- if (dataTransfer.getData('text/html')) {
191
- contentData = normalizeClipboardData(dataTransfer.getData('text/html'));
192
- }
193
- else if (dataTransfer.getData('text/plain')) {
194
- contentData = plainTextToHtml(dataTransfer.getData('text/plain'));
195
- }
196
- content = this.editor.data.htmlProcessor.toView(contentData);
197
- }
198
- const eventInfo = new EventInfo(this, 'inputTransformation');
199
- const sourceEditorId = dataTransfer.getData('application/ckeditor5-editor-id') || null;
200
- this.fire(eventInfo, {
201
- content,
202
- dataTransfer,
203
- sourceEditorId,
204
- targetRanges: data.targetRanges,
205
- method: data.method
206
- });
207
- // If CKEditor handled the input, do not bubble the original event any further.
208
- // This helps external integrations recognize this fact and act accordingly.
209
- // https://github.com/ckeditor/ckeditor5-upload/issues/92
210
- if (eventInfo.stop.called) {
211
- evt.stop();
212
- }
213
- view.scrollToTheSelection();
214
- }, { priority: 'low' });
215
- this.listenTo(this, 'inputTransformation', (evt, data) => {
216
- if (data.content.isEmpty) {
217
- return;
218
- }
219
- const dataController = this.editor.data;
220
- // Convert the pasted content into a model document fragment.
221
- // The conversion is contextual, but in this case an "all allowed" context is needed
222
- // and for that we use the $clipboardHolder item.
223
- const modelFragment = dataController.toModel(data.content, '$clipboardHolder');
224
- if (modelFragment.childCount == 0) {
225
- return;
226
- }
227
- evt.stop();
228
- // Fire content insertion event in a single change block to allow other handlers to run in the same block
229
- // without post-fixers called in between (i.e., the selection post-fixer).
230
- model.change(() => {
231
- this.fire('contentInsertion', {
232
- content: modelFragment,
233
- method: data.method,
234
- sourceEditorId: data.sourceEditorId,
235
- dataTransfer: data.dataTransfer,
236
- targetRanges: data.targetRanges
237
- });
238
- });
239
- }, { priority: 'low' });
240
- this.listenTo(this, 'contentInsertion', (evt, data) => {
241
- data.resultRange = clipboardMarkersUtils._pasteFragmentWithMarkers(data.content);
242
- }, { priority: 'low' });
243
- }
244
- /**
245
- * The clipboard copy/cut pipeline.
246
- */
247
- _setupCopyCut() {
248
- const editor = this.editor;
249
- const modelDocument = editor.model.document;
250
- const view = editor.editing.view;
251
- const viewDocument = view.document;
252
- const onCopyCut = (evt, data) => {
253
- const dataTransfer = data.dataTransfer;
254
- data.preventDefault();
255
- this._fireOutputTransformationEvent(dataTransfer, modelDocument.selection, evt.name);
256
- };
257
- this.listenTo(viewDocument, 'copy', onCopyCut, { priority: 'low' });
258
- this.listenTo(viewDocument, 'cut', (evt, data) => {
259
- // Cutting is disabled when selection is in non-editable place.
260
- // See: https://github.com/ckeditor/ckeditor5-clipboard/issues/26.
261
- if (!editor.model.canEditAt(editor.model.document.selection)) {
262
- data.preventDefault();
263
- }
264
- else {
265
- onCopyCut(evt, data);
266
- }
267
- }, { priority: 'low' });
268
- this.listenTo(this, 'outputTransformation', (evt, data) => {
269
- const content = editor.data.toView(data.content, { isClipboardPipeline: true });
270
- viewDocument.fire('clipboardOutput', {
271
- dataTransfer: data.dataTransfer,
272
- content,
273
- method: data.method
274
- });
275
- }, { priority: 'low' });
276
- this.listenTo(viewDocument, 'clipboardOutput', (evt, data) => {
277
- if (!data.content.isEmpty) {
278
- data.dataTransfer.setData('text/html', this.editor.data.htmlProcessor.toData(data.content));
279
- data.dataTransfer.setData('text/plain', viewToPlainText(editor.data.htmlProcessor.domConverter, data.content));
280
- data.dataTransfer.setData('application/ckeditor5-editor-id', this.editor.id);
281
- }
282
- if (data.method == 'cut') {
283
- editor.model.deleteContent(modelDocument.selection);
284
- }
285
- }, { priority: 'low' });
286
- }
287
- }