@ckeditor/ckeditor5-link 36.0.1 → 37.0.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/src/autolink.js CHANGED
@@ -2,284 +2,215 @@
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/autolink
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
9
  import { Delete, TextWatcher, getLastTextLine } from 'ckeditor5/src/typing';
12
-
13
10
  import { addLinkProtocolIfApplicable, linkHasProtocol } from './utils';
14
-
15
11
  const MIN_LINK_LENGTH_WITH_SPACE_AT_END = 4; // Ie: "t.co " (length 5).
16
-
17
12
  // This was a tweak from https://gist.github.com/dperini/729294.
18
13
  const URL_REG_EXP = new RegExp(
19
- // Group 1: Line start or after a space.
20
- '(^|\\s)' +
21
- // Group 2: Detected URL (or e-mail).
22
- '(' +
23
- // Protocol identifier or short syntax "//"
24
- // a. Full form http://user@foo.bar.baz:8080/foo/bar.html#baz?foo=bar
25
- '(' +
26
- '(?:(?:(?:https?|ftp):)?\\/\\/)' +
27
- // BasicAuth using user:pass (optional)
28
- '(?:\\S+(?::\\S*)?@)?' +
29
- '(?:' +
30
- // IP address dotted notation octets
31
- // excludes loopback network 0.0.0.0
32
- // excludes reserved space >= 224.0.0.0
33
- // excludes network & broadcast addresses
34
- // (first & last IP address of each class)
35
- '(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])' +
36
- '(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}' +
37
- '(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))' +
38
- '|' +
39
- '(' +
40
- // Do not allow `www.foo` - see https://github.com/ckeditor/ckeditor5/issues/8050.
41
- '((?!www\\.)|(www\\.))' +
42
- // Host & domain names.
43
- '(?![-_])(?:[-_a-z0-9\\u00a1-\\uffff]{1,63}\\.)+' +
44
- // TLD identifier name.
45
- '(?:[a-z\\u00a1-\\uffff]{2,63})' +
46
- ')' +
47
- ')' +
48
- // port number (optional)
49
- '(?::\\d{2,5})?' +
50
- // resource path (optional)
51
- '(?:[/?#]\\S*)?' +
52
- ')' +
53
- '|' +
54
- // b. Short form (either www.example.com or example@example.com)
55
- '(' +
56
- '(www.|(\\S+@))' +
57
- // Host & domain names.
58
- '((?![-_])(?:[-_a-z0-9\\u00a1-\\uffff]{1,63}\\.))+' +
59
- // TLD identifier name.
60
- '(?:[a-z\\u00a1-\\uffff]{2,63})' +
61
- ')' +
62
- ')$', 'i' );
63
-
14
+ // Group 1: Line start or after a space.
15
+ '(^|\\s)' +
16
+ // Group 2: Detected URL (or e-mail).
17
+ '(' +
18
+ // Protocol identifier or short syntax "//"
19
+ // a. Full form http://user@foo.bar.baz:8080/foo/bar.html#baz?foo=bar
20
+ '(' +
21
+ '(?:(?:(?:https?|ftp):)?\\/\\/)' +
22
+ // BasicAuth using user:pass (optional)
23
+ '(?:\\S+(?::\\S*)?@)?' +
24
+ '(?:' +
25
+ // IP address dotted notation octets
26
+ // excludes loopback network 0.0.0.0
27
+ // excludes reserved space >= 224.0.0.0
28
+ // excludes network & broadcast addresses
29
+ // (first & last IP address of each class)
30
+ '(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])' +
31
+ '(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}' +
32
+ '(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))' +
33
+ '|' +
34
+ '(' +
35
+ // Do not allow `www.foo` - see https://github.com/ckeditor/ckeditor5/issues/8050.
36
+ '((?!www\\.)|(www\\.))' +
37
+ // Host & domain names.
38
+ '(?![-_])(?:[-_a-z0-9\\u00a1-\\uffff]{1,63}\\.)+' +
39
+ // TLD identifier name.
40
+ '(?:[a-z\\u00a1-\\uffff]{2,63})' +
41
+ ')' +
42
+ ')' +
43
+ // port number (optional)
44
+ '(?::\\d{2,5})?' +
45
+ // resource path (optional)
46
+ '(?:[/?#]\\S*)?' +
47
+ ')' +
48
+ '|' +
49
+ // b. Short form (either www.example.com or example@example.com)
50
+ '(' +
51
+ '(www.|(\\S+@))' +
52
+ // Host & domain names.
53
+ '((?![-_])(?:[-_a-z0-9\\u00a1-\\uffff]{1,63}\\.))+' +
54
+ // TLD identifier name.
55
+ '(?:[a-z\\u00a1-\\uffff]{2,63})' +
56
+ ')' +
57
+ ')$', 'i');
64
58
  const URL_GROUP_IN_MATCH = 2;
