@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/build/link.js +1 -1
- package/package.json +30 -25
- package/src/autolink.d.ts +64 -0
- package/src/autolink.js +192 -261
- package/src/index.d.ts +14 -0
- package/src/index.js +0 -2
- package/src/link.d.ts +30 -0
- package/src/link.js +13 -241
- package/src/linkcommand.d.ts +127 -0
- package/src/linkcommand.js +224 -273
- package/src/linkconfig.d.ts +261 -0
- package/src/linkconfig.js +5 -0
- package/src/linkediting.d.ts +110 -0
- package/src/linkediting.js +516 -664
- package/src/linkimage.d.ts +30 -0
- package/src/linkimage.js +12 -19
- package/src/linkimageediting.d.ts +43 -0
- package/src/linkimageediting.js +222 -275
- package/src/linkimageui.d.ts +43 -0
- package/src/linkimageui.js +75 -101
- package/src/linkui.d.ts +170 -0
- package/src/linkui.js +557 -729
- package/src/ui/linkactionsview.d.ts +101 -0
- package/src/ui/linkactionsview.js +134 -227
- package/src/ui/linkformview.d.ts +141 -0
- package/src/ui/linkformview.js +212 -342
- package/src/unlinkcommand.d.ts +36 -0
- package/src/unlinkcommand.js +51 -65
- package/src/utils/automaticdecorators.d.ts +44 -0
- package/src/utils/automaticdecorators.js +126 -148
- package/src/utils/manualdecorator.d.ts +72 -0
- package/src/utils/manualdecorator.js +35 -89
- package/src/utils.d.ts +80 -0
- package/src/utils.js +55 -121
|
@@ -2,100 +2,46 @@
|
|
|
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
|
-
* @module link/utils
|
|
6
|
+
* @module link/utils/manualdecorator
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
|
-
import { ObservableMixin, mix } from 'ckeditor5/src/utils';
|
|
11
|
-
|
|
8
|
+
import { ObservableMixin } from 'ckeditor5/src/utils';
|
|
12
9
|
/**
|
|
13
10
|
* Helper class that stores manual decorators with observable {@link module:link/utils~ManualDecorator#value}
|
|
14
11
|
* to support integration with the UI state. An instance of this class is a model with the state of individual manual decorators.
|
|
15
12
|
* These decorators are kept as collections in {@link module:link/linkcommand~LinkCommand#manualDecorators}.
|
|
16
|
-
*
|
|
17
|
-
* @mixes module:utils/observablemixin~ObservableMixin
|
|
18
13
|
*/
|
|
19
|
-
export default class ManualDecorator {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
this.defaultValue = defaultValue;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The label used in the user interface to toggle the manual decorator.
|
|
56
|
-
*
|
|
57
|
-
* @type {String}
|
|
58
|
-
*/
|
|
59
|
-
this.label = label;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* A set of attributes added to downcasted data when the decorator is activated for a specific link.
|
|
63
|
-
* Attributes should be added in a form of attributes defined in {@link module:engine/view/elementdefinition~ElementDefinition}.
|
|
64
|
-
*
|
|
65
|
-
* @type {Object}
|
|
66
|
-
*/
|
|
67
|
-
this.attributes = attributes;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* A set of classes added to downcasted data when the decorator is activated for a specific link.
|
|
71
|
-
* Classes should be added in a form of classes defined in {@link module:engine/view/elementdefinition~ElementDefinition}.
|
|
72
|
-
*
|
|
73
|
-
* @type {Object}
|
|
74
|
-
*/
|
|
75
|
-
this.classes = classes;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* A set of styles added to downcasted data when the decorator is activated for a specific link.
|
|
79
|
-
* Styles should be added in a form of styles defined in {@link module:engine/view/elementdefinition~ElementDefinition}.
|
|
80
|
-
*
|
|
81
|
-
* @type {Object}
|
|
82
|
-
*/
|
|
83
|
-
this.styles = styles;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Returns {@link module:engine/view/matcher~MatcherPattern} with decorator attributes.
|
|
88
|
-
*
|
|
89
|
-
* @protected
|
|
90
|
-
* @returns {module:engine/view/matcher~MatcherPattern}
|
|
91
|
-
*/
|
|
92
|
-
_createPattern() {
|
|
93
|
-
return {
|
|
94
|
-
attributes: this.attributes,
|
|
95
|
-
classes: this.classes,
|
|
96
|
-
styles: this.styles
|
|
97
|
-
};
|
|
98
|
-
}
|
|
14
|
+
export default class ManualDecorator extends ObservableMixin() {
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new instance of {@link module:link/utils~ManualDecorator}.
|
|
17
|
+
*
|
|
18
|
+
* @param config.id The name of the attribute used in the model that represents a given manual decorator.
|
|
19
|
+
* For example: `'linkIsExternal'`.
|
|
20
|
+
* @param config.label The label used in the user interface to toggle the manual decorator.
|
|
21
|
+
* @param config.attributes A set of attributes added to output data when the decorator is active for a specific link.
|
|
22
|
+
* Attributes should keep the format of attributes defined in {@link module:engine/view/elementdefinition~ElementDefinition}.
|
|
23
|
+
* @param [config.defaultValue] Controls whether the decorator is "on" by default.
|
|
24
|
+
*/
|
|
25
|
+
constructor({ id, label, attributes, classes, styles, defaultValue }) {
|
|
26
|
+
super();
|
|
27
|
+
this.id = id;
|
|
28
|
+
this.set('value', undefined);
|
|
29
|
+
this.defaultValue = defaultValue;
|
|
30
|
+
this.label = label;
|
|
31
|
+
this.attributes = attributes;
|
|
32
|
+
this.classes = classes;
|
|
33
|
+
this.styles = styles;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns {@link module:engine/view/matcher~MatcherPattern} with decorator attributes.
|
|
37
|
+
*
|
|
38
|
+
* @internal
|
|
39
|
+
*/
|
|
40
|
+
_createPattern() {
|
|
41
|
+
return {
|
|
42
|
+
attributes: this.attributes,
|
|
43
|
+
classes: this.classes,
|
|
44
|
+
styles: this.styles
|
|
45
|
+
};
|
|
46
|
+
}
|
|
99
47
|
}
|
|
100
|
-
|
|
101
|
-
mix( ManualDecorator, ObservableMixin );
|
package/src/utils.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
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/utils
|
|
7
|
+
*/
|
|
8
|
+
import type { DowncastConversionApi, Element, Schema, ViewAttributeElement, ViewNode, ViewDocumentFragment } from 'ckeditor5/src/engine';
|
|
9
|
+
import type { LocaleTranslate } from 'ckeditor5/src/utils';
|
|
10
|
+
import type { LinkDecoratorAutomaticDefinition, LinkDecoratorDefinition, LinkDecoratorManualDefinition } from './linkconfig';
|
|
11
|
+
/**
|
|
12
|
+
* A keystroke used by the {@link module:link/linkui~LinkUI link UI feature}.
|
|
13
|
+
*/
|
|
14
|
+
export declare const LINK_KEYSTROKE = "Ctrl+K";
|
|
15
|
+
/**
|
|
16
|
+
* Returns `true` if a given view node is the link element.
|
|
17
|
+
*/
|
|
18
|
+
export declare function isLinkElement(node: ViewNode | ViewDocumentFragment): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a link {@link module:engine/view/attributeelement~AttributeElement} with the provided `href` attribute.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createLinkElement(href: string, { writer }: DowncastConversionApi): ViewAttributeElement;
|
|
23
|
+
/**
|
|
24
|
+
* Returns a safe URL based on a given value.
|
|
25
|
+
*
|
|
26
|
+
* A URL is considered safe if it is safe for the user (does not contain any malicious code).
|
|
27
|
+
*
|
|
28
|
+
* If a URL is considered unsafe, a simple `"#"` is returned.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare function ensureSafeUrl(url: unknown): string;
|
|
33
|
+
/**
|
|
34
|
+
* Returns the {@link module:link/link~LinkConfig#decorators `config.link.decorators`} configuration processed
|
|
35
|
+
* to respect the locale of the editor, i.e. to display the {@link module:link/link~LinkDecoratorManualDefinition label}
|
|
36
|
+
* in the correct language.
|
|
37
|
+
*
|
|
38
|
+
* **Note**: Only the few most commonly used labels are translated automatically. Other labels should be manually
|
|
39
|
+
* translated in the {@link module:link/link~LinkConfig#decorators `config.link.decorators`} configuration.
|
|
40
|
+
*
|
|
41
|
+
* @param t Shorthand for {@link module:utils/locale~Locale#t Locale#t}.
|
|
42
|
+
* @param decorators The decorator reference where the label values should be localized.
|
|
43
|
+
*/
|
|
44
|
+
export declare function getLocalizedDecorators(t: LocaleTranslate, decorators: Array<NormalizedLinkDecoratorDefinition>): Array<NormalizedLinkDecoratorDefinition>;
|
|
45
|
+
/**
|
|
46
|
+
* Converts an object with defined decorators to a normalized array of decorators. The `id` key is added for each decorator and
|
|
47
|
+
* is used as the attribute's name in the model.
|
|
48
|
+
*/
|
|
49
|
+
export declare function normalizeDecorators(decorators?: Record<string, LinkDecoratorDefinition>): Array<NormalizedLinkDecoratorDefinition>;
|
|
50
|
+
/**
|
|
51
|
+
* Returns `true` if the specified `element` can be linked (the element allows the `linkHref` attribute).
|
|
52
|
+
*/
|
|
53
|
+
export declare function isLinkableElement(element: Element | null, schema: Schema): element is Element;
|
|
54
|
+
/**
|
|
55
|
+
* Returns `true` if the specified `value` is an email.
|
|
56
|
+
*/
|
|
57
|
+
export declare function isEmail(value: string): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Adds the protocol prefix to the specified `link` when:
|
|
60
|
+
*
|
|
61
|
+
* * it does not contain it already, and there is a {@link module:link/link~LinkConfig#defaultProtocol `defaultProtocol` }
|
|
62
|
+
* configuration value provided,
|
|
63
|
+
* * or the link is an email address.
|
|
64
|
+
*/
|
|
65
|
+
export declare function addLinkProtocolIfApplicable(link: string, defaultProtocol?: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Checks if protocol is already included in the link.
|
|
68
|
+
*/
|
|
69
|
+
export declare function linkHasProtocol(link: string): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Opens the link in a new browser tab.
|
|
72
|
+
*/
|
|
73
|
+
export declare function openLink(link: string): void;
|
|
74
|
+
export type NormalizedLinkDecoratorAutomaticDefinition = LinkDecoratorAutomaticDefinition & {
|
|
75
|
+
id: string;
|
|
76
|
+
};
|
|
77
|
+
export type NormalizedLinkDecoratorManualDefinition = LinkDecoratorManualDefinition & {
|
|
78
|
+
id: string;
|
|
79
|
+
};
|
|
80
|
+
export type NormalizedLinkDecoratorDefinition = NormalizedLinkDecoratorAutomaticDefinition | NormalizedLinkDecoratorManualDefinition;
|
package/src/utils.js
CHANGED
|
@@ -2,55 +2,33 @@
|
|
|
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
|
-
/**
|
|
7
|
-
* @module link/utils
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/* global window */
|
|
11
|
-
|
|
12
5
|
import { upperFirst } from 'lodash-es';
|
|
13
|
-
|
|
14
6
|
const ATTRIBUTE_WHITESPACES = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205f\u3000]/g; // eslint-disable-line no-control-regex
|
|
15
7
|
const SAFE_URL = /^(?:(?:https?|ftps?|mailto):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/i;
|
|
16
|
-
|
|
17
8
|
// Simplified email test - should be run over previously found URL.
|
|
18
9
|
const EMAIL_REG_EXP = /^[\S]+@((?![-_])(?:[-\w\u00a1-\uffff]{0,63}[^-_]\.))+(?:[a-z\u00a1-\uffff]{2,})$/i;
|
|
19
|
-
|
|
20
10
|
// The regex checks for the protocol syntax ('xxxx://' or 'xxxx:')
|
|
21
11
|
// or non-word characters at the beginning of the link ('/', '#' etc.).
|
|
22
12
|
const PROTOCOL_REG_EXP = /^((\w+:(\/{2,})?)|(\W))/i;
|
|
23
|
-
|
|
24
13
|
/**
|
|
25
14
|
* A keystroke used by the {@link module:link/linkui~LinkUI link UI feature}.
|
|
26
15
|
*/
|
|
27
16
|
export const LINK_KEYSTROKE = 'Ctrl+K';
|
|
28
|
-
|
|
29
17
|
/**
|
|
30
18
|
* Returns `true` if a given view node is the link element.
|
|
31
|
-
*
|
|
32
|
-
* @param {module:engine/view/node~Node} node
|
|
33
|
-
* @returns {Boolean}
|
|
34
19
|
*/
|
|
35
|
-
export function isLinkElement(
|
|
36
|
-
|
|
20
|
+
export function isLinkElement(node) {
|
|
21
|
+
return node.is('attributeElement') && !!node.getCustomProperty('link');
|
|
37
22
|
}
|
|
38
|
-
|
|
39
23
|
/**
|
|
40
24
|
* Creates a link {@link module:engine/view/attributeelement~AttributeElement} with the provided `href` attribute.
|
|
41
|
-
*
|
|
42
|
-
* @param {String} href
|
|
43
|
-
* @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi
|
|
44
|
-
* @returns {module:engine/view/attributeelement~AttributeElement}
|
|
45
25
|
*/
|
|
46
|
-
export function createLinkElement(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return linkElement;
|
|
26
|
+
export function createLinkElement(href, { writer }) {
|
|
27
|
+
// Priority 5 - https://github.com/ckeditor/ckeditor5-link/issues/121.
|
|
28
|
+
const linkElement = writer.createAttributeElement('a', { href }, { priority: 5 });
|
|
29
|
+
writer.setCustomProperty('link', true, linkElement);
|
|
30
|
+
return linkElement;
|
|
52
31
|
}
|
|
53
|
-
|
|
54
32
|
/**
|
|
55
33
|
* Returns a safe URL based on a given value.
|
|
56
34
|
*
|
|
@@ -58,25 +36,19 @@ export function createLinkElement( href, { writer } ) {
|
|
|
58
36
|
*
|
|
59
37
|
* If a URL is considered unsafe, a simple `"#"` is returned.
|
|
60
38
|
*
|
|
61
|
-
* @
|
|
62
|
-
* @param {*} url
|
|
63
|
-
* @returns {String} Safe URL.
|
|
39
|
+
* @internal
|
|
64
40
|
*/
|
|
65
|
-
export function ensureSafeUrl(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return isSafeUrl( url ) ? url : '#';
|
|
41
|
+
export function ensureSafeUrl(url) {
|
|
42
|
+
const urlString = String(url);
|
|
43
|
+
return isSafeUrl(urlString) ? urlString : '#';
|
|
69
44
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return normalizedUrl.match( SAFE_URL );
|
|
45
|
+
/**
|
|
46
|
+
* Checks whether the given URL is safe for the user (does not contain any malicious code).
|
|
47
|
+
*/
|
|
48
|
+
function isSafeUrl(url) {
|
|
49
|
+
const normalizedUrl = url.replace(ATTRIBUTE_WHITESPACES, '');
|
|
50
|
+
return !!normalizedUrl.match(SAFE_URL);
|
|
78
51
|
}
|
|
79
|
-
|
|
80
52
|
/**
|
|
81
53
|
* Returns the {@link module:link/link~LinkConfig#decorators `config.link.decorators`} configuration processed
|
|
82
54
|
* to respect the locale of the editor, i.e. to display the {@link module:link/link~LinkDecoratorManualDefinition label}
|
|
@@ -85,110 +57,72 @@ function isSafeUrl( url ) {
|
|
|
85
57
|
* **Note**: Only the few most commonly used labels are translated automatically. Other labels should be manually
|
|
86
58
|
* translated in the {@link module:link/link~LinkConfig#decorators `config.link.decorators`} configuration.
|
|
87
59
|
*
|
|
88
|
-
* @param
|
|
89
|
-
* @param
|
|
90
|
-
* where the label values should be localized.
|
|
91
|
-
* @returns {Array.<module:link/link~LinkDecoratorDefinition>}
|
|
60
|
+
* @param t Shorthand for {@link module:utils/locale~Locale#t Locale#t}.
|
|
61
|
+
* @param decorators The decorator reference where the label values should be localized.
|
|
92
62
|
*/
|
|
93
|
-
export function getLocalizedDecorators(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return decorators;
|
|
63
|
+
export function getLocalizedDecorators(t, decorators) {
|
|
64
|
+
const localizedDecoratorsLabels = {
|
|
65
|
+
'Open in a new tab': t('Open in a new tab'),
|
|
66
|
+
'Downloadable': t('Downloadable')
|
|
67
|
+
};
|
|
68
|
+
decorators.forEach(decorator => {
|
|
69
|
+
if ('label' in decorator && localizedDecoratorsLabels[decorator.label]) {
|
|
70
|
+
decorator.label = localizedDecoratorsLabels[decorator.label];
|
|
71
|
+
}
|
|
72
|
+
return decorator;
|
|
73
|
+
});
|
|
74
|
+
return decorators;
|
|
107
75
|
}
|
|
108
|
-
|
|
109
76
|
/**
|
|
110
77
|
* Converts an object with defined decorators to a normalized array of decorators. The `id` key is added for each decorator and
|
|
111
78
|
* is used as the attribute's name in the model.
|
|
112
|
-
*
|
|
113
|
-
* @param {Object.<String, module:link/link~LinkDecoratorDefinition>} decorators
|
|
114
|
-
* @returns {Array.<module:link/link~LinkDecoratorDefinition>}
|
|
115
79
|
*/
|
|
116
|
-
export function normalizeDecorators(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
);
|
|
126
|
-
retArray.push( decorator );
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return retArray;
|
|
80
|
+
export function normalizeDecorators(decorators) {
|
|
81
|
+
const retArray = [];
|
|
82
|
+
if (decorators) {
|
|
83
|
+
for (const [key, value] of Object.entries(decorators)) {
|
|
84
|
+
const decorator = Object.assign({}, value, { id: `link${upperFirst(key)}` });
|
|
85
|
+
retArray.push(decorator);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return retArray;
|
|
131
89
|
}
|
|
132
|
-
|
|
133
90
|
/**
|
|
134
91
|
* Returns `true` if the specified `element` can be linked (the element allows the `linkHref` attribute).
|
|
135
|
-
*
|
|
136
|
-
* @params {module:engine/model/element~Element|null} element
|
|
137
|
-
* @params {module:engine/model/schema~Schema} schema
|
|
138
|
-
* @returns {Boolean}
|
|
139
92
|
*/
|
|
140
|
-
export function isLinkableElement(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return schema.checkAttribute( element.name, 'linkHref' );
|
|
93
|
+
export function isLinkableElement(element, schema) {
|
|
94
|
+
if (!element) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return schema.checkAttribute(element.name, 'linkHref');
|
|
146
98
|
}
|
|
147
|
-
|
|
148
99
|
/**
|
|
149
100
|
* Returns `true` if the specified `value` is an email.
|
|
150
|
-
*
|
|
151
|
-
* @params {String} value
|
|
152
|
-
* @returns {Boolean}
|
|
153
101
|
*/
|
|
154
|
-
export function isEmail(
|
|
155
|
-
|
|
102
|
+
export function isEmail(value) {
|
|
103
|
+
return EMAIL_REG_EXP.test(value);
|
|
156
104
|
}
|
|
157
|
-
|
|
158
105
|
/**
|
|
159
106
|
* Adds the protocol prefix to the specified `link` when:
|
|
160
107
|
*
|
|
161
108
|
* * it does not contain it already, and there is a {@link module:link/link~LinkConfig#defaultProtocol `defaultProtocol` }
|
|
162
109
|
* configuration value provided,
|
|
163
110
|
* * or the link is an email address.
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
* @params {String} link
|
|
167
|
-
* @params {String} defaultProtocol
|
|
168
|
-
* @returns {String}
|
|
169
111
|
*/
|
|
170
|
-
export function addLinkProtocolIfApplicable(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return link && isProtocolNeeded ? protocol + link : link;
|
|
112
|
+
export function addLinkProtocolIfApplicable(link, defaultProtocol) {
|
|
113
|
+
const protocol = isEmail(link) ? 'mailto:' : defaultProtocol;
|
|
114
|
+
const isProtocolNeeded = !!protocol && !linkHasProtocol(link);
|
|
115
|
+
return link && isProtocolNeeded ? protocol + link : link;
|
|
175
116
|
}
|
|
176
|
-
|
|
177
117
|
/**
|
|
178
118
|
* Checks if protocol is already included in the link.
|
|
179
|
-
*
|
|
180
|
-
* @param {String} link
|
|
181
|
-
* @returns {Boolean}
|
|
182
119
|
*/
|
|
183
|
-
export function linkHasProtocol(
|
|
184
|
-
|
|
120
|
+
export function linkHasProtocol(link) {
|
|
121
|
+
return PROTOCOL_REG_EXP.test(link);
|
|
185
122
|
}
|
|
186
|
-
|
|
187
123
|
/**
|
|
188
124
|
* Opens the link in a new browser tab.
|
|
189
|
-
*
|
|
190
|
-
* @param {String} link
|
|
191
125
|
*/
|
|
192
|
-
export function openLink(
|
|
193
|
-
|
|
126
|
+
export function openLink(link) {
|
|
127
|
+
window.open(link, '_blank', 'noopener');
|
|
194
128
|
}
|