@ckeditor/ckeditor5-clipboard 41.1.0 → 41.3.0-alpha.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.
- package/dist/content-index.css +4 -0
- package/dist/editor-index.css +23 -0
- package/dist/index.css +42 -0
- package/dist/index.css.map +1 -0
- package/dist/index.js +2175 -0
- package/dist/index.js.map +1 -0
- package/dist/types/augmentation.d.ts +16 -0
- package/dist/types/clipboard.d.ts +36 -0
- package/dist/types/clipboardmarkersutils.d.ts +186 -0
- package/dist/types/clipboardobserver.d.ts +312 -0
- package/dist/types/clipboardpipeline.d.ts +265 -0
- package/dist/types/dragdrop.d.ts +102 -0
- package/dist/types/dragdropblocktoolbar.d.ts +47 -0
- package/dist/types/dragdroptarget.d.ts +94 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/lineview.d.ts +45 -0
- package/dist/types/pasteplaintext.d.ts +28 -0
- package/dist/types/utils/normalizeclipboarddata.d.ts +15 -0
- package/dist/types/utils/plaintexttohtml.d.ts +14 -0
- package/dist/types/utils/viewtoplaintext.d.ts +15 -0
- package/lang/contexts.json +5 -0
- package/lang/translations/ar.po +30 -0
- package/lang/translations/bg.po +30 -0
- package/lang/translations/bn.po +30 -0
- package/lang/translations/ca.po +30 -0
- package/lang/translations/cs.po +30 -0
- package/lang/translations/da.po +30 -0
- package/lang/translations/de.po +30 -0
- package/lang/translations/el.po +30 -0
- package/lang/translations/en.po +30 -0
- package/lang/translations/es.po +30 -0
- package/lang/translations/et.po +30 -0
- package/lang/translations/fi.po +30 -0
- package/lang/translations/fr.po +30 -0
- package/lang/translations/he.po +30 -0
- package/lang/translations/hi.po +30 -0
- package/lang/translations/hr.po +30 -0
- package/lang/translations/hu.po +30 -0
- package/lang/translations/id.po +30 -0
- package/lang/translations/it.po +30 -0
- package/lang/translations/ja.po +30 -0
- package/lang/translations/ko.po +30 -0
- package/lang/translations/lt.po +30 -0
- package/lang/translations/lv.po +30 -0
- package/lang/translations/ms.po +30 -0
- package/lang/translations/nl.po +30 -0
- package/lang/translations/no.po +30 -0
- package/lang/translations/pl.po +30 -0
- package/lang/translations/pt-br.po +30 -0
- package/lang/translations/pt.po +30 -0
- package/lang/translations/ro.po +30 -0
- package/lang/translations/ru.po +30 -0
- package/lang/translations/sk.po +30 -0
- package/lang/translations/sr.po +30 -0
- package/lang/translations/sv.po +30 -0
- package/lang/translations/th.po +30 -0
- package/lang/translations/tr.po +30 -0
- package/lang/translations/uk.po +30 -0
- package/lang/translations/vi.po +30 -0
- package/lang/translations/zh-cn.po +30 -0
- package/lang/translations/zh.po +30 -0
- package/package.json +7 -6
- package/src/augmentation.d.ts +2 -1
- package/src/clipboard.d.ts +6 -1
- package/src/clipboard.js +26 -1
- package/src/clipboardmarkersutils.d.ts +186 -0
- package/src/clipboardmarkersutils.js +424 -0
- package/src/clipboardpipeline.d.ts +5 -0
- package/src/clipboardpipeline.js +14 -2
- package/src/index.d.ts +2 -1
- package/src/index.js +1 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,2175 @@
|
|
|
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
|
+
import { Plugin } from '@ckeditor/ckeditor5-core/dist/index.js';
|
|
6
|
+
import { EventInfo, uid, toUnit, delay, DomEmitterMixin, global, Rect, ResizeObserver, env, createElement } from '@ckeditor/ckeditor5-utils/dist/index.js';
|
|
7
|
+
import { DomEventObserver, DataTransfer, Range, MouseObserver, LiveRange } from '@ckeditor/ckeditor5-engine/dist/index.js';
|
|
8
|
+
import { mapValues, throttle } from 'lodash-es';
|
|
9
|
+
import { Widget, isWidget } from '@ckeditor/ckeditor5-widget/dist/index.js';
|
|
10
|
+
import { View } from '@ckeditor/ckeditor5-ui/dist/index.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
14
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @module clipboard/clipboardobserver
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Clipboard events observer.
|
|
21
|
+
*
|
|
22
|
+
* Fires the following events:
|
|
23
|
+
*
|
|
24
|
+
* * {@link module:engine/view/document~Document#event:clipboardInput},
|
|
25
|
+
* * {@link module:engine/view/document~Document#event:paste},
|
|
26
|
+
* * {@link module:engine/view/document~Document#event:copy},
|
|
27
|
+
* * {@link module:engine/view/document~Document#event:cut},
|
|
28
|
+
* * {@link module:engine/view/document~Document#event:drop},
|
|
29
|
+
* * {@link module:engine/view/document~Document#event:dragover},
|
|
30
|
+
* * {@link module:engine/view/document~Document#event:dragging},
|
|
31
|
+
* * {@link module:engine/view/document~Document#event:dragstart},
|
|
32
|
+
* * {@link module:engine/view/document~Document#event:dragend},
|
|
33
|
+
* * {@link module:engine/view/document~Document#event:dragenter},
|
|
34
|
+
* * {@link module:engine/view/document~Document#event:dragleave}.
|
|
35
|
+
*
|
|
36
|
+
* **Note**: This observer is not available by default (ckeditor5-engine does not add it on its own).
|
|
37
|
+
* To make it available, it needs to be added to {@link module:engine/view/document~Document} by using
|
|
38
|
+
* the {@link module:engine/view/view~View#addObserver `View#addObserver()`} method. Alternatively, you can load the
|
|
39
|
+
* {@link module:clipboard/clipboard~Clipboard} plugin which adds this observer automatically (because it uses it).
|
|
40
|
+
*/
|
|
41
|
+
class ClipboardObserver extends DomEventObserver {
|
|
42
|
+
constructor(view) {
|
|
43
|
+
super(view);
|
|
44
|
+
this.domEventType = [
|
|
45
|
+
'paste', 'copy', 'cut', 'drop', 'dragover', 'dragstart', 'dragend', 'dragenter', 'dragleave'
|
|
46
|
+
];
|
|
47
|
+
const viewDocument = this.document;
|
|
48
|
+
this.listenTo(viewDocument, 'paste', handleInput('clipboardInput'), { priority: 'low' });
|
|
49
|
+
this.listenTo(viewDocument, 'drop', handleInput('clipboardInput'), { priority: 'low' });
|
|
50
|
+
this.listenTo(viewDocument, 'dragover', handleInput('dragging'), { priority: 'low' });
|
|
51
|
+
function handleInput(type) {
|
|
52
|
+
return (evt, data) => {
|
|
53
|
+
data.preventDefault();
|
|
54
|
+
const targetRanges = data.dropRange ? [data.dropRange] : null;
|
|
55
|
+
const eventInfo = new EventInfo(viewDocument, type);
|
|
56
|
+
viewDocument.fire(eventInfo, {
|
|
57
|
+
dataTransfer: data.dataTransfer,
|
|
58
|
+
method: evt.name,
|
|
59
|
+
targetRanges,
|
|
60
|
+
target: data.target,
|
|
61
|
+
domEvent: data.domEvent
|
|
62
|
+
});
|
|
63
|
+
// If CKEditor handled the input, do not bubble the original event any further.
|
|
64
|
+
// This helps external integrations recognize that fact and act accordingly.
|
|
65
|
+
// https://github.com/ckeditor/ckeditor5-upload/issues/92
|
|
66
|
+
if (eventInfo.stop.called) {
|
|
67
|
+
data.stopPropagation();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
onDomEvent(domEvent) {
|
|
73
|
+
const nativeDataTransfer = 'clipboardData' in domEvent ? domEvent.clipboardData : domEvent.dataTransfer;
|
|
74
|
+
const cacheFiles = domEvent.type == 'drop' || domEvent.type == 'paste';
|
|
75
|
+
const evtData = {
|
|
76
|
+
dataTransfer: new DataTransfer(nativeDataTransfer, { cacheFiles })
|
|
77
|
+
};
|
|
78
|
+
if (domEvent.type == 'drop' || domEvent.type == 'dragover') {
|
|
79
|
+
evtData.dropRange = getDropViewRange(this.view, domEvent);
|
|
80
|
+
}
|
|
81
|
+
this.fire(domEvent.type, domEvent, evtData);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function getDropViewRange(view, domEvent) {
|
|
85
|
+
const domDoc = domEvent.target.ownerDocument;
|
|
86
|
+
const x = domEvent.clientX;
|
|
87
|
+
const y = domEvent.clientY;
|
|
88
|
+
let domRange;
|
|
89
|
+
// Webkit & Blink.
|
|
90
|
+
if (domDoc.caretRangeFromPoint && domDoc.caretRangeFromPoint(x, y)) {
|
|
91
|
+
domRange = domDoc.caretRangeFromPoint(x, y);
|
|
92
|
+
}
|
|
93
|
+
// FF.
|
|
94
|
+
else if (domEvent.rangeParent) {
|
|
95
|
+
domRange = domDoc.createRange();
|
|
96
|
+
domRange.setStart(domEvent.rangeParent, domEvent.rangeOffset);
|
|
97
|
+
domRange.collapse(true);
|
|
98
|
+
}
|
|
99
|
+
if (domRange) {
|
|
100
|
+
return view.domConverter.domRangeToView(domRange);
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
107
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
108
|
+
*/
|
|
109
|
+
/**
|
|
110
|
+
* @module clipboard/utils/plaintexttohtml
|
|
111
|
+
*/
|
|
112
|
+
/**
|
|
113
|
+
* Converts plain text to its HTML-ized version.
|
|
114
|
+
*
|
|
115
|
+
* @param text The plain text to convert.
|
|
116
|
+
* @returns HTML generated from the plain text.
|
|
117
|
+
*/
|
|
118
|
+
function plainTextToHtml(text) {
|
|
119
|
+
text = text
|
|
120
|
+
// Encode &.
|
|
121
|
+
.replace(/&/g, '&')
|
|
122
|
+
// Encode <>.
|
|
123
|
+
.replace(/</g, '<')
|
|
124
|
+
.replace(/>/g, '>')
|
|
125
|
+
// Creates a paragraph for each double line break.
|
|
126
|
+
.replace(/\r?\n\r?\n/g, '</p><p>')
|
|
127
|
+
// Creates a line break for each single line break.
|
|
128
|
+
.replace(/\r?\n/g, '<br>')
|
|
129
|
+
// Replace tabs with four spaces.
|
|
130
|
+
.replace(/\t/g, ' ')
|
|
131
|
+
// Preserve trailing spaces (only the first and last one – the rest is handled below).
|
|
132
|
+
.replace(/^\s/, ' ')
|
|
133
|
+
.replace(/\s$/, ' ')
|
|
134
|
+
// Preserve other subsequent spaces now.
|
|
135
|
+
.replace(/\s\s/g, ' ');
|
|
136
|
+
if (text.includes('</p><p>') || text.includes('<br>')) {
|
|
137
|
+
// If we created paragraphs above, add the trailing ones.
|
|
138
|
+
text = `<p>${text}</p>`;
|
|
139
|
+
}
|
|
140
|
+
// TODO:
|
|
141
|
+
// * What about '\nfoo' vs ' foo'?
|
|
142
|
+
return text;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
147
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
148
|
+
*/
|
|
149
|
+
/**
|
|
150
|
+
* @module clipboard/utils/normalizeclipboarddata
|
|
151
|
+
*/
|
|
152
|
+
/**
|
|
153
|
+
* Removes some popular browser quirks out of the clipboard data (HTML).
|
|
154
|
+
* Removes all HTML comments. These are considered an internal thing and it makes little sense if they leak into the editor data.
|
|
155
|
+
*
|
|
156
|
+
* @param data The HTML data to normalize.
|
|
157
|
+
* @returns Normalized HTML.
|
|
158
|
+
*/
|
|
159
|
+
function normalizeClipboardData(data) {
|
|
160
|
+
return data
|
|
161
|
+
.replace(/<span(?: class="Apple-converted-space"|)>(\s+)<\/span>/g, (fullMatch, spaces) => {
|
|
162
|
+
// Handle the most popular and problematic case when even a single space becomes an nbsp;.
|
|
163
|
+
// Decode those to normal spaces. Read more in https://github.com/ckeditor/ckeditor5-clipboard/issues/2.
|
|
164
|
+
if (spaces.length == 1) {
|
|
165
|
+
return ' ';
|
|
166
|
+
}
|
|
167
|
+
return spaces;
|
|
168
|
+
})
|
|
169
|
+
// Remove all HTML comments.
|
|
170
|
+
.replace(/<!--[\s\S]*?-->/g, '');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
175
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
176
|
+
*/
|
|
177
|
+
// Elements which should not have empty-line padding.
|
|
178
|
+
// Most `view.ContainerElement` want to be separate by new-line, but some are creating one structure
|
|
179
|
+
// together (like `<li>`) so it is better to separate them by only one "\n".
|
|
180
|
+
const smallPaddingElements = ['figcaption', 'li'];
|
|
181
|
+
const listElements = ['ol', 'ul'];
|
|
182
|
+
/**
|
|
183
|
+
* Converts {@link module:engine/view/item~Item view item} and all of its children to plain text.
|
|
184
|
+
*
|
|
185
|
+
* @param viewItem View item to convert.
|
|
186
|
+
* @returns Plain text representation of `viewItem`.
|
|
187
|
+
*/
|
|
188
|
+
function viewToPlainText(viewItem) {
|
|
189
|
+
if (viewItem.is('$text') || viewItem.is('$textProxy')) {
|
|
190
|
+
return viewItem.data;
|
|
191
|
+
}
|
|
192
|
+
if (viewItem.is('element', 'img') && viewItem.hasAttribute('alt')) {
|
|
193
|
+
return viewItem.getAttribute('alt');
|
|
194
|
+
}
|
|
195
|
+
if (viewItem.is('element', 'br')) {
|
|
196
|
+
return '\n'; // Convert soft breaks to single line break (#8045).
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Item is a document fragment, attribute element or container element. It doesn't
|
|
200
|
+
* have it's own text value, so we need to convert its children elements.
|
|
201
|
+
*/
|
|
202
|
+
let text = '';
|
|
203
|
+
let prev = null;
|
|
204
|
+
for (const child of viewItem.getChildren()) {
|
|
205
|
+
text += newLinePadding(child, prev) + viewToPlainText(child);
|
|
206
|
+
prev = child;
|
|
207
|
+
}
|
|
208
|
+
return text;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Returns new line padding to prefix the given elements with.
|
|
212
|
+
*/
|
|
213
|
+
function newLinePadding(element, previous) {
|
|
214
|
+
if (!previous) {
|
|
215
|
+
// Don't add padding to first elements in a level.
|
|
216
|
+
return '';
|
|
217
|
+
}
|
|
218
|
+
if (element.is('element', 'li') && !element.isEmpty && element.getChild(0).is('containerElement')) {
|
|
219
|
+
// Separate document list items with empty lines.
|
|
220
|
+
return '\n\n';
|
|
221
|
+
}
|
|
222
|
+
if (listElements.includes(element.name) && listElements.includes(previous.name)) {
|
|
223
|
+
/**
|
|
224
|
+
* Because `<ul>` and `<ol>` are AttributeElements, two consecutive lists will not have any padding between
|
|
225
|
+
* them (see the `if` statement below). To fix this, we need to make an exception for this case.
|
|
226
|
+
*/
|
|
227
|
+
return '\n\n';
|
|
228
|
+
}
|
|
229
|
+
if (!element.is('containerElement') && !previous.is('containerElement')) {
|
|
230
|
+
// Don't add padding between non-container elements.
|
|
231
|
+
return '';
|
|
232
|
+
}
|
|
233
|
+
if (smallPaddingElements.includes(element.name) || smallPaddingElements.includes(previous.name)) {
|
|
234
|
+
// Add small padding between selected container elements.
|
|
235
|
+
return '\n';
|
|
236
|
+
}
|
|
237
|
+
// Add empty lines between container elements.
|
|
238
|
+
return '\n\n';
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
243
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
244
|
+
*/
|
|
245
|
+
/**
|
|
246
|
+
* @module clipboard/clipboardmarkersutils
|
|
247
|
+
*/
|
|
248
|
+
/**
|
|
249
|
+
* Part of the clipboard logic. Responsible for collecting markers from selected fragments
|
|
250
|
+
* and restoring them with proper positions in pasted elements.
|
|
251
|
+
*
|
|
252
|
+
* @internal
|
|
253
|
+
*/
|
|
254
|
+
class ClipboardMarkersUtils extends Plugin {
|
|
255
|
+
constructor() {
|
|
256
|
+
super(...arguments);
|
|
257
|
+
/**
|
|
258
|
+
* Map of marker names that can be copied.
|
|
259
|
+
*
|
|
260
|
+
* @internal
|
|
261
|
+
*/
|
|
262
|
+
this._markersToCopy = new Map();
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* @inheritDoc
|
|
266
|
+
*/
|
|
267
|
+
static get pluginName() {
|
|
268
|
+
return 'ClipboardMarkersUtils';
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Registers marker name as copyable in clipboard pipeline.
|
|
272
|
+
*
|
|
273
|
+
* @param markerName Name of marker that can be copied.
|
|
274
|
+
* @param restrictions Preset or specified array of actions that can be performed on specified marker name.
|
|
275
|
+
* @internal
|
|
276
|
+
*/
|
|
277
|
+
_registerMarkerToCopy(markerName, restrictions) {
|
|
278
|
+
const allowedActions = Array.isArray(restrictions) ? restrictions : this._mapRestrictionPresetToActions(restrictions);
|
|
279
|
+
if (allowedActions.length) {
|
|
280
|
+
this._markersToCopy.set(markerName, allowedActions);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Maps preset into array of clipboard operations to be allowed on marker.
|
|
285
|
+
*
|
|
286
|
+
* @param preset Restrictions preset to be mapped to actions
|
|
287
|
+
* @internal
|
|
288
|
+
*/
|
|
289
|
+
_mapRestrictionPresetToActions(preset) {
|
|
290
|
+
switch (preset) {
|
|
291
|
+
case 'always':
|
|
292
|
+
return ['copy', 'cut', 'dragstart'];
|
|
293
|
+
case 'default':
|
|
294
|
+
return ['cut', 'dragstart'];
|
|
295
|
+
case 'never':
|
|
296
|
+
return [];
|
|
297
|
+
default: {
|
|
298
|
+
return [];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Performs copy markers on provided selection and paste it to fragment returned from `getCopiedFragment`.
|
|
304
|
+
*
|
|
305
|
+
* 1. Picks all markers in provided selection.
|
|
306
|
+
* 2. Inserts fake markers to document.
|
|
307
|
+
* 3. Gets copied selection fragment from document.
|
|
308
|
+
* 4. Removes fake elements from fragment and document.
|
|
309
|
+
* 5. Inserts markers in the place of removed fake markers.
|
|
310
|
+
*
|
|
311
|
+
* Due to selection modification, when inserting items, `getCopiedFragment` must *always* operate on `writer.model.document.selection'.
|
|
312
|
+
* Do not use any other custom selection object within callback, as this will lead to out-of-bounds exceptions in rare scenarios.
|
|
313
|
+
*
|
|
314
|
+
* @param action Type of clipboard action.
|
|
315
|
+
* @param writer An instance of the model writer.
|
|
316
|
+
* @param selection Selection to be checked.
|
|
317
|
+
* @param getCopiedFragment Callback that performs copy of selection and returns it as fragment.
|
|
318
|
+
* @internal
|
|
319
|
+
*/
|
|
320
|
+
_copySelectedFragmentWithMarkers(action, selection, getCopiedFragment = writer => writer.model.getSelectedContent(writer.model.document.selection)) {
|
|
321
|
+
return this.editor.model.change(writer => {
|
|
322
|
+
const oldSelection = writer.model.document.selection;
|
|
323
|
+
// In some scenarios, such like in drag & drop, passed `selection` parameter is not actually
|
|
324
|
+
// the same `selection` as the `writer.model.document.selection` which means that `_insertFakeMarkersToSelection`
|
|
325
|
+
// is not affecting passed `selection` `start` and `end` positions but rather modifies `writer.model.document.selection`.
|
|
326
|
+
//
|
|
327
|
+
// It is critical due to fact that when we have selection that starts [ 0, 0 ] and ends at [ 1, 0 ]
|
|
328
|
+
// and after inserting fake marker it will point to such marker instead of new widget position at start: [ 1, 0 ] end: [2, 0 ].
|
|
329
|
+
// `writer.insert` modifies only original `writer.model.document.selection`.
|
|
330
|
+
writer.setSelection(selection);
|
|
331
|
+
const sourceSelectionInsertedMarkers = this._insertFakeMarkersIntoSelection(writer, writer.model.document.selection, action);
|
|
332
|
+
const fragment = getCopiedFragment(writer);
|
|
333
|
+
const fakeMarkersRangesInsideRange = this._removeFakeMarkersInsideElement(writer, fragment);
|
|
334
|
+
// <fake-marker> [Foo] Bar</fake-marker>
|
|
335
|
+
// ^ ^
|
|
336
|
+
// In `_insertFakeMarkersIntoSelection` call we inserted fake marker just before first element.
|
|
337
|
+
// The problem is that the first element can be start position of selection so insertion fake-marker
|
|
338
|
+
// before such element shifts selection (so selection that was at [0, 0] now is at [0, 1]).
|
|
339
|
+
// It means that inserted fake-marker is no longer present inside such selection and is orphaned.
|
|
340
|
+
// This function checks special case of such problem. Markers that are orphaned at the start position
|
|
341
|
+
// and end position in the same time. Basically it means that they overlaps whole element.
|
|
342
|
+
for (const [markerName, elements] of Object.entries(sourceSelectionInsertedMarkers)) {
|
|
343
|
+
fakeMarkersRangesInsideRange[markerName] || (fakeMarkersRangesInsideRange[markerName] = writer.createRangeIn(fragment));
|
|
344
|
+
for (const element of elements) {
|
|
345
|
+
writer.remove(element);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
fragment.markers.clear();
|
|
349
|
+
for (const [markerName, range] of Object.entries(fakeMarkersRangesInsideRange)) {
|
|
350
|
+
fragment.markers.set(markerName, range);
|
|
351
|
+
}
|
|
352
|
+
// Revert back selection to previous one.
|
|
353
|
+
writer.setSelection(oldSelection);
|
|
354
|
+
return fragment;
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Performs paste of markers on already pasted element.
|
|
359
|
+
*
|
|
360
|
+
* 1. Inserts fake markers that are present in fragment element (such fragment will be processed in `getPastedDocumentElement`).
|
|
361
|
+
* 2. Calls `getPastedDocumentElement` and gets element that is inserted into root model.
|
|
362
|
+
* 3. Removes all fake markers present in transformed element.
|
|
363
|
+
* 4. Inserts new markers with removed fake markers ranges into pasted fragment.
|
|
364
|
+
*
|
|
365
|
+
* There are multiple edge cases that have to be considered before calling this function:
|
|
366
|
+
*
|
|
367
|
+
* * `markers` are inserted into the same element that must be later transformed inside `getPastedDocumentElement`.
|
|
368
|
+
* * Fake marker elements inside `getPastedDocumentElement` can be cloned, but their ranges cannot overlap.
|
|
369
|
+
*
|
|
370
|
+
* @param action Type of clipboard action.
|
|
371
|
+
* @param markers Object that maps marker name to corresponding range.
|
|
372
|
+
* @param getPastedDocumentElement Getter used to get target markers element.
|
|
373
|
+
* @internal
|
|
374
|
+
*/
|
|
375
|
+
_pasteMarkersIntoTransformedElement(markers, getPastedDocumentElement) {
|
|
376
|
+
const copyableMarkers = this._getCopyableMarkersFromRangeMap(markers);
|
|
377
|
+
return this.editor.model.change(writer => {
|
|
378
|
+
const sourceFragmentFakeMarkers = this._insertFakeMarkersElements(writer, copyableMarkers);
|
|
379
|
+
const transformedElement = getPastedDocumentElement(writer);
|
|
380
|
+
const removedFakeMarkers = this._removeFakeMarkersInsideElement(writer, transformedElement);
|
|
381
|
+
// Cleanup fake markers inserted into transformed element.
|
|
382
|
+
for (const element of Object.values(sourceFragmentFakeMarkers).flat()) {
|
|
383
|
+
writer.remove(element);
|
|
384
|
+
}
|
|
385
|
+
for (const [markerName, range] of Object.entries(removedFakeMarkers)) {
|
|
386
|
+
const uniqueName = writer.model.markers.has(markerName) ? this._getUniqueMarkerName(markerName) : markerName;
|
|
387
|
+
writer.addMarker(uniqueName, {
|
|
388
|
+
usingOperation: true,
|
|
389
|
+
affectsData: true,
|
|
390
|
+
range
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
return transformedElement;
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* In some situations we have to perform copy on selected fragment with certain markers. This function allows to temporarily bypass
|
|
398
|
+
* restrictions on markers that we want to copy.
|
|
399
|
+
*
|
|
400
|
+
* This function executes `executor()` callback. For the duration of the callback, if the clipboard pipeline is used to copy
|
|
401
|
+
* content, markers with the specified name will be copied to the clipboard as well.
|
|
402
|
+
*
|
|
403
|
+
* @param markerName Which markers should be copied.
|
|
404
|
+
* @param executor Callback executed.
|
|
405
|
+
* @internal
|
|
406
|
+
*/
|
|
407
|
+
_forceMarkersCopy(markerName, executor) {
|
|
408
|
+
const before = this._markersToCopy.get(markerName);
|
|
409
|
+
this._markersToCopy.set(markerName, this._mapRestrictionPresetToActions('always'));
|
|
410
|
+
executor();
|
|
411
|
+
if (before) {
|
|
412
|
+
this._markersToCopy.set(markerName, before);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
this._markersToCopy.delete(markerName);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Checks if marker can be copied.
|
|
420
|
+
*
|
|
421
|
+
* @param markerName Name of checked marker.
|
|
422
|
+
* @param action Type of clipboard action. If null then checks only if marker is registered as copyable.
|
|
423
|
+
* @internal
|
|
424
|
+
*/
|
|
425
|
+
_canPerformMarkerClipboardAction(markerName, action) {
|
|
426
|
+
const [markerNamePrefix] = markerName.split(':');
|
|
427
|
+
if (!action) {
|
|
428
|
+
return this._markersToCopy.has(markerNamePrefix);
|
|
429
|
+
}
|
|
430
|
+
const possibleActions = this._markersToCopy.get(markerNamePrefix) || [];
|
|
431
|
+
return possibleActions.includes(action);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Changes marker names for markers stored in given document fragment so that they are unique.
|
|
435
|
+
*
|
|
436
|
+
* @param fragment
|
|
437
|
+
* @internal
|
|
438
|
+
*/
|
|
439
|
+
_setUniqueMarkerNamesInFragment(fragment) {
|
|
440
|
+
const markers = Array.from(fragment.markers);
|
|
441
|
+
fragment.markers.clear();
|
|
442
|
+
for (const [name, range] of markers) {
|
|
443
|
+
fragment.markers.set(this._getUniqueMarkerName(name), range);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* First step of copying markers. It looks for markers intersecting with given selection and inserts `$marker` elements
|
|
448
|
+
* at positions where document markers start or end. This way `$marker` elements can be easily copied together with
|
|
449
|
+
* the rest of the content of the selection.
|
|
450
|
+
*
|
|
451
|
+
* @param writer An instance of the model writer.
|
|
452
|
+
* @param selection Selection to be checked.
|
|
453
|
+
* @param action Type of clipboard action.
|
|
454
|
+
*/
|
|
455
|
+
_insertFakeMarkersIntoSelection(writer, selection, action) {
|
|
456
|
+
const copyableMarkers = this._getCopyableMarkersFromSelection(writer, selection, action);
|
|
457
|
+
return this._insertFakeMarkersElements(writer, copyableMarkers);
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Returns array of markers that can be copied in specified selection.
|
|
461
|
+
*
|
|
462
|
+
* @param writer An instance of the model writer.
|
|
463
|
+
* @param selection Selection which will be checked.
|
|
464
|
+
* @param action Type of clipboard action. If null then checks only if marker is registered as copyable.
|
|
465
|
+
*/
|
|
466
|
+
_getCopyableMarkersFromSelection(writer, selection, action) {
|
|
467
|
+
return Array
|
|
468
|
+
.from(selection.getRanges())
|
|
469
|
+
.flatMap(selectionRange => Array.from(writer.model.markers.getMarkersIntersectingRange(selectionRange)))
|
|
470
|
+
.filter(marker => this._canPerformMarkerClipboardAction(marker.name, action))
|
|
471
|
+
.map((marker) => ({
|
|
472
|
+
name: marker.name,
|
|
473
|
+
range: marker.getRange()
|
|
474
|
+
}));
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Picks all markers from markers map that can be copied.
|
|
478
|
+
*
|
|
479
|
+
* @param markers Object that maps marker name to corresponding range.
|
|
480
|
+
* @param action Type of clipboard action. If null then checks only if marker is registered as copyable.
|
|
481
|
+
*/
|
|
482
|
+
_getCopyableMarkersFromRangeMap(markers, action = null) {
|
|
483
|
+
const entries = markers instanceof Map ? Array.from(markers.entries()) : Object.entries(markers);
|
|
484
|
+
return entries
|
|
485
|
+
.map(([markerName, range]) => ({
|
|
486
|
+
name: markerName,
|
|
487
|
+
range
|
|
488
|
+
}))
|
|
489
|
+
.filter(marker => this._canPerformMarkerClipboardAction(marker.name, action));
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Inserts specified array of fake markers elements to document and assigns them `type` and `name` attributes.
|
|
493
|
+
* Fake markers elements are used to calculate position of markers on pasted fragment that were transformed during
|
|
494
|
+
* steps between copy and paste.
|
|
495
|
+
*
|
|
496
|
+
* @param writer An instance of the model writer.
|
|
497
|
+
* @param markers Array of markers that will be inserted.
|
|
498
|
+
*/
|
|
499
|
+
_insertFakeMarkersElements(writer, markers) {
|
|
500
|
+
const mappedMarkers = {};
|
|
501
|
+
const sortedMarkers = markers
|
|
502
|
+
.flatMap(marker => {
|
|
503
|
+
const { start, end } = marker.range;
|
|
504
|
+
return [
|
|
505
|
+
{ position: start, marker, type: 'start' },
|
|
506
|
+
{ position: end, marker, type: 'end' }
|
|
507
|
+
];
|
|
508
|
+
})
|
|
509
|
+
// Markers position is sorted backwards to ensure that the insertion of fake markers will not change
|
|
510
|
+
// the position of the next markers.
|
|
511
|
+
.sort(({ position: posA }, { position: posB }) => posA.isBefore(posB) ? 1 : -1);
|
|
512
|
+
for (const { position, marker, type } of sortedMarkers) {
|
|
513
|
+
const fakeMarker = writer.createElement('$marker', {
|
|
514
|
+
'data-name': marker.name,
|
|
515
|
+
'data-type': type
|
|
516
|
+
});
|
|
517
|
+
if (!mappedMarkers[marker.name]) {
|
|
518
|
+
mappedMarkers[marker.name] = [];
|
|
519
|
+
}
|
|
520
|
+
mappedMarkers[marker.name].push(fakeMarker);
|
|
521
|
+
writer.insert(fakeMarker, position);
|
|
522
|
+
}
|
|
523
|
+
return mappedMarkers;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Removes all `$marker` elements from the given document fragment.
|
|
527
|
+
*
|
|
528
|
+
* Returns an object where keys are marker names, and values are ranges corresponding to positions
|
|
529
|
+
* where `$marker` elements were inserted.
|
|
530
|
+
*
|
|
531
|
+
* If the document fragment had only one `$marker` element for given marker (start or end) the other boundary is set automatically
|
|
532
|
+
* (to the end or start of the document fragment, respectively).
|
|
533
|
+
*
|
|
534
|
+
* @param writer An instance of the model writer.
|
|
535
|
+
* @param rootElement The element to be checked.
|
|
536
|
+
*/
|
|
537
|
+
_removeFakeMarkersInsideElement(writer, rootElement) {
|
|
538
|
+
const fakeMarkersElements = this._getAllFakeMarkersFromElement(writer, rootElement);
|
|
539
|
+
const fakeMarkersRanges = fakeMarkersElements.reduce((acc, fakeMarker) => {
|
|
540
|
+
const position = fakeMarker.markerElement && writer.createPositionBefore(fakeMarker.markerElement);
|
|
541
|
+
let prevFakeMarker = acc[fakeMarker.name];
|
|
542
|
+
// Handle scenario when tables clone cells with the same fake node. Example:
|
|
543
|
+
//
|
|
544
|
+
// <cell><fake-marker-a></cell> <cell><fake-marker-a></cell> <cell><fake-marker-a></cell>
|
|
545
|
+
// ^ cloned ^ cloned
|
|
546
|
+
//
|
|
547
|
+
// The easiest way to bypass this issue is to rename already existing in map nodes and
|
|
548
|
+
// set them new unique name.
|
|
549
|
+
if (prevFakeMarker && prevFakeMarker.start && prevFakeMarker.end) {
|
|
550
|
+
acc[this._getUniqueMarkerName(fakeMarker.name)] = acc[fakeMarker.name];
|
|
551
|
+
prevFakeMarker = null;
|
|
552
|
+
}
|
|
553
|
+
acc[fakeMarker.name] = {
|
|
554
|
+
...prevFakeMarker,
|
|
555
|
+
[fakeMarker.type]: position
|
|
556
|
+
};
|
|
557
|
+
if (fakeMarker.markerElement) {
|
|
558
|
+
writer.remove(fakeMarker.markerElement);
|
|
559
|
+
}
|
|
560
|
+
return acc;
|
|
561
|
+
}, {});
|
|
562
|
+
// We cannot construct ranges directly in previous reduce because element ranges can overlap.
|
|
563
|
+
// In other words lets assume we have such scenario:
|
|
564
|
+
// <fake-marker-start /> <paragraph /> <fake-marker-2-start /> <fake-marker-end /> <fake-marker-2-end />
|
|
565
|
+
//
|
|
566
|
+
// We have to remove `fake-marker-start` firstly and then remove `fake-marker-2-start`.
|
|
567
|
+
// Removal of `fake-marker-2-start` affects `fake-marker-end` position so we cannot create
|
|
568
|
+
// connection between `fake-marker-start` and `fake-marker-end` without iterating whole set firstly.
|
|
569
|
+
return mapValues(fakeMarkersRanges, range => new Range(range.start || writer.createPositionFromPath(rootElement, [0]), range.end || writer.createPositionAt(rootElement, 'end')));
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Returns array that contains list of fake markers with corresponding `$marker` elements.
|
|
573
|
+
*
|
|
574
|
+
* For each marker, there can be two `$marker` elements or only one (if the document fragment contained
|
|
575
|
+
* only the beginning or only the end of a marker).
|
|
576
|
+
*
|
|
577
|
+
* @param writer An instance of the model writer.
|
|
578
|
+
* @param rootElement The element to be checked.
|
|
579
|
+
*/
|
|
580
|
+
_getAllFakeMarkersFromElement(writer, rootElement) {
|
|
581
|
+
const foundFakeMarkers = Array
|
|
582
|
+
.from(writer.createRangeIn(rootElement))
|
|
583
|
+
.flatMap(({ item }) => {
|
|
584
|
+
if (!item.is('element', '$marker')) {
|
|
585
|
+
return [];
|
|
586
|
+
}
|
|
587
|
+
const name = item.getAttribute('data-name');
|
|
588
|
+
const type = item.getAttribute('data-type');
|
|
589
|
+
return [
|
|
590
|
+
{
|
|
591
|
+
markerElement: item,
|
|
592
|
+
name,
|
|
593
|
+
type
|
|
594
|
+
}
|
|
595
|
+
];
|
|
596
|
+
});
|
|
597
|
+
const prependFakeMarkers = [];
|
|
598
|
+
const appendFakeMarkers = [];
|
|
599
|
+
for (const fakeMarker of foundFakeMarkers) {
|
|
600
|
+
if (fakeMarker.type === 'end') {
|
|
601
|
+
// <fake-marker> [ phrase</fake-marker> phrase ]
|
|
602
|
+
// ^
|
|
603
|
+
// Handle case when marker is just before start of selection.
|
|
604
|
+
// Only end marker is inside selection.
|
|
605
|
+
const hasMatchingStartMarker = foundFakeMarkers.some(otherFakeMarker => otherFakeMarker.name === fakeMarker.name && otherFakeMarker.type === 'start');
|
|
606
|
+
if (!hasMatchingStartMarker) {
|
|
607
|
+
prependFakeMarkers.push({
|
|
608
|
+
markerElement: null,
|
|
609
|
+
name: fakeMarker.name,
|
|
610
|
+
type: 'start'
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (fakeMarker.type === 'start') {
|
|
615
|
+
// [<fake-marker>phrase]</fake-marker>
|
|
616
|
+
// ^
|
|
617
|
+
// Handle case when fake marker is after selection.
|
|
618
|
+
// Only start marker is inside selection.
|
|
619
|
+
const hasMatchingEndMarker = foundFakeMarkers.some(otherFakeMarker => otherFakeMarker.name === fakeMarker.name && otherFakeMarker.type === 'end');
|
|
620
|
+
if (!hasMatchingEndMarker) {
|
|
621
|
+
appendFakeMarkers.unshift({
|
|
622
|
+
markerElement: null,
|
|
623
|
+
name: fakeMarker.name,
|
|
624
|
+
type: 'end'
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return [
|
|
630
|
+
...prependFakeMarkers,
|
|
631
|
+
...foundFakeMarkers,
|
|
632
|
+
...appendFakeMarkers
|
|
633
|
+
];
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* When copy of markers occurs we have to make sure that pasted markers have different names
|
|
637
|
+
* than source markers. This functions helps with assigning unique part to marker name to
|
|
638
|
+
* prevent duplicated markers error.
|
|
639
|
+
*
|
|
640
|
+
* @param name Name of marker
|
|
641
|
+
*/
|
|
642
|
+
_getUniqueMarkerName(name) {
|
|
643
|
+
const parts = name.split(':');
|
|
644
|
+
const newId = uid().substring(1, 6);
|
|
645
|
+
// It looks like the marker already is UID marker so in this scenario just swap
|
|
646
|
+
// last part of marker name and assign new UID.
|
|
647
|
+
//
|
|
648
|
+
// example: comment:{ threadId }:{ id } => comment:{ threadId }:{ newId }
|
|
649
|
+
if (parts.length === 3) {
|
|
650
|
+
return `${parts.slice(0, 2).join(':')}:${newId}`;
|
|
651
|
+
}
|
|
652
|
+
// Assign new segment to marker name with id.
|
|
653
|
+
//
|
|
654
|
+
// example: comment => comment:{ newId }
|
|
655
|
+
return `${parts.join(':')}:${newId}`;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
661
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
662
|
+
*/
|
|
663
|
+
/**
|
|
664
|
+
* @module clipboard/clipboardpipeline
|
|
665
|
+
*/
|
|
666
|
+
// Input pipeline events overview:
|
|
667
|
+
//
|
|
668
|
+
// ┌──────────────────────┐ ┌──────────────────────┐
|
|
669
|
+
// │ view.Document │ │ view.Document │
|
|
670
|
+
// │ paste │ │ drop │
|
|
671
|
+
// └───────────┬──────────┘ └───────────┬──────────┘
|
|
672
|
+
// │ │
|
|
673
|
+
// └────────────────┌────────────────┘
|
|
674
|
+
// │
|
|
675
|
+
// ┌─────────V────────┐
|
|
676
|
+
// │ view.Document │ Retrieves text/html or text/plain from data.dataTransfer
|
|
677
|
+
// │ clipboardInput │ and processes it to view.DocumentFragment.
|
|
678
|
+
// └─────────┬────────┘
|
|
679
|
+
// │
|
|
680
|
+
// ┌───────────V───────────┐
|
|
681
|
+
// │ ClipboardPipeline │ Converts view.DocumentFragment to model.DocumentFragment.
|
|
682
|
+
// │ inputTransformation │
|
|
683
|
+
// └───────────┬───────────┘
|
|
684
|
+
// │
|
|
685
|
+
// ┌──────────V──────────┐
|
|
686
|
+
// │ ClipboardPipeline │ Calls model.insertContent().
|
|
687
|
+
// │ contentInsertion │
|
|
688
|
+
// └─────────────────────┘
|
|
689
|
+
//
|
|
690
|
+
//
|
|
691
|
+
// Output pipeline events overview:
|
|
692
|
+
//
|
|
693
|
+
// ┌──────────────────────┐ ┌──────────────────────┐
|
|
694
|
+
// │ view.Document │ │ view.Document │ Retrieves the selected model.DocumentFragment
|
|
695
|
+
// │ copy │ │ cut │ and fires the `outputTransformation` event.
|
|
696
|
+
// └───────────┬──────────┘ └───────────┬──────────┘
|
|
697
|
+
// │ │
|
|
698
|
+
// └────────────────┌────────────────┘
|
|
699
|
+
// │
|
|
700
|
+
// ┌───────────V───────────┐
|
|
701
|
+
// │ ClipboardPipeline │ Processes model.DocumentFragment and converts it to
|
|
702
|
+
// │ outputTransformation │ view.DocumentFragment.
|
|
703
|
+
// └───────────┬───────────┘
|
|
704
|
+
// │
|
|
705
|
+
// ┌─────────V────────┐
|
|
706
|
+
// │ view.Document │ Processes view.DocumentFragment to text/html and text/plain
|
|
707
|
+
// │ clipboardOutput │ and stores the results in data.dataTransfer.
|
|
708
|
+
// └──────────────────┘
|
|
709
|
+
//
|
|
710
|
+
/**
|
|
711
|
+
* The clipboard pipeline feature. It is responsible for intercepting the `paste` and `drop` events and
|
|
712
|
+
* passing the pasted content through a series of events in order to insert it into the editor's content.
|
|
713
|
+
* It also handles the `cut` and `copy` events to fill the native clipboard with the serialized editor's data.
|
|
714
|
+
*
|
|
715
|
+
* # Input pipeline
|
|
716
|
+
*
|
|
717
|
+
* The behavior of the default handlers (all at a `low` priority):
|
|
718
|
+
*
|
|
719
|
+
* ## Event: `paste` or `drop`
|
|
720
|
+
*
|
|
721
|
+
* 1. Translates the event data.
|
|
722
|
+
* 2. Fires the {@link module:engine/view/document~Document#event:clipboardInput `view.Document#clipboardInput`} event.
|
|
723
|
+
*
|
|
724
|
+
* ## Event: `view.Document#clipboardInput`
|
|
725
|
+
*
|
|
726
|
+
* 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
|
|
727
|
+
* from the last point.
|
|
728
|
+
* 2. Otherwise, it retrieves `text/html` or `text/plain` from `data.dataTransfer`.
|
|
729
|
+
* 3. Normalizes the raw data by applying simple filters on string data.
|
|
730
|
+
* 4. Processes the raw data to {@link module:engine/view/documentfragment~DocumentFragment `view.DocumentFragment`} with the
|
|
731
|
+
* {@link module:engine/controller/datacontroller~DataController#htmlProcessor `DataController#htmlProcessor`}.
|
|
732
|
+
* 5. Fires the {@link module:clipboard/clipboardpipeline~ClipboardPipeline#event:inputTransformation
|
|
733
|
+
* `ClipboardPipeline#inputTransformation`} event with the view document fragment in the `data.content` event field.
|
|
734
|
+
*
|
|
735
|
+
* ## Event: `ClipboardPipeline#inputTransformation`
|
|
736
|
+
*
|
|
737
|
+
* 1. Converts {@link module:engine/view/documentfragment~DocumentFragment `view.DocumentFragment`} from the `data.content` field to
|
|
738
|
+
* {@link module:engine/model/documentfragment~DocumentFragment `model.DocumentFragment`}.
|
|
739
|
+
* 2. Fires the {@link module:clipboard/clipboardpipeline~ClipboardPipeline#event:contentInsertion `ClipboardPipeline#contentInsertion`}
|
|
740
|
+
* event with the model document fragment in the `data.content` event field.
|
|
741
|
+
* **Note**: The `ClipboardPipeline#contentInsertion` event is fired within a model change block to allow other handlers
|
|
742
|
+
* to run in the same block without post-fixers called in between (i.e., the selection post-fixer).
|
|
743
|
+
*
|
|
744
|
+
* ## Event: `ClipboardPipeline#contentInsertion`
|
|
745
|
+
*
|
|
746
|
+
* 1. Calls {@link module:engine/model/model~Model#insertContent `model.insertContent()`} to insert `data.content`
|
|
747
|
+
* at the current selection position.
|
|
748
|
+
*
|
|
749
|
+
* # Output pipeline
|
|
750
|
+
*
|
|
751
|
+
* The behavior of the default handlers (all at a `low` priority):
|
|
752
|
+
*
|
|
753
|
+
* ## Event: `copy`, `cut` or `dragstart`
|
|
754
|
+
*
|
|
755
|
+
* 1. Retrieves the selected {@link module:engine/model/documentfragment~DocumentFragment `model.DocumentFragment`} by calling
|
|
756
|
+
* {@link module:engine/model/model~Model#getSelectedContent `model#getSelectedContent()`}.
|
|
757
|
+
* 2. Converts the model document fragment to {@link module:engine/view/documentfragment~DocumentFragment `view.DocumentFragment`}.
|
|
758
|
+
* 3. Fires the {@link module:engine/view/document~Document#event:clipboardOutput `view.Document#clipboardOutput`} event
|
|
759
|
+
* with the view document fragment in the `data.content` event field.
|
|
760
|
+
*
|
|
761
|
+
* ## Event: `view.Document#clipboardOutput`
|
|
762
|
+
*
|
|
763
|
+
* 1. Processes `data.content` to HTML and plain text with the
|
|
764
|
+
* {@link module:engine/controller/datacontroller~DataController#htmlProcessor `DataController#htmlProcessor`}.
|
|
765
|
+
* 2. Updates the `data.dataTransfer` data for `text/html` and `text/plain` with the processed data.
|
|
766
|
+
* 3. For the `cut` method, calls {@link module:engine/model/model~Model#deleteContent `model.deleteContent()`}
|
|
767
|
+
* on the current selection.
|
|
768
|
+
*
|
|
769
|
+
* Read more about the clipboard integration in the {@glink framework/deep-dive/clipboard clipboard deep-dive} guide.
|
|
770
|
+
*/
|
|
771
|
+
class ClipboardPipeline extends Plugin {
|
|
772
|
+
/**
|
|
773
|
+
* @inheritDoc
|
|
774
|
+
*/
|
|
775
|
+
static get pluginName() {
|
|
776
|
+
return 'ClipboardPipeline';
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* @inheritDoc
|
|
780
|
+
*/
|
|
781
|
+
static get requires() {
|
|
782
|
+
return [ClipboardMarkersUtils];
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* @inheritDoc
|
|
786
|
+
*/
|
|
787
|
+
init() {
|
|
788
|
+
const editor = this.editor;
|
|
789
|
+
const view = editor.editing.view;
|
|
790
|
+
view.addObserver(ClipboardObserver);
|
|
791
|
+
this._setupPasteDrop();
|
|
792
|
+
this._setupCopyCut();
|
|
793
|
+
}
|
|
794
|
+
/**
|
|
795
|
+
* Fires Clipboard `'outputTransformation'` event for given parameters.
|
|
796
|
+
*
|
|
797
|
+
* @internal
|
|
798
|
+
*/
|
|
799
|
+
_fireOutputTransformationEvent(dataTransfer, selection, method) {
|
|
800
|
+
const clipboardMarkersUtils = this.editor.plugins.get('ClipboardMarkersUtils');
|
|
801
|
+
const documentFragment = clipboardMarkersUtils._copySelectedFragmentWithMarkers(method, selection);
|
|
802
|
+
this.fire('outputTransformation', {
|
|
803
|
+
dataTransfer,
|
|
804
|
+
content: documentFragment,
|
|
805
|
+
method
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* The clipboard paste pipeline.
|
|
810
|
+
*/
|
|
811
|
+
_setupPasteDrop() {
|
|
812
|
+
const editor = this.editor;
|
|
813
|
+
const model = editor.model;
|
|
814
|
+
const view = editor.editing.view;
|
|
815
|
+
const viewDocument = view.document;
|
|
816
|
+
const clipboardMarkersUtils = this.editor.plugins.get('ClipboardMarkersUtils');
|
|
817
|
+
// Pasting is disabled when selection is in non-editable place.
|
|
818
|
+
// Dropping is disabled in drag and drop handler.
|
|
819
|
+
this.listenTo(viewDocument, 'clipboardInput', (evt, data) => {
|
|
820
|
+
if (data.method == 'paste' && !editor.model.canEditAt(editor.model.document.selection)) {
|
|
821
|
+
evt.stop();
|
|
822
|
+
}
|
|
823
|
+
}, { priority: 'highest' });
|
|
824
|
+
this.listenTo(viewDocument, 'clipboardInput', (evt, data) => {
|
|
825
|
+
const dataTransfer = data.dataTransfer;
|
|
826
|
+
let content;
|
|
827
|
+
// Some feature could already inject content in the higher priority event handler (i.e., codeBlock).
|
|
828
|
+
if (data.content) {
|
|
829
|
+
content = data.content;
|
|
830
|
+
}
|
|
831
|
+
else {
|
|
832
|
+
let contentData = '';
|
|
833
|
+
if (dataTransfer.getData('text/html')) {
|
|
834
|
+
contentData = normalizeClipboardData(dataTransfer.getData('text/html'));
|
|
835
|
+
}
|
|
836
|
+
else if (dataTransfer.getData('text/plain')) {
|
|
837
|
+
contentData = plainTextToHtml(dataTransfer.getData('text/plain'));
|
|
838
|
+
}
|
|
839
|
+
content = this.editor.data.htmlProcessor.toView(contentData);
|
|
840
|
+
}
|
|
841
|
+
const eventInfo = new EventInfo(this, 'inputTransformation');
|
|
842
|
+
this.fire(eventInfo, {
|
|
843
|
+
content,
|
|
844
|
+
dataTransfer,
|
|
845
|
+
targetRanges: data.targetRanges,
|
|
846
|
+
method: data.method
|
|
847
|
+
});
|
|
848
|
+
// If CKEditor handled the input, do not bubble the original event any further.
|
|
849
|
+
// This helps external integrations recognize this fact and act accordingly.
|
|
850
|
+
// https://github.com/ckeditor/ckeditor5-upload/issues/92
|
|
851
|
+
if (eventInfo.stop.called) {
|
|
852
|
+
evt.stop();
|
|
853
|
+
}
|
|
854
|
+
view.scrollToTheSelection();
|
|
855
|
+
}, { priority: 'low' });
|
|
856
|
+
this.listenTo(this, 'inputTransformation', (evt, data) => {
|
|
857
|
+
if (data.content.isEmpty) {
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
const dataController = this.editor.data;
|
|
861
|
+
// Convert the pasted content into a model document fragment.
|
|
862
|
+
// The conversion is contextual, but in this case an "all allowed" context is needed
|
|
863
|
+
// and for that we use the $clipboardHolder item.
|
|
864
|
+
const modelFragment = dataController.toModel(data.content, '$clipboardHolder');
|
|
865
|
+
if (modelFragment.childCount == 0) {
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
evt.stop();
|
|
869
|
+
// Fire content insertion event in a single change block to allow other handlers to run in the same block
|
|
870
|
+
// without post-fixers called in between (i.e., the selection post-fixer).
|
|
871
|
+
model.change(() => {
|
|
872
|
+
this.fire('contentInsertion', {
|
|
873
|
+
content: modelFragment,
|
|
874
|
+
method: data.method,
|
|
875
|
+
dataTransfer: data.dataTransfer,
|
|
876
|
+
targetRanges: data.targetRanges
|
|
877
|
+
});
|
|
878
|
+
});
|
|
879
|
+
}, { priority: 'low' });
|
|
880
|
+
this.listenTo(this, 'contentInsertion', (evt, data) => {
|
|
881
|
+
clipboardMarkersUtils._setUniqueMarkerNamesInFragment(data.content);
|
|
882
|
+
}, { priority: 'highest' });
|
|
883
|
+
this.listenTo(this, 'contentInsertion', (evt, data) => {
|
|
884
|
+
data.resultRange = model.insertContent(data.content);
|
|
885
|
+
}, { priority: 'low' });
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* The clipboard copy/cut pipeline.
|
|
889
|
+
*/
|
|
890
|
+
_setupCopyCut() {
|
|
891
|
+
const editor = this.editor;
|
|
892
|
+
const modelDocument = editor.model.document;
|
|
893
|
+
const view = editor.editing.view;
|
|
894
|
+
const viewDocument = view.document;
|
|
895
|
+
const onCopyCut = (evt, data) => {
|
|
896
|
+
const dataTransfer = data.dataTransfer;
|
|
897
|
+
data.preventDefault();
|
|
898
|
+
this._fireOutputTransformationEvent(dataTransfer, modelDocument.selection, evt.name);
|
|
899
|
+
};
|
|
900
|
+
this.listenTo(viewDocument, 'copy', onCopyCut, { priority: 'low' });
|
|
901
|
+
this.listenTo(viewDocument, 'cut', (evt, data) => {
|
|
902
|
+
// Cutting is disabled when selection is in non-editable place.
|
|
903
|
+
// See: https://github.com/ckeditor/ckeditor5-clipboard/issues/26.
|
|
904
|
+
if (!editor.model.canEditAt(editor.model.document.selection)) {
|
|
905
|
+
data.preventDefault();
|
|
906
|
+
}
|
|
907
|
+
else {
|
|
908
|
+
onCopyCut(evt, data);
|
|
909
|
+
}
|
|
910
|
+
}, { priority: 'low' });
|
|
911
|
+
this.listenTo(this, 'outputTransformation', (evt, data) => {
|
|
912
|
+
const content = editor.data.toView(data.content);
|
|
913
|
+
viewDocument.fire('clipboardOutput', {
|
|
914
|
+
dataTransfer: data.dataTransfer,
|
|
915
|
+
content,
|
|
916
|
+
method: data.method
|
|
917
|
+
});
|
|
918
|
+
}, { priority: 'low' });
|
|
919
|
+
this.listenTo(viewDocument, 'clipboardOutput', (evt, data) => {
|
|
920
|
+
if (!data.content.isEmpty) {
|
|
921
|
+
data.dataTransfer.setData('text/html', this.editor.data.htmlProcessor.toData(data.content));
|
|
922
|
+
data.dataTransfer.setData('text/plain', viewToPlainText(data.content));
|
|
923
|
+
}
|
|
924
|
+
if (data.method == 'cut') {
|
|
925
|
+
editor.model.deleteContent(modelDocument.selection);
|
|
926
|
+
}
|
|
927
|
+
}, { priority: 'low' });
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
933
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
934
|
+
*/
|
|
935
|
+
/**
|
|
936
|
+
* @module clipboard/lineview
|
|
937
|
+
*/
|
|
938
|
+
/* istanbul ignore file -- @preserve */
|
|
939
|
+
const toPx = toUnit('px');
|
|
940
|
+
/**
|
|
941
|
+
* The horizontal drop target line view.
|
|
942
|
+
*/
|
|
943
|
+
class LineView extends View {
|
|
944
|
+
/**
|
|
945
|
+
* @inheritDoc
|
|
946
|
+
*/
|
|
947
|
+
constructor() {
|
|
948
|
+
super();
|
|
949
|
+
const bind = this.bindTemplate;
|
|
950
|
+
this.set({
|
|
951
|
+
isVisible: false,
|
|
952
|
+
left: null,
|
|
953
|
+
top: null,
|
|
954
|
+
width: null
|
|
955
|
+
});
|
|
956
|
+
this.setTemplate({
|
|
957
|
+
tag: 'div',
|
|
958
|
+
attributes: {
|
|
959
|
+
class: [
|
|
960
|
+
'ck',
|
|
961
|
+
'ck-clipboard-drop-target-line',
|
|
962
|
+
bind.if('isVisible', 'ck-hidden', value => !value)
|
|
963
|
+
],
|
|
964
|
+
style: {
|
|
965
|
+
left: bind.to('left', left => toPx(left)),
|
|
966
|
+
top: bind.to('top', top => toPx(top)),
|
|
967
|
+
width: bind.to('width', width => toPx(width))
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
976
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
977
|
+
*/
|
|
978
|
+
/**
|
|
979
|
+
* @module clipboard/dragdroptarget
|
|
980
|
+
*/
|
|
981
|
+
/**
|
|
982
|
+
* Part of the Drag and Drop handling. Responsible for finding and displaying the drop target.
|
|
983
|
+
*
|
|
984
|
+
* @internal
|
|
985
|
+
*/
|
|
986
|
+
class DragDropTarget extends Plugin {
|
|
987
|
+
constructor() {
|
|
988
|
+
super(...arguments);
|
|
989
|
+
/**
|
|
990
|
+
* A delayed callback removing the drop marker.
|
|
991
|
+
*
|
|
992
|
+
* @internal
|
|
993
|
+
*/
|
|
994
|
+
this.removeDropMarkerDelayed = delay(() => this.removeDropMarker(), 40);
|
|
995
|
+
/**
|
|
996
|
+
* A throttled callback updating the drop marker.
|
|
997
|
+
*/
|
|
998
|
+
this._updateDropMarkerThrottled = throttle(targetRange => this._updateDropMarker(targetRange), 40);
|
|
999
|
+
/**
|
|
1000
|
+
* A throttled callback reconverting the drop parker.
|
|
1001
|
+
*/
|
|
1002
|
+
this._reconvertMarkerThrottled = throttle(() => {
|
|
1003
|
+
if (this.editor.model.markers.has('drop-target')) {
|
|
1004
|
+
this.editor.editing.reconvertMarker('drop-target');
|
|
1005
|
+
}
|
|
1006
|
+
}, 0);
|
|
1007
|
+
/**
|
|
1008
|
+
* The horizontal drop target line view.
|
|
1009
|
+
*/
|
|
1010
|
+
this._dropTargetLineView = new LineView();
|
|
1011
|
+
/**
|
|
1012
|
+
* DOM Emitter.
|
|
1013
|
+
*/
|
|
1014
|
+
this._domEmitter = new (DomEmitterMixin())();
|
|
1015
|
+
/**
|
|
1016
|
+
* Map of document scrollable elements.
|
|
1017
|
+
*/
|
|
1018
|
+
this._scrollables = new Map();
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* @inheritDoc
|
|
1022
|
+
*/
|
|
1023
|
+
static get pluginName() {
|
|
1024
|
+
return 'DragDropTarget';
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* @inheritDoc
|
|
1028
|
+
*/
|
|
1029
|
+
init() {
|
|
1030
|
+
this._setupDropMarker();
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* @inheritDoc
|
|
1034
|
+
*/
|
|
1035
|
+
destroy() {
|
|
1036
|
+
this._domEmitter.stopListening();
|
|
1037
|
+
for (const { resizeObserver } of this._scrollables.values()) {
|
|
1038
|
+
resizeObserver.destroy();
|
|
1039
|
+
}
|
|
1040
|
+
this._updateDropMarkerThrottled.cancel();
|
|
1041
|
+
this.removeDropMarkerDelayed.cancel();
|
|
1042
|
+
this._reconvertMarkerThrottled.cancel();
|
|
1043
|
+
return super.destroy();
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* Finds the drop target range and updates the drop marker.
|
|
1047
|
+
*
|
|
1048
|
+
* @internal
|
|
1049
|
+
*/
|
|
1050
|
+
updateDropMarker(targetViewElement, targetViewRanges, clientX, clientY, blockMode, draggedRange) {
|
|
1051
|
+
this.removeDropMarkerDelayed.cancel();
|
|
1052
|
+
const targetRange = findDropTargetRange(this.editor, targetViewElement, targetViewRanges, clientX, clientY, blockMode, draggedRange);
|
|
1053
|
+
/* istanbul ignore next -- @preserve */
|
|
1054
|
+
if (!targetRange) {
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
1057
|
+
if (draggedRange && draggedRange.containsRange(targetRange)) {
|
|
1058
|
+
// Target range is inside the dragged range.
|
|
1059
|
+
return this.removeDropMarker();
|
|
1060
|
+
}
|
|
1061
|
+
this._updateDropMarkerThrottled(targetRange);
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Finds the final drop target range.
|
|
1065
|
+
*
|
|
1066
|
+
* @internal
|
|
1067
|
+
*/
|
|
1068
|
+
getFinalDropRange(targetViewElement, targetViewRanges, clientX, clientY, blockMode, draggedRange) {
|
|
1069
|
+
const targetRange = findDropTargetRange(this.editor, targetViewElement, targetViewRanges, clientX, clientY, blockMode, draggedRange);
|
|
1070
|
+
// The dragging markers must be removed after searching for the target range because sometimes
|
|
1071
|
+
// the target lands on the marker itself.
|
|
1072
|
+
this.removeDropMarker();
|
|
1073
|
+
return targetRange;
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Removes the drop target marker.
|
|
1077
|
+
*
|
|
1078
|
+
* @internal
|
|
1079
|
+
*/
|
|
1080
|
+
removeDropMarker() {
|
|
1081
|
+
const model = this.editor.model;
|
|
1082
|
+
this.removeDropMarkerDelayed.cancel();
|
|
1083
|
+
this._updateDropMarkerThrottled.cancel();
|
|
1084
|
+
this._dropTargetLineView.isVisible = false;
|
|
1085
|
+
if (model.markers.has('drop-target')) {
|
|
1086
|
+
model.change(writer => {
|
|
1087
|
+
writer.removeMarker('drop-target');
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* Creates downcast conversion for the drop target marker.
|
|
1093
|
+
*/
|
|
1094
|
+
_setupDropMarker() {
|
|
1095
|
+
const editor = this.editor;
|
|
1096
|
+
editor.ui.view.body.add(this._dropTargetLineView);
|
|
1097
|
+
// Drop marker conversion for hovering over widgets.
|
|
1098
|
+
editor.conversion.for('editingDowncast').markerToHighlight({
|
|
1099
|
+
model: 'drop-target',
|
|
1100
|
+
view: {
|
|
1101
|
+
classes: ['ck-clipboard-drop-target-range']
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1104
|
+
// Drop marker conversion for in text and block drop target.
|
|
1105
|
+
editor.conversion.for('editingDowncast').markerToElement({
|
|
1106
|
+
model: 'drop-target',
|
|
1107
|
+
view: (data, { writer }) => {
|
|
1108
|
+
// Inline drop.
|
|
1109
|
+
if (editor.model.schema.checkChild(data.markerRange.start, '$text')) {
|
|
1110
|
+
this._dropTargetLineView.isVisible = false;
|
|
1111
|
+
return this._createDropTargetPosition(writer);
|
|
1112
|
+
}
|
|
1113
|
+
// Block drop.
|
|
1114
|
+
else {
|
|
1115
|
+
if (data.markerRange.isCollapsed) {
|
|
1116
|
+
this._updateDropTargetLine(data.markerRange);
|
|
1117
|
+
}
|
|
1118
|
+
else {
|
|
1119
|
+
this._dropTargetLineView.isVisible = false;
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Updates the drop target marker to the provided range.
|
|
1127
|
+
*
|
|
1128
|
+
* @param targetRange The range to set the marker to.
|
|
1129
|
+
*/
|
|
1130
|
+
_updateDropMarker(targetRange) {
|
|
1131
|
+
const editor = this.editor;
|
|
1132
|
+
const markers = editor.model.markers;
|
|
1133
|
+
editor.model.change(writer => {
|
|
1134
|
+
if (markers.has('drop-target')) {
|
|
1135
|
+
if (!markers.get('drop-target').getRange().isEqual(targetRange)) {
|
|
1136
|
+
writer.updateMarker('drop-target', { range: targetRange });
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
else {
|
|
1140
|
+
writer.addMarker('drop-target', {
|
|
1141
|
+
range: targetRange,
|
|
1142
|
+
usingOperation: false,
|
|
1143
|
+
affectsData: false
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Creates the UI element for vertical (in-line) drop target.
|
|
1150
|
+
*/
|
|
1151
|
+
_createDropTargetPosition(writer) {
|
|
1152
|
+
return writer.createUIElement('span', { class: 'ck ck-clipboard-drop-target-position' }, function (domDocument) {
|
|
1153
|
+
const domElement = this.toDomElement(domDocument);
|
|
1154
|
+
// Using word joiner to make this marker as high as text and also making text not break on marker.
|
|
1155
|
+
domElement.append('\u2060', domDocument.createElement('span'), '\u2060');
|
|
1156
|
+
return domElement;
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Updates the horizontal drop target line.
|
|
1161
|
+
*/
|
|
1162
|
+
_updateDropTargetLine(range) {
|
|
1163
|
+
const editing = this.editor.editing;
|
|
1164
|
+
const nodeBefore = range.start.nodeBefore;
|
|
1165
|
+
const nodeAfter = range.start.nodeAfter;
|
|
1166
|
+
const nodeParent = range.start.parent;
|
|
1167
|
+
const viewElementBefore = nodeBefore ? editing.mapper.toViewElement(nodeBefore) : null;
|
|
1168
|
+
const domElementBefore = viewElementBefore ? editing.view.domConverter.mapViewToDom(viewElementBefore) : null;
|
|
1169
|
+
const viewElementAfter = nodeAfter ? editing.mapper.toViewElement(nodeAfter) : null;
|
|
1170
|
+
const domElementAfter = viewElementAfter ? editing.view.domConverter.mapViewToDom(viewElementAfter) : null;
|
|
1171
|
+
const viewElementParent = editing.mapper.toViewElement(nodeParent);
|
|
1172
|
+
if (!viewElementParent) {
|
|
1173
|
+
return;
|
|
1174
|
+
}
|
|
1175
|
+
const domElementParent = editing.view.domConverter.mapViewToDom(viewElementParent);
|
|
1176
|
+
const domScrollableRect = this._getScrollableRect(viewElementParent);
|
|
1177
|
+
const { scrollX, scrollY } = global.window;
|
|
1178
|
+
const rectBefore = domElementBefore ? new Rect(domElementBefore) : null;
|
|
1179
|
+
const rectAfter = domElementAfter ? new Rect(domElementAfter) : null;
|
|
1180
|
+
const rectParent = new Rect(domElementParent).excludeScrollbarsAndBorders();
|
|
1181
|
+
const above = rectBefore ? rectBefore.bottom : rectParent.top;
|
|
1182
|
+
const below = rectAfter ? rectAfter.top : rectParent.bottom;
|
|
1183
|
+
const parentStyle = global.window.getComputedStyle(domElementParent);
|
|
1184
|
+
const top = (above <= below ? (above + below) / 2 : below);
|
|
1185
|
+
if (domScrollableRect.top < top && top < domScrollableRect.bottom) {
|
|
1186
|
+
const left = rectParent.left + parseFloat(parentStyle.paddingLeft);
|
|
1187
|
+
const right = rectParent.right - parseFloat(parentStyle.paddingRight);
|
|
1188
|
+
const leftClamped = Math.max(left + scrollX, domScrollableRect.left);
|
|
1189
|
+
const rightClamped = Math.min(right + scrollX, domScrollableRect.right);
|
|
1190
|
+
this._dropTargetLineView.set({
|
|
1191
|
+
isVisible: true,
|
|
1192
|
+
left: leftClamped,
|
|
1193
|
+
top: top + scrollY,
|
|
1194
|
+
width: rightClamped - leftClamped
|
|
1195
|
+
});
|
|
1196
|
+
}
|
|
1197
|
+
else {
|
|
1198
|
+
this._dropTargetLineView.isVisible = false;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* Finds the closest scrollable element rect for the given view element.
|
|
1203
|
+
*/
|
|
1204
|
+
_getScrollableRect(viewElement) {
|
|
1205
|
+
const rootName = viewElement.root.rootName;
|
|
1206
|
+
let domScrollable;
|
|
1207
|
+
if (this._scrollables.has(rootName)) {
|
|
1208
|
+
domScrollable = this._scrollables.get(rootName).domElement;
|
|
1209
|
+
}
|
|
1210
|
+
else {
|
|
1211
|
+
const domElement = this.editor.editing.view.domConverter.mapViewToDom(viewElement);
|
|
1212
|
+
domScrollable = findScrollableElement(domElement);
|
|
1213
|
+
this._domEmitter.listenTo(domScrollable, 'scroll', this._reconvertMarkerThrottled, { usePassive: true });
|
|
1214
|
+
const resizeObserver = new ResizeObserver(domScrollable, this._reconvertMarkerThrottled);
|
|
1215
|
+
this._scrollables.set(rootName, {
|
|
1216
|
+
domElement: domScrollable,
|
|
1217
|
+
resizeObserver
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
return new Rect(domScrollable).excludeScrollbarsAndBorders();
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Returns fixed selection range for given position and target element.
|
|
1225
|
+
*/
|
|
1226
|
+
function findDropTargetRange(editor, targetViewElement, targetViewRanges, clientX, clientY, blockMode, draggedRange) {
|
|
1227
|
+
const model = editor.model;
|
|
1228
|
+
const mapper = editor.editing.mapper;
|
|
1229
|
+
const targetModelElement = getClosestMappedModelElement(editor, targetViewElement);
|
|
1230
|
+
let modelElement = targetModelElement;
|
|
1231
|
+
while (modelElement) {
|
|
1232
|
+
if (!blockMode) {
|
|
1233
|
+
if (model.schema.checkChild(modelElement, '$text')) {
|
|
1234
|
+
if (targetViewRanges) {
|
|
1235
|
+
const targetViewPosition = targetViewRanges[0].start;
|
|
1236
|
+
const targetModelPosition = mapper.toModelPosition(targetViewPosition);
|
|
1237
|
+
const canDropOnPosition = !draggedRange || Array
|
|
1238
|
+
.from(draggedRange.getItems())
|
|
1239
|
+
.every(item => model.schema.checkChild(targetModelPosition, item));
|
|
1240
|
+
if (canDropOnPosition) {
|
|
1241
|
+
if (model.schema.checkChild(targetModelPosition, '$text')) {
|
|
1242
|
+
return model.createRange(targetModelPosition);
|
|
1243
|
+
}
|
|
1244
|
+
else if (targetViewPosition) {
|
|
1245
|
+
// This is the case of dropping inside a span wrapper of an inline image.
|
|
1246
|
+
return findDropTargetRangeForElement(editor, getClosestMappedModelElement(editor, targetViewPosition.parent), clientX, clientY);
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
else if (model.schema.isInline(modelElement)) {
|
|
1252
|
+
return findDropTargetRangeForElement(editor, modelElement, clientX, clientY);
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
if (model.schema.isBlock(modelElement)) {
|
|
1256
|
+
return findDropTargetRangeForElement(editor, modelElement, clientX, clientY);
|
|
1257
|
+
}
|
|
1258
|
+
else if (model.schema.checkChild(modelElement, '$block')) {
|
|
1259
|
+
const childNodes = Array.from(modelElement.getChildren())
|
|
1260
|
+
.filter((node) => node.is('element') && !shouldIgnoreElement(editor, node));
|
|
1261
|
+
let startIndex = 0;
|
|
1262
|
+
let endIndex = childNodes.length;
|
|
1263
|
+
if (endIndex == 0) {
|
|
1264
|
+
return model.createRange(model.createPositionAt(modelElement, 'end'));
|
|
1265
|
+
}
|
|
1266
|
+
while (startIndex < endIndex - 1) {
|
|
1267
|
+
const middleIndex = Math.floor((startIndex + endIndex) / 2);
|
|
1268
|
+
const side = findElementSide(editor, childNodes[middleIndex], clientX, clientY);
|
|
1269
|
+
if (side == 'before') {
|
|
1270
|
+
endIndex = middleIndex;
|
|
1271
|
+
}
|
|
1272
|
+
else {
|
|
1273
|
+
startIndex = middleIndex;
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
return findDropTargetRangeForElement(editor, childNodes[startIndex], clientX, clientY);
|
|
1277
|
+
}
|
|
1278
|
+
modelElement = modelElement.parent;
|
|
1279
|
+
}
|
|
1280
|
+
return null;
|
|
1281
|
+
}
|
|
1282
|
+
/**
|
|
1283
|
+
* Returns true for elements which should be ignored.
|
|
1284
|
+
*/
|
|
1285
|
+
function shouldIgnoreElement(editor, modelElement) {
|
|
1286
|
+
const mapper = editor.editing.mapper;
|
|
1287
|
+
const domConverter = editor.editing.view.domConverter;
|
|
1288
|
+
const viewElement = mapper.toViewElement(modelElement);
|
|
1289
|
+
if (!viewElement) {
|
|
1290
|
+
return true;
|
|
1291
|
+
}
|
|
1292
|
+
const domElement = domConverter.mapViewToDom(viewElement);
|
|
1293
|
+
return global.window.getComputedStyle(domElement).float != 'none';
|
|
1294
|
+
}
|
|
1295
|
+
/**
|
|
1296
|
+
* Returns target range relative to the given element.
|
|
1297
|
+
*/
|
|
1298
|
+
function findDropTargetRangeForElement(editor, modelElement, clientX, clientY) {
|
|
1299
|
+
const model = editor.model;
|
|
1300
|
+
return model.createRange(model.createPositionAt(modelElement, findElementSide(editor, modelElement, clientX, clientY)));
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1303
|
+
* Resolves whether drop marker should be before or after the given element.
|
|
1304
|
+
*/
|
|
1305
|
+
function findElementSide(editor, modelElement, clientX, clientY) {
|
|
1306
|
+
const mapper = editor.editing.mapper;
|
|
1307
|
+
const domConverter = editor.editing.view.domConverter;
|
|
1308
|
+
const viewElement = mapper.toViewElement(modelElement);
|
|
1309
|
+
const domElement = domConverter.mapViewToDom(viewElement);
|
|
1310
|
+
const rect = new Rect(domElement);
|
|
1311
|
+
if (editor.model.schema.isInline(modelElement)) {
|
|
1312
|
+
return clientX < (rect.left + rect.right) / 2 ? 'before' : 'after';
|
|
1313
|
+
}
|
|
1314
|
+
else {
|
|
1315
|
+
return clientY < (rect.top + rect.bottom) / 2 ? 'before' : 'after';
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
/**
|
|
1319
|
+
* Returns the closest model element for the specified view element.
|
|
1320
|
+
*/
|
|
1321
|
+
function getClosestMappedModelElement(editor, element) {
|
|
1322
|
+
const mapper = editor.editing.mapper;
|
|
1323
|
+
const view = editor.editing.view;
|
|
1324
|
+
const targetModelElement = mapper.toModelElement(element);
|
|
1325
|
+
if (targetModelElement) {
|
|
1326
|
+
return targetModelElement;
|
|
1327
|
+
}
|
|
1328
|
+
// Find mapped ancestor if the target is inside not mapped element (for example inline code element).
|
|
1329
|
+
const viewPosition = view.createPositionBefore(element);
|
|
1330
|
+
const viewElement = mapper.findMappedViewAncestor(viewPosition);
|
|
1331
|
+
return mapper.toModelElement(viewElement);
|
|
1332
|
+
}
|
|
1333
|
+
/**
|
|
1334
|
+
* Returns the closest scrollable ancestor DOM element.
|
|
1335
|
+
*
|
|
1336
|
+
* It is assumed that `domNode` is attached to the document.
|
|
1337
|
+
*/
|
|
1338
|
+
function findScrollableElement(domNode) {
|
|
1339
|
+
let domElement = domNode;
|
|
1340
|
+
do {
|
|
1341
|
+
domElement = domElement.parentElement;
|
|
1342
|
+
const overflow = global.window.getComputedStyle(domElement).overflowY;
|
|
1343
|
+
if (overflow == 'auto' || overflow == 'scroll') {
|
|
1344
|
+
break;
|
|
1345
|
+
}
|
|
1346
|
+
} while (domElement.tagName != 'BODY');
|
|
1347
|
+
return domElement;
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
/**
|
|
1351
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
1352
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
1353
|
+
*/
|
|
1354
|
+
/**
|
|
1355
|
+
* @module clipboard/dragdropblocktoolbar
|
|
1356
|
+
*/
|
|
1357
|
+
/**
|
|
1358
|
+
* Integration of a block Drag and Drop support with the block toolbar.
|
|
1359
|
+
*
|
|
1360
|
+
* @internal
|
|
1361
|
+
*/
|
|
1362
|
+
class DragDropBlockToolbar extends Plugin {
|
|
1363
|
+
constructor() {
|
|
1364
|
+
super(...arguments);
|
|
1365
|
+
/**
|
|
1366
|
+
* Whether current dragging is started by block toolbar button dragging.
|
|
1367
|
+
*/
|
|
1368
|
+
this._isBlockDragging = false;
|
|
1369
|
+
/**
|
|
1370
|
+
* DOM Emitter.
|
|
1371
|
+
*/
|
|
1372
|
+
this._domEmitter = new (DomEmitterMixin())();
|
|
1373
|
+
}
|
|
1374
|
+
/**
|
|
1375
|
+
* @inheritDoc
|
|
1376
|
+
*/
|
|
1377
|
+
static get pluginName() {
|
|
1378
|
+
return 'DragDropBlockToolbar';
|
|
1379
|
+
}
|
|
1380
|
+
/**
|
|
1381
|
+
* @inheritDoc
|
|
1382
|
+
*/
|
|
1383
|
+
init() {
|
|
1384
|
+
const editor = this.editor;
|
|
1385
|
+
this.listenTo(editor, 'change:isReadOnly', (evt, name, isReadOnly) => {
|
|
1386
|
+
if (isReadOnly) {
|
|
1387
|
+
this.forceDisabled('readOnlyMode');
|
|
1388
|
+
this._isBlockDragging = false;
|
|
1389
|
+
}
|
|
1390
|
+
else {
|
|
1391
|
+
this.clearForceDisabled('readOnlyMode');
|
|
1392
|
+
}
|
|
1393
|
+
});
|
|
1394
|
+
if (env.isAndroid) {
|
|
1395
|
+
this.forceDisabled('noAndroidSupport');
|
|
1396
|
+
}
|
|
1397
|
+
if (editor.plugins.has('BlockToolbar')) {
|
|
1398
|
+
const blockToolbar = editor.plugins.get('BlockToolbar');
|
|
1399
|
+
const element = blockToolbar.buttonView.element;
|
|
1400
|
+
this._domEmitter.listenTo(element, 'dragstart', (evt, data) => this._handleBlockDragStart(data));
|
|
1401
|
+
this._domEmitter.listenTo(global.document, 'dragover', (evt, data) => this._handleBlockDragging(data));
|
|
1402
|
+
this._domEmitter.listenTo(global.document, 'drop', (evt, data) => this._handleBlockDragging(data));
|
|
1403
|
+
this._domEmitter.listenTo(global.document, 'dragend', () => this._handleBlockDragEnd(), { useCapture: true });
|
|
1404
|
+
if (this.isEnabled) {
|
|
1405
|
+
element.setAttribute('draggable', 'true');
|
|
1406
|
+
}
|
|
1407
|
+
this.on('change:isEnabled', (evt, name, isEnabled) => {
|
|
1408
|
+
element.setAttribute('draggable', isEnabled ? 'true' : 'false');
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* @inheritDoc
|
|
1414
|
+
*/
|
|
1415
|
+
destroy() {
|
|
1416
|
+
this._domEmitter.stopListening();
|
|
1417
|
+
return super.destroy();
|
|
1418
|
+
}
|
|
1419
|
+
/**
|
|
1420
|
+
* The `dragstart` event handler.
|
|
1421
|
+
*/
|
|
1422
|
+
_handleBlockDragStart(domEvent) {
|
|
1423
|
+
if (!this.isEnabled) {
|
|
1424
|
+
return;
|
|
1425
|
+
}
|
|
1426
|
+
const model = this.editor.model;
|
|
1427
|
+
const selection = model.document.selection;
|
|
1428
|
+
const view = this.editor.editing.view;
|
|
1429
|
+
const blocks = Array.from(selection.getSelectedBlocks());
|
|
1430
|
+
const draggedRange = model.createRange(model.createPositionBefore(blocks[0]), model.createPositionAfter(blocks[blocks.length - 1]));
|
|
1431
|
+
model.change(writer => writer.setSelection(draggedRange));
|
|
1432
|
+
this._isBlockDragging = true;
|
|
1433
|
+
view.focus();
|
|
1434
|
+
view.getObserver(ClipboardObserver).onDomEvent(domEvent);
|
|
1435
|
+
}
|
|
1436
|
+
/**
|
|
1437
|
+
* The `dragover` and `drop` event handler.
|
|
1438
|
+
*/
|
|
1439
|
+
_handleBlockDragging(domEvent) {
|
|
1440
|
+
if (!this.isEnabled || !this._isBlockDragging) {
|
|
1441
|
+
return;
|
|
1442
|
+
}
|
|
1443
|
+
const clientX = domEvent.clientX + (this.editor.locale.contentLanguageDirection == 'ltr' ? 100 : -100);
|
|
1444
|
+
const clientY = domEvent.clientY;
|
|
1445
|
+
const target = document.elementFromPoint(clientX, clientY);
|
|
1446
|
+
const view = this.editor.editing.view;
|
|
1447
|
+
if (!target || !target.closest('.ck-editor__editable')) {
|
|
1448
|
+
return;
|
|
1449
|
+
}
|
|
1450
|
+
view.getObserver(ClipboardObserver).onDomEvent({
|
|
1451
|
+
...domEvent,
|
|
1452
|
+
type: domEvent.type,
|
|
1453
|
+
dataTransfer: domEvent.dataTransfer,
|
|
1454
|
+
target,
|
|
1455
|
+
clientX,
|
|
1456
|
+
clientY,
|
|
1457
|
+
preventDefault: () => domEvent.preventDefault(),
|
|
1458
|
+
stopPropagation: () => domEvent.stopPropagation()
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
/**
|
|
1462
|
+
* The `dragend` event handler.
|
|
1463
|
+
*/
|
|
1464
|
+
_handleBlockDragEnd() {
|
|
1465
|
+
this._isBlockDragging = false;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
/**
|
|
1470
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
1471
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
1472
|
+
*/
|
|
1473
|
+
/**
|
|
1474
|
+
* @module clipboard/dragdrop
|
|
1475
|
+
*/
|
|
1476
|
+
// Drag and drop events overview:
|
|
1477
|
+
//
|
|
1478
|
+
// ┌──────────────────┐
|
|
1479
|
+
// │ mousedown │ Sets the draggable attribute.
|
|
1480
|
+
// └─────────┬────────┘
|
|
1481
|
+
// │
|
|
1482
|
+
// └─────────────────────┐
|
|
1483
|
+
// │ │
|
|
1484
|
+
// │ ┌─────────V────────┐
|
|
1485
|
+
// │ │ mouseup │ Dragging did not start, removes the draggable attribute.
|
|
1486
|
+
// │ └──────────────────┘
|
|
1487
|
+
// │
|
|
1488
|
+
// ┌─────────V────────┐ Retrieves the selected model.DocumentFragment
|
|
1489
|
+
// │ dragstart │ and converts it to view.DocumentFragment.
|
|
1490
|
+
// └─────────┬────────┘
|
|
1491
|
+
// │
|
|
1492
|
+
// ┌─────────V────────┐ Processes view.DocumentFragment to text/html and text/plain
|
|
1493
|
+
// │ clipboardOutput │ and stores the results in data.dataTransfer.
|
|
1494
|
+
// └─────────┬────────┘
|
|
1495
|
+
// │
|
|
1496
|
+
// │ DOM dragover
|
|
1497
|
+
// ┌────────────┐
|
|
1498
|
+
// │ │
|
|
1499
|
+
// ┌─────────V────────┐ │
|
|
1500
|
+
// │ dragging │ │ Updates the drop target marker.
|
|
1501
|
+
// └─────────┬────────┘ │
|
|
1502
|
+
// │ │
|
|
1503
|
+
// ┌─────────────└────────────┘
|
|
1504
|
+
// │ │ │
|
|
1505
|
+
// │ ┌─────────V────────┐ │
|
|
1506
|
+
// │ │ dragleave │ │ Removes the drop target marker.
|
|
1507
|
+
// │ └─────────┬────────┘ │
|
|
1508
|
+
// │ │ │
|
|
1509
|
+
// ┌───│─────────────┘ │
|
|
1510
|
+
// │ │ │ │
|
|
1511
|
+
// │ │ ┌─────────V────────┐ │
|
|
1512
|
+
// │ │ │ dragenter │ │ Focuses the editor view.
|
|
1513
|
+
// │ │ └─────────┬────────┘ │
|
|
1514
|
+
// │ │ │ │
|
|
1515
|
+
// │ │ └────────────┘
|
|
1516
|
+
// │ │
|
|
1517
|
+
// │ └─────────────┐
|
|
1518
|
+
// │ │ │
|
|
1519
|
+
// │ │ ┌─────────V────────┐
|
|
1520
|
+
// └───┐ │ drop │ (The default handler of the clipboard pipeline).
|
|
1521
|
+
// │ └─────────┬────────┘
|
|
1522
|
+
// │ │
|
|
1523
|
+
// │ ┌─────────V────────┐ Resolves the final data.targetRanges.
|
|
1524
|
+
// │ │ clipboardInput │ Aborts if dropping on dragged content.
|
|
1525
|
+
// │ └─────────┬────────┘
|
|
1526
|
+
// │ │
|
|
1527
|
+
// │ ┌─────────V────────┐
|
|
1528
|
+
// │ │ clipboardInput │ (The default handler of the clipboard pipeline).
|
|
1529
|
+
// │ └─────────┬────────┘
|
|
1530
|
+
// │ │
|
|
1531
|
+
// │ ┌───────────V───────────┐
|
|
1532
|
+
// │ │ inputTransformation │ (The default handler of the clipboard pipeline).
|
|
1533
|
+
// │ └───────────┬───────────┘
|
|
1534
|
+
// │ │
|
|
1535
|
+
// │ ┌──────────V──────────┐
|
|
1536
|
+
// │ │ contentInsertion │ Updates the document selection to drop range.
|
|
1537
|
+
// │ └──────────┬──────────┘
|
|
1538
|
+
// │ │
|
|
1539
|
+
// │ ┌──────────V──────────┐
|
|
1540
|
+
// │ │ contentInsertion │ (The default handler of the clipboard pipeline).
|
|
1541
|
+
// │ └──────────┬──────────┘
|
|
1542
|
+
// │ │
|
|
1543
|
+
// │ ┌──────────V──────────┐
|
|
1544
|
+
// │ │ contentInsertion │ Removes the content from the original range if the insertion was successful.
|
|
1545
|
+
// │ └──────────┬──────────┘
|
|
1546
|
+
// │ │
|
|
1547
|
+
// └─────────────┐
|
|
1548
|
+
// │
|
|
1549
|
+
// ┌─────────V────────┐
|
|
1550
|
+
// │ dragend │ Removes the drop marker and cleans the state.
|
|
1551
|
+
// └──────────────────┘
|
|
1552
|
+
//
|
|
1553
|
+
/**
|
|
1554
|
+
* The drag and drop feature. It works on top of the {@link module:clipboard/clipboardpipeline~ClipboardPipeline}.
|
|
1555
|
+
*
|
|
1556
|
+
* Read more about the clipboard integration in the {@glink framework/deep-dive/clipboard clipboard deep-dive} guide.
|
|
1557
|
+
*
|
|
1558
|
+
* @internal
|
|
1559
|
+
*/
|
|
1560
|
+
class DragDrop extends Plugin {
|
|
1561
|
+
constructor() {
|
|
1562
|
+
super(...arguments);
|
|
1563
|
+
/**
|
|
1564
|
+
* A delayed callback removing draggable attributes.
|
|
1565
|
+
*/
|
|
1566
|
+
this._clearDraggableAttributesDelayed = delay(() => this._clearDraggableAttributes(), 40);
|
|
1567
|
+
/**
|
|
1568
|
+
* Whether the dragged content can be dropped only in block context.
|
|
1569
|
+
*/
|
|
1570
|
+
// TODO handle drag from other editor instance
|
|
1571
|
+
// TODO configure to use block, inline or both
|
|
1572
|
+
this._blockMode = false;
|
|
1573
|
+
/**
|
|
1574
|
+
* DOM Emitter.
|
|
1575
|
+
*/
|
|
1576
|
+
this._domEmitter = new (DomEmitterMixin())();
|
|
1577
|
+
}
|
|
1578
|
+
/**
|
|
1579
|
+
* @inheritDoc
|
|
1580
|
+
*/
|
|
1581
|
+
static get pluginName() {
|
|
1582
|
+
return 'DragDrop';
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* @inheritDoc
|
|
1586
|
+
*/
|
|
1587
|
+
static get requires() {
|
|
1588
|
+
return [ClipboardPipeline, Widget, DragDropTarget, DragDropBlockToolbar];
|
|
1589
|
+
}
|
|
1590
|
+
/**
|
|
1591
|
+
* @inheritDoc
|
|
1592
|
+
*/
|
|
1593
|
+
init() {
|
|
1594
|
+
const editor = this.editor;
|
|
1595
|
+
const view = editor.editing.view;
|
|
1596
|
+
this._draggedRange = null;
|
|
1597
|
+
this._draggingUid = '';
|
|
1598
|
+
this._draggableElement = null;
|
|
1599
|
+
view.addObserver(ClipboardObserver);
|
|
1600
|
+
view.addObserver(MouseObserver);
|
|
1601
|
+
this._setupDragging();
|
|
1602
|
+
this._setupContentInsertionIntegration();
|
|
1603
|
+
this._setupClipboardInputIntegration();
|
|
1604
|
+
this._setupDraggableAttributeHandling();
|
|
1605
|
+
this.listenTo(editor, 'change:isReadOnly', (evt, name, isReadOnly) => {
|
|
1606
|
+
if (isReadOnly) {
|
|
1607
|
+
this.forceDisabled('readOnlyMode');
|
|
1608
|
+
}
|
|
1609
|
+
else {
|
|
1610
|
+
this.clearForceDisabled('readOnlyMode');
|
|
1611
|
+
}
|
|
1612
|
+
});
|
|
1613
|
+
this.on('change:isEnabled', (evt, name, isEnabled) => {
|
|
1614
|
+
if (!isEnabled) {
|
|
1615
|
+
this._finalizeDragging(false);
|
|
1616
|
+
}
|
|
1617
|
+
});
|
|
1618
|
+
if (env.isAndroid) {
|
|
1619
|
+
this.forceDisabled('noAndroidSupport');
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
/**
|
|
1623
|
+
* @inheritDoc
|
|
1624
|
+
*/
|
|
1625
|
+
destroy() {
|
|
1626
|
+
if (this._draggedRange) {
|
|
1627
|
+
this._draggedRange.detach();
|
|
1628
|
+
this._draggedRange = null;
|
|
1629
|
+
}
|
|
1630
|
+
if (this._previewContainer) {
|
|
1631
|
+
this._previewContainer.remove();
|
|
1632
|
+
}
|
|
1633
|
+
this._domEmitter.stopListening();
|
|
1634
|
+
this._clearDraggableAttributesDelayed.cancel();
|
|
1635
|
+
return super.destroy();
|
|
1636
|
+
}
|
|
1637
|
+
/**
|
|
1638
|
+
* Drag and drop events handling.
|
|
1639
|
+
*/
|
|
1640
|
+
_setupDragging() {
|
|
1641
|
+
const editor = this.editor;
|
|
1642
|
+
const model = editor.model;
|
|
1643
|
+
const view = editor.editing.view;
|
|
1644
|
+
const viewDocument = view.document;
|
|
1645
|
+
const dragDropTarget = editor.plugins.get(DragDropTarget);
|
|
1646
|
+
// The handler for the drag start; it is responsible for setting data transfer object.
|
|
1647
|
+
this.listenTo(viewDocument, 'dragstart', (evt, data) => {
|
|
1648
|
+
// Don't drag the editable element itself.
|
|
1649
|
+
if (data.target && data.target.is('editableElement')) {
|
|
1650
|
+
data.preventDefault();
|
|
1651
|
+
return;
|
|
1652
|
+
}
|
|
1653
|
+
this._prepareDraggedRange(data.target);
|
|
1654
|
+
if (!this._draggedRange) {
|
|
1655
|
+
data.preventDefault();
|
|
1656
|
+
return;
|
|
1657
|
+
}
|
|
1658
|
+
this._draggingUid = uid();
|
|
1659
|
+
data.dataTransfer.effectAllowed = this.isEnabled ? 'copyMove' : 'copy';
|
|
1660
|
+
data.dataTransfer.setData('application/ckeditor5-dragging-uid', this._draggingUid);
|
|
1661
|
+
const draggedSelection = model.createSelection(this._draggedRange.toRange());
|
|
1662
|
+
const clipboardPipeline = this.editor.plugins.get('ClipboardPipeline');
|
|
1663
|
+
clipboardPipeline._fireOutputTransformationEvent(data.dataTransfer, draggedSelection, 'dragstart');
|
|
1664
|
+
const { dataTransfer, domTarget, domEvent } = data;
|
|
1665
|
+
const { clientX } = domEvent;
|
|
1666
|
+
this._updatePreview({ dataTransfer, domTarget, clientX });
|
|
1667
|
+
data.stopPropagation();
|
|
1668
|
+
if (!this.isEnabled) {
|
|
1669
|
+
this._draggedRange.detach();
|
|
1670
|
+
this._draggedRange = null;
|
|
1671
|
+
this._draggingUid = '';
|
|
1672
|
+
}
|
|
1673
|
+
}, { priority: 'low' });
|
|
1674
|
+
// The handler for finalizing drag and drop. It should always be triggered after dragging completes
|
|
1675
|
+
// even if it was completed in a different application.
|
|
1676
|
+
// Note: This is not fired if source text node got removed while downcasting a marker.
|
|
1677
|
+
this.listenTo(viewDocument, 'dragend', (evt, data) => {
|
|
1678
|
+
this._finalizeDragging(!data.dataTransfer.isCanceled && data.dataTransfer.dropEffect == 'move');
|
|
1679
|
+
}, { priority: 'low' });
|
|
1680
|
+
// Reset block dragging mode even if dropped outside the editable.
|
|
1681
|
+
this._domEmitter.listenTo(global.document, 'dragend', () => {
|
|
1682
|
+
this._blockMode = false;
|
|
1683
|
+
}, { useCapture: true });
|
|
1684
|
+
// Dragging over the editable.
|
|
1685
|
+
this.listenTo(viewDocument, 'dragenter', () => {
|
|
1686
|
+
if (!this.isEnabled) {
|
|
1687
|
+
return;
|
|
1688
|
+
}
|
|
1689
|
+
view.focus();
|
|
1690
|
+
});
|
|
1691
|
+
// Dragging out of the editable.
|
|
1692
|
+
this.listenTo(viewDocument, 'dragleave', () => {
|
|
1693
|
+
// We do not know if the mouse left the editor or just some element in it, so let us wait a few milliseconds
|
|
1694
|
+
// to check if 'dragover' is not fired.
|
|
1695
|
+
dragDropTarget.removeDropMarkerDelayed();
|
|
1696
|
+
});
|
|
1697
|
+
// Handler for moving dragged content over the target area.
|
|
1698
|
+
this.listenTo(viewDocument, 'dragging', (evt, data) => {
|
|
1699
|
+
if (!this.isEnabled) {
|
|
1700
|
+
data.dataTransfer.dropEffect = 'none';
|
|
1701
|
+
return;
|
|
1702
|
+
}
|
|
1703
|
+
const { clientX, clientY } = data.domEvent;
|
|
1704
|
+
dragDropTarget.updateDropMarker(data.target, data.targetRanges, clientX, clientY, this._blockMode, this._draggedRange);
|
|
1705
|
+
// If this is content being dragged from another editor, moving out of current editor instance
|
|
1706
|
+
// is not possible until 'dragend' event case will be fixed.
|
|
1707
|
+
if (!this._draggedRange) {
|
|
1708
|
+
data.dataTransfer.dropEffect = 'copy';
|
|
1709
|
+
}
|
|
1710
|
+
// In Firefox it is already set and effect allowed remains the same as originally set.
|
|
1711
|
+
if (!env.isGecko) {
|
|
1712
|
+
if (data.dataTransfer.effectAllowed == 'copy') {
|
|
1713
|
+
data.dataTransfer.dropEffect = 'copy';
|
|
1714
|
+
}
|
|
1715
|
+
else if (['all', 'copyMove'].includes(data.dataTransfer.effectAllowed)) {
|
|
1716
|
+
data.dataTransfer.dropEffect = 'move';
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
evt.stop();
|
|
1720
|
+
}, { priority: 'low' });
|
|
1721
|
+
}
|
|
1722
|
+
/**
|
|
1723
|
+
* Integration with the `clipboardInput` event.
|
|
1724
|
+
*/
|
|
1725
|
+
_setupClipboardInputIntegration() {
|
|
1726
|
+
const editor = this.editor;
|
|
1727
|
+
const view = editor.editing.view;
|
|
1728
|
+
const viewDocument = view.document;
|
|
1729
|
+
const dragDropTarget = editor.plugins.get(DragDropTarget);
|
|
1730
|
+
// Update the event target ranges and abort dropping if dropping over itself.
|
|
1731
|
+
this.listenTo(viewDocument, 'clipboardInput', (evt, data) => {
|
|
1732
|
+
if (data.method != 'drop') {
|
|
1733
|
+
return;
|
|
1734
|
+
}
|
|
1735
|
+
const { clientX, clientY } = data.domEvent;
|
|
1736
|
+
const targetRange = dragDropTarget.getFinalDropRange(data.target, data.targetRanges, clientX, clientY, this._blockMode, this._draggedRange);
|
|
1737
|
+
if (!targetRange) {
|
|
1738
|
+
this._finalizeDragging(false);
|
|
1739
|
+
evt.stop();
|
|
1740
|
+
return;
|
|
1741
|
+
}
|
|
1742
|
+
// Since we cannot rely on the drag end event, we must check if the local drag range is from the current drag and drop
|
|
1743
|
+
// or it is from some previous not cleared one.
|
|
1744
|
+
if (this._draggedRange && this._draggingUid != data.dataTransfer.getData('application/ckeditor5-dragging-uid')) {
|
|
1745
|
+
this._draggedRange.detach();
|
|
1746
|
+
this._draggedRange = null;
|
|
1747
|
+
this._draggingUid = '';
|
|
1748
|
+
}
|
|
1749
|
+
// Do not do anything if some content was dragged within the same document to the same position.
|
|
1750
|
+
const isMove = getFinalDropEffect(data.dataTransfer) == 'move';
|
|
1751
|
+
if (isMove && this._draggedRange && this._draggedRange.containsRange(targetRange, true)) {
|
|
1752
|
+
this._finalizeDragging(false);
|
|
1753
|
+
evt.stop();
|
|
1754
|
+
return;
|
|
1755
|
+
}
|
|
1756
|
+
// Override the target ranges with the one adjusted to the best one for a drop.
|
|
1757
|
+
data.targetRanges = [editor.editing.mapper.toViewRange(targetRange)];
|
|
1758
|
+
}, { priority: 'high' });
|
|
1759
|
+
}
|
|
1760
|
+
/**
|
|
1761
|
+
* Integration with the `contentInsertion` event of the clipboard pipeline.
|
|
1762
|
+
*/
|
|
1763
|
+
_setupContentInsertionIntegration() {
|
|
1764
|
+
const clipboardPipeline = this.editor.plugins.get(ClipboardPipeline);
|
|
1765
|
+
clipboardPipeline.on('contentInsertion', (evt, data) => {
|
|
1766
|
+
if (!this.isEnabled || data.method !== 'drop') {
|
|
1767
|
+
return;
|
|
1768
|
+
}
|
|
1769
|
+
// Update the selection to the target range in the same change block to avoid selection post-fixing
|
|
1770
|
+
// and to be able to clone text attributes for plain text dropping.
|
|
1771
|
+
const ranges = data.targetRanges.map(viewRange => this.editor.editing.mapper.toModelRange(viewRange));
|
|
1772
|
+
this.editor.model.change(writer => writer.setSelection(ranges));
|
|
1773
|
+
}, { priority: 'high' });
|
|
1774
|
+
clipboardPipeline.on('contentInsertion', (evt, data) => {
|
|
1775
|
+
if (!this.isEnabled || data.method !== 'drop') {
|
|
1776
|
+
return;
|
|
1777
|
+
}
|
|
1778
|
+
// Remove dragged range content, remove markers, clean after dragging.
|
|
1779
|
+
const isMove = getFinalDropEffect(data.dataTransfer) == 'move';
|
|
1780
|
+
// Whether any content was inserted (insertion might fail if the schema is disallowing some elements
|
|
1781
|
+
// (for example an image caption allows only the content of a block but not blocks themselves.
|
|
1782
|
+
// Some integrations might not return valid range (i.e., table pasting).
|
|
1783
|
+
const isSuccess = !data.resultRange || !data.resultRange.isCollapsed;
|
|
1784
|
+
this._finalizeDragging(isSuccess && isMove);
|
|
1785
|
+
}, { priority: 'lowest' });
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* Adds listeners that add the `draggable` attribute to the elements while the mouse button is down so the dragging could start.
|
|
1789
|
+
*/
|
|
1790
|
+
_setupDraggableAttributeHandling() {
|
|
1791
|
+
const editor = this.editor;
|
|
1792
|
+
const view = editor.editing.view;
|
|
1793
|
+
const viewDocument = view.document;
|
|
1794
|
+
// Add the 'draggable' attribute to the widget while pressing the selection handle.
|
|
1795
|
+
// This is required for widgets to be draggable. In Chrome it will enable dragging text nodes.
|
|
1796
|
+
this.listenTo(viewDocument, 'mousedown', (evt, data) => {
|
|
1797
|
+
// The lack of data can be caused by editor tests firing fake mouse events. This should not occur
|
|
1798
|
+
// in real-life scenarios but this greatly simplifies editor tests that would otherwise fail a lot.
|
|
1799
|
+
if (env.isAndroid || !data) {
|
|
1800
|
+
return;
|
|
1801
|
+
}
|
|
1802
|
+
this._clearDraggableAttributesDelayed.cancel();
|
|
1803
|
+
// Check if this is a mousedown over the widget (but not a nested editable).
|
|
1804
|
+
let draggableElement = findDraggableWidget(data.target);
|
|
1805
|
+
// Note: There is a limitation that if more than a widget is selected (a widget and some text)
|
|
1806
|
+
// and dragging starts on the widget, then only the widget is dragged.
|
|
1807
|
+
// If this was not a widget then we should check if we need to drag some text content.
|
|
1808
|
+
// In Chrome set a 'draggable' attribute on closest editable to allow immediate dragging of the selected text range.
|
|
1809
|
+
// In Firefox this is not needed. In Safari it makes the whole editable draggable (not just textual content).
|
|
1810
|
+
// Disabled in read-only mode because draggable="true" + contenteditable="false" results
|
|
1811
|
+
// in not firing selectionchange event ever, which makes the selection stuck in read-only mode.
|
|
1812
|
+
if (env.isBlink && !editor.isReadOnly && !draggableElement && !viewDocument.selection.isCollapsed) {
|
|
1813
|
+
const selectedElement = viewDocument.selection.getSelectedElement();
|
|
1814
|
+
if (!selectedElement || !isWidget(selectedElement)) {
|
|
1815
|
+
draggableElement = viewDocument.selection.editableElement;
|
|
1816
|
+
}
|
|
1817
|
+
}
|
|
1818
|
+
if (draggableElement) {
|
|
1819
|
+
view.change(writer => {
|
|
1820
|
+
writer.setAttribute('draggable', 'true', draggableElement);
|
|
1821
|
+
});
|
|
1822
|
+
// Keep the reference to the model element in case the view element gets removed while dragging.
|
|
1823
|
+
this._draggableElement = editor.editing.mapper.toModelElement(draggableElement);
|
|
1824
|
+
}
|
|
1825
|
+
});
|
|
1826
|
+
// Remove the draggable attribute in case no dragging started (only mousedown + mouseup).
|
|
1827
|
+
this.listenTo(viewDocument, 'mouseup', () => {
|
|
1828
|
+
if (!env.isAndroid) {
|
|
1829
|
+
this._clearDraggableAttributesDelayed();
|
|
1830
|
+
}
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Removes the `draggable` attribute from the element that was used for dragging.
|
|
1835
|
+
*/
|
|
1836
|
+
_clearDraggableAttributes() {
|
|
1837
|
+
const editing = this.editor.editing;
|
|
1838
|
+
editing.view.change(writer => {
|
|
1839
|
+
// Remove 'draggable' attribute.
|
|
1840
|
+
if (this._draggableElement && this._draggableElement.root.rootName != '$graveyard') {
|
|
1841
|
+
writer.removeAttribute('draggable', editing.mapper.toViewElement(this._draggableElement));
|
|
1842
|
+
}
|
|
1843
|
+
this._draggableElement = null;
|
|
1844
|
+
});
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Deletes the dragged content from its original range and clears the dragging state.
|
|
1848
|
+
*
|
|
1849
|
+
* @param moved Whether the move succeeded.
|
|
1850
|
+
*/
|
|
1851
|
+
_finalizeDragging(moved) {
|
|
1852
|
+
const editor = this.editor;
|
|
1853
|
+
const model = editor.model;
|
|
1854
|
+
const dragDropTarget = editor.plugins.get(DragDropTarget);
|
|
1855
|
+
dragDropTarget.removeDropMarker();
|
|
1856
|
+
this._clearDraggableAttributes();
|
|
1857
|
+
if (editor.plugins.has('WidgetToolbarRepository')) {
|
|
1858
|
+
const widgetToolbarRepository = editor.plugins.get('WidgetToolbarRepository');
|
|
1859
|
+
widgetToolbarRepository.clearForceDisabled('dragDrop');
|
|
1860
|
+
}
|
|
1861
|
+
this._draggingUid = '';
|
|
1862
|
+
if (this._previewContainer) {
|
|
1863
|
+
this._previewContainer.remove();
|
|
1864
|
+
this._previewContainer = undefined;
|
|
1865
|
+
}
|
|
1866
|
+
if (!this._draggedRange) {
|
|
1867
|
+
return;
|
|
1868
|
+
}
|
|
1869
|
+
// Delete moved content.
|
|
1870
|
+
if (moved && this.isEnabled) {
|
|
1871
|
+
model.change(writer => {
|
|
1872
|
+
const selection = model.createSelection(this._draggedRange);
|
|
1873
|
+
model.deleteContent(selection, { doNotAutoparagraph: true });
|
|
1874
|
+
// Check result selection if it does not require auto-paragraphing of empty container.
|
|
1875
|
+
const selectionParent = selection.getFirstPosition().parent;
|
|
1876
|
+
if (selectionParent.isEmpty &&
|
|
1877
|
+
!model.schema.checkChild(selectionParent, '$text') &&
|
|
1878
|
+
model.schema.checkChild(selectionParent, 'paragraph')) {
|
|
1879
|
+
writer.insertElement('paragraph', selectionParent, 0);
|
|
1880
|
+
}
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
this._draggedRange.detach();
|
|
1884
|
+
this._draggedRange = null;
|
|
1885
|
+
}
|
|
1886
|
+
/**
|
|
1887
|
+
* Sets the dragged source range based on event target and document selection.
|
|
1888
|
+
*/
|
|
1889
|
+
_prepareDraggedRange(target) {
|
|
1890
|
+
const editor = this.editor;
|
|
1891
|
+
const model = editor.model;
|
|
1892
|
+
const selection = model.document.selection;
|
|
1893
|
+
// Check if this is dragstart over the widget (but not a nested editable).
|
|
1894
|
+
const draggableWidget = target ? findDraggableWidget(target) : null;
|
|
1895
|
+
if (draggableWidget) {
|
|
1896
|
+
const modelElement = editor.editing.mapper.toModelElement(draggableWidget);
|
|
1897
|
+
this._draggedRange = LiveRange.fromRange(model.createRangeOn(modelElement));
|
|
1898
|
+
this._blockMode = model.schema.isBlock(modelElement);
|
|
1899
|
+
// Disable toolbars so they won't obscure the drop area.
|
|
1900
|
+
if (editor.plugins.has('WidgetToolbarRepository')) {
|
|
1901
|
+
const widgetToolbarRepository = editor.plugins.get('WidgetToolbarRepository');
|
|
1902
|
+
widgetToolbarRepository.forceDisabled('dragDrop');
|
|
1903
|
+
}
|
|
1904
|
+
return;
|
|
1905
|
+
}
|
|
1906
|
+
// If this was not a widget we should check if we need to drag some text content.
|
|
1907
|
+
if (selection.isCollapsed && !selection.getFirstPosition().parent.isEmpty) {
|
|
1908
|
+
return;
|
|
1909
|
+
}
|
|
1910
|
+
const blocks = Array.from(selection.getSelectedBlocks());
|
|
1911
|
+
const draggedRange = selection.getFirstRange();
|
|
1912
|
+
if (blocks.length == 0) {
|
|
1913
|
+
this._draggedRange = LiveRange.fromRange(draggedRange);
|
|
1914
|
+
return;
|
|
1915
|
+
}
|
|
1916
|
+
const blockRange = getRangeIncludingFullySelectedParents(model, blocks);
|
|
1917
|
+
if (blocks.length > 1) {
|
|
1918
|
+
this._draggedRange = LiveRange.fromRange(blockRange);
|
|
1919
|
+
this._blockMode = true;
|
|
1920
|
+
// TODO block mode for dragging from outside editor? or inline? or both?
|
|
1921
|
+
}
|
|
1922
|
+
else if (blocks.length == 1) {
|
|
1923
|
+
const touchesBlockEdges = draggedRange.start.isTouching(blockRange.start) &&
|
|
1924
|
+
draggedRange.end.isTouching(blockRange.end);
|
|
1925
|
+
this._draggedRange = LiveRange.fromRange(touchesBlockEdges ? blockRange : draggedRange);
|
|
1926
|
+
this._blockMode = touchesBlockEdges;
|
|
1927
|
+
}
|
|
1928
|
+
model.change(writer => writer.setSelection(this._draggedRange.toRange()));
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* Updates the dragged preview image.
|
|
1932
|
+
*/
|
|
1933
|
+
_updatePreview({ dataTransfer, domTarget, clientX }) {
|
|
1934
|
+
const view = this.editor.editing.view;
|
|
1935
|
+
const editable = view.document.selection.editableElement;
|
|
1936
|
+
const domEditable = view.domConverter.mapViewToDom(editable);
|
|
1937
|
+
const computedStyle = global.window.getComputedStyle(domEditable);
|
|
1938
|
+
if (!this._previewContainer) {
|
|
1939
|
+
this._previewContainer = createElement(global.document, 'div', {
|
|
1940
|
+
style: 'position: fixed; left: -999999px;'
|
|
1941
|
+
});
|
|
1942
|
+
global.document.body.appendChild(this._previewContainer);
|
|
1943
|
+
}
|
|
1944
|
+
else if (this._previewContainer.firstElementChild) {
|
|
1945
|
+
this._previewContainer.removeChild(this._previewContainer.firstElementChild);
|
|
1946
|
+
}
|
|
1947
|
+
const domRect = new Rect(domEditable);
|
|
1948
|
+
// If domTarget is inside the editable root, browsers will display the preview correctly by themselves.
|
|
1949
|
+
if (domEditable.contains(domTarget)) {
|
|
1950
|
+
return;
|
|
1951
|
+
}
|
|
1952
|
+
const domEditablePaddingLeft = parseFloat(computedStyle.paddingLeft);
|
|
1953
|
+
const preview = createElement(global.document, 'div');
|
|
1954
|
+
preview.className = 'ck ck-content';
|
|
1955
|
+
preview.style.width = computedStyle.width;
|
|
1956
|
+
preview.style.paddingLeft = `${domRect.left - clientX + domEditablePaddingLeft}px`;
|
|
1957
|
+
/**
|
|
1958
|
+
* Set white background in drag and drop preview if iOS.
|
|
1959
|
+
* Check: https://github.com/ckeditor/ckeditor5/issues/15085
|
|
1960
|
+
*/
|
|
1961
|
+
if (env.isiOS) {
|
|
1962
|
+
preview.style.backgroundColor = 'white';
|
|
1963
|
+
}
|
|
1964
|
+
preview.innerHTML = dataTransfer.getData('text/html');
|
|
1965
|
+
dataTransfer.setDragImage(preview, 0, 0);
|
|
1966
|
+
this._previewContainer.appendChild(preview);
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
/**
|
|
1970
|
+
* Returns the drop effect that should be a result of dragging the content.
|
|
1971
|
+
* This function is handling a quirk when checking the effect in the 'drop' DOM event.
|
|
1972
|
+
*/
|
|
1973
|
+
function getFinalDropEffect(dataTransfer) {
|
|
1974
|
+
if (env.isGecko) {
|
|
1975
|
+
return dataTransfer.dropEffect;
|
|
1976
|
+
}
|
|
1977
|
+
return ['all', 'copyMove'].includes(dataTransfer.effectAllowed) ? 'move' : 'copy';
|
|
1978
|
+
}
|
|
1979
|
+
/**
|
|
1980
|
+
* Returns a widget element that should be dragged.
|
|
1981
|
+
*/
|
|
1982
|
+
function findDraggableWidget(target) {
|
|
1983
|
+
// This is directly an editable so not a widget for sure.
|
|
1984
|
+
if (target.is('editableElement')) {
|
|
1985
|
+
return null;
|
|
1986
|
+
}
|
|
1987
|
+
// TODO: Let's have a isWidgetSelectionHandleDomElement() helper in ckeditor5-widget utils.
|
|
1988
|
+
if (target.hasClass('ck-widget__selection-handle')) {
|
|
1989
|
+
return target.findAncestor(isWidget);
|
|
1990
|
+
}
|
|
1991
|
+
// Direct hit on a widget.
|
|
1992
|
+
if (isWidget(target)) {
|
|
1993
|
+
return target;
|
|
1994
|
+
}
|
|
1995
|
+
// Find closest ancestor that is either a widget or an editable element...
|
|
1996
|
+
const ancestor = target.findAncestor(node => isWidget(node) || node.is('editableElement'));
|
|
1997
|
+
// ...and if closer was the widget then enable dragging it.
|
|
1998
|
+
if (isWidget(ancestor)) {
|
|
1999
|
+
return ancestor;
|
|
2000
|
+
}
|
|
2001
|
+
return null;
|
|
2002
|
+
}
|
|
2003
|
+
/**
|
|
2004
|
+
* Recursively checks if common parent of provided elements doesn't have any other children. If that's the case,
|
|
2005
|
+
* it returns range including this parent. Otherwise, it returns only the range from first to last element.
|
|
2006
|
+
*
|
|
2007
|
+
* Example:
|
|
2008
|
+
*
|
|
2009
|
+
* <blockQuote>
|
|
2010
|
+
* <paragraph>[Test 1</paragraph>
|
|
2011
|
+
* <paragraph>Test 2</paragraph>
|
|
2012
|
+
* <paragraph>Test 3]</paragraph>
|
|
2013
|
+
* <blockQuote>
|
|
2014
|
+
*
|
|
2015
|
+
* Because all elements inside the `blockQuote` are selected, the range is extended to include the `blockQuote` too.
|
|
2016
|
+
* If only first and second paragraphs would be selected, the range would not include it.
|
|
2017
|
+
*/
|
|
2018
|
+
function getRangeIncludingFullySelectedParents(model, elements) {
|
|
2019
|
+
const firstElement = elements[0];
|
|
2020
|
+
const lastElement = elements[elements.length - 1];
|
|
2021
|
+
const parent = firstElement.getCommonAncestor(lastElement);
|
|
2022
|
+
const startPosition = model.createPositionBefore(firstElement);
|
|
2023
|
+
const endPosition = model.createPositionAfter(lastElement);
|
|
2024
|
+
if (parent &&
|
|
2025
|
+
parent.is('element') &&
|
|
2026
|
+
!model.schema.isLimit(parent)) {
|
|
2027
|
+
const parentRange = model.createRangeOn(parent);
|
|
2028
|
+
const touchesStart = startPosition.isTouching(parentRange.start);
|
|
2029
|
+
const touchesEnd = endPosition.isTouching(parentRange.end);
|
|
2030
|
+
if (touchesStart && touchesEnd) {
|
|
2031
|
+
// Selection includes all elements in the parent.
|
|
2032
|
+
return getRangeIncludingFullySelectedParents(model, [parent]);
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
return model.createRange(startPosition, endPosition);
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
/**
|
|
2039
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
2040
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
2041
|
+
*/
|
|
2042
|
+
/**
|
|
2043
|
+
* @module clipboard/pasteplaintext
|
|
2044
|
+
*/
|
|
2045
|
+
/**
|
|
2046
|
+
* The plugin detects the user's intention to paste plain text.
|
|
2047
|
+
*
|
|
2048
|
+
* For example, it detects the <kbd>Ctrl/Cmd</kbd> + <kbd>Shift</kbd> + <kbd>V</kbd> keystroke.
|
|
2049
|
+
*/
|
|
2050
|
+
class PastePlainText extends Plugin {
|
|
2051
|
+
/**
|
|
2052
|
+
* @inheritDoc
|
|
2053
|
+
*/
|
|
2054
|
+
static get pluginName() {
|
|
2055
|
+
return 'PastePlainText';
|
|
2056
|
+
}
|
|
2057
|
+
/**
|
|
2058
|
+
* @inheritDoc
|
|
2059
|
+
*/
|
|
2060
|
+
static get requires() {
|
|
2061
|
+
return [ClipboardPipeline];
|
|
2062
|
+
}
|
|
2063
|
+
/**
|
|
2064
|
+
* @inheritDoc
|
|
2065
|
+
*/
|
|
2066
|
+
init() {
|
|
2067
|
+
const editor = this.editor;
|
|
2068
|
+
const model = editor.model;
|
|
2069
|
+
const view = editor.editing.view;
|
|
2070
|
+
const viewDocument = view.document;
|
|
2071
|
+
const selection = model.document.selection;
|
|
2072
|
+
let shiftPressed = false;
|
|
2073
|
+
view.addObserver(ClipboardObserver);
|
|
2074
|
+
this.listenTo(viewDocument, 'keydown', (evt, data) => {
|
|
2075
|
+
shiftPressed = data.shiftKey;
|
|
2076
|
+
});
|
|
2077
|
+
editor.plugins.get(ClipboardPipeline).on('contentInsertion', (evt, data) => {
|
|
2078
|
+
// Plain text can be determined based on the event flag (#7799) or auto-detection (#1006). If detected,
|
|
2079
|
+
// preserve selection attributes on pasted items.
|
|
2080
|
+
if (!shiftPressed && !isPlainTextFragment(data.content, model.schema)) {
|
|
2081
|
+
return;
|
|
2082
|
+
}
|
|
2083
|
+
model.change(writer => {
|
|
2084
|
+
// Formatting attributes should be preserved.
|
|
2085
|
+
const textAttributes = Array.from(selection.getAttributes())
|
|
2086
|
+
.filter(([key]) => model.schema.getAttributeProperties(key).isFormatting);
|
|
2087
|
+
if (!selection.isCollapsed) {
|
|
2088
|
+
model.deleteContent(selection, { doNotAutoparagraph: true });
|
|
2089
|
+
}
|
|
2090
|
+
// Also preserve other attributes if they survived the content deletion (because they were not fully selected).
|
|
2091
|
+
// For example linkHref is not a formatting attribute but it should be preserved if pasted text was in the middle
|
|
2092
|
+
// of a link.
|
|
2093
|
+
textAttributes.push(...selection.getAttributes());
|
|
2094
|
+
const range = writer.createRangeIn(data.content);
|
|
2095
|
+
for (const item of range.getItems()) {
|
|
2096
|
+
if (item.is('$textProxy')) {
|
|
2097
|
+
writer.setAttributes(textAttributes, item);
|
|
2098
|
+
}
|
|
2099
|
+
}
|
|
2100
|
+
});
|
|
2101
|
+
});
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
/**
|
|
2105
|
+
* Returns true if specified `documentFragment` represents a plain text.
|
|
2106
|
+
*/
|
|
2107
|
+
function isPlainTextFragment(documentFragment, schema) {
|
|
2108
|
+
if (documentFragment.childCount > 1) {
|
|
2109
|
+
return false;
|
|
2110
|
+
}
|
|
2111
|
+
const child = documentFragment.getChild(0);
|
|
2112
|
+
if (schema.isObject(child)) {
|
|
2113
|
+
return false;
|
|
2114
|
+
}
|
|
2115
|
+
return Array.from(child.getAttributeKeys()).length == 0;
|
|
2116
|
+
}
|
|
2117
|
+
|
|
2118
|
+
/**
|
|
2119
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
2120
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
2121
|
+
*/
|
|
2122
|
+
/**
|
|
2123
|
+
* @module clipboard/clipboard
|
|
2124
|
+
*/
|
|
2125
|
+
/**
|
|
2126
|
+
* The clipboard feature.
|
|
2127
|
+
*
|
|
2128
|
+
* Read more about the clipboard integration in the {@glink framework/deep-dive/clipboard clipboard deep-dive} guide.
|
|
2129
|
+
*
|
|
2130
|
+
* This is a "glue" plugin which loads the following plugins:
|
|
2131
|
+
* * {@link module:clipboard/clipboardpipeline~ClipboardPipeline}
|
|
2132
|
+
* * {@link module:clipboard/dragdrop~DragDrop}
|
|
2133
|
+
* * {@link module:clipboard/pasteplaintext~PastePlainText}
|
|
2134
|
+
*/
|
|
2135
|
+
class Clipboard extends Plugin {
|
|
2136
|
+
/**
|
|
2137
|
+
* @inheritDoc
|
|
2138
|
+
*/
|
|
2139
|
+
static get pluginName() {
|
|
2140
|
+
return 'Clipboard';
|
|
2141
|
+
}
|
|
2142
|
+
/**
|
|
2143
|
+
* @inheritDoc
|
|
2144
|
+
*/
|
|
2145
|
+
static get requires() {
|
|
2146
|
+
return [ClipboardMarkersUtils, ClipboardPipeline, DragDrop, PastePlainText];
|
|
2147
|
+
}
|
|
2148
|
+
/**
|
|
2149
|
+
* @inheritDoc
|
|
2150
|
+
*/
|
|
2151
|
+
init() {
|
|
2152
|
+
const editor = this.editor;
|
|
2153
|
+
const t = this.editor.t;
|
|
2154
|
+
// Add the information about the keystrokes to the accessibility database.
|
|
2155
|
+
editor.accessibility.addKeystrokeInfos({
|
|
2156
|
+
keystrokes: [
|
|
2157
|
+
{
|
|
2158
|
+
label: t('Copy selected content'),
|
|
2159
|
+
keystroke: 'CTRL+C'
|
|
2160
|
+
},
|
|
2161
|
+
{
|
|
2162
|
+
label: t('Paste content'),
|
|
2163
|
+
keystroke: 'CTRL+V'
|
|
2164
|
+
},
|
|
2165
|
+
{
|
|
2166
|
+
label: t('Paste content as plain text'),
|
|
2167
|
+
keystroke: 'CTRL+SHIFT+V'
|
|
2168
|
+
}
|
|
2169
|
+
]
|
|
2170
|
+
});
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
export { Clipboard, ClipboardMarkersUtils, ClipboardPipeline, DragDrop, DragDropBlockToolbar, DragDropTarget, PastePlainText };
|
|
2175
|
+
//# sourceMappingURL=index.js.map
|