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