@ckeditor/ckeditor5-link 38.1.1 → 38.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/src/augmentation.d.ts +30 -30
- package/src/augmentation.js +5 -5
- package/src/autolink.d.ts +60 -60
- package/src/autolink.js +216 -216
- package/src/index.d.ts +18 -18
- package/src/index.js +17 -17
- package/src/link.d.ts +27 -27
- package/src/link.js +31 -31
- package/src/linkcommand.d.ts +132 -132
- package/src/linkcommand.js +285 -285
- package/src/linkconfig.d.ts +251 -251
- package/src/linkconfig.js +5 -5
- package/src/linkediting.d.ts +106 -106
- package/src/linkediting.js +547 -547
- package/src/linkimage.d.ts +27 -27
- package/src/linkimage.js +31 -31
- package/src/linkimageediting.d.ts +39 -39
- package/src/linkimageediting.js +245 -245
- package/src/linkimageui.d.ts +40 -40
- package/src/linkimageui.js +96 -96
- package/src/linkui.d.ts +165 -165
- package/src/linkui.js +581 -581
- package/src/ui/linkactionsview.d.ts +101 -101
- package/src/ui/linkactionsview.js +156 -156
- package/src/ui/linkformview.d.ts +141 -141
- package/src/ui/linkformview.js +232 -232
- package/src/unlinkcommand.d.ts +31 -31
- package/src/unlinkcommand.js +66 -66
- package/src/utils/automaticdecorators.d.ts +45 -45
- package/src/utils/automaticdecorators.js +140 -140
- package/src/utils/manualdecorator.d.ts +72 -72
- package/src/utils/manualdecorator.js +47 -47
- package/src/utils.d.ts +80 -80
- package/src/utils.js +128 -128
- package/build/link.js.map +0 -1
package/src/linkediting.js
CHANGED
|
@@ -1,547 +1,547 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, 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
|
-
/**
|
|
6
|
-
* @module link/linkediting
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
-
import { MouseObserver } from 'ckeditor5/src/engine';
|
|
10
|
-
import { Input, TwoStepCaretMovement, inlineHighlight, findAttributeRange } from 'ckeditor5/src/typing';
|
|
11
|
-
import { ClipboardPipeline } from 'ckeditor5/src/clipboard';
|
|
12
|
-
import { keyCodes, env } from 'ckeditor5/src/utils';
|
|
13
|
-
import LinkCommand from './linkcommand';
|
|
14
|
-
import UnlinkCommand from './unlinkcommand';
|
|
15
|
-
import ManualDecorator from './utils/manualdecorator';
|
|
16
|
-
import { createLinkElement, ensureSafeUrl, getLocalizedDecorators, normalizeDecorators, openLink, addLinkProtocolIfApplicable } from './utils';
|
|
17
|
-
import '../theme/link.css';
|
|
18
|
-
const HIGHLIGHT_CLASS = 'ck-link_selected';
|
|
19
|
-
const DECORATOR_AUTOMATIC = 'automatic';
|
|
20
|
-
const DECORATOR_MANUAL = 'manual';
|
|
21
|
-
const EXTERNAL_LINKS_REGEXP = /^(https?:)?\/\//;
|
|
22
|
-
/**
|
|
23
|
-
* The link engine feature.
|
|
24
|
-
*
|
|
25
|
-
* It introduces the `linkHref="url"` attribute in the model which renders to the view as a `<a href="url">` element
|
|
26
|
-
* as well as `'link'` and `'unlink'` commands.
|
|
27
|
-
*/
|
|
28
|
-
export default class LinkEditing extends Plugin {
|
|
29
|
-
/**
|
|
30
|
-
* @inheritDoc
|
|
31
|
-
*/
|
|
32
|
-
static get pluginName() {
|
|
33
|
-
return 'LinkEditing';
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* @inheritDoc
|
|
37
|
-
*/
|
|
38
|
-
static get requires() {
|
|
39
|
-
// Clipboard is required for handling cut and paste events while typing over the link.
|
|
40
|
-
return [TwoStepCaretMovement, Input, ClipboardPipeline];
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* @inheritDoc
|
|
44
|
-
*/
|
|
45
|
-
constructor(editor) {
|
|
46
|
-
super(editor);
|
|
47
|
-
editor.config.define('link', {
|
|
48
|
-
addTargetToExternalLinks: false
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* @inheritDoc
|
|
53
|
-
*/
|
|
54
|
-
init() {
|
|
55
|
-
const editor = this.editor;
|
|
56
|
-
// Allow link attribute on all inline nodes.
|
|
57
|
-
editor.model.schema.extend('$text', { allowAttributes: 'linkHref' });
|
|
58
|
-
editor.conversion.for('dataDowncast')
|
|
59
|
-
.attributeToElement({ model: 'linkHref', view: createLinkElement });
|
|
60
|
-
editor.conversion.for('editingDowncast')
|
|
61
|
-
.attributeToElement({ model: 'linkHref', view: (href, conversionApi) => {
|
|
62
|
-
return createLinkElement(ensureSafeUrl(href), conversionApi);
|
|
63
|
-
} });
|
|
64
|
-
editor.conversion.for('upcast')
|
|
65
|
-
.elementToAttribute({
|
|
66
|
-
view: {
|
|
67
|
-
name: 'a',
|
|
68
|
-
attributes: {
|
|
69
|
-
href: true
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
model: {
|
|
73
|
-
key: 'linkHref',
|
|
74
|
-
value: (viewElement) => viewElement.getAttribute('href')
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
// Create linking commands.
|
|
78
|
-
editor.commands.add('link', new LinkCommand(editor));
|
|
79
|
-
editor.commands.add('unlink', new UnlinkCommand(editor));
|
|
80
|
-
const linkDecorators = getLocalizedDecorators(editor.t, normalizeDecorators(editor.config.get('link.decorators')));
|
|
81
|
-
this._enableAutomaticDecorators(linkDecorators
|
|
82
|
-
.filter((item) => item.mode === DECORATOR_AUTOMATIC));
|
|
83
|
-
this._enableManualDecorators(linkDecorators
|
|
84
|
-
.filter((item) => item.mode === DECORATOR_MANUAL));
|
|
85
|
-
// Enable two-step caret movement for `linkHref` attribute.
|
|
86
|
-
const twoStepCaretMovementPlugin = editor.plugins.get(TwoStepCaretMovement);
|
|
87
|
-
twoStepCaretMovementPlugin.registerAttribute('linkHref');
|
|
88
|
-
// Setup highlight over selected link.
|
|
89
|
-
inlineHighlight(editor, 'linkHref', 'a', HIGHLIGHT_CLASS);
|
|
90
|
-
// Handle link following by CTRL+click or ALT+ENTER
|
|
91
|
-
this._enableLinkOpen();
|
|
92
|
-
// Change the attributes of the selection in certain situations after the link was inserted into the document.
|
|
93
|
-
this._enableInsertContentSelectionAttributesFixer();
|
|
94
|
-
// Handle a click at the beginning/end of a link element.
|
|
95
|
-
this._enableClickingAfterLink();
|
|
96
|
-
// Handle typing over the link.
|
|
97
|
-
this._enableTypingOverLink();
|
|
98
|
-
// Handle removing the content after the link element.
|
|
99
|
-
this._handleDeleteContentAfterLink();
|
|
100
|
-
// Handle adding default protocol to pasted links.
|
|
101
|
-
this._enableClipboardIntegration();
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Processes an array of configured {@link module:link/linkconfig~LinkDecoratorAutomaticDefinition automatic decorators}
|
|
105
|
-
* and registers a {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher downcast dispatcher}
|
|
106
|
-
* for each one of them. Downcast dispatchers are obtained using the
|
|
107
|
-
* {@link module:link/utils/automaticdecorators~AutomaticDecorators#getDispatcher} method.
|
|
108
|
-
*
|
|
109
|
-
* **Note**: This method also activates the automatic external link decorator if enabled with
|
|
110
|
-
* {@link module:link/linkconfig~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}.
|
|
111
|
-
*/
|
|
112
|
-
_enableAutomaticDecorators(automaticDecoratorDefinitions) {
|
|
113
|
-
const editor = this.editor;
|
|
114
|
-
// Store automatic decorators in the command instance as we do the same with manual decorators.
|
|
115
|
-
// Thanks to that, `LinkImageEditing` plugin can re-use the same definitions.
|
|
116
|
-
const command = editor.commands.get('link');
|
|
117
|
-
const automaticDecorators = command.automaticDecorators;
|
|
118
|
-
// Adds a default decorator for external links.
|
|
119
|
-
if (editor.config.get('link.addTargetToExternalLinks')) {
|
|
120
|
-
automaticDecorators.add({
|
|
121
|
-
id: 'linkIsExternal',
|
|
122
|
-
mode: DECORATOR_AUTOMATIC,
|
|
123
|
-
callback: url => !!url && EXTERNAL_LINKS_REGEXP.test(url),
|
|
124
|
-
attributes: {
|
|
125
|
-
target: '_blank',
|
|
126
|
-
rel: 'noopener noreferrer'
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
automaticDecorators.add(automaticDecoratorDefinitions);
|
|
131
|
-
if (automaticDecorators.length) {
|
|
132
|
-
editor.conversion.for('downcast').add(automaticDecorators.getDispatcher());
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Processes an array of configured {@link module:link/linkconfig~LinkDecoratorManualDefinition manual decorators},
|
|
137
|
-
* transforms them into {@link module:link/utils/manualdecorator~ManualDecorator} instances and stores them in the
|
|
138
|
-
* {@link module:link/linkcommand~LinkCommand#manualDecorators} collection (a model for manual decorators state).
|
|
139
|
-
*
|
|
140
|
-
* Also registers an {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToElement attribute-to-element}
|
|
141
|
-
* converter for each manual decorator and extends the {@link module:engine/model/schema~Schema model's schema}
|
|
142
|
-
* with adequate model attributes.
|
|
143
|
-
*/
|
|
144
|
-
_enableManualDecorators(manualDecoratorDefinitions) {
|
|
145
|
-
if (!manualDecoratorDefinitions.length) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
const editor = this.editor;
|
|
149
|
-
const command = editor.commands.get('link');
|
|
150
|
-
const manualDecorators = command.manualDecorators;
|
|
151
|
-
manualDecoratorDefinitions.forEach(decoratorDefinition => {
|
|
152
|
-
editor.model.schema.extend('$text', { allowAttributes: decoratorDefinition.id });
|
|
153
|
-
// Keeps reference to manual decorator to decode its name to attributes during downcast.
|
|
154
|
-
const decorator = new ManualDecorator(decoratorDefinition);
|
|
155
|
-
manualDecorators.add(decorator);
|
|
156
|
-
editor.conversion.for('downcast').attributeToElement({
|
|
157
|
-
model: decorator.id,
|
|
158
|
-
view: (manualDecoratorValue, { writer, schema }, { item }) => {
|
|
159
|
-
// Manual decorators for block links are handled e.g. in LinkImageEditing.
|
|
160
|
-
if (!(item.is('selection') || schema.isInline(item))) {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
if (manualDecoratorValue) {
|
|
164
|
-
const element = writer.createAttributeElement('a', decorator.attributes, { priority: 5 });
|
|
165
|
-
if (decorator.classes) {
|
|
166
|
-
writer.addClass(decorator.classes, element);
|
|
167
|
-
}
|
|
168
|
-
for (const key in decorator.styles) {
|
|
169
|
-
writer.setStyle(key, decorator.styles[key], element);
|
|
170
|
-
}
|
|
171
|
-
writer.setCustomProperty('link', true, element);
|
|
172
|
-
return element;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
editor.conversion.for('upcast').elementToAttribute({
|
|
177
|
-
view: {
|
|
178
|
-
name: 'a',
|
|
179
|
-
...decorator._createPattern()
|
|
180
|
-
},
|
|
181
|
-
model: {
|
|
182
|
-
key: decorator.id
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Attaches handlers for {@link module:engine/view/document~Document#event:enter} and
|
|
189
|
-
* {@link module:engine/view/document~Document#event:click} to enable link following.
|
|
190
|
-
*/
|
|
191
|
-
_enableLinkOpen() {
|
|
192
|
-
const editor = this.editor;
|
|
193
|
-
const view = editor.editing.view;
|
|
194
|
-
const viewDocument = view.document;
|
|
195
|
-
this.listenTo(viewDocument, 'click', (evt, data) => {
|
|
196
|
-
const shouldOpen = env.isMac ? data.domEvent.metaKey : data.domEvent.ctrlKey;
|
|
197
|
-
if (!shouldOpen) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
let clickedElement = data.domTarget;
|
|
201
|
-
if (clickedElement.tagName.toLowerCase() != 'a') {
|
|
202
|
-
clickedElement = clickedElement.closest('a');
|
|
203
|
-
}
|
|
204
|
-
if (!clickedElement) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
const url = clickedElement.getAttribute('href');
|
|
208
|
-
if (!url) {
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
evt.stop();
|
|
212
|
-
data.preventDefault();
|
|
213
|
-
openLink(url);
|
|
214
|
-
}, { context: '$capture' });
|
|
215
|
-
// Open link on Alt+Enter.
|
|
216
|
-
this.listenTo(viewDocument, 'keydown', (evt, data) => {
|
|
217
|
-
const linkCommand = editor.commands.get('link');
|
|
218
|
-
const url = linkCommand.value;
|
|
219
|
-
const shouldOpen = !!url && data.keyCode === keyCodes.enter && data.altKey;
|
|
220
|
-
if (!shouldOpen) {
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
evt.stop();
|
|
224
|
-
openLink(url);
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Starts listening to {@link module:engine/model/model~Model#event:insertContent} and corrects the model
|
|
229
|
-
* selection attributes if the selection is at the end of a link after inserting the content.
|
|
230
|
-
*
|
|
231
|
-
* The purpose of this action is to improve the overall UX because the user is no longer "trapped" by the
|
|
232
|
-
* `linkHref` attribute of the selection and they can type a "clean" (`linkHref`–less) text right away.
|
|
233
|
-
*
|
|
234
|
-
* See https://github.com/ckeditor/ckeditor5/issues/6053.
|
|
235
|
-
*/
|
|
236
|
-
_enableInsertContentSelectionAttributesFixer() {
|
|
237
|
-
const editor = this.editor;
|
|
238
|
-
const model = editor.model;
|
|
239
|
-
const selection = model.document.selection;
|
|
240
|
-
this.listenTo(model, 'insertContent', () => {
|
|
241
|
-
const nodeBefore = selection.anchor.nodeBefore;
|
|
242
|
-
const nodeAfter = selection.anchor.nodeAfter;
|
|
243
|
-
// NOTE: ↰ and ↱ represent the gravity of the selection.
|
|
244
|
-
// The only truly valid case is:
|
|
245
|
-
//
|
|
246
|
-
// ↰
|
|
247
|
-
// ...<$text linkHref="foo">INSERTED[]</$text>
|
|
248
|
-
//
|
|
249
|
-
// If the selection is not "trapped" by the `linkHref` attribute after inserting, there's nothing
|
|
250
|
-
// to fix there.
|
|
251
|
-
if (!selection.hasAttribute('linkHref')) {
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
// Filter out the following case where a link with the same href (e.g. <a href="foo">INSERTED</a>) is inserted
|
|
255
|
-
// in the middle of an existing link:
|
|
256
|
-
//
|
|
257
|
-
// Before insertion:
|
|
258
|
-
// ↰
|
|
259
|
-
// <$text linkHref="foo">l[]ink</$text>
|
|
260
|
-
//
|
|
261
|
-
// Expected after insertion:
|
|
262
|
-
// ↰
|
|
263
|
-
// <$text linkHref="foo">lINSERTED[]ink</$text>
|
|
264
|
-
//
|
|
265
|
-
if (!nodeBefore) {
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
// Filter out the following case where the selection has the "linkHref" attribute because the
|
|
269
|
-
// gravity is overridden and some text with another attribute (e.g. <b>INSERTED</b>) is inserted:
|
|
270
|
-
//
|
|
271
|
-
// Before insertion:
|
|
272
|
-
//
|
|
273
|
-
// ↱
|
|
274
|
-
// <$text linkHref="foo">[]link</$text>
|
|
275
|
-
//
|
|
276
|
-
// Expected after insertion:
|
|
277
|
-
//
|
|
278
|
-
// ↱
|
|
279
|
-
// <$text bold="true">INSERTED</$text><$text linkHref="foo">[]link</$text>
|
|
280
|
-
//
|
|
281
|
-
if (!nodeBefore.hasAttribute('linkHref')) {
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
// Filter out the following case where a link is a inserted in the middle (or before) another link
|
|
285
|
-
// (different URLs, so they will not merge). In this (let's say weird) case, we can leave the selection
|
|
286
|
-
// attributes as they are because the user will end up writing in one link or another anyway.
|
|
287
|
-
//
|
|
288
|
-
// Before insertion:
|
|
289
|
-
//
|
|
290
|
-
// ↰
|
|
291
|
-
// <$text linkHref="foo">l[]ink</$text>
|
|
292
|
-
//
|
|
293
|
-
// Expected after insertion:
|
|
294
|
-
//
|
|
295
|
-
// ↰
|
|
296
|
-
// <$text linkHref="foo">l</$text><$text linkHref="bar">INSERTED[]</$text><$text linkHref="foo">ink</$text>
|
|
297
|
-
//
|
|
298
|
-
if (nodeAfter && nodeAfter.hasAttribute('linkHref')) {
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
model.change(writer => {
|
|
302
|
-
removeLinkAttributesFromSelection(writer, getLinkAttributesAllowedOnText(model.schema));
|
|
303
|
-
});
|
|
304
|
-
}, { priority: 'low' });
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Starts listening to {@link module:engine/view/document~Document#event:mousedown} and
|
|
308
|
-
* {@link module:engine/view/document~Document#event:selectionChange} and puts the selection before/after a link node
|
|
309
|
-
* if clicked at the beginning/ending of the link.
|
|
310
|
-
*
|
|
311
|
-
* The purpose of this action is to allow typing around the link node directly after a click.
|
|
312
|
-
*
|
|
313
|
-
* See https://github.com/ckeditor/ckeditor5/issues/1016.
|
|
314
|
-
*/
|
|
315
|
-
_enableClickingAfterLink() {
|
|
316
|
-
const editor = this.editor;
|
|
317
|
-
const model = editor.model;
|
|
318
|
-
editor.editing.view.addObserver(MouseObserver);
|
|
319
|
-
let clicked = false;
|
|
320
|
-
// Detect the click.
|
|
321
|
-
this.listenTo(editor.editing.view.document, 'mousedown', () => {
|
|
322
|
-
clicked = true;
|
|
323
|
-
});
|
|
324
|
-
// When the selection has changed...
|
|
325
|
-
this.listenTo(editor.editing.view.document, 'selectionChange', () => {
|
|
326
|
-
if (!clicked) {
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
// ...and it was caused by the click...
|
|
330
|
-
clicked = false;
|
|
331
|
-
const selection = model.document.selection;
|
|
332
|
-
// ...and no text is selected...
|
|
333
|
-
if (!selection.isCollapsed) {
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
// ...and clicked text is the link...
|
|
337
|
-
if (!selection.hasAttribute('linkHref')) {
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
const position = selection.getFirstPosition();
|
|
341
|
-
const linkRange = findAttributeRange(position, 'linkHref', selection.getAttribute('linkHref'), model);
|
|
342
|
-
// ...check whether clicked start/end boundary of the link.
|
|
343
|
-
// If so, remove the `linkHref` attribute.
|
|
344
|
-
if (position.isTouching(linkRange.start) || position.isTouching(linkRange.end)) {
|
|
345
|
-
model.change(writer => {
|
|
346
|
-
removeLinkAttributesFromSelection(writer, getLinkAttributesAllowedOnText(model.schema));
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Starts listening to {@link module:engine/model/model~Model#deleteContent} and {@link module:engine/model/model~Model#insertContent}
|
|
353
|
-
* and checks whether typing over the link. If so, attributes of removed text are preserved and applied to the inserted text.
|
|
354
|
-
*
|
|
355
|
-
* The purpose of this action is to allow modifying a text without loosing the `linkHref` attribute (and other).
|
|
356
|
-
*
|
|
357
|
-
* See https://github.com/ckeditor/ckeditor5/issues/4762.
|
|
358
|
-
*/
|
|
359
|
-
_enableTypingOverLink() {
|
|
360
|
-
const editor = this.editor;
|
|
361
|
-
const view = editor.editing.view;
|
|
362
|
-
// Selection attributes when started typing over the link.
|
|
363
|
-
let selectionAttributes = null;
|
|
364
|
-
// Whether pressed `Backspace` or `Delete`. If so, attributes should not be preserved.
|
|
365
|
-
let deletedContent = false;
|
|
366
|
-
// Detect pressing `Backspace` / `Delete`.
|
|
367
|
-
this.listenTo(view.document, 'delete', () => {
|
|
368
|
-
deletedContent = true;
|
|
369
|
-
}, { priority: 'high' });
|
|
370
|
-
// Listening to `model#deleteContent` allows detecting whether selected content was a link.
|
|
371
|
-
// If so, before removing the element, we will copy its attributes.
|
|
372
|
-
this.listenTo(editor.model, 'deleteContent', () => {
|
|
373
|
-
const selection = editor.model.document.selection;
|
|
374
|
-
// Copy attributes only if anything is selected.
|
|
375
|
-
if (selection.isCollapsed) {
|
|
376
|
-
return;
|
|
377
|
-
}
|
|
378
|
-
// When the content was deleted, do not preserve attributes.
|
|
379
|
-
if (deletedContent) {
|
|
380
|
-
deletedContent = false;
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
// Enabled only when typing.
|
|
384
|
-
if (!isTyping(editor)) {
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
if (shouldCopyAttributes(editor.model)) {
|
|
388
|
-
selectionAttributes = selection.getAttributes();
|
|
389
|
-
}
|
|
390
|
-
}, { priority: 'high' });
|
|
391
|
-
// Listening to `model#insertContent` allows detecting the content insertion.
|
|
392
|
-
// We want to apply attributes that were removed while typing over the link.
|
|
393
|
-
this.listenTo(editor.model, 'insertContent', (evt, [element]) => {
|
|
394
|
-
deletedContent = false;
|
|
395
|
-
// Enabled only when typing.
|
|
396
|
-
if (!isTyping(editor)) {
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
if (!selectionAttributes) {
|
|
400
|
-
return;
|
|
401
|
-
}
|
|
402
|
-
editor.model.change(writer => {
|
|
403
|
-
for (const [attribute, value] of selectionAttributes) {
|
|
404
|
-
writer.setAttribute(attribute, value, element);
|
|
405
|
-
}
|
|
406
|
-
});
|
|
407
|
-
selectionAttributes = null;
|
|
408
|
-
}, { priority: 'high' });
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Starts listening to {@link module:engine/model/model~Model#deleteContent} and checks whether
|
|
412
|
-
* removing a content right after the "linkHref" attribute.
|
|
413
|
-
*
|
|
414
|
-
* If so, the selection should not preserve the `linkHref` attribute. However, if
|
|
415
|
-
* the {@link module:typing/twostepcaretmovement~TwoStepCaretMovement} plugin is active and
|
|
416
|
-
* the selection has the "linkHref" attribute due to overriden gravity (at the end), the `linkHref` attribute should stay untouched.
|
|
417
|
-
*
|
|
418
|
-
* The purpose of this action is to allow removing the link text and keep the selection outside the link.
|
|
419
|
-
*
|
|
420
|
-
* See https://github.com/ckeditor/ckeditor5/issues/7521.
|
|
421
|
-
*/
|
|
422
|
-
_handleDeleteContentAfterLink() {
|
|
423
|
-
const editor = this.editor;
|
|
424
|
-
const model = editor.model;
|
|
425
|
-
const selection = model.document.selection;
|
|
426
|
-
const view = editor.editing.view;
|
|
427
|
-
// A flag whether attributes `linkHref` attribute should be preserved.
|
|
428
|
-
let shouldPreserveAttributes = false;
|
|
429
|
-
// A flag whether the `Backspace` key was pressed.
|
|
430
|
-
let hasBackspacePressed = false;
|
|
431
|
-
// Detect pressing `Backspace`.
|
|
432
|
-
this.listenTo(view.document, 'delete', (evt, data) => {
|
|
433
|
-
hasBackspacePressed = data.direction === 'backward';
|
|
434
|
-
}, { priority: 'high' });
|
|
435
|
-
// Before removing the content, check whether the selection is inside a link or at the end of link but with 2-SCM enabled.
|
|
436
|
-
// If so, we want to preserve link attributes.
|
|
437
|
-
this.listenTo(model, 'deleteContent', () => {
|
|
438
|
-
// Reset the state.
|
|
439
|
-
shouldPreserveAttributes = false;
|
|
440
|
-
const position = selection.getFirstPosition();
|
|
441
|
-
const linkHref = selection.getAttribute('linkHref');
|
|
442
|
-
if (!linkHref) {
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
const linkRange = findAttributeRange(position, 'linkHref', linkHref, model);
|
|
446
|
-
// Preserve `linkHref` attribute if the selection is in the middle of the link or
|
|
447
|
-
// the selection is at the end of the link and 2-SCM is activated.
|
|
448
|
-
shouldPreserveAttributes = linkRange.containsPosition(position) || linkRange.end.isEqual(position);
|
|
449
|
-
}, { priority: 'high' });
|
|
450
|
-
// After removing the content, check whether the current selection should preserve the `linkHref` attribute.
|
|
451
|
-
this.listenTo(model, 'deleteContent', () => {
|
|
452
|
-
// If didn't press `Backspace`.
|
|
453
|
-
if (!hasBackspacePressed) {
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
hasBackspacePressed = false;
|
|
457
|
-
// Disable the mechanism if inside a link (`<$text url="foo">F[]oo</$text>` or <$text url="foo">Foo[]</$text>`).
|
|
458
|
-
if (shouldPreserveAttributes) {
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
// Use `model.enqueueChange()` in order to execute the callback at the end of the changes process.
|
|
462
|
-
editor.model.enqueueChange(writer => {
|
|
463
|
-
removeLinkAttributesFromSelection(writer, getLinkAttributesAllowedOnText(model.schema));
|
|
464
|
-
});
|
|
465
|
-
}, { priority: 'low' });
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Enables URL fixing on pasting.
|
|
469
|
-
*/
|
|
470
|
-
_enableClipboardIntegration() {
|
|
471
|
-
const editor = this.editor;
|
|
472
|
-
const model = editor.model;
|
|
473
|
-
const defaultProtocol = this.editor.config.get('link.defaultProtocol');
|
|
474
|
-
if (!defaultProtocol) {
|
|
475
|
-
return;
|
|
476
|
-
}
|
|
477
|
-
this.listenTo(editor.plugins.get('ClipboardPipeline'), 'contentInsertion', (evt, data) => {
|
|
478
|
-
model.change(writer => {
|
|
479
|
-
const range = writer.createRangeIn(data.content);
|
|
480
|
-
for (const item of range.getItems()) {
|
|
481
|
-
if (item.hasAttribute('linkHref')) {
|
|
482
|
-
const newLink = addLinkProtocolIfApplicable(item.getAttribute('linkHref'), defaultProtocol);
|
|
483
|
-
writer.setAttribute('linkHref', newLink, item);
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
});
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* Make the selection free of link-related model attributes.
|
|
492
|
-
* All link-related model attributes start with "link". That includes not only "linkHref"
|
|
493
|
-
* but also all decorator attributes (they have dynamic names), or even custom plugins.
|
|
494
|
-
*/
|
|
495
|
-
function removeLinkAttributesFromSelection(writer, linkAttributes) {
|
|
496
|
-
writer.removeSelectionAttribute('linkHref');
|
|
497
|
-
for (const attribute of linkAttributes) {
|
|
498
|
-
writer.removeSelectionAttribute(attribute);
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
/**
|
|
502
|
-
* Checks whether selection's attributes should be copied to the new inserted text.
|
|
503
|
-
*/
|
|
504
|
-
function shouldCopyAttributes(model) {
|
|
505
|
-
const selection = model.document.selection;
|
|
506
|
-
const firstPosition = selection.getFirstPosition();
|
|
507
|
-
const lastPosition = selection.getLastPosition();
|
|
508
|
-
const nodeAtFirstPosition = firstPosition.nodeAfter;
|
|
509
|
-
// The text link node does not exist...
|
|
510
|
-
if (!nodeAtFirstPosition) {
|
|
511
|
-
return false;
|
|
512
|
-
}
|
|
513
|
-
// ...or it isn't the text node...
|
|
514
|
-
if (!nodeAtFirstPosition.is('$text')) {
|
|
515
|
-
return false;
|
|
516
|
-
}
|
|
517
|
-
// ...or isn't the link.
|
|
518
|
-
if (!nodeAtFirstPosition.hasAttribute('linkHref')) {
|
|
519
|
-
return false;
|
|
520
|
-
}
|
|
521
|
-
// `textNode` = the position is inside the link element.
|
|
522
|
-
// `nodeBefore` = the position is at the end of the link element.
|
|
523
|
-
const nodeAtLastPosition = lastPosition.textNode || lastPosition.nodeBefore;
|
|
524
|
-
// If both references the same node selection contains a single text node.
|
|
525
|
-
if (nodeAtFirstPosition === nodeAtLastPosition) {
|
|
526
|
-
return true;
|
|
527
|
-
}
|
|
528
|
-
// If nodes are not equal, maybe the link nodes has defined additional attributes inside.
|
|
529
|
-
// First, we need to find the entire link range.
|
|
530
|
-
const linkRange = findAttributeRange(firstPosition, 'linkHref', nodeAtFirstPosition.getAttribute('linkHref'), model);
|
|
531
|
-
// Then we can check whether selected range is inside the found link range. If so, attributes should be preserved.
|
|
532
|
-
return linkRange.containsRange(model.createRange(firstPosition, lastPosition), true);
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* Checks whether provided changes were caused by typing.
|
|
536
|
-
*/
|
|
537
|
-
function isTyping(editor) {
|
|
538
|
-
const currentBatch = editor.model.change(writer => writer.batch);
|
|
539
|
-
return currentBatch.isTyping;
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* Returns an array containing names of the attributes allowed on `$text` that describes the link item.
|
|
543
|
-
*/
|
|
544
|
-
function getLinkAttributesAllowedOnText(schema) {
|
|
545
|
-
const textAttributes = schema.getDefinition('$text').allowAttributes;
|
|
546
|
-
return textAttributes.filter(attribute => attribute.startsWith('link'));
|
|
547
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
|
6
|
+
* @module link/linkediting
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
|
9
|
+
import { MouseObserver } from 'ckeditor5/src/engine.js';
|
|
10
|
+
import { Input, TwoStepCaretMovement, inlineHighlight, findAttributeRange } from 'ckeditor5/src/typing.js';
|
|
11
|
+
import { ClipboardPipeline } from 'ckeditor5/src/clipboard.js';
|
|
12
|
+
import { keyCodes, env } from 'ckeditor5/src/utils.js';
|
|
13
|
+
import LinkCommand from './linkcommand.js';
|
|
14
|
+
import UnlinkCommand from './unlinkcommand.js';
|
|
15
|
+
import ManualDecorator from './utils/manualdecorator.js';
|
|
16
|
+
import { createLinkElement, ensureSafeUrl, getLocalizedDecorators, normalizeDecorators, openLink, addLinkProtocolIfApplicable } from './utils.js';
|
|
17
|
+
import '../theme/link.css';
|
|
18
|
+
const HIGHLIGHT_CLASS = 'ck-link_selected';
|
|
19
|
+
const DECORATOR_AUTOMATIC = 'automatic';
|
|
20
|
+
const DECORATOR_MANUAL = 'manual';
|
|
21
|
+
const EXTERNAL_LINKS_REGEXP = /^(https?:)?\/\//;
|
|
22
|
+
/**
|
|
23
|
+
* The link engine feature.
|
|
24
|
+
*
|
|
25
|
+
* It introduces the `linkHref="url"` attribute in the model which renders to the view as a `<a href="url">` element
|
|
26
|
+
* as well as `'link'` and `'unlink'` commands.
|
|
27
|
+
*/
|
|
28
|
+
export default class LinkEditing extends Plugin {
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get pluginName() {
|
|
33
|
+
return 'LinkEditing';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @inheritDoc
|
|
37
|
+
*/
|
|
38
|
+
static get requires() {
|
|
39
|
+
// Clipboard is required for handling cut and paste events while typing over the link.
|
|
40
|
+
return [TwoStepCaretMovement, Input, ClipboardPipeline];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @inheritDoc
|
|
44
|
+
*/
|
|
45
|
+
constructor(editor) {
|
|
46
|
+
super(editor);
|
|
47
|
+
editor.config.define('link', {
|
|
48
|
+
addTargetToExternalLinks: false
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @inheritDoc
|
|
53
|
+
*/
|
|
54
|
+
init() {
|
|
55
|
+
const editor = this.editor;
|
|
56
|
+
// Allow link attribute on all inline nodes.
|
|
57
|
+
editor.model.schema.extend('$text', { allowAttributes: 'linkHref' });
|
|
58
|
+
editor.conversion.for('dataDowncast')
|
|
59
|
+
.attributeToElement({ model: 'linkHref', view: createLinkElement });
|
|
60
|
+
editor.conversion.for('editingDowncast')
|
|
61
|
+
.attributeToElement({ model: 'linkHref', view: (href, conversionApi) => {
|
|
62
|
+
return createLinkElement(ensureSafeUrl(href), conversionApi);
|
|
63
|
+
} });
|
|
64
|
+
editor.conversion.for('upcast')
|
|
65
|
+
.elementToAttribute({
|
|
66
|
+
view: {
|
|
67
|
+
name: 'a',
|
|
68
|
+
attributes: {
|
|
69
|
+
href: true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
model: {
|
|
73
|
+
key: 'linkHref',
|
|
74
|
+
value: (viewElement) => viewElement.getAttribute('href')
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
// Create linking commands.
|
|
78
|
+
editor.commands.add('link', new LinkCommand(editor));
|
|
79
|
+
editor.commands.add('unlink', new UnlinkCommand(editor));
|
|
80
|
+
const linkDecorators = getLocalizedDecorators(editor.t, normalizeDecorators(editor.config.get('link.decorators')));
|
|
81
|
+
this._enableAutomaticDecorators(linkDecorators
|
|
82
|
+
.filter((item) => item.mode === DECORATOR_AUTOMATIC));
|
|
83
|
+
this._enableManualDecorators(linkDecorators
|
|
84
|
+
.filter((item) => item.mode === DECORATOR_MANUAL));
|
|
85
|
+
// Enable two-step caret movement for `linkHref` attribute.
|
|
86
|
+
const twoStepCaretMovementPlugin = editor.plugins.get(TwoStepCaretMovement);
|
|
87
|
+
twoStepCaretMovementPlugin.registerAttribute('linkHref');
|
|
88
|
+
// Setup highlight over selected link.
|
|
89
|
+
inlineHighlight(editor, 'linkHref', 'a', HIGHLIGHT_CLASS);
|
|
90
|
+
// Handle link following by CTRL+click or ALT+ENTER
|
|
91
|
+
this._enableLinkOpen();
|
|
92
|
+
// Change the attributes of the selection in certain situations after the link was inserted into the document.
|
|
93
|
+
this._enableInsertContentSelectionAttributesFixer();
|
|
94
|
+
// Handle a click at the beginning/end of a link element.
|
|
95
|
+
this._enableClickingAfterLink();
|
|
96
|
+
// Handle typing over the link.
|
|
97
|
+
this._enableTypingOverLink();
|
|
98
|
+
// Handle removing the content after the link element.
|
|
99
|
+
this._handleDeleteContentAfterLink();
|
|
100
|
+
// Handle adding default protocol to pasted links.
|
|
101
|
+
this._enableClipboardIntegration();
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Processes an array of configured {@link module:link/linkconfig~LinkDecoratorAutomaticDefinition automatic decorators}
|
|
105
|
+
* and registers a {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher downcast dispatcher}
|
|
106
|
+
* for each one of them. Downcast dispatchers are obtained using the
|
|
107
|
+
* {@link module:link/utils/automaticdecorators~AutomaticDecorators#getDispatcher} method.
|
|
108
|
+
*
|
|
109
|
+
* **Note**: This method also activates the automatic external link decorator if enabled with
|
|
110
|
+
* {@link module:link/linkconfig~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}.
|
|
111
|
+
*/
|
|
112
|
+
_enableAutomaticDecorators(automaticDecoratorDefinitions) {
|
|
113
|
+
const editor = this.editor;
|
|
114
|
+
// Store automatic decorators in the command instance as we do the same with manual decorators.
|
|
115
|
+
// Thanks to that, `LinkImageEditing` plugin can re-use the same definitions.
|
|
116
|
+
const command = editor.commands.get('link');
|
|
117
|
+
const automaticDecorators = command.automaticDecorators;
|
|
118
|
+
// Adds a default decorator for external links.
|
|
119
|
+
if (editor.config.get('link.addTargetToExternalLinks')) {
|
|
120
|
+
automaticDecorators.add({
|
|
121
|
+
id: 'linkIsExternal',
|
|
122
|
+
mode: DECORATOR_AUTOMATIC,
|
|
123
|
+
callback: url => !!url && EXTERNAL_LINKS_REGEXP.test(url),
|
|
124
|
+
attributes: {
|
|
125
|
+
target: '_blank',
|
|
126
|
+
rel: 'noopener noreferrer'
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
automaticDecorators.add(automaticDecoratorDefinitions);
|
|
131
|
+
if (automaticDecorators.length) {
|
|
132
|
+
editor.conversion.for('downcast').add(automaticDecorators.getDispatcher());
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Processes an array of configured {@link module:link/linkconfig~LinkDecoratorManualDefinition manual decorators},
|
|
137
|
+
* transforms them into {@link module:link/utils/manualdecorator~ManualDecorator} instances and stores them in the
|
|
138
|
+
* {@link module:link/linkcommand~LinkCommand#manualDecorators} collection (a model for manual decorators state).
|
|
139
|
+
*
|
|
140
|
+
* Also registers an {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToElement attribute-to-element}
|
|
141
|
+
* converter for each manual decorator and extends the {@link module:engine/model/schema~Schema model's schema}
|
|
142
|
+
* with adequate model attributes.
|
|
143
|
+
*/
|
|
144
|
+
_enableManualDecorators(manualDecoratorDefinitions) {
|
|
145
|
+
if (!manualDecoratorDefinitions.length) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const editor = this.editor;
|
|
149
|
+
const command = editor.commands.get('link');
|
|
150
|
+
const manualDecorators = command.manualDecorators;
|
|
151
|
+
manualDecoratorDefinitions.forEach(decoratorDefinition => {
|
|
152
|
+
editor.model.schema.extend('$text', { allowAttributes: decoratorDefinition.id });
|
|
153
|
+
// Keeps reference to manual decorator to decode its name to attributes during downcast.
|
|
154
|
+
const decorator = new ManualDecorator(decoratorDefinition);
|
|
155
|
+
manualDecorators.add(decorator);
|
|
156
|
+
editor.conversion.for('downcast').attributeToElement({
|
|
157
|
+
model: decorator.id,
|
|
158
|
+
view: (manualDecoratorValue, { writer, schema }, { item }) => {
|
|
159
|
+
// Manual decorators for block links are handled e.g. in LinkImageEditing.
|
|
160
|
+
if (!(item.is('selection') || schema.isInline(item))) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (manualDecoratorValue) {
|
|
164
|
+
const element = writer.createAttributeElement('a', decorator.attributes, { priority: 5 });
|
|
165
|
+
if (decorator.classes) {
|
|
166
|
+
writer.addClass(decorator.classes, element);
|
|
167
|
+
}
|
|
168
|
+
for (const key in decorator.styles) {
|
|
169
|
+
writer.setStyle(key, decorator.styles[key], element);
|
|
170
|
+
}
|
|
171
|
+
writer.setCustomProperty('link', true, element);
|
|
172
|
+
return element;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
editor.conversion.for('upcast').elementToAttribute({
|
|
177
|
+
view: {
|
|
178
|
+
name: 'a',
|
|
179
|
+
...decorator._createPattern()
|
|
180
|
+
},
|
|
181
|
+
model: {
|
|
182
|
+
key: decorator.id
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Attaches handlers for {@link module:engine/view/document~Document#event:enter} and
|
|
189
|
+
* {@link module:engine/view/document~Document#event:click} to enable link following.
|
|
190
|
+
*/
|
|
191
|
+
_enableLinkOpen() {
|
|
192
|
+
const editor = this.editor;
|
|
193
|
+
const view = editor.editing.view;
|
|
194
|
+
const viewDocument = view.document;
|
|
195
|
+
this.listenTo(viewDocument, 'click', (evt, data) => {
|
|
196
|
+
const shouldOpen = env.isMac ? data.domEvent.metaKey : data.domEvent.ctrlKey;
|
|
197
|
+
if (!shouldOpen) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
let clickedElement = data.domTarget;
|
|
201
|
+
if (clickedElement.tagName.toLowerCase() != 'a') {
|
|
202
|
+
clickedElement = clickedElement.closest('a');
|
|
203
|
+
}
|
|
204
|
+
if (!clickedElement) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const url = clickedElement.getAttribute('href');
|
|
208
|
+
if (!url) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
evt.stop();
|
|
212
|
+
data.preventDefault();
|
|
213
|
+
openLink(url);
|
|
214
|
+
}, { context: '$capture' });
|
|
215
|
+
// Open link on Alt+Enter.
|
|
216
|
+
this.listenTo(viewDocument, 'keydown', (evt, data) => {
|
|
217
|
+
const linkCommand = editor.commands.get('link');
|
|
218
|
+
const url = linkCommand.value;
|
|
219
|
+
const shouldOpen = !!url && data.keyCode === keyCodes.enter && data.altKey;
|
|
220
|
+
if (!shouldOpen) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
evt.stop();
|
|
224
|
+
openLink(url);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Starts listening to {@link module:engine/model/model~Model#event:insertContent} and corrects the model
|
|
229
|
+
* selection attributes if the selection is at the end of a link after inserting the content.
|
|
230
|
+
*
|
|
231
|
+
* The purpose of this action is to improve the overall UX because the user is no longer "trapped" by the
|
|
232
|
+
* `linkHref` attribute of the selection and they can type a "clean" (`linkHref`–less) text right away.
|
|
233
|
+
*
|
|
234
|
+
* See https://github.com/ckeditor/ckeditor5/issues/6053.
|
|
235
|
+
*/
|
|
236
|
+
_enableInsertContentSelectionAttributesFixer() {
|
|
237
|
+
const editor = this.editor;
|
|
238
|
+
const model = editor.model;
|
|
239
|
+
const selection = model.document.selection;
|
|
240
|
+
this.listenTo(model, 'insertContent', () => {
|
|
241
|
+
const nodeBefore = selection.anchor.nodeBefore;
|
|
242
|
+
const nodeAfter = selection.anchor.nodeAfter;
|
|
243
|
+
// NOTE: ↰ and ↱ represent the gravity of the selection.
|
|
244
|
+
// The only truly valid case is:
|
|
245
|
+
//
|
|
246
|
+
// ↰
|
|
247
|
+
// ...<$text linkHref="foo">INSERTED[]</$text>
|
|
248
|
+
//
|
|
249
|
+
// If the selection is not "trapped" by the `linkHref` attribute after inserting, there's nothing
|
|
250
|
+
// to fix there.
|
|
251
|
+
if (!selection.hasAttribute('linkHref')) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
// Filter out the following case where a link with the same href (e.g. <a href="foo">INSERTED</a>) is inserted
|
|
255
|
+
// in the middle of an existing link:
|
|
256
|
+
//
|
|
257
|
+
// Before insertion:
|
|
258
|
+
// ↰
|
|
259
|
+
// <$text linkHref="foo">l[]ink</$text>
|
|
260
|
+
//
|
|
261
|
+
// Expected after insertion:
|
|
262
|
+
// ↰
|
|
263
|
+
// <$text linkHref="foo">lINSERTED[]ink</$text>
|
|
264
|
+
//
|
|
265
|
+
if (!nodeBefore) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
// Filter out the following case where the selection has the "linkHref" attribute because the
|
|
269
|
+
// gravity is overridden and some text with another attribute (e.g. <b>INSERTED</b>) is inserted:
|
|
270
|
+
//
|
|
271
|
+
// Before insertion:
|
|
272
|
+
//
|
|
273
|
+
// ↱
|
|
274
|
+
// <$text linkHref="foo">[]link</$text>
|
|
275
|
+
//
|
|
276
|
+
// Expected after insertion:
|
|
277
|
+
//
|
|
278
|
+
// ↱
|
|
279
|
+
// <$text bold="true">INSERTED</$text><$text linkHref="foo">[]link</$text>
|
|
280
|
+
//
|
|
281
|
+
if (!nodeBefore.hasAttribute('linkHref')) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
// Filter out the following case where a link is a inserted in the middle (or before) another link
|
|
285
|
+
// (different URLs, so they will not merge). In this (let's say weird) case, we can leave the selection
|
|
286
|
+
// attributes as they are because the user will end up writing in one link or another anyway.
|
|
287
|
+
//
|
|
288
|
+
// Before insertion:
|
|
289
|
+
//
|
|
290
|
+
// ↰
|
|
291
|
+
// <$text linkHref="foo">l[]ink</$text>
|
|
292
|
+
//
|
|
293
|
+
// Expected after insertion:
|
|
294
|
+
//
|
|
295
|
+
// ↰
|
|
296
|
+
// <$text linkHref="foo">l</$text><$text linkHref="bar">INSERTED[]</$text><$text linkHref="foo">ink</$text>
|
|
297
|
+
//
|
|
298
|
+
if (nodeAfter && nodeAfter.hasAttribute('linkHref')) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
model.change(writer => {
|
|
302
|
+
removeLinkAttributesFromSelection(writer, getLinkAttributesAllowedOnText(model.schema));
|
|
303
|
+
});
|
|
304
|
+
}, { priority: 'low' });
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Starts listening to {@link module:engine/view/document~Document#event:mousedown} and
|
|
308
|
+
* {@link module:engine/view/document~Document#event:selectionChange} and puts the selection before/after a link node
|
|
309
|
+
* if clicked at the beginning/ending of the link.
|
|
310
|
+
*
|
|
311
|
+
* The purpose of this action is to allow typing around the link node directly after a click.
|
|
312
|
+
*
|
|
313
|
+
* See https://github.com/ckeditor/ckeditor5/issues/1016.
|
|
314
|
+
*/
|
|
315
|
+
_enableClickingAfterLink() {
|
|
316
|
+
const editor = this.editor;
|
|
317
|
+
const model = editor.model;
|
|
318
|
+
editor.editing.view.addObserver(MouseObserver);
|
|
319
|
+
let clicked = false;
|
|
320
|
+
// Detect the click.
|
|
321
|
+
this.listenTo(editor.editing.view.document, 'mousedown', () => {
|
|
322
|
+
clicked = true;
|
|
323
|
+
});
|
|
324
|
+
// When the selection has changed...
|
|
325
|
+
this.listenTo(editor.editing.view.document, 'selectionChange', () => {
|
|
326
|
+
if (!clicked) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
// ...and it was caused by the click...
|
|
330
|
+
clicked = false;
|
|
331
|
+
const selection = model.document.selection;
|
|
332
|
+
// ...and no text is selected...
|
|
333
|
+
if (!selection.isCollapsed) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
// ...and clicked text is the link...
|
|
337
|
+
if (!selection.hasAttribute('linkHref')) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const position = selection.getFirstPosition();
|
|
341
|
+
const linkRange = findAttributeRange(position, 'linkHref', selection.getAttribute('linkHref'), model);
|
|
342
|
+
// ...check whether clicked start/end boundary of the link.
|
|
343
|
+
// If so, remove the `linkHref` attribute.
|
|
344
|
+
if (position.isTouching(linkRange.start) || position.isTouching(linkRange.end)) {
|
|
345
|
+
model.change(writer => {
|
|
346
|
+
removeLinkAttributesFromSelection(writer, getLinkAttributesAllowedOnText(model.schema));
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Starts listening to {@link module:engine/model/model~Model#deleteContent} and {@link module:engine/model/model~Model#insertContent}
|
|
353
|
+
* and checks whether typing over the link. If so, attributes of removed text are preserved and applied to the inserted text.
|
|
354
|
+
*
|
|
355
|
+
* The purpose of this action is to allow modifying a text without loosing the `linkHref` attribute (and other).
|
|
356
|
+
*
|
|
357
|
+
* See https://github.com/ckeditor/ckeditor5/issues/4762.
|
|
358
|
+
*/
|
|
359
|
+
_enableTypingOverLink() {
|
|
360
|
+
const editor = this.editor;
|
|
361
|
+
const view = editor.editing.view;
|
|
362
|
+
// Selection attributes when started typing over the link.
|
|
363
|
+
let selectionAttributes = null;
|
|
364
|
+
// Whether pressed `Backspace` or `Delete`. If so, attributes should not be preserved.
|
|
365
|
+
let deletedContent = false;
|
|
366
|
+
// Detect pressing `Backspace` / `Delete`.
|
|
367
|
+
this.listenTo(view.document, 'delete', () => {
|
|
368
|
+
deletedContent = true;
|
|
369
|
+
}, { priority: 'high' });
|
|
370
|
+
// Listening to `model#deleteContent` allows detecting whether selected content was a link.
|
|
371
|
+
// If so, before removing the element, we will copy its attributes.
|
|
372
|
+
this.listenTo(editor.model, 'deleteContent', () => {
|
|
373
|
+
const selection = editor.model.document.selection;
|
|
374
|
+
// Copy attributes only if anything is selected.
|
|
375
|
+
if (selection.isCollapsed) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
// When the content was deleted, do not preserve attributes.
|
|
379
|
+
if (deletedContent) {
|
|
380
|
+
deletedContent = false;
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
// Enabled only when typing.
|
|
384
|
+
if (!isTyping(editor)) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (shouldCopyAttributes(editor.model)) {
|
|
388
|
+
selectionAttributes = selection.getAttributes();
|
|
389
|
+
}
|
|
390
|
+
}, { priority: 'high' });
|
|
391
|
+
// Listening to `model#insertContent` allows detecting the content insertion.
|
|
392
|
+
// We want to apply attributes that were removed while typing over the link.
|
|
393
|
+
this.listenTo(editor.model, 'insertContent', (evt, [element]) => {
|
|
394
|
+
deletedContent = false;
|
|
395
|
+
// Enabled only when typing.
|
|
396
|
+
if (!isTyping(editor)) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (!selectionAttributes) {
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
editor.model.change(writer => {
|
|
403
|
+
for (const [attribute, value] of selectionAttributes) {
|
|
404
|
+
writer.setAttribute(attribute, value, element);
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
selectionAttributes = null;
|
|
408
|
+
}, { priority: 'high' });
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Starts listening to {@link module:engine/model/model~Model#deleteContent} and checks whether
|
|
412
|
+
* removing a content right after the "linkHref" attribute.
|
|
413
|
+
*
|
|
414
|
+
* If so, the selection should not preserve the `linkHref` attribute. However, if
|
|
415
|
+
* the {@link module:typing/twostepcaretmovement~TwoStepCaretMovement} plugin is active and
|
|
416
|
+
* the selection has the "linkHref" attribute due to overriden gravity (at the end), the `linkHref` attribute should stay untouched.
|
|
417
|
+
*
|
|
418
|
+
* The purpose of this action is to allow removing the link text and keep the selection outside the link.
|
|
419
|
+
*
|
|
420
|
+
* See https://github.com/ckeditor/ckeditor5/issues/7521.
|
|
421
|
+
*/
|
|
422
|
+
_handleDeleteContentAfterLink() {
|
|
423
|
+
const editor = this.editor;
|
|
424
|
+
const model = editor.model;
|
|
425
|
+
const selection = model.document.selection;
|
|
426
|
+
const view = editor.editing.view;
|
|
427
|
+
// A flag whether attributes `linkHref` attribute should be preserved.
|
|
428
|
+
let shouldPreserveAttributes = false;
|
|
429
|
+
// A flag whether the `Backspace` key was pressed.
|
|
430
|
+
let hasBackspacePressed = false;
|
|
431
|
+
// Detect pressing `Backspace`.
|
|
432
|
+
this.listenTo(view.document, 'delete', (evt, data) => {
|
|
433
|
+
hasBackspacePressed = data.direction === 'backward';
|
|
434
|
+
}, { priority: 'high' });
|
|
435
|
+
// Before removing the content, check whether the selection is inside a link or at the end of link but with 2-SCM enabled.
|
|
436
|
+
// If so, we want to preserve link attributes.
|
|
437
|
+
this.listenTo(model, 'deleteContent', () => {
|
|
438
|
+
// Reset the state.
|
|
439
|
+
shouldPreserveAttributes = false;
|
|
440
|
+
const position = selection.getFirstPosition();
|
|
441
|
+
const linkHref = selection.getAttribute('linkHref');
|
|
442
|
+
if (!linkHref) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
const linkRange = findAttributeRange(position, 'linkHref', linkHref, model);
|
|
446
|
+
// Preserve `linkHref` attribute if the selection is in the middle of the link or
|
|
447
|
+
// the selection is at the end of the link and 2-SCM is activated.
|
|
448
|
+
shouldPreserveAttributes = linkRange.containsPosition(position) || linkRange.end.isEqual(position);
|
|
449
|
+
}, { priority: 'high' });
|
|
450
|
+
// After removing the content, check whether the current selection should preserve the `linkHref` attribute.
|
|
451
|
+
this.listenTo(model, 'deleteContent', () => {
|
|
452
|
+
// If didn't press `Backspace`.
|
|
453
|
+
if (!hasBackspacePressed) {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
hasBackspacePressed = false;
|
|
457
|
+
// Disable the mechanism if inside a link (`<$text url="foo">F[]oo</$text>` or <$text url="foo">Foo[]</$text>`).
|
|
458
|
+
if (shouldPreserveAttributes) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
// Use `model.enqueueChange()` in order to execute the callback at the end of the changes process.
|
|
462
|
+
editor.model.enqueueChange(writer => {
|
|
463
|
+
removeLinkAttributesFromSelection(writer, getLinkAttributesAllowedOnText(model.schema));
|
|
464
|
+
});
|
|
465
|
+
}, { priority: 'low' });
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Enables URL fixing on pasting.
|
|
469
|
+
*/
|
|
470
|
+
_enableClipboardIntegration() {
|
|
471
|
+
const editor = this.editor;
|
|
472
|
+
const model = editor.model;
|
|
473
|
+
const defaultProtocol = this.editor.config.get('link.defaultProtocol');
|
|
474
|
+
if (!defaultProtocol) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
this.listenTo(editor.plugins.get('ClipboardPipeline'), 'contentInsertion', (evt, data) => {
|
|
478
|
+
model.change(writer => {
|
|
479
|
+
const range = writer.createRangeIn(data.content);
|
|
480
|
+
for (const item of range.getItems()) {
|
|
481
|
+
if (item.hasAttribute('linkHref')) {
|
|
482
|
+
const newLink = addLinkProtocolIfApplicable(item.getAttribute('linkHref'), defaultProtocol);
|
|
483
|
+
writer.setAttribute('linkHref', newLink, item);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Make the selection free of link-related model attributes.
|
|
492
|
+
* All link-related model attributes start with "link". That includes not only "linkHref"
|
|
493
|
+
* but also all decorator attributes (they have dynamic names), or even custom plugins.
|
|
494
|
+
*/
|
|
495
|
+
function removeLinkAttributesFromSelection(writer, linkAttributes) {
|
|
496
|
+
writer.removeSelectionAttribute('linkHref');
|
|
497
|
+
for (const attribute of linkAttributes) {
|
|
498
|
+
writer.removeSelectionAttribute(attribute);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Checks whether selection's attributes should be copied to the new inserted text.
|
|
503
|
+
*/
|
|
504
|
+
function shouldCopyAttributes(model) {
|
|
505
|
+
const selection = model.document.selection;
|
|
506
|
+
const firstPosition = selection.getFirstPosition();
|
|
507
|
+
const lastPosition = selection.getLastPosition();
|
|
508
|
+
const nodeAtFirstPosition = firstPosition.nodeAfter;
|
|
509
|
+
// The text link node does not exist...
|
|
510
|
+
if (!nodeAtFirstPosition) {
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
// ...or it isn't the text node...
|
|
514
|
+
if (!nodeAtFirstPosition.is('$text')) {
|
|
515
|
+
return false;
|
|
516
|
+
}
|
|
517
|
+
// ...or isn't the link.
|
|
518
|
+
if (!nodeAtFirstPosition.hasAttribute('linkHref')) {
|
|
519
|
+
return false;
|
|
520
|
+
}
|
|
521
|
+
// `textNode` = the position is inside the link element.
|
|
522
|
+
// `nodeBefore` = the position is at the end of the link element.
|
|
523
|
+
const nodeAtLastPosition = lastPosition.textNode || lastPosition.nodeBefore;
|
|
524
|
+
// If both references the same node selection contains a single text node.
|
|
525
|
+
if (nodeAtFirstPosition === nodeAtLastPosition) {
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
// If nodes are not equal, maybe the link nodes has defined additional attributes inside.
|
|
529
|
+
// First, we need to find the entire link range.
|
|
530
|
+
const linkRange = findAttributeRange(firstPosition, 'linkHref', nodeAtFirstPosition.getAttribute('linkHref'), model);
|
|
531
|
+
// Then we can check whether selected range is inside the found link range. If so, attributes should be preserved.
|
|
532
|
+
return linkRange.containsRange(model.createRange(firstPosition, lastPosition), true);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Checks whether provided changes were caused by typing.
|
|
536
|
+
*/
|
|
537
|
+
function isTyping(editor) {
|
|
538
|
+
const currentBatch = editor.model.change(writer => writer.batch);
|
|
539
|
+
return currentBatch.isTyping;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Returns an array containing names of the attributes allowed on `$text` that describes the link item.
|
|
543
|
+
*/
|
|
544
|
+
function getLinkAttributesAllowedOnText(schema) {
|
|
545
|
+
const textAttributes = schema.getDefinition('$text').allowAttributes;
|
|
546
|
+
return textAttributes.filter(attribute => attribute.startsWith('link'));
|
|
547
|
+
}
|