@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.
package/src/link.js CHANGED
@@ -2,259 +2,31 @@
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/link
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
9
  import LinkEditing from './linkediting';
12
10
  import LinkUI from './linkui';
13
11
  import AutoLink from './autolink';
14
-
12
+ import './linkconfig';
15
13
  /**
16
14
  * The link plugin.
17
15
  *
18
16
  * This is a "glue" plugin that loads the {@link module:link/linkediting~LinkEditing link editing feature}
19
17
  * and {@link module:link/linkui~LinkUI link UI feature}.
20
- *
21
- * @extends module:core/plugin~Plugin
22
18
  */
23
19
  export default class Link extends Plugin {
24
- /**
25
- * @inheritDoc
26
- */
27
- static get requires() {
28
- return [ LinkEditing, LinkUI, AutoLink ];
29
- }
30
-
31
- /**
32
- * @inheritDoc
33
- */
34
- static get pluginName() {
35
- return 'Link';
36
- }
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ static get requires() {
24
+ return [LinkEditing, LinkUI, AutoLink];
25
+ }
26
+ /**
27
+ * @inheritDoc
28
+ */
29
+ static get pluginName() {
30
+ return 'Link';
31
+ }
37
32
  }
38
-
39
- /**
40
- * The configuration of the {@link module:link/link~Link} feature.
41
- *
42
- * Read more in {@link module:link/link~LinkConfig}.
43
- *
44
- * @member {module:link/link~LinkConfig} module:core/editor/editorconfig~EditorConfig#link
45
- */
46
-
47
- /**
48
- * The configuration of the {@link module:link/link~Link link feature}.
49
- *
50
- * ClassicEditor
51
- * .create( editorElement, {
52
- * link: ... // Link feature configuration.
53
- * } )
54
- * .then( ... )
55
- * .catch( ... );
56
- *
57
- * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
58
- * @interface LinkConfig
59
- */
60
-
61
- /**
62
- * When set, the editor will add the given protocol to the link when the user creates a link without one.
63
- * For example, when the user is creating a link and types `ckeditor.com` in the link form input, during link submission
64
- * the editor will automatically add the `http://` protocol, so the link will look as follows: `http://ckeditor.com`.
65
- *
66
- * The feature also provides email address auto-detection. When you submit `hello@example.com`,
67
- * the plugin will automatically change it to `mailto:hello@example.com`.
68
- *
69
- * ClassicEditor
70
- * .create( editorElement, {
71
- * link: {
72
- * defaultProtocol: 'http://'
73
- * }
74
- * } )
75
- * .then( ... )
76
- * .catch( ... );
77
- *
78
- * **NOTE:** If no configuration is provided, the editor will not auto-fix the links.
79
- *
80
- * @member {String} module:link/link~LinkConfig#defaultProtocol
81
- */
82
-
83
- /**
84
- * When set to `true`, the `target="blank"` and `rel="noopener noreferrer"` attributes are automatically added to all external links
85
- * in the editor. "External links" are all links in the editor content starting with `http`, `https`, or `//`.
86
- *
87
- * ClassicEditor
88
- * .create( editorElement, {
89
- * link: {
90
- * addTargetToExternalLinks: true
91
- * }
92
- * } )
93
- * .then( ... )
94
- * .catch( ... );
95
- *
96
- * Internally, this option activates a predefined {@link module:link/link~LinkConfig#decorators automatic link decorator}
97
- * that extends all external links with the `target` and `rel` attributes.
98
- *
99
- * **Note**: To control the `target` and `rel` attributes of specific links in the edited content, a dedicated
100
- * {@link module:link/link~LinkDecoratorManualDefinition manual} decorator must be defined in the
101
- * {@link module:link/link~LinkConfig#decorators `config.link.decorators`} array. In such scenario,
102
- * the `config.link.addTargetToExternalLinks` option should remain `undefined` or `false` to not interfere with the manual decorator.
103
- *
104
- * It is possible to add other {@link module:link/link~LinkDecoratorAutomaticDefinition automatic}
105
- * or {@link module:link/link~LinkDecoratorManualDefinition manual} link decorators when this option is active.
106
- *
107
- * More information about decorators can be found in the {@link module:link/link~LinkConfig#decorators decorators configuration}
108
- * reference.
109
- *
110
- * @default false
111
- * @member {Boolean} module:link/link~LinkConfig#addTargetToExternalLinks
112
- */
113
-
114
- /**
115
- * Decorators provide an easy way to configure and manage additional link attributes in the editor content. There are
116
- * two types of link decorators:
117
- *
118
- * * {@link module:link/link~LinkDecoratorAutomaticDefinition Automatic} – They match links against pre–defined rules and
119
- * manage their attributes based on the results.
120
- * * {@link module:link/link~LinkDecoratorManualDefinition Manual} – They allow users to control link attributes individually,
121
- * using the editor UI.
122
- *
123
- * Link decorators are defined as objects with key-value pairs, where the key is the name provided for a given decorator and the
124
- * value is the decorator definition.
125
- *
126
- * The name of the decorator also corresponds to the {@glink framework/guides/architecture/editing-engine#text-attributes text attribute}
127
- * in the model. For instance, the `isExternal` decorator below is represented as a `linkIsExternal` attribute in the model.
128
- *
129
- * ClassicEditor
130
- * .create( editorElement, {
131
- * link: {
132
- * decorators: {
133
- * isExternal: {
134
- * mode: 'automatic',
135
- * callback: url => url.startsWith( 'http://' ),
136
- * attributes: {
137
- * target: '_blank',
138
- * rel: 'noopener noreferrer'
139
- * }
140
- * },
141
- * isDownloadable: {
142
- * mode: 'manual',
143
- * label: 'Downloadable',
144
- * attributes: {
145
- * download: 'file.png',
146
- * }
147
- * },
148
- * // ...
149
- * }
150
- * }
151
- * } )
152
- * .then( ... )
153
- * .catch( ... );
154
- *
155
- * To learn more about the configuration syntax, check out the {@link module:link/link~LinkDecoratorAutomaticDefinition automatic}
156
- * and {@link module:link/link~LinkDecoratorManualDefinition manual} decorator option reference.
157
- *
158
- * **Warning:** Currently, link decorators work independently of one another and no conflict resolution mechanism exists.
159
- * For example, configuring the `target` attribute using both an automatic and a manual decorator at the same time could end up with
160
- * quirky results. The same applies if multiple manual or automatic decorators were defined for the same attribute.
161
- *
162
- * **Note**: Since the `target` attribute management for external links is a common use case, there is a predefined automatic decorator
163
- * dedicated for that purpose which can be enabled by turning a single option on. Check out the
164
- * {@link module:link/link~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}
165
- * configuration description to learn more.
166
- *
167
- * See also the {@glink features/link#custom-link-attributes-decorators link feature guide} for more information.
168
- *
169
- * @member {Object.<String, module:link/link~LinkDecoratorDefinition>} module:link/link~LinkConfig#decorators
170
- */
171
-
172
- /**
173
- * A link decorator definition. Two types implement this defition:
174
- *
175
- * * {@link module:link/link~LinkDecoratorManualDefinition}
176
- * * {@link module:link/link~LinkDecoratorAutomaticDefinition}
177
- *
178
- * Refer to their document for more information about available options or to the
179
- * {@glink features/link#custom-link-attributes-decorators link feature guide} for general information.
180
- *
181
- * @interface LinkDecoratorDefinition
182
- */
183
-
184
- /**
185
- * Link decorator type.
186
- *
187
- * Check out the {@glink features/link#custom-link-attributes-decorators link feature guide} for more information.
188
- *
189
- * @member {'manual'|'automatic'} module:link/link~LinkDecoratorDefinition#mode
190
- */
191
-
192
- /**
193
- * Describes an automatic {@link module:link/link~LinkConfig#decorators link decorator}. This decorator type matches
194
- * all links in the editor content against a function that decides whether the link should receive a pre–defined set of attributes.
195
- *
196
- * It takes an object with key-value pairs of attributes and a callback function that must return a Boolean value based on the link's
197
- * `href` (URL). When the callback returns `true`, attributes are applied to the link.
198
- *
199
- * For example, to add the `target="_blank"` attribute to all links in the editor starting with `http://`, the
200
- * configuration could look like this:
201
- *
202
- * {
203
- * mode: 'automatic',
204
- * callback: url => url.startsWith( 'http://' ),
205
- * attributes: {
206
- * target: '_blank'
207
- * }
208
- * }
209
- *
210
- * **Note**: Since the `target` attribute management for external links is a common use case, there is a predefined automatic decorator
211
- * dedicated for that purpose that can be enabled by turning a single option on. Check out the
212
- * {@link module:link/link~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}
213
- * configuration description to learn more.
214
- *
215
- * @typedef {Object} module:link/link~LinkDecoratorAutomaticDefinition
216
- * @property {'automatic'} mode Link decorator type. It is `'automatic'` for all automatic decorators.
217
- * @property {Function} callback Takes a `url` as a parameter and returns `true` if the `attributes` should be applied to the link.
218
- * @property {Object} [attributes] Key-value pairs used as link attributes added to the output during the
219
- * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
220
- * Attributes should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
221
- * @property {Object} [styles] Key-value pairs used as link styles added to the output during the
222
- * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
223
- * Styles should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
224
- * @property {String|Array.<String>} [classes] Class names used as link classes added to the output during the
225
- * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
226
- * Classes should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
227
- */
228
-
229
- /**
230
- * Describes a manual {@link module:link/link~LinkConfig#decorators link decorator}. This decorator type is represented in
231
- * the link feature's {@link module:link/linkui user interface} as a switch that the user can use to control the presence
232
- * of a predefined set of attributes.
233
- *
234
- * For instance, to allow the users to manually control the presence of the `target="_blank"` and
235
- * `rel="noopener noreferrer"` attributes on specific links, the decorator could look as follows:
236
- *
237
- * {
238
- * mode: 'manual',
239
- * label: 'Open in a new tab',
240
- * defaultValue: true,
241
- * attributes: {
242
- * target: '_blank',
243
- * rel: 'noopener noreferrer'
244
- * }
245
- * }
246
- *
247
- * @typedef {Object} module:link/link~LinkDecoratorManualDefinition
248
- * @property {'manual'} mode Link decorator type. It is `'manual'` for all manual decorators.
249
- * @property {String} label The label of the UI button that the user can use to control the presence of link attributes.
250
- * @property {Object} [attributes] Key-value pairs used as link attributes added to the output during the
251
- * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
252
- * Attributes should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
253
- * @property {Object} [styles] Key-value pairs used as link styles added to the output during the
254
- * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
255
- * Styles should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
256
- * @property {String|Array.<String>} [classes] Class names used as link classes added to the output during the
257
- * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
258
- * Classes should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
259
- * @property {Boolean} [defaultValue] Controls whether the decorator is "on" by default.
260
- */
@@ -0,0 +1,127 @@
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/linkcommand
7
+ */
8
+ import { Command } from 'ckeditor5/src/core';
9
+ import { Collection } from 'ckeditor5/src/utils';
10
+ import AutomaticDecorators from './utils/automaticdecorators';
11
+ import type ManualDecorator from './utils/manualdecorator';
12
+ /**
13
+ * The link command. It is used by the {@link module:link/link~Link link feature}.
14
+ */
15
+ export default class LinkCommand extends Command {
16
+ /**
17
+ * The value of the `'linkHref'` attribute if the start of the selection is located in a node with this attribute.
18
+ *
19
+ * @observable
20
+ * @readonly
21
+ */
22
+ value: string | undefined;
23
+ /**
24
+ * A collection of {@link module:link/utils~ManualDecorator manual decorators}
25
+ * corresponding to the {@link module:link/link~LinkConfig#decorators decorator configuration}.
26
+ *
27
+ * You can consider it a model with states of manual decorators added to the currently selected link.
28
+ */
29
+ readonly manualDecorators: Collection<ManualDecorator>;
30
+ /**
31
+ * An instance of the helper that ties together all {@link module:link/link~LinkDecoratorAutomaticDefinition}
32
+ * that are used by the {@glink features/link link} and the {@glink features/images/images-linking linking images} features.
33
+ */
34
+ readonly automaticDecorators: AutomaticDecorators;
35
+ /**
36
+ * Synchronizes the state of {@link #manualDecorators} with the currently present elements in the model.
37
+ */
38
+ restoreManualDecoratorStates(): void;
39
+ /**
40
+ * @inheritDoc
41
+ */
42
+ refresh(): void;
43
+ /**
44
+ * Executes the command.
45
+ *
46
+ * When the selection is non-collapsed, the `linkHref` attribute will be applied to nodes inside the selection, but only to
47
+ * those nodes where the `linkHref` attribute is allowed (disallowed nodes will be omitted).
48
+ *
49
+ * When the selection is collapsed and is not inside the text with the `linkHref` attribute, a
50
+ * new {@link module:engine/model/text~Text text node} with the `linkHref` attribute will be inserted in place of the caret, but
51
+ * only if such element is allowed in this place. The `_data` of the inserted text will equal the `href` parameter.
52
+ * The selection will be updated to wrap the just inserted text node.
53
+ *
54
+ * When the selection is collapsed and inside the text with the `linkHref` attribute, the attribute value will be updated.
55
+ *
56
+ * # Decorators and model attribute management
57
+ *
58
+ * There is an optional argument to this command that applies or removes model
59
+ * {@glink framework/guides/architecture/editing-engine#text-attributes text attributes} brought by
60
+ * {@link module:link/utils~ManualDecorator manual link decorators}.
61
+ *
62
+ * Text attribute names in the model correspond to the entries in the {@link module:link/link~LinkConfig#decorators configuration}.
63
+ * For every decorator configured, a model text attribute exists with the "link" prefix. For example, a `'linkMyDecorator'` attribute
64
+ * corresponds to `'myDecorator'` in the configuration.
65
+ *
66
+ * To learn more about link decorators, check out the {@link module:link/link~LinkConfig#decorators `config.link.decorators`}
67
+ * documentation.
68
+ *
69
+ * Here is how to manage decorator attributes with the link command:
70
+ *
71
+ * ```ts
72
+ * const linkCommand = editor.commands.get( 'link' );
73
+ *
74
+ * // Adding a new decorator attribute.
75
+ * linkCommand.execute( 'http://example.com', {
76
+ * linkIsExternal: true
77
+ * } );
78
+ *
79
+ * // Removing a decorator attribute from the selection.
80
+ * linkCommand.execute( 'http://example.com', {
81
+ * linkIsExternal: false
82
+ * } );
83
+ *
84
+ * // Adding multiple decorator attributes at the same time.
85
+ * linkCommand.execute( 'http://example.com', {
86
+ * linkIsExternal: true,
87
+ * linkIsDownloadable: true,
88
+ * } );
89
+ *
90
+ * // Removing and adding decorator attributes at the same time.
91
+ * linkCommand.execute( 'http://example.com', {
92
+ * linkIsExternal: false,
93
+ * linkFoo: true,
94
+ * linkIsDownloadable: false,
95
+ * } );
96
+ * ```
97
+ *
98
+ * **Note**: If the decorator attribute name is not specified, its state remains untouched.
99
+ *
100
+ * **Note**: {@link module:link/unlinkcommand~UnlinkCommand#execute `UnlinkCommand#execute()`} removes all
101
+ * decorator attributes.
102
+ *
103
+ * @fires execute
104
+ * @param href Link destination.
105
+ * @param manualDecoratorIds The information about manual decorator attributes to be applied or removed upon execution.
106
+ */
107
+ execute(href: string, manualDecoratorIds?: Record<string, boolean>): void;
108
+ /**
109
+ * Provides information whether a decorator with a given name is present in the currently processed selection.
110
+ *
111
+ * @param decoratorName The name of the manual decorator used in the model
112
+ * @returns The information whether a given decorator is currently present in the selection.
113
+ */
114
+ private _getDecoratorStateFromModel;
115
+ /**
116
+ * Checks whether specified `range` is inside an element that accepts the `linkHref` attribute.
117
+ *
118
+ * @param range A range to check.
119
+ * @param allowedRanges An array of ranges created on elements where the attribute is accepted.
120
+ */
121
+ private _isRangeToUpdate;
122
+ }
123
+ declare module '@ckeditor/ckeditor5-core' {
124
+ interface CommandsMap {
125
+ link: LinkCommand;
126
+ }
127
+ }