65
-
66
59
  /**
67
60
  * The autolink plugin.
68
- *
69
- * @extends module:core/plugin~Plugin
70
61
  */
71
62
  export default class AutoLink extends Plugin {
72
- /**
73
- * @inheritDoc
74
- */
75
- static get requires() {
76
- return [ Delete ];
77
- }
78
-
79
- /**
80
- * @inheritDoc
81
- */
82
- static get pluginName() {
83
- return 'AutoLink';
84
- }
85
-
86
- /**
87
- * @inheritDoc
88
- */
89
- init() {
90
- const editor = this.editor;
91
- const selection = editor.model.document.selection;
92
-
93
- selection.on( 'change:range', () => {
94
- // Disable plugin when selection is inside a code block.
95
- this.isEnabled = !selection.anchor.parent.is( 'element', 'codeBlock' );
96
- } );
97
-
98
- this._enableTypingHandling();
99
- }
100
-
101
- /**
102
- * @inheritDoc
103
- */
104
- afterInit() {
105
- this._enableEnterHandling();
106
- this._enableShiftEnterHandling();
107
- }
108
-
109
- /**
110
- * Enables autolinking on typing.
111
- *
112
- * @private
113
- */
114
- _enableTypingHandling() {
115
- const editor = this.editor;
116
-
117
- const watcher = new TextWatcher( editor.model, text => {
118
- // 1. Detect <kbd>Space</kbd> after a text with a potential link.
119
- if ( !isSingleSpaceAtTheEnd( text ) ) {
120
- return;
121
- }
122
-
123
- // 2. Check text before last typed <kbd>Space</kbd>.
124
- const url = getUrlAtTextEnd( text.substr( 0, text.length - 1 ) );
125
-
126
- if ( url ) {
127
- return { url };
128
- }
129
- } );
130
-
131
- watcher.on( 'matched:data', ( evt, data ) => {
132
- const { batch, range, url } = data;
133
-
134
- if ( !batch.isTyping ) {
135
- return;
136
- }
137
-
138
- const linkEnd = range.end.getShiftedBy( -1 ); // Executed after a space character.
139
- const linkStart = linkEnd.getShiftedBy( -url.length );
140
-
141
- const linkRange = editor.model.createRange( linkStart, linkEnd );
142
-
143
- this._applyAutoLink( url, linkRange );
144
- } );
145
-
146
- watcher.bind( 'isEnabled' ).to( this );
147
- }
148
-
149
- /**
150
- * Enables autolinking on the <kbd>Enter</kbd> key.
151
- *
152
- * @private
153
- */
154
- _enableEnterHandling() {
155
- const editor = this.editor;
156
- const model = editor.model;
157
- const enterCommand = editor.commands.get( 'enter' );
158
-
159
- if ( !enterCommand ) {
160
- return;
161
- }
162
-
163
- enterCommand.on( 'execute', () => {
164
- const position = model.document.selection.getFirstPosition();
165
-
166
- if ( !position.parent.previousSibling ) {
167
- return;
168
- }
169
-
170
- const rangeToCheck = model.createRangeIn( position.parent.previousSibling );
171
-
172
- this._checkAndApplyAutoLinkOnRange( rangeToCheck );
173
- } );
174
- }
175
-
176
- /**
177
- * Enables autolinking on the <kbd>Shift</kbd>+<kbd>Enter</kbd> keyboard shortcut.
178
- *
179
- * @private
180
- */
181
- _enableShiftEnterHandling() {
182
- const editor = this.editor;
183
- const model = editor.model;
184
-
185
- const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
186
-
187
- if ( !shiftEnterCommand ) {
188
- return;
189
- }
190
-
191
- shiftEnterCommand.on( 'execute', () => {
192
- const position = model.document.selection.getFirstPosition();
193
-
194
- const rangeToCheck = model.createRange(
195
- model.createPositionAt( position.parent, 0 ),
196
- position.getShiftedBy( -1 )
197
- );
198
-
199
- this._checkAndApplyAutoLinkOnRange( rangeToCheck );
200
- } );
201
- }
202
-
203
- /**
204
- * Checks if the passed range contains a linkable text.
205
- *
206
- * @param {module:engine/model/range~Range} rangeToCheck
207
- * @private
208
- */
209
- _checkAndApplyAutoLinkOnRange( rangeToCheck ) {
210
- const model = this.editor.model;
211
- const { text, range } = getLastTextLine( rangeToCheck, model );
212
-
213
- const url = getUrlAtTextEnd( text );
214
-
215
- if ( url ) {
216
- const linkRange = model.createRange(
217
- range.end.getShiftedBy( -url.length ),
218
- range.end
219
- );
220
-
221
- this._applyAutoLink( url, linkRange );
222
- }
223
- }
224
-
225
- /**
226
- * Applies a link on a given range if the link should be applied.
227
- *
228
- * @param {String} url The URL to link.
229
- * @param {module:engine/model/range~Range} range The text range to apply the link attribute to.
230
- * @private
231
- */
232
- _applyAutoLink( url, range ) {
233
- const model = this.editor.model;
234
-
235
- const defaultProtocol = this.editor.config.get( 'link.defaultProtocol' );
236
- const fullUrl = addLinkProtocolIfApplicable( url, defaultProtocol );
237
-
238
- if ( !this.isEnabled || !isLinkAllowedOnRange( range, model ) || !linkHasProtocol( fullUrl ) || linkIsAlreadySet( range ) ) {
239
- return;
240
- }
241
-
242
- this._persistAutoLink( fullUrl, range );
243
- }
244
-
245
- /**
246
- * Enqueues autolink changes in the model.
247
- *
248
- * @param {String} url The URL to link.
249
- * @param {module:engine/model/range~Range} range The text range to apply the link attribute to.
250
- * @protected
251
- */
252
- _persistAutoLink( url, range ) {
253
- const model = this.editor.model;
254
- const deletePlugin = this.editor.plugins.get( 'Delete' );
255
-
256
- // Enqueue change to make undo step.
257
- model.enqueueChange( writer => {
258
- writer.setAttribute( 'linkHref', url, range );
259
-
260
- model.enqueueChange( () => {
261
- deletePlugin.requestUndoOnBackspace();
262
- } );
263
- } );
264
- }
63
+ /**
64
+ * @inheritDoc
65
+ */
66
+ static get requires() {
67
+ return [Delete];
68
+ }
69
+ /**
70
+ * @inheritDoc
71
+ */
72
+ static get pluginName() {
73
+ return 'AutoLink';
74
+ }
75
+ /**
76
+ * @inheritDoc
77
+ */
78
+ init() {
79
+ const editor = this.editor;
80
+ const selection = editor.model.document.selection;
81
+ selection.on('change:range', () => {
82
+ // Disable plugin when selection is inside a code block.
83
+ this.isEnabled = !selection.anchor.parent.is('element', 'codeBlock');
84
+ });
85
+ this._enableTypingHandling();
86
+ }
87
+ /**
88
+ * @inheritDoc
89
+ */
90
+ afterInit() {
91
+ this._enableEnterHandling();
92
+ this._enableShiftEnterHandling();
93
+ }
94
+ /**
95
+ * Enables autolinking on typing.
96
+ */
97
+ _enableTypingHandling() {
98
+ const editor = this.editor;
99
+ const watcher = new TextWatcher(editor.model, text => {
100
+ // 1. Detect <kbd>Space</kbd> after a text with a potential link.
101
+ if (!isSingleSpaceAtTheEnd(text)) {
102
+ return;
103
+ }
104
+ // 2. Check text before last typed <kbd>Space</kbd>.
105
+ const url = getUrlAtTextEnd(text.substr(0, text.length - 1));
106
+ if (url) {
107
+ return { url };
108
+ }
109
+ });
110
+ watcher.on('matched:data', (evt, data) => {
111
+ const { batch, range, url } = data;
112
+ if (!batch.isTyping) {
113
+ return;
114
+ }
115
+ const linkEnd = range.end.getShiftedBy(-1); // Executed after a space character.
116
+ const linkStart = linkEnd.getShiftedBy(-url.length);
117
+ const linkRange = editor.model.createRange(linkStart, linkEnd);
118
+ this._applyAutoLink(url, linkRange);
119
+ });
120
+ watcher.bind('isEnabled').to(this);
121
+ }
122
+ /**
123
+ * Enables autolinking on the <kbd>Enter</kbd> key.
124
+ */
125
+ _enableEnterHandling() {
126
+ const editor = this.editor;
127
+ const model = editor.model;
128
+ const enterCommand = editor.commands.get('enter');
129
+ if (!enterCommand) {
130
+ return;
131
+ }
132
+ enterCommand.on('execute', () => {
133
+ const position = model.document.selection.getFirstPosition();
134
+ if (!position.parent.previousSibling) {
135
+ return;
136
+ }
137
+ const rangeToCheck = model.createRangeIn(position.parent.previousSibling);
138
+ this._checkAndApplyAutoLinkOnRange(rangeToCheck);
139
+ });
140
+ }
141
+ /**
142
+ * Enables autolinking on the <kbd>Shift</kbd>+<kbd>Enter</kbd> keyboard shortcut.
143
+ */
144
+ _enableShiftEnterHandling() {
145
+ const editor = this.editor;
146
+ const model = editor.model;
147
+ const shiftEnterCommand = editor.commands.get('shiftEnter');
148
+ if (!shiftEnterCommand) {
149
+ return;
150
+ }
151
+ shiftEnterCommand.on('execute', () => {
152
+ const position = model.document.selection.getFirstPosition();
153
+ const rangeToCheck = model.createRange(model.createPositionAt(position.parent, 0), position.getShiftedBy(-1));
154
+ this._checkAndApplyAutoLinkOnRange(rangeToCheck);
155
+ });
156
+ }
157
+ /**
158
+ * Checks if the passed range contains a linkable text.
159
+ */
160
+ _checkAndApplyAutoLinkOnRange(rangeToCheck) {
161
+ const model = this.editor.model;
162
+ const { text, range } = getLastTextLine(rangeToCheck, model);
163
+ const url = getUrlAtTextEnd(text);
164
+ if (url) {
165
+ const linkRange = model.createRange(range.end.getShiftedBy(-url.length), range.end);
166
+ this._applyAutoLink(url, linkRange);
167
+ }
168
+ }
169
+ /**
170
+ * Applies a link on a given range if the link should be applied.
171
+ *
172
+ * @param url The URL to link.
173
+ * @param range The text range to apply the link attribute to.
174
+ */
175
+ _applyAutoLink(url, range) {
176
+ const model = this.editor.model;
177
+ const defaultProtocol = this.editor.config.get('link.defaultProtocol');
178
+ const fullUrl = addLinkProtocolIfApplicable(url, defaultProtocol);
179
+ if (!this.isEnabled || !isLinkAllowedOnRange(range, model) || !linkHasProtocol(fullUrl) || linkIsAlreadySet(range)) {
180
+ return;
181
+ }
182
+ this._persistAutoLink(fullUrl, range);
183
+ }
184
+ /**
185
+ * Enqueues autolink changes in the model.
186
+ *
187
+ * @param url The URL to link.
188
+ * @param range The text range to apply the link attribute to.
189
+ */
190
+ _persistAutoLink(url, range) {
191
+ const model = this.editor.model;
192
+ const deletePlugin = this.editor.plugins.get('Delete');
193
+ // Enqueue change to make undo step.
194
+ model.enqueueChange(writer => {
195
+ writer.setAttribute('linkHref', url, range);
196
+ model.enqueueChange(() => {
197
+ deletePlugin.requestUndoOnBackspace();
198
+ });
199
+ });
200
+ }
265
201
  }
