@ckeditor/ckeditor5-link 36.0.0 → 37.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,289 +2,240 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module link/linkcommand
8
7
  */
9
-
10
8
  import { Command } from 'ckeditor5/src/core';
11
9
  import { findAttributeRange } from 'ckeditor5/src/typing';
12
10
  import { Collection, first, toMap } from 'ckeditor5/src/utils';
13
-
14
11
  import AutomaticDecorators from './utils/automaticdecorators';
15
12
  import { isLinkableElement } from './utils';
16
-
17
13
  /**
18
14
  * The link command. It is used by the {@link module:link/link~Link link feature}.
19
- *
20
- * @extends module:core/command~Command
21
15
  */
22
16
  export default class LinkCommand extends Command {
23
- /**
24
- * The value of the `'linkHref'` attribute if the start of the selection is located in a node with this attribute.
25
- *
26
- * @observable
27
- * @readonly
28
- * @member {Object|undefined} #value
29
- */
30
-
31
- constructor( editor ) {
32
- super( editor );
33
-
34
- /**
35
- * A collection of {@link module:link/utils~ManualDecorator manual decorators}
36
- * corresponding to the {@link module:link/link~LinkConfig#decorators decorator configuration}.
37
- *
38
- * You can consider it a model with states of manual decorators added to the currently selected link.
39
- *
40
- * @readonly
41
- * @type {module:utils/collection~Collection}
42
- */
43
- this.manualDecorators = new Collection();
44
-
45
- /**
46
- * An instance of the helper that ties together all {@link module:link/link~LinkDecoratorAutomaticDefinition}
47
- * that are used by the {@glink features/link link} and the {@glink features/images/images-linking linking images} features.
48
- *
49
- * @readonly
50
- * @type {module:link/utils~AutomaticDecorators}
51
- */
52
- this.automaticDecorators = new AutomaticDecorators();
53
- }
54
-
55
- /**
56
- * Synchronizes the state of {@link #manualDecorators} with the currently present elements in the model.
57
- */
58
- restoreManualDecoratorStates() {
59
- for ( const manualDecorator of this.manualDecorators ) {
60
- manualDecorator.value = this._getDecoratorStateFromModel( manualDecorator.id );
61
- }
62
- }
63
-
64
- /**
65
- * @inheritDoc
66
- */
67
- refresh() {
68
- const model = this.editor.model;
69
- const selection = model.document.selection;
70
- const selectedElement = selection.getSelectedElement() || first( selection.getSelectedBlocks() );
71
-
72
- // A check for any integration that allows linking elements (e.g. `LinkImage`).
73
- // Currently the selection reads attributes from text nodes only. See #7429 and #7465.
74
- if ( isLinkableElement( selectedElement, model.schema ) ) {
75
- this.value = selectedElement.getAttribute( 'linkHref' );
76
- this.isEnabled = model.schema.checkAttribute( selectedElement, 'linkHref' );
77
- } else {
78
- this.value = selection.getAttribute( 'linkHref' );
79
- this.isEnabled = model.schema.checkAttributeInSelection( selection, 'linkHref' );
80
- }
81
-
82
- for ( const manualDecorator of this.manualDecorators ) {
83
- manualDecorator.value = this._getDecoratorStateFromModel( manualDecorator.id );
84
- }
85
- }
86
-
87
- /**
88
- * Executes the command.
89
- *
90
- * When the selection is non-collapsed, the `linkHref` attribute will be applied to nodes inside the selection, but only to
91
- * those nodes where the `linkHref` attribute is allowed (disallowed nodes will be omitted).
92
- *
93
- * When the selection is collapsed and is not inside the text with the `linkHref` attribute, a
94
- * new {@link module:engine/model/text~Text text node} with the `linkHref` attribute will be inserted in place of the caret, but
95
- * only if such element is allowed in this place. The `_data` of the inserted text will equal the `href` parameter.
96
- * The selection will be updated to wrap the just inserted text node.
97
- *
98
- * When the selection is collapsed and inside the text with the `linkHref` attribute, the attribute value will be updated.
99
- *
100
- * # Decorators and model attribute management
101
- *
102
- * There is an optional argument to this command that applies or removes model
103
- * {@glink framework/guides/architecture/editing-engine#text-attributes text attributes} brought by
104
- * {@link module:link/utils~ManualDecorator manual link decorators}.
105
- *
106
- * Text attribute names in the model correspond to the entries in the {@link module:link/link~LinkConfig#decorators configuration}.
107
- * For every decorator configured, a model text attribute exists with the "link" prefix. For example, a `'linkMyDecorator'` attribute
108
- * corresponds to `'myDecorator'` in the configuration.
109
- *
110
- * To learn more about link decorators, check out the {@link module:link/link~LinkConfig#decorators `config.link.decorators`}
111
- * documentation.
112
- *
113
- * Here is how to manage decorator attributes with the link command:
114
- *
115
- * const linkCommand = editor.commands.get( 'link' );
116
- *
117
- * // Adding a new decorator attribute.
118
- * linkCommand.execute( 'http://example.com', {
119
- * linkIsExternal: true
120
- * } );
121
- *
122
- * // Removing a decorator attribute from the selection.
123
- * linkCommand.execute( 'http://example.com', {
124
- * linkIsExternal: false
125
- * } );
126
- *
127
- * // Adding multiple decorator attributes at the same time.
128
- * linkCommand.execute( 'http://example.com', {
129
- * linkIsExternal: true,
130
- * linkIsDownloadable: true,
131
- * } );
132
- *
133
- * // Removing and adding decorator attributes at the same time.
134
- * linkCommand.execute( 'http://example.com', {
135
- * linkIsExternal: false,
136
- * linkFoo: true,
137
- * linkIsDownloadable: false,
138
- * } );
139
- *
140
- * **Note**: If the decorator attribute name is not specified, its state remains untouched.
141
- *
142
- * **Note**: {@link module:link/unlinkcommand~UnlinkCommand#execute `UnlinkCommand#execute()`} removes all
143
- * decorator attributes.
144
- *
145
- * @fires execute
146
- * @param {String} href Link destination.
147
- * @param {Object} [manualDecoratorIds={}] The information about manual decorator attributes to be applied or removed upon execution.
148
- */
149
- execute( href, manualDecoratorIds = {} ) {
150
- const model = this.editor.model;
151
- const selection = model.document.selection;
152
- // Stores information about manual decorators to turn them on/off when command is applied.
153
- const truthyManualDecorators = [];
154
- const falsyManualDecorators = [];
155
-
156
- for ( const name in manualDecoratorIds ) {
157
- if ( manualDecoratorIds[ name ] ) {
158
- truthyManualDecorators.push( name );
159
- } else {
160
- falsyManualDecorators.push( name );
161
- }
162
- }
163
-
164
- model.change( writer => {
165
- // If selection is collapsed then update selected link or insert new one at the place of caret.
166
- if ( selection.isCollapsed ) {
167
- const position = selection.getFirstPosition();
168
-
169
- // When selection is inside text with `linkHref` attribute.
170
- if ( selection.hasAttribute( 'linkHref' ) ) {
171
- // Then update `linkHref` value.
172
- const linkRange = findAttributeRange( position, 'linkHref', selection.getAttribute( 'linkHref' ), model );
173
-
174
- writer.setAttribute( 'linkHref', href, linkRange );
175
-
176
- truthyManualDecorators.forEach( item => {
177
- writer.setAttribute( item, true, linkRange );
178
- } );
179
-
180
- falsyManualDecorators.forEach( item => {
181
- writer.removeAttribute( item, linkRange );
182
- } );
183
-
184
- // Put the selection at the end of the updated link.
185
- writer.setSelection( writer.createPositionAfter( linkRange.end.nodeBefore ) );
186
- }
187
- // If not then insert text node with `linkHref` attribute in place of caret.
188
- // However, since selection is collapsed, attribute value will be used as data for text node.
189
- // So, if `href` is empty, do not create text node.
190
- else if ( href !== '' ) {
191
- const attributes = toMap( selection.getAttributes() );
192
-
193
- attributes.set( 'linkHref', href );
194
-
195
- truthyManualDecorators.forEach( item => {
196
- attributes.set( item, true );
197
- } );
198
-
199
- const { end: positionAfter } = model.insertContent( writer.createText( href, attributes ), position );
200
-
201
- // Put the selection at the end of the inserted link.
202
- // Using end of range returned from insertContent in case nodes with the same attributes got merged.
203
- writer.setSelection( positionAfter );
204
- }
205
-
206
- // Remove the `linkHref` attribute and all link decorators from the selection.
207
- // It stops adding a new content into the link element.
208
- [ 'linkHref', ...truthyManualDecorators, ...falsyManualDecorators ].forEach( item => {
209
- writer.removeSelectionAttribute( item );
210
- } );
211
- } else {
212
- // If selection has non-collapsed ranges, we change attribute on nodes inside those ranges
213
- // omitting nodes where the `linkHref` attribute is disallowed.
214
- const ranges = model.schema.getValidRanges( selection.getRanges(), 'linkHref' );
215
-
216
- // But for the first, check whether the `linkHref` attribute is allowed on selected blocks (e.g. the "image" element).
217
- const allowedRanges = [];
218
-
219
- for ( const element of selection.getSelectedBlocks() ) {
220
- if ( model.schema.checkAttribute( element, 'linkHref' ) ) {
221
- allowedRanges.push( writer.createRangeOn( element ) );
222
- }
223
- }
224
-
225
- // Ranges that accept the `linkHref` attribute. Since we will iterate over `allowedRanges`, let's clone it.
226
- const rangesToUpdate = allowedRanges.slice();
227
-
228
- // For all selection ranges we want to check whether given range is inside an element that accepts the `linkHref` attribute.
229
- // If so, we don't want to propagate applying the attribute to its children.
230
- for ( const range of ranges ) {
231
- if ( this._isRangeToUpdate( range, allowedRanges ) ) {
232
- rangesToUpdate.push( range );
233
- }
234
- }
235
-
236
- for ( const range of rangesToUpdate ) {
237
- writer.setAttribute( 'linkHref', href, range );
238
-
239
- truthyManualDecorators.forEach( item => {
240
- writer.setAttribute( item, true, range );
241
- } );
242
-
243
- falsyManualDecorators.forEach( item => {
244
- writer.removeAttribute( item, range );
245
- } );
246
- }
247
- }
248
- } );
249
- }
250
-
251
- /**
252
- * Provides information whether a decorator with a given name is present in the currently processed selection.
253
- *
254
- * @private
255
- * @param {String} decoratorName The name of the manual decorator used in the model
256
- * @returns {Boolean} The information whether a given decorator is currently present in the selection.
257
- */
258
- _getDecoratorStateFromModel( decoratorName ) {
259
- const model = this.editor.model;
260
- const selection = model.document.selection;
261
- const selectedElement = selection.getSelectedElement();
262
-
263
- // A check for the `LinkImage` plugin. If the selection contains an element, get values from the element.
264
- // Currently the selection reads attributes from text nodes only. See #7429 and #7465.
265
- if ( isLinkableElement( selectedElement, model.schema ) ) {
266
- return selectedElement.getAttribute( decoratorName );
267
- }
268
-
269
- return selection.getAttribute( decoratorName );
270
- }
271
-
272
- /**
273
- * Checks whether specified `range` is inside an element that accepts the `linkHref` attribute.
274
- *
275
- * @private
276
- * @param {module:engine/view/range~Range} range A range to check.
277
- * @param {Array.<module:engine/view/range~Range>} allowedRanges An array of ranges created on elements where the attribute is accepted.
278
- * @returns {Boolean}
279
- */
280
- _isRangeToUpdate( range, allowedRanges ) {
281
- for ( const allowedRange of allowedRanges ) {
282
- // A range is inside an element that will have the `linkHref` attribute. Do not modify its nodes.
283
- if ( allowedRange.containsRange( range ) ) {
284
- return false;
285
- }
286
- }
287
-
288
- return true;
289
- }
17
+ constructor() {
18
+ super(...arguments);
19
+ /**
20
+ * A collection of {@link module:link/utils~ManualDecorator manual decorators}
21
+ * corresponding to the {@link module:link/link~LinkConfig#decorators decorator configuration}.
22
+ *
23
+ * You can consider it a model with states of manual decorators added to the currently selected link.
24
+ */
25
+ this.manualDecorators = new Collection();
26
+ /**
27
+ * An instance of the helper that ties together all {@link module:link/link~LinkDecoratorAutomaticDefinition}
28
+ * that are used by the {@glink features/link link} and the {@glink features/images/images-linking linking images} features.
29
+ */
30
+ this.automaticDecorators = new AutomaticDecorators();
31
+ }
32
+ /**
33
+ * Synchronizes the state of {@link #manualDecorators} with the currently present elements in the model.
34
+ */
35
+ restoreManualDecoratorStates() {
36
+ for (const manualDecorator of this.manualDecorators) {
37
+ manualDecorator.value = this._getDecoratorStateFromModel(manualDecorator.id);
38
+ }
39
+ }
40
+ /**
41
+ * @inheritDoc
42
+ */
43
+ refresh() {
44
+ const model = this.editor.model;
45
+ const selection = model.document.selection;
46
+ const selectedElement = selection.getSelectedElement() || first(selection.getSelectedBlocks());
47
+ // A check for any integration that allows linking elements (e.g. `LinkImage`).
48
+ // Currently the selection reads attributes from text nodes only. See #7429 and #7465.
49
+ if (isLinkableElement(selectedElement, model.schema)) {
50
+ this.value = selectedElement.getAttribute('linkHref');
51
+ this.isEnabled = model.schema.checkAttribute(selectedElement, 'linkHref');
52
+ }
53
+ else {
54
+ this.value = selection.getAttribute('linkHref');
55
+ this.isEnabled = model.schema.checkAttributeInSelection(selection, 'linkHref');
56
+ }
57
+ for (const manualDecorator of this.manualDecorators) {
58
+ manualDecorator.value = this._getDecoratorStateFromModel(manualDecorator.id);
59
+ }
60
+ }
61
+ /**
62
+ * Executes the command.
63
+ *
64
+ * When the selection is non-collapsed, the `linkHref` attribute will be applied to nodes inside the selection, but only to
65
+ * those nodes where the `linkHref` attribute is allowed (disallowed nodes will be omitted).
66
+ *
67
+ * When the selection is collapsed and is not inside the text with the `linkHref` attribute, a
68
+ * new {@link module:engine/model/text~Text text node} with the `linkHref` attribute will be inserted in place of the caret, but
69
+ * only if such element is allowed in this place. The `_data` of the inserted text will equal the `href` parameter.
70
+ * The selection will be updated to wrap the just inserted text node.
71
+ *
72
+ * When the selection is collapsed and inside the text with the `linkHref` attribute, the attribute value will be updated.
73
+ *
74
+ * # Decorators and model attribute management
75
+ *
76
+ * There is an optional argument to this command that applies or removes model
77
+ * {@glink framework/guides/architecture/editing-engine#text-attributes text attributes} brought by
78
+ * {@link module:link/utils~ManualDecorator manual link decorators}.
79
+ *
80
+ * Text attribute names in the model correspond to the entries in the {@link module:link/link~LinkConfig#decorators configuration}.
81
+ * For every decorator configured, a model text attribute exists with the "link" prefix. For example, a `'linkMyDecorator'` attribute
82
+ * corresponds to `'myDecorator'` in the configuration.
83
+ *
84
+ * To learn more about link decorators, check out the {@link module:link/link~LinkConfig#decorators `config.link.decorators`}
85
+ * documentation.
86
+ *
87
+ * Here is how to manage decorator attributes with the link command:
88
+ *
89
+ * ```ts
90
+ * const linkCommand = editor.commands.get( 'link' );
91
+ *
92
+ * // Adding a new decorator attribute.
93
+ * linkCommand.execute( 'http://example.com', {
94
+ * linkIsExternal: true
95
+ * } );
96
+ *
97
+ * // Removing a decorator attribute from the selection.
98
+ * linkCommand.execute( 'http://example.com', {
99
+ * linkIsExternal: false
100
+ * } );
101
+ *
102
+ * // Adding multiple decorator attributes at the same time.
103
+ * linkCommand.execute( 'http://example.com', {
104
+ * linkIsExternal: true,
105
+ * linkIsDownloadable: true,
106
+ * } );
107
+ *
108
+ * // Removing and adding decorator attributes at the same time.
109
+ * linkCommand.execute( 'http://example.com', {
110
+ * linkIsExternal: false,
111
+ * linkFoo: true,
112
+ * linkIsDownloadable: false,
113
+ * } );
114
+ * ```
115
+ *
116
+ * **Note**: If the decorator attribute name is not specified, its state remains untouched.
117
+ *
118
+ * **Note**: {@link module:link/unlinkcommand~UnlinkCommand#execute `UnlinkCommand#execute()`} removes all
119
+ * decorator attributes.
120
+ *
121
+ * @fires execute
122
+ * @param href Link destination.
123
+ * @param manualDecoratorIds The information about manual decorator attributes to be applied or removed upon execution.
124
+ */
125
+ execute(href, manualDecoratorIds = {}) {
126
+ const model = this.editor.model;
127
+ const selection = model.document.selection;
128
+ // Stores information about manual decorators to turn them on/off when command is applied.
129
+ const truthyManualDecorators = [];
130
+ const falsyManualDecorators = [];
131
+ for (const name in manualDecoratorIds) {
132
+ if (manualDecoratorIds[name]) {
133
+ truthyManualDecorators.push(name);
134
+ }
135
+ else {
136
+ falsyManualDecorators.push(name);
137
+ }
138
+ }
139
+ model.change(writer => {
140
+ // If selection is collapsed then update selected link or insert new one at the place of caret.
141
+ if (selection.isCollapsed) {
142
+ const position = selection.getFirstPosition();
143
+ // When selection is inside text with `linkHref` attribute.
144
+ if (selection.hasAttribute('linkHref')) {
145
+ // Then update `linkHref` value.
146
+ const linkRange = findAttributeRange(position, 'linkHref', selection.getAttribute('linkHref'), model);
147
+ writer.setAttribute('linkHref', href, linkRange);
148
+ truthyManualDecorators.forEach(item => {
149
+ writer.setAttribute(item, true, linkRange);
150
+ });
151
+ falsyManualDecorators.forEach(item => {
152
+ writer.removeAttribute(item, linkRange);
153
+ });
154
+ // Put the selection at the end of the updated link.
155
+ writer.setSelection(writer.createPositionAfter(linkRange.end.nodeBefore));
156
+ }
157
+ // If not then insert text node with `linkHref` attribute in place of caret.
158
+ // However, since selection is collapsed, attribute value will be used as data for text node.
159
+ // So, if `href` is empty, do not create text node.
160
+ else if (href !== '') {
161
+ const attributes = toMap(selection.getAttributes());
162
+ attributes.set('linkHref', href);
163
+ truthyManualDecorators.forEach(item => {
164
+ attributes.set(item, true);
165
+ });
166
+ const { end: positionAfter } = model.insertContent(writer.createText(href, attributes), position);
167
+ // Put the selection at the end of the inserted link.
168
+ // Using end of range returned from insertContent in case nodes with the same attributes got merged.
169
+ writer.setSelection(positionAfter);
170
+ }
171
+ // Remove the `linkHref` attribute and all link decorators from the selection.
172
+ // It stops adding a new content into the link element.
173
+ ['linkHref', ...truthyManualDecorators, ...falsyManualDecorators].forEach(item => {
174
+ writer.removeSelectionAttribute(item);
175
+ });
176
+ }
177
+ else {
178
+ // If selection has non-collapsed ranges, we change attribute on nodes inside those ranges
179
+ // omitting nodes where the `linkHref` attribute is disallowed.
180
+ const ranges = model.schema.getValidRanges(selection.getRanges(), 'linkHref');
181
+ // But for the first, check whether the `linkHref` attribute is allowed on selected blocks (e.g. the "image" element).
182
+ const allowedRanges = [];
183
+ for (const element of selection.getSelectedBlocks()) {
184
+ if (model.schema.checkAttribute(element, 'linkHref')) {
185
+ allowedRanges.push(writer.createRangeOn(element));
186
+ }
187
+ }
188
+ // Ranges that accept the `linkHref` attribute. Since we will iterate over `allowedRanges`, let's clone it.
189
+ const rangesToUpdate = allowedRanges.slice();
190
+ // For all selection ranges we want to check whether given range is inside an element that accepts the `linkHref` attribute.
191
+ // If so, we don't want to propagate applying the attribute to its children.
192
+ for (const range of ranges) {
193
+ if (this._isRangeToUpdate(range, allowedRanges)) {
194
+ rangesToUpdate.push(range);
195
+ }
196
+ }
197
+ for (const range of rangesToUpdate) {
198
+ writer.setAttribute('linkHref', href, range);
199
+ truthyManualDecorators.forEach(item => {
200
+ writer.setAttribute(item, true, range);
201
+ });
202
+ falsyManualDecorators.forEach(item => {
203
+ writer.removeAttribute(item, range);
204
+ });
205
+ }
206
+ }
207
+ });
208
+ }
209
+ /**
210
+ * Provides information whether a decorator with a given name is present in the currently processed selection.
211
+ *
212
+ * @param decoratorName The name of the manual decorator used in the model
213
+ * @returns The information whether a given decorator is currently present in the selection.
214
+ */
215
+ _getDecoratorStateFromModel(decoratorName) {
216
+ const model = this.editor.model;
217
+ const selection = model.document.selection;
218
+ const selectedElement = selection.getSelectedElement();
219
+ // A check for the `LinkImage` plugin. If the selection contains an element, get values from the element.
220
+ // Currently the selection reads attributes from text nodes only. See #7429 and #7465.
221
+ if (isLinkableElement(selectedElement, model.schema)) {
222
+ return selectedElement.getAttribute(decoratorName);
223
+ }
224
+ return selection.getAttribute(decoratorName);
225
+ }
226
+ /**
227
+ * Checks whether specified `range` is inside an element that accepts the `linkHref` attribute.
228
+ *
229
+ * @param range A range to check.
230
+ * @param allowedRanges An array of ranges created on elements where the attribute is accepted.
231
+ */
232
+ _isRangeToUpdate(range, allowedRanges) {
233
+ for (const allowedRange of allowedRanges) {
234
+ // A range is inside an element that will have the `linkHref` attribute. Do not modify its nodes.
235
+ if (allowedRange.containsRange(range)) {
236
+ return false;
237
+ }
238
+ }
239
+ return true;
240
+ }
290
241
  }