@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckeditor/ckeditor5-link",
|
|
3
|
-
"version": "38.
|
|
3
|
+
"version": "38.2.0-alpha.1",
|
|
4
4
|
"description": "Link feature for CKEditor 5.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ckeditor",
|
|
@@ -11,9 +11,10 @@
|
|
|
11
11
|
"ckeditor5-dll"
|
|
12
12
|
],
|
|
13
13
|
"main": "src/index.js",
|
|
14
|
+
"type": "module",
|
|
14
15
|
"dependencies": {
|
|
15
|
-
"@ckeditor/ckeditor5-ui": "38.
|
|
16
|
-
"ckeditor5": "38.
|
|
16
|
+
"@ckeditor/ckeditor5-ui": "38.2.0-alpha.1",
|
|
17
|
+
"ckeditor5": "38.2.0-alpha.1",
|
|
17
18
|
"lodash-es": "^4.17.15"
|
|
18
19
|
},
|
|
19
20
|
"engines": {
|
package/src/augmentation.d.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
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
|
-
import type { LinkConfig, AutoLink, Link, LinkEditing, LinkImage, LinkImageEditing, LinkImageUI, LinkUI, LinkCommand, UnlinkCommand } from './index';
|
|
6
|
-
declare module '@ckeditor/ckeditor5-core' {
|
|
7
|
-
interface EditorConfig {
|
|
8
|
-
/**
|
|
9
|
-
* The configuration of the {@link module:link/link~Link} feature.
|
|
10
|
-
*
|
|
11
|
-
* Read more in {@link module:link/linkconfig~LinkConfig}.
|
|
12
|
-
*/
|
|
13
|
-
link?: LinkConfig;
|
|
14
|
-
}
|
|
15
|
-
interface PluginsMap {
|
|
16
|
-
[AutoLink.pluginName]: AutoLink;
|
|
17
|
-
[Link.pluginName]: Link;
|
|
18
|
-
[LinkEditing.pluginName]: LinkEditing;
|
|
19
|
-
[LinkImage.pluginName]: LinkImage;
|
|
20
|
-
[LinkImageEditing.pluginName]: LinkImageEditing;
|
|
21
|
-
[LinkImageUI.pluginName]: LinkImageUI;
|
|
22
|
-
[LinkUI.pluginName]: LinkUI;
|
|
23
|
-
}
|
|
24
|
-
interface CommandsMap {
|
|
25
|
-
link: LinkCommand;
|
|
26
|
-
}
|
|
27
|
-
interface CommandsMap {
|
|
28
|
-
unlink: UnlinkCommand;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
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
|
+
import type { LinkConfig, AutoLink, Link, LinkEditing, LinkImage, LinkImageEditing, LinkImageUI, LinkUI, LinkCommand, UnlinkCommand } from './index.js';
|
|
6
|
+
declare module '@ckeditor/ckeditor5-core' {
|
|
7
|
+
interface EditorConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The configuration of the {@link module:link/link~Link} feature.
|
|
10
|
+
*
|
|
11
|
+
* Read more in {@link module:link/linkconfig~LinkConfig}.
|
|
12
|
+
*/
|
|
13
|
+
link?: LinkConfig;
|
|
14
|
+
}
|
|
15
|
+
interface PluginsMap {
|
|
16
|
+
[AutoLink.pluginName]: AutoLink;
|
|
17
|
+
[Link.pluginName]: Link;
|
|
18
|
+
[LinkEditing.pluginName]: LinkEditing;
|
|
19
|
+
[LinkImage.pluginName]: LinkImage;
|
|
20
|
+
[LinkImageEditing.pluginName]: LinkImageEditing;
|
|
21
|
+
[LinkImageUI.pluginName]: LinkImageUI;
|
|
22
|
+
[LinkUI.pluginName]: LinkUI;
|
|
23
|
+
}
|
|
24
|
+
interface CommandsMap {
|
|
25
|
+
link: LinkCommand;
|
|
26
|
+
}
|
|
27
|
+
interface CommandsMap {
|
|
28
|
+
unlink: UnlinkCommand;
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/augmentation.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
|
-
export {};
|
|
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
|
+
export {};
|
package/src/autolink.d.ts
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
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/autolink
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
-
import { Delete } from 'ckeditor5/src/typing';
|
|
10
|
-
/**
|
|
11
|
-
* The autolink plugin.
|
|
12
|
-
*/
|
|
13
|
-
export default class AutoLink extends Plugin {
|
|
14
|
-
/**
|
|
15
|
-
* @inheritDoc
|
|
16
|
-
*/
|
|
17
|
-
static get requires(): readonly [typeof Delete];
|
|
18
|
-
/**
|
|
19
|
-
* @inheritDoc
|
|
20
|
-
*/
|
|
21
|
-
static get pluginName(): "AutoLink";
|
|
22
|
-
/**
|
|
23
|
-
* @inheritDoc
|
|
24
|
-
*/
|
|
25
|
-
init(): void;
|
|
26
|
-
/**
|
|
27
|
-
* @inheritDoc
|
|
28
|
-
*/
|
|
29
|
-
afterInit(): void;
|
|
30
|
-
/**
|
|
31
|
-
* Enables autolinking on typing.
|
|
32
|
-
*/
|
|
33
|
-
private _enableTypingHandling;
|
|
34
|
-
/**
|
|
35
|
-
* Enables autolinking on the <kbd>Enter</kbd> key.
|
|
36
|
-
*/
|
|
37
|
-
private _enableEnterHandling;
|
|
38
|
-
/**
|
|
39
|
-
* Enables autolinking on the <kbd>Shift</kbd>+<kbd>Enter</kbd> keyboard shortcut.
|
|
40
|
-
*/
|
|
41
|
-
private _enableShiftEnterHandling;
|
|
42
|
-
/**
|
|
43
|
-
* Checks if the passed range contains a linkable text.
|
|
44
|
-
*/
|
|
45
|
-
private _checkAndApplyAutoLinkOnRange;
|
|
46
|
-
/**
|
|
47
|
-
* Applies a link on a given range if the link should be applied.
|
|
48
|
-
*
|
|
49
|
-
* @param url The URL to link.
|
|
50
|
-
* @param range The text range to apply the link attribute to.
|
|
51
|
-
*/
|
|
52
|
-
private _applyAutoLink;
|
|
53
|
-
/**
|
|
54
|
-
* Enqueues autolink changes in the model.
|
|
55
|
-
*
|
|
56
|
-
* @param url The URL to link.
|
|
57
|
-
* @param range The text range to apply the link attribute to.
|
|
58
|
-
*/
|
|
59
|
-
private _persistAutoLink;
|
|
60
|
-
}
|
|
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/autolink
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
|
9
|
+
import { Delete } from 'ckeditor5/src/typing.js';
|
|
10
|
+
/**
|
|
11
|
+
* The autolink plugin.
|
|
12
|
+
*/
|
|
13
|
+
export default class AutoLink extends Plugin {
|
|
14
|
+
/**
|
|
15
|
+
* @inheritDoc
|
|
16
|
+
*/
|
|
17
|
+
static get requires(): readonly [typeof Delete];
|
|
18
|
+
/**
|
|
19
|
+
* @inheritDoc
|
|
20
|
+
*/
|
|
21
|
+
static get pluginName(): "AutoLink";
|
|
22
|
+
/**
|
|
23
|
+
* @inheritDoc
|
|
24
|
+
*/
|
|
25
|
+
init(): void;
|
|
26
|
+
/**
|
|
27
|
+
* @inheritDoc
|
|
28
|
+
*/
|
|
29
|
+
afterInit(): void;
|
|
30
|
+
/**
|
|
31
|
+
* Enables autolinking on typing.
|
|
32
|
+
*/
|
|
33
|
+
private _enableTypingHandling;
|
|
34
|
+
/**
|
|
35
|
+
* Enables autolinking on the <kbd>Enter</kbd> key.
|
|
36
|
+
*/
|
|
37
|
+
private _enableEnterHandling;
|
|
38
|
+
/**
|
|
39
|
+
* Enables autolinking on the <kbd>Shift</kbd>+<kbd>Enter</kbd> keyboard shortcut.
|
|
40
|
+
*/
|
|
41
|
+
private _enableShiftEnterHandling;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if the passed range contains a linkable text.
|
|
44
|
+
*/
|
|
45
|
+
private _checkAndApplyAutoLinkOnRange;
|
|
46
|
+
/**
|
|
47
|
+
* Applies a link on a given range if the link should be applied.
|
|
48
|
+
*
|
|
49
|
+
* @param url The URL to link.
|
|
50
|
+
* @param range The text range to apply the link attribute to.
|
|
51
|
+
*/
|
|
52
|
+
private _applyAutoLink;
|
|
53
|
+
/**
|
|
54
|
+
* Enqueues autolink changes in the model.
|
|
55
|
+
*
|
|
56
|
+
* @param url The URL to link.
|
|
57
|
+
* @param range The text range to apply the link attribute to.
|
|
58
|
+
*/
|
|
59
|
+
private _persistAutoLink;
|
|
60
|
+
}
|
package/src/autolink.js
CHANGED
|
@@ -1,216 +1,216 @@
|
|
|
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/autolink
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
-
import { Delete, TextWatcher, getLastTextLine } from 'ckeditor5/src/typing';
|
|
10
|
-
import { addLinkProtocolIfApplicable, linkHasProtocol } from './utils';
|
|
11
|
-
const MIN_LINK_LENGTH_WITH_SPACE_AT_END = 4; // Ie: "t.co " (length 5).
|
|
12
|
-
// This was a tweak from https://gist.github.com/dperini/729294.
|
|
13
|
-
const URL_REG_EXP = new RegExp(
|
|
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');
|
|
58
|
-
const URL_GROUP_IN_MATCH = 2;
|
|
59
|
-
/**
|
|
60
|
-
* The autolink plugin.
|
|
61
|
-
*/
|
|
62
|
-
export default class AutoLink extends Plugin {
|
|
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
|
-
}
|
|
201
|
-
}
|
|
202
|
-
// Check if text should be evaluated by the plugin in order to reduce number of RegExp checks on whole text.
|
|
203
|
-
function isSingleSpaceAtTheEnd(text) {
|
|
204
|
-
return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[text.length - 1] === ' ' && text[text.length - 2] !== ' ';
|
|
205
|
-
}
|
|
206
|
-
function getUrlAtTextEnd(text) {
|
|
207
|
-
const match = URL_REG_EXP.exec(text);
|
|
208
|
-
return match ? match[URL_GROUP_IN_MATCH] : null;
|
|
209
|
-
}
|
|
210
|
-
function isLinkAllowedOnRange(range, model) {
|
|
211
|
-
return model.schema.checkAttributeInSelection(model.createSelection(range), 'linkHref');
|
|
212
|
-
}
|
|
213
|
-
function linkIsAlreadySet(range) {
|
|
214
|
-
const item = range.start.nodeAfter;
|
|
215
|
-
return !!item && item.hasAttribute('linkHref');
|
|
216
|
-
}
|
|
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/autolink
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
|
9
|
+
import { Delete, TextWatcher, getLastTextLine } from 'ckeditor5/src/typing.js';
|
|
10
|
+
import { addLinkProtocolIfApplicable, linkHasProtocol } from './utils.js';
|
|
11
|
+
const MIN_LINK_LENGTH_WITH_SPACE_AT_END = 4; // Ie: "t.co " (length 5).
|
|
12
|
+
// This was a tweak from https://gist.github.com/dperini/729294.
|
|
13
|
+
const URL_REG_EXP = new RegExp(
|
|
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');
|
|
58
|
+
const URL_GROUP_IN_MATCH = 2;
|
|
59
|
+
/**
|
|
60
|
+
* The autolink plugin.
|
|
61
|
+
*/
|
|
62
|
+
export default class AutoLink extends Plugin {
|
|
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
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// Check if text should be evaluated by the plugin in order to reduce number of RegExp checks on whole text.
|
|
203
|
+
function isSingleSpaceAtTheEnd(text) {
|
|
204
|
+
return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[text.length - 1] === ' ' && text[text.length - 2] !== ' ';
|
|
205
|
+
}
|
|
206
|
+
function getUrlAtTextEnd(text) {
|
|
207
|
+
const match = URL_REG_EXP.exec(text);
|
|
208
|
+
return match ? match[URL_GROUP_IN_MATCH] : null;
|
|
209
|
+
}
|
|
210
|
+
function isLinkAllowedOnRange(range, model) {
|
|
211
|
+
return model.schema.checkAttributeInSelection(model.createSelection(range), 'linkHref');
|
|
212
|
+
}
|
|
213
|
+
function linkIsAlreadySet(range) {
|
|
214
|
+
const item = range.start.nodeAfter;
|
|
215
|
+
return !!item && item.hasAttribute('linkHref');
|
|
216
|
+
}
|