266
-
267
202
  // Check if text should be evaluated by the plugin in order to reduce number of RegExp checks on whole text.
268
- function isSingleSpaceAtTheEnd( text ) {
269
- return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' && text[ text.length - 2 ] !== ' ';
203
+ function isSingleSpaceAtTheEnd(text) {
204
+ return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[text.length - 1] === ' ' && text[text.length - 2] !== ' ';
270
205
  }
271
-
272
- function getUrlAtTextEnd( text ) {
273
- const match = URL_REG_EXP.exec( text );
274
-
275
- return match ? match[ URL_GROUP_IN_MATCH ] : null;
206
+ function getUrlAtTextEnd(text) {
207
+ const match = URL_REG_EXP.exec(text);
208
+ return match ? match[URL_GROUP_IN_MATCH] : null;
276
209
  }
277
-
278
- function isLinkAllowedOnRange( range, model ) {
279
- return model.schema.checkAttributeInSelection( model.createSelection( range ), 'linkHref' );
210
+ function isLinkAllowedOnRange(range, model) {
211
+ return model.schema.checkAttributeInSelection(model.createSelection(range), 'linkHref');
280
212
  }
281
-
282
- function linkIsAlreadySet( range ) {
283
- const item = range.start.nodeAfter;
284
- return item && item.hasAttribute( 'linkHref' );
213
+ function linkIsAlreadySet(range) {
214
+ const item = range.start.nodeAfter;
215
+ return !!item && item.hasAttribute('linkHref');
285
216
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,18 @@
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
7
+ */
8
+ export { default as Link } from './link';
9
+ export { default as LinkEditing } from './linkediting';
10
+ export { default as LinkUI } from './linkui';
11
+ export { default as LinkImage } from './linkimage';
12
+ export { default as LinkImageEditing } from './linkimageediting';
13
+ export { default as LinkImageUI } from './linkimageui';
14
+ export { default as AutoLink } from './autolink';
15
+ export { LinkConfig } from './linkconfig';
16
+ export { default as LinkCommand } from './linkcommand';
17
+ export { default as UnlinkCommand } from './unlinkcommand';
18
+ import './augmentation';
package/src/index.js CHANGED
@@ -2,11 +2,9 @@
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
8
7
  */
9
-
10
8
  export { default as Link } from './link';
11
9
  export { default as LinkEditing } from './linkediting';
12
10
  export { default as LinkUI } from './linkui';
@@ -14,3 +12,6 @@ export { default as LinkImage } from './linkimage';
14
12
  export { default as LinkImageEditing } from './linkimageediting';
15
13
  export { default as LinkImageUI } from './linkimageui';
16
14
  export { default as AutoLink } from './autolink';
15
+ export { default as LinkCommand } from './linkcommand';
16
+ export { default as UnlinkCommand } from './unlinkcommand';
17
+ import './augmentation';
package/src/link.d.ts ADDED
@@ -0,0 +1,24 @@
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/link
7
+ */
8
+ import { Plugin, type PluginDependencies } from 'ckeditor5/src/core';
9
+ /**
10
+ * The link plugin.
11
+ *
12
+ * This is a "glue" plugin that loads the {@link module:link/linkediting~LinkEditing link editing feature}
13
+ * and {@link module:link/linkui~LinkUI link UI feature}.
14
+ */
15
+ export default class Link extends Plugin {
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ static get requires(): PluginDependencies;
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ static get pluginName(): 'Link';
24
+ }