@ekz/lexical-link 0.40.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/ClickableLinkExtension.d.ts +22 -0
- package/EkzLexicalLink.dev.js +1190 -0
- package/EkzLexicalLink.dev.mjs +1172 -0
- package/EkzLexicalLink.js +11 -0
- package/EkzLexicalLink.mjs +28 -0
- package/EkzLexicalLink.node.mjs +26 -0
- package/EkzLexicalLink.prod.js +9 -0
- package/EkzLexicalLink.prod.mjs +9 -0
- package/LICENSE +21 -0
- package/LexicalAutoLinkExtension.d.ts +41 -0
- package/LexicalLink.js.flow +168 -0
- package/LexicalLinkExtension.d.ts +37 -0
- package/LexicalLinkNode.d.ts +126 -0
- package/README.md +7 -0
- package/index.d.ts +14 -0
- package/package.json +43 -0
|
@@ -0,0 +1,1172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { addClassNamesToElement, isHTMLAnchorElement, $findMatchingParent, mergeRegister, objectKlassEquals } from '@ekz/lexical-utils';
|
|
10
|
+
import { createCommand, ElementNode, $isRangeSelection, $applyNodeReplacement, $isElementNode, $getSelection, $isNodeSelection, $normalizeSelection__EXPERIMENTAL, $setSelection, defineExtension, shallowMergeConfig, COMMAND_PRIORITY_LOW, PASTE_COMMAND, safeCast, isDOMNode, getNearestEditorFromDOMNode, $getNearestNodeFromDOMNode, TextNode, $isTextNode, $isLineBreakNode, $createTextNode } from '@ekz/lexical';
|
|
11
|
+
import { namedSignals, effect } from '@ekz/lexical-extension';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
15
|
+
*
|
|
16
|
+
* This source code is licensed under the MIT license found in the
|
|
17
|
+
* LICENSE file in the root directory of this source tree.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
// Do not require this module directly! Use normal `invariant` calls.
|
|
22
|
+
|
|
23
|
+
function formatDevErrorMessage(message) {
|
|
24
|
+
throw new Error(message);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const SUPPORTED_URL_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'sms:', 'tel:']);
|
|
28
|
+
|
|
29
|
+
/** @noInheritDoc */
|
|
30
|
+
class LinkNode extends ElementNode {
|
|
31
|
+
/** @internal */
|
|
32
|
+
__url;
|
|
33
|
+
/** @internal */
|
|
34
|
+
__target;
|
|
35
|
+
/** @internal */
|
|
36
|
+
__rel;
|
|
37
|
+
/** @internal */
|
|
38
|
+
__title;
|
|
39
|
+
static getType() {
|
|
40
|
+
return 'link';
|
|
41
|
+
}
|
|
42
|
+
static clone(node) {
|
|
43
|
+
return new LinkNode(node.__url, {
|
|
44
|
+
rel: node.__rel,
|
|
45
|
+
target: node.__target,
|
|
46
|
+
title: node.__title
|
|
47
|
+
}, node.__key);
|
|
48
|
+
}
|
|
49
|
+
constructor(url = '', attributes = {}, key) {
|
|
50
|
+
super(key);
|
|
51
|
+
const {
|
|
52
|
+
target = null,
|
|
53
|
+
rel = null,
|
|
54
|
+
title = null
|
|
55
|
+
} = attributes;
|
|
56
|
+
this.__url = url;
|
|
57
|
+
this.__target = target;
|
|
58
|
+
this.__rel = rel;
|
|
59
|
+
this.__title = title;
|
|
60
|
+
}
|
|
61
|
+
createDOM(config) {
|
|
62
|
+
const element = document.createElement('a');
|
|
63
|
+
this.updateLinkDOM(null, element, config);
|
|
64
|
+
addClassNamesToElement(element, config.theme.link);
|
|
65
|
+
return element;
|
|
66
|
+
}
|
|
67
|
+
updateLinkDOM(prevNode, anchor, config) {
|
|
68
|
+
if (isHTMLAnchorElement(anchor)) {
|
|
69
|
+
if (!prevNode || prevNode.__url !== this.__url) {
|
|
70
|
+
anchor.href = this.sanitizeUrl(this.__url);
|
|
71
|
+
}
|
|
72
|
+
for (const attr of ['target', 'rel', 'title']) {
|
|
73
|
+
const key = `__${attr}`;
|
|
74
|
+
const value = this[key];
|
|
75
|
+
if (!prevNode || prevNode[key] !== value) {
|
|
76
|
+
if (value) {
|
|
77
|
+
anchor[attr] = value;
|
|
78
|
+
} else {
|
|
79
|
+
anchor.removeAttribute(attr);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
updateDOM(prevNode, anchor, config) {
|
|
86
|
+
this.updateLinkDOM(prevNode, anchor, config);
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
static importDOM() {
|
|
90
|
+
return {
|
|
91
|
+
a: node => ({
|
|
92
|
+
conversion: $convertAnchorElement,
|
|
93
|
+
priority: 1
|
|
94
|
+
})
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
static importJSON(serializedNode) {
|
|
98
|
+
return $createLinkNode().updateFromJSON(serializedNode);
|
|
99
|
+
}
|
|
100
|
+
updateFromJSON(serializedNode) {
|
|
101
|
+
return super.updateFromJSON(serializedNode).setURL(serializedNode.url).setRel(serializedNode.rel || null).setTarget(serializedNode.target || null).setTitle(serializedNode.title || null);
|
|
102
|
+
}
|
|
103
|
+
sanitizeUrl(url) {
|
|
104
|
+
url = formatUrl(url);
|
|
105
|
+
try {
|
|
106
|
+
const parsedUrl = new URL(formatUrl(url));
|
|
107
|
+
// eslint-disable-next-line no-script-url
|
|
108
|
+
if (!SUPPORTED_URL_PROTOCOLS.has(parsedUrl.protocol)) {
|
|
109
|
+
return 'about:blank';
|
|
110
|
+
}
|
|
111
|
+
} catch (_unused) {
|
|
112
|
+
return url;
|
|
113
|
+
}
|
|
114
|
+
return url;
|
|
115
|
+
}
|
|
116
|
+
exportJSON() {
|
|
117
|
+
return {
|
|
118
|
+
...super.exportJSON(),
|
|
119
|
+
rel: this.getRel(),
|
|
120
|
+
target: this.getTarget(),
|
|
121
|
+
title: this.getTitle(),
|
|
122
|
+
url: this.getURL()
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
getURL() {
|
|
126
|
+
return this.getLatest().__url;
|
|
127
|
+
}
|
|
128
|
+
setURL(url) {
|
|
129
|
+
const writable = this.getWritable();
|
|
130
|
+
writable.__url = url;
|
|
131
|
+
return writable;
|
|
132
|
+
}
|
|
133
|
+
getTarget() {
|
|
134
|
+
return this.getLatest().__target;
|
|
135
|
+
}
|
|
136
|
+
setTarget(target) {
|
|
137
|
+
const writable = this.getWritable();
|
|
138
|
+
writable.__target = target;
|
|
139
|
+
return writable;
|
|
140
|
+
}
|
|
141
|
+
getRel() {
|
|
142
|
+
return this.getLatest().__rel;
|
|
143
|
+
}
|
|
144
|
+
setRel(rel) {
|
|
145
|
+
const writable = this.getWritable();
|
|
146
|
+
writable.__rel = rel;
|
|
147
|
+
return writable;
|
|
148
|
+
}
|
|
149
|
+
getTitle() {
|
|
150
|
+
return this.getLatest().__title;
|
|
151
|
+
}
|
|
152
|
+
setTitle(title) {
|
|
153
|
+
const writable = this.getWritable();
|
|
154
|
+
writable.__title = title;
|
|
155
|
+
return writable;
|
|
156
|
+
}
|
|
157
|
+
insertNewAfter(_, restoreSelection = true) {
|
|
158
|
+
const linkNode = $createLinkNode(this.__url, {
|
|
159
|
+
rel: this.__rel,
|
|
160
|
+
target: this.__target,
|
|
161
|
+
title: this.__title
|
|
162
|
+
});
|
|
163
|
+
this.insertAfter(linkNode, restoreSelection);
|
|
164
|
+
return linkNode;
|
|
165
|
+
}
|
|
166
|
+
canInsertTextBefore() {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
canInsertTextAfter() {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
canBeEmpty() {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
isInline() {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
extractWithChild(child, selection, destination) {
|
|
179
|
+
if (!$isRangeSelection(selection)) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
const anchorNode = selection.anchor.getNode();
|
|
183
|
+
const focusNode = selection.focus.getNode();
|
|
184
|
+
return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && selection.getTextContent().length > 0;
|
|
185
|
+
}
|
|
186
|
+
isEmailURI() {
|
|
187
|
+
return this.__url.startsWith('mailto:');
|
|
188
|
+
}
|
|
189
|
+
isWebSiteURI() {
|
|
190
|
+
return this.__url.startsWith('https://') || this.__url.startsWith('http://');
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function $convertAnchorElement(domNode) {
|
|
194
|
+
let node = null;
|
|
195
|
+
if (isHTMLAnchorElement(domNode)) {
|
|
196
|
+
const content = domNode.textContent;
|
|
197
|
+
if (content !== null && content !== '' || domNode.children.length > 0) {
|
|
198
|
+
node = $createLinkNode(domNode.getAttribute('href') || '', {
|
|
199
|
+
rel: domNode.getAttribute('rel'),
|
|
200
|
+
target: domNode.getAttribute('target'),
|
|
201
|
+
title: domNode.getAttribute('title')
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
node
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Takes a URL and creates a LinkNode.
|
|
212
|
+
* @param url - The URL the LinkNode should direct to.
|
|
213
|
+
* @param attributes - Optional HTML a tag attributes \\{ target, rel, title \\}
|
|
214
|
+
* @returns The LinkNode.
|
|
215
|
+
*/
|
|
216
|
+
function $createLinkNode(url = '', attributes) {
|
|
217
|
+
return $applyNodeReplacement(new LinkNode(url, attributes));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Determines if node is a LinkNode.
|
|
222
|
+
* @param node - The node to be checked.
|
|
223
|
+
* @returns true if node is a LinkNode, false otherwise.
|
|
224
|
+
*/
|
|
225
|
+
function $isLinkNode(node) {
|
|
226
|
+
return node instanceof LinkNode;
|
|
227
|
+
}
|
|
228
|
+
// Custom node type to override `canInsertTextAfter` that will
|
|
229
|
+
// allow typing within the link
|
|
230
|
+
class AutoLinkNode extends LinkNode {
|
|
231
|
+
/** @internal */
|
|
232
|
+
/** Indicates whether the autolink was ever unlinked. **/
|
|
233
|
+
__isUnlinked;
|
|
234
|
+
constructor(url = '', attributes = {}, key) {
|
|
235
|
+
super(url, attributes, key);
|
|
236
|
+
this.__isUnlinked = attributes.isUnlinked !== undefined && attributes.isUnlinked !== null ? attributes.isUnlinked : false;
|
|
237
|
+
}
|
|
238
|
+
static getType() {
|
|
239
|
+
return 'autolink';
|
|
240
|
+
}
|
|
241
|
+
static clone(node) {
|
|
242
|
+
return new AutoLinkNode(node.__url, {
|
|
243
|
+
isUnlinked: node.__isUnlinked,
|
|
244
|
+
rel: node.__rel,
|
|
245
|
+
target: node.__target,
|
|
246
|
+
title: node.__title
|
|
247
|
+
}, node.__key);
|
|
248
|
+
}
|
|
249
|
+
getIsUnlinked() {
|
|
250
|
+
return this.__isUnlinked;
|
|
251
|
+
}
|
|
252
|
+
setIsUnlinked(value) {
|
|
253
|
+
const self = this.getWritable();
|
|
254
|
+
self.__isUnlinked = value;
|
|
255
|
+
return self;
|
|
256
|
+
}
|
|
257
|
+
createDOM(config) {
|
|
258
|
+
if (this.__isUnlinked) {
|
|
259
|
+
return document.createElement('span');
|
|
260
|
+
} else {
|
|
261
|
+
return super.createDOM(config);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
updateDOM(prevNode, anchor, config) {
|
|
265
|
+
return super.updateDOM(prevNode, anchor, config) || prevNode.__isUnlinked !== this.__isUnlinked;
|
|
266
|
+
}
|
|
267
|
+
static importJSON(serializedNode) {
|
|
268
|
+
return $createAutoLinkNode().updateFromJSON(serializedNode);
|
|
269
|
+
}
|
|
270
|
+
updateFromJSON(serializedNode) {
|
|
271
|
+
return super.updateFromJSON(serializedNode).setIsUnlinked(serializedNode.isUnlinked || false);
|
|
272
|
+
}
|
|
273
|
+
static importDOM() {
|
|
274
|
+
// TODO: Should link node should handle the import over autolink?
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
exportJSON() {
|
|
278
|
+
return {
|
|
279
|
+
...super.exportJSON(),
|
|
280
|
+
isUnlinked: this.__isUnlinked
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
insertNewAfter(selection, restoreSelection = true) {
|
|
284
|
+
const element = this.getParentOrThrow().insertNewAfter(selection, restoreSelection);
|
|
285
|
+
if ($isElementNode(element)) {
|
|
286
|
+
const linkNode = $createAutoLinkNode(this.__url, {
|
|
287
|
+
isUnlinked: this.__isUnlinked,
|
|
288
|
+
rel: this.__rel,
|
|
289
|
+
target: this.__target,
|
|
290
|
+
title: this.__title
|
|
291
|
+
});
|
|
292
|
+
element.append(linkNode);
|
|
293
|
+
return linkNode;
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Takes a URL and creates an AutoLinkNode. AutoLinkNodes are generally automatically generated
|
|
301
|
+
* during typing, which is especially useful when a button to generate a LinkNode is not practical.
|
|
302
|
+
* @param url - The URL the LinkNode should direct to.
|
|
303
|
+
* @param attributes - Optional HTML a tag attributes. \\{ target, rel, title \\}
|
|
304
|
+
* @returns The LinkNode.
|
|
305
|
+
*/
|
|
306
|
+
function $createAutoLinkNode(url = '', attributes) {
|
|
307
|
+
return $applyNodeReplacement(new AutoLinkNode(url, attributes));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Determines if node is an AutoLinkNode.
|
|
312
|
+
* @param node - The node to be checked.
|
|
313
|
+
* @returns true if node is an AutoLinkNode, false otherwise.
|
|
314
|
+
*/
|
|
315
|
+
function $isAutoLinkNode(node) {
|
|
316
|
+
return node instanceof AutoLinkNode;
|
|
317
|
+
}
|
|
318
|
+
const TOGGLE_LINK_COMMAND = createCommand('TOGGLE_LINK_COMMAND');
|
|
319
|
+
function $getPointNode(point, offset) {
|
|
320
|
+
if (point.type === 'element') {
|
|
321
|
+
const node = point.getNode();
|
|
322
|
+
if (!$isElementNode(node)) {
|
|
323
|
+
formatDevErrorMessage(`$getPointNode: element point is not an ElementNode`);
|
|
324
|
+
}
|
|
325
|
+
const childNode = node.getChildren()[point.offset + offset];
|
|
326
|
+
return childNode || null;
|
|
327
|
+
}
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Preserve the logical start/end of a RangeSelection in situations where
|
|
333
|
+
* the point is an element that may be reparented in the callback.
|
|
334
|
+
*
|
|
335
|
+
* @param $fn The function to run
|
|
336
|
+
* @returns The result of the callback
|
|
337
|
+
*/
|
|
338
|
+
function $withSelectedNodes($fn) {
|
|
339
|
+
const initialSelection = $getSelection();
|
|
340
|
+
if (!$isRangeSelection(initialSelection)) {
|
|
341
|
+
return $fn();
|
|
342
|
+
}
|
|
343
|
+
const normalized = $normalizeSelection__EXPERIMENTAL(initialSelection);
|
|
344
|
+
const isBackwards = normalized.isBackward();
|
|
345
|
+
const anchorNode = $getPointNode(normalized.anchor, isBackwards ? -1 : 0);
|
|
346
|
+
const focusNode = $getPointNode(normalized.focus, isBackwards ? 0 : -1);
|
|
347
|
+
const rval = $fn();
|
|
348
|
+
if (anchorNode || focusNode) {
|
|
349
|
+
const updatedSelection = $getSelection();
|
|
350
|
+
if ($isRangeSelection(updatedSelection)) {
|
|
351
|
+
const finalSelection = updatedSelection.clone();
|
|
352
|
+
if (anchorNode) {
|
|
353
|
+
const anchorParent = anchorNode.getParent();
|
|
354
|
+
if (anchorParent) {
|
|
355
|
+
finalSelection.anchor.set(anchorParent.getKey(), anchorNode.getIndexWithinParent() + (isBackwards ? 1 : 0), 'element');
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (focusNode) {
|
|
359
|
+
const focusParent = focusNode.getParent();
|
|
360
|
+
if (focusParent) {
|
|
361
|
+
finalSelection.focus.set(focusParent.getKey(), focusNode.getIndexWithinParent() + (isBackwards ? 0 : 1), 'element');
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
$setSelection($normalizeSelection__EXPERIMENTAL(finalSelection));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return rval;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Splits a LinkNode by removing selected children from it.
|
|
372
|
+
* Handles three cases: selection at start, end, or middle of the link.
|
|
373
|
+
* @param parentLink - The LinkNode to split
|
|
374
|
+
* @param extractedNodes - The nodes that were extracted from the selection
|
|
375
|
+
*/
|
|
376
|
+
function $splitLinkAtSelection(parentLink, extractedNodes) {
|
|
377
|
+
const extractedKeys = new Set(extractedNodes.filter(n => parentLink.isParentOf(n)).map(n => n.getKey()));
|
|
378
|
+
const allChildren = parentLink.getChildren();
|
|
379
|
+
const extractedChildren = allChildren.filter(child => extractedKeys.has(child.getKey()));
|
|
380
|
+
if (extractedChildren.length === allChildren.length) {
|
|
381
|
+
allChildren.forEach(child => parentLink.insertBefore(child));
|
|
382
|
+
parentLink.remove();
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
const firstExtractedIndex = allChildren.findIndex(child => extractedKeys.has(child.getKey()));
|
|
386
|
+
const lastExtractedIndex = allChildren.findLastIndex(child => extractedKeys.has(child.getKey()));
|
|
387
|
+
const isAtStart = firstExtractedIndex === 0;
|
|
388
|
+
const isAtEnd = lastExtractedIndex === allChildren.length - 1;
|
|
389
|
+
if (isAtStart) {
|
|
390
|
+
extractedChildren.forEach(child => parentLink.insertBefore(child));
|
|
391
|
+
} else if (isAtEnd) {
|
|
392
|
+
for (let i = extractedChildren.length - 1; i >= 0; i--) {
|
|
393
|
+
parentLink.insertAfter(extractedChildren[i]);
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
for (let i = extractedChildren.length - 1; i >= 0; i--) {
|
|
397
|
+
parentLink.insertAfter(extractedChildren[i]);
|
|
398
|
+
}
|
|
399
|
+
const trailingChildren = allChildren.slice(lastExtractedIndex + 1);
|
|
400
|
+
if (trailingChildren.length > 0) {
|
|
401
|
+
const newLink = $createLinkNode(parentLink.getURL(), {
|
|
402
|
+
rel: parentLink.getRel(),
|
|
403
|
+
target: parentLink.getTarget(),
|
|
404
|
+
title: parentLink.getTitle()
|
|
405
|
+
});
|
|
406
|
+
extractedChildren[extractedChildren.length - 1].insertAfter(newLink);
|
|
407
|
+
trailingChildren.forEach(child => newLink.append(child));
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Generates or updates a LinkNode. It can also delete a LinkNode if the URL is null,
|
|
414
|
+
* but saves any children and brings them up to the parent node.
|
|
415
|
+
* @param urlOrAttributes - The URL the link directs to, or an attributes object with an url property
|
|
416
|
+
* @param attributes - Optional HTML a tag attributes. \\{ target, rel, title \\}
|
|
417
|
+
*/
|
|
418
|
+
function $toggleLink(urlOrAttributes, attributes = {}) {
|
|
419
|
+
let url;
|
|
420
|
+
if (urlOrAttributes && typeof urlOrAttributes === 'object') {
|
|
421
|
+
const {
|
|
422
|
+
url: urlProp,
|
|
423
|
+
...rest
|
|
424
|
+
} = urlOrAttributes;
|
|
425
|
+
url = urlProp;
|
|
426
|
+
attributes = {
|
|
427
|
+
...rest,
|
|
428
|
+
...attributes
|
|
429
|
+
};
|
|
430
|
+
} else {
|
|
431
|
+
url = urlOrAttributes;
|
|
432
|
+
}
|
|
433
|
+
const {
|
|
434
|
+
target,
|
|
435
|
+
title
|
|
436
|
+
} = attributes;
|
|
437
|
+
const rel = attributes.rel === undefined ? 'noreferrer' : attributes.rel;
|
|
438
|
+
const selection = $getSelection();
|
|
439
|
+
if (selection === null || !$isRangeSelection(selection) && !$isNodeSelection(selection)) {
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if ($isNodeSelection(selection)) {
|
|
443
|
+
const nodes = selection.getNodes();
|
|
444
|
+
if (nodes.length === 0) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// Handle all selected nodes
|
|
449
|
+
nodes.forEach(node => {
|
|
450
|
+
if (url === null) {
|
|
451
|
+
// Remove link
|
|
452
|
+
const linkParent = $findMatchingParent(node, parent => !$isAutoLinkNode(parent) && $isLinkNode(parent));
|
|
453
|
+
if (linkParent) {
|
|
454
|
+
linkParent.insertBefore(node);
|
|
455
|
+
if (linkParent.getChildren().length === 0) {
|
|
456
|
+
linkParent.remove();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
} else {
|
|
460
|
+
// Add/Update link
|
|
461
|
+
const existingLink = $findMatchingParent(node, parent => !$isAutoLinkNode(parent) && $isLinkNode(parent));
|
|
462
|
+
if (existingLink) {
|
|
463
|
+
existingLink.setURL(url);
|
|
464
|
+
if (target !== undefined) {
|
|
465
|
+
existingLink.setTarget(target);
|
|
466
|
+
}
|
|
467
|
+
if (rel !== undefined) {
|
|
468
|
+
existingLink.setRel(rel);
|
|
469
|
+
}
|
|
470
|
+
} else {
|
|
471
|
+
const linkNode = $createLinkNode(url, {
|
|
472
|
+
rel,
|
|
473
|
+
target
|
|
474
|
+
});
|
|
475
|
+
node.insertBefore(linkNode);
|
|
476
|
+
linkNode.append(node);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Handle RangeSelection
|
|
484
|
+
const nodes = selection.extract();
|
|
485
|
+
if (url === null) {
|
|
486
|
+
const processedLinks = new Set();
|
|
487
|
+
nodes.forEach(node => {
|
|
488
|
+
const parentLink = node.getParent();
|
|
489
|
+
if ($isLinkNode(parentLink) && !$isAutoLinkNode(parentLink)) {
|
|
490
|
+
const linkKey = parentLink.getKey();
|
|
491
|
+
if (processedLinks.has(linkKey)) {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
$splitLinkAtSelection(parentLink, nodes);
|
|
495
|
+
processedLinks.add(linkKey);
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const updatedNodes = new Set();
|
|
501
|
+
const updateLinkNode = linkNode => {
|
|
502
|
+
if (updatedNodes.has(linkNode.getKey())) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
updatedNodes.add(linkNode.getKey());
|
|
506
|
+
linkNode.setURL(url);
|
|
507
|
+
if (target !== undefined) {
|
|
508
|
+
linkNode.setTarget(target);
|
|
509
|
+
}
|
|
510
|
+
if (rel !== undefined) {
|
|
511
|
+
linkNode.setRel(rel);
|
|
512
|
+
}
|
|
513
|
+
if (title !== undefined) {
|
|
514
|
+
linkNode.setTitle(title);
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
// Add or merge LinkNodes
|
|
518
|
+
if (nodes.length === 1) {
|
|
519
|
+
const firstNode = nodes[0];
|
|
520
|
+
// if the first node is a LinkNode or if its
|
|
521
|
+
// parent is a LinkNode, we update the URL, target and rel.
|
|
522
|
+
const linkNode = $findMatchingParent(firstNode, $isLinkNode);
|
|
523
|
+
if (linkNode !== null) {
|
|
524
|
+
return updateLinkNode(linkNode);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
$withSelectedNodes(() => {
|
|
528
|
+
let linkNode = null;
|
|
529
|
+
for (const node of nodes) {
|
|
530
|
+
if (!node.isAttached()) {
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
const parentLinkNode = $findMatchingParent(node, $isLinkNode);
|
|
534
|
+
if (parentLinkNode) {
|
|
535
|
+
updateLinkNode(parentLinkNode);
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
if ($isElementNode(node)) {
|
|
539
|
+
if (!node.isInline()) {
|
|
540
|
+
// Ignore block nodes, if there are any children we will see them
|
|
541
|
+
// later and wrap in a new LinkNode
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
if ($isLinkNode(node)) {
|
|
545
|
+
// If it's not an autolink node and we don't already have a LinkNode
|
|
546
|
+
// in this block then we can update it and re-use it
|
|
547
|
+
if (!$isAutoLinkNode(node) && (linkNode === null || !linkNode.getParentOrThrow().isParentOf(node))) {
|
|
548
|
+
updateLinkNode(node);
|
|
549
|
+
linkNode = node;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
// Unwrap LinkNode, we already have one or it's an AutoLinkNode
|
|
553
|
+
for (const child of node.getChildren()) {
|
|
554
|
+
node.insertBefore(child);
|
|
555
|
+
}
|
|
556
|
+
node.remove();
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
const prevLinkNode = node.getPreviousSibling();
|
|
561
|
+
if ($isLinkNode(prevLinkNode) && prevLinkNode.is(linkNode)) {
|
|
562
|
+
prevLinkNode.append(node);
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
linkNode = $createLinkNode(url, {
|
|
566
|
+
rel,
|
|
567
|
+
target,
|
|
568
|
+
title
|
|
569
|
+
});
|
|
570
|
+
node.insertAfter(linkNode);
|
|
571
|
+
linkNode.append(node);
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
const PHONE_NUMBER_REGEX = /^\+?[0-9\s()-]{5,}$/;
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Formats a URL string by adding appropriate protocol if missing
|
|
579
|
+
*
|
|
580
|
+
* @param url - URL to format
|
|
581
|
+
* @returns Formatted URL with appropriate protocol
|
|
582
|
+
*/
|
|
583
|
+
function formatUrl(url) {
|
|
584
|
+
// Check if URL already has a protocol
|
|
585
|
+
if (url.match(/^[a-z][a-z0-9+.-]*:/i)) {
|
|
586
|
+
// URL already has a protocol, leave it as is
|
|
587
|
+
return url;
|
|
588
|
+
}
|
|
589
|
+
// Check if it's a relative path (starting with '/', '.', or '#')
|
|
590
|
+
else if (url.match(/^[/#.]/)) {
|
|
591
|
+
// Relative path, leave it as is
|
|
592
|
+
return url;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// Check for email address
|
|
596
|
+
else if (url.includes('@')) {
|
|
597
|
+
return `mailto:${url}`;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// Check for phone number
|
|
601
|
+
else if (PHONE_NUMBER_REGEX.test(url)) {
|
|
602
|
+
return `tel:${url}`;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// For everything else, return with https:// prefix
|
|
606
|
+
return `https://${url}`;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
611
|
+
*
|
|
612
|
+
* This source code is licensed under the MIT license found in the
|
|
613
|
+
* LICENSE file in the root directory of this source tree.
|
|
614
|
+
*
|
|
615
|
+
*/
|
|
616
|
+
|
|
617
|
+
const defaultProps = {
|
|
618
|
+
attributes: undefined,
|
|
619
|
+
validateUrl: undefined
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
/** @internal */
|
|
623
|
+
function registerLink(editor, stores) {
|
|
624
|
+
return mergeRegister(effect(() => editor.registerCommand(TOGGLE_LINK_COMMAND, payload => {
|
|
625
|
+
const validateUrl = stores.validateUrl.peek();
|
|
626
|
+
const attributes = stores.attributes.peek();
|
|
627
|
+
if (payload === null) {
|
|
628
|
+
$toggleLink(null);
|
|
629
|
+
return true;
|
|
630
|
+
} else if (typeof payload === 'string') {
|
|
631
|
+
if (validateUrl === undefined || validateUrl(payload)) {
|
|
632
|
+
$toggleLink(payload, attributes);
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
return false;
|
|
636
|
+
} else {
|
|
637
|
+
const {
|
|
638
|
+
url,
|
|
639
|
+
target,
|
|
640
|
+
rel,
|
|
641
|
+
title
|
|
642
|
+
} = payload;
|
|
643
|
+
$toggleLink(url, {
|
|
644
|
+
...attributes,
|
|
645
|
+
rel,
|
|
646
|
+
target,
|
|
647
|
+
title
|
|
648
|
+
});
|
|
649
|
+
return true;
|
|
650
|
+
}
|
|
651
|
+
}, COMMAND_PRIORITY_LOW)), effect(() => {
|
|
652
|
+
const validateUrl = stores.validateUrl.value;
|
|
653
|
+
if (!validateUrl) {
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
const attributes = stores.attributes.value;
|
|
657
|
+
return editor.registerCommand(PASTE_COMMAND, event => {
|
|
658
|
+
const selection = $getSelection();
|
|
659
|
+
if (!$isRangeSelection(selection) || selection.isCollapsed() || !objectKlassEquals(event, ClipboardEvent)) {
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
if (event.clipboardData === null) {
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
const clipboardText = event.clipboardData.getData('text');
|
|
666
|
+
if (!validateUrl(clipboardText)) {
|
|
667
|
+
return false;
|
|
668
|
+
}
|
|
669
|
+
// If we select nodes that are elements then avoid applying the link.
|
|
670
|
+
if (!selection.getNodes().some(node => $isElementNode(node))) {
|
|
671
|
+
editor.dispatchCommand(TOGGLE_LINK_COMMAND, {
|
|
672
|
+
...attributes,
|
|
673
|
+
url: clipboardText
|
|
674
|
+
});
|
|
675
|
+
event.preventDefault();
|
|
676
|
+
return true;
|
|
677
|
+
}
|
|
678
|
+
return false;
|
|
679
|
+
}, COMMAND_PRIORITY_LOW);
|
|
680
|
+
}));
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Provides {@link LinkNode}, an implementation of
|
|
685
|
+
* {@link TOGGLE_LINK_COMMAND}, and a {@link PASTE_COMMAND}
|
|
686
|
+
* listener to wrap selected nodes in a link when a
|
|
687
|
+
* URL is pasted and `validateUrl` is defined.
|
|
688
|
+
*/
|
|
689
|
+
const LinkExtension = defineExtension({
|
|
690
|
+
build(editor, config, state) {
|
|
691
|
+
return namedSignals(config);
|
|
692
|
+
},
|
|
693
|
+
config: defaultProps,
|
|
694
|
+
mergeConfig(config, overrides) {
|
|
695
|
+
const merged = shallowMergeConfig(config, overrides);
|
|
696
|
+
if (config.attributes) {
|
|
697
|
+
merged.attributes = shallowMergeConfig(config.attributes, merged.attributes);
|
|
698
|
+
}
|
|
699
|
+
return merged;
|
|
700
|
+
},
|
|
701
|
+
name: '@ekz/lexical-link/Link',
|
|
702
|
+
nodes: () => [LinkNode],
|
|
703
|
+
register(editor, config, state) {
|
|
704
|
+
return registerLink(editor, state.getOutput());
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
710
|
+
*
|
|
711
|
+
* This source code is licensed under the MIT license found in the
|
|
712
|
+
* LICENSE file in the root directory of this source tree.
|
|
713
|
+
*
|
|
714
|
+
*/
|
|
715
|
+
|
|
716
|
+
function findMatchingDOM(startNode, predicate) {
|
|
717
|
+
let node = startNode;
|
|
718
|
+
while (node != null) {
|
|
719
|
+
if (predicate(node)) {
|
|
720
|
+
return node;
|
|
721
|
+
}
|
|
722
|
+
node = node.parentNode;
|
|
723
|
+
}
|
|
724
|
+
return null;
|
|
725
|
+
}
|
|
726
|
+
function registerClickableLink(editor, stores, eventOptions = {}) {
|
|
727
|
+
const onClick = event => {
|
|
728
|
+
const target = event.target;
|
|
729
|
+
if (!isDOMNode(target)) {
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
const nearestEditor = getNearestEditorFromDOMNode(target);
|
|
733
|
+
if (nearestEditor === null) {
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
let url = null;
|
|
737
|
+
let urlTarget = null;
|
|
738
|
+
nearestEditor.update(() => {
|
|
739
|
+
const clickedNode = $getNearestNodeFromDOMNode(target);
|
|
740
|
+
if (clickedNode !== null) {
|
|
741
|
+
const maybeLinkNode = $findMatchingParent(clickedNode, $isElementNode);
|
|
742
|
+
if (!stores.disabled.peek()) {
|
|
743
|
+
if ($isLinkNode(maybeLinkNode)) {
|
|
744
|
+
url = maybeLinkNode.sanitizeUrl(maybeLinkNode.getURL());
|
|
745
|
+
urlTarget = maybeLinkNode.getTarget();
|
|
746
|
+
} else {
|
|
747
|
+
const a = findMatchingDOM(target, isHTMLAnchorElement);
|
|
748
|
+
if (a !== null) {
|
|
749
|
+
url = a.href;
|
|
750
|
+
urlTarget = a.target;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
if (url === null || url === '') {
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// Allow user to select link text without following url
|
|
761
|
+
const selection = editor.getEditorState().read($getSelection);
|
|
762
|
+
if ($isRangeSelection(selection) && !selection.isCollapsed()) {
|
|
763
|
+
event.preventDefault();
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
const isMiddle = event.type === 'auxclick' && event.button === 1;
|
|
767
|
+
window.open(url, stores.newTab.peek() || isMiddle || event.metaKey || event.ctrlKey || urlTarget === '_blank' ? '_blank' : '_self');
|
|
768
|
+
event.preventDefault();
|
|
769
|
+
};
|
|
770
|
+
const onMouseUp = event => {
|
|
771
|
+
if (event.button === 1) {
|
|
772
|
+
onClick(event);
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
return editor.registerRootListener((rootElement, prevRootElement) => {
|
|
776
|
+
if (prevRootElement !== null) {
|
|
777
|
+
prevRootElement.removeEventListener('click', onClick);
|
|
778
|
+
prevRootElement.removeEventListener('mouseup', onMouseUp);
|
|
779
|
+
}
|
|
780
|
+
if (rootElement !== null) {
|
|
781
|
+
rootElement.addEventListener('click', onClick, eventOptions);
|
|
782
|
+
rootElement.addEventListener('mouseup', onMouseUp, eventOptions);
|
|
783
|
+
}
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* Normally in a Lexical editor the `CLICK_COMMAND` on a LinkNode will cause the
|
|
789
|
+
* selection to change instead of opening a link. This extension can be used to
|
|
790
|
+
* restore the default behavior, e.g. when the editor is not editable.
|
|
791
|
+
*/
|
|
792
|
+
const ClickableLinkExtension = defineExtension({
|
|
793
|
+
build(editor, config, state) {
|
|
794
|
+
return namedSignals(config);
|
|
795
|
+
},
|
|
796
|
+
config: safeCast({
|
|
797
|
+
disabled: false,
|
|
798
|
+
newTab: false
|
|
799
|
+
}),
|
|
800
|
+
dependencies: [LinkExtension],
|
|
801
|
+
name: '@ekz/lexical-link/ClickableLink',
|
|
802
|
+
register(editor, config, state) {
|
|
803
|
+
return registerClickableLink(editor, state.getOutput());
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
809
|
+
*
|
|
810
|
+
* This source code is licensed under the MIT license found in the
|
|
811
|
+
* LICENSE file in the root directory of this source tree.
|
|
812
|
+
*
|
|
813
|
+
*/
|
|
814
|
+
|
|
815
|
+
function createLinkMatcherWithRegExp(regExp, urlTransformer = text => text) {
|
|
816
|
+
return text => {
|
|
817
|
+
const match = regExp.exec(text);
|
|
818
|
+
if (match === null) {
|
|
819
|
+
return null;
|
|
820
|
+
}
|
|
821
|
+
return {
|
|
822
|
+
index: match.index,
|
|
823
|
+
length: match[0].length,
|
|
824
|
+
text: match[0],
|
|
825
|
+
url: urlTransformer(match[0])
|
|
826
|
+
};
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
function findFirstMatch(text, matchers) {
|
|
830
|
+
for (let i = 0; i < matchers.length; i++) {
|
|
831
|
+
const match = matchers[i](text);
|
|
832
|
+
if (match) {
|
|
833
|
+
return match;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
return null;
|
|
837
|
+
}
|
|
838
|
+
const PUNCTUATION_OR_SPACE = /[.,;\s]/;
|
|
839
|
+
function isSeparator(char) {
|
|
840
|
+
return PUNCTUATION_OR_SPACE.test(char);
|
|
841
|
+
}
|
|
842
|
+
function endsWithSeparator(textContent) {
|
|
843
|
+
return isSeparator(textContent[textContent.length - 1]);
|
|
844
|
+
}
|
|
845
|
+
function startsWithSeparator(textContent) {
|
|
846
|
+
return isSeparator(textContent[0]);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Check if the text content starts with a fullstop followed by a top-level domain.
|
|
851
|
+
* Meaning if the text content can be a beginning of a top level domain.
|
|
852
|
+
* @param textContent
|
|
853
|
+
* @param isEmail
|
|
854
|
+
* @returns boolean
|
|
855
|
+
*/
|
|
856
|
+
function startsWithTLD(textContent, isEmail) {
|
|
857
|
+
if (isEmail) {
|
|
858
|
+
return /^\.[a-zA-Z]{2,}/.test(textContent);
|
|
859
|
+
} else {
|
|
860
|
+
return /^\.[a-zA-Z0-9]{1,}/.test(textContent);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
function isPreviousNodeValid(node) {
|
|
864
|
+
let previousNode = node.getPreviousSibling();
|
|
865
|
+
if ($isElementNode(previousNode)) {
|
|
866
|
+
previousNode = previousNode.getLastDescendant();
|
|
867
|
+
}
|
|
868
|
+
return previousNode === null || $isLineBreakNode(previousNode) || $isTextNode(previousNode) && endsWithSeparator(previousNode.getTextContent());
|
|
869
|
+
}
|
|
870
|
+
function isNextNodeValid(node) {
|
|
871
|
+
let nextNode = node.getNextSibling();
|
|
872
|
+
if ($isElementNode(nextNode)) {
|
|
873
|
+
nextNode = nextNode.getFirstDescendant();
|
|
874
|
+
}
|
|
875
|
+
return nextNode === null || $isLineBreakNode(nextNode) || $isTextNode(nextNode) && startsWithSeparator(nextNode.getTextContent());
|
|
876
|
+
}
|
|
877
|
+
function isContentAroundIsValid(matchStart, matchEnd, text, nodes) {
|
|
878
|
+
const contentBeforeIsValid = matchStart > 0 ? isSeparator(text[matchStart - 1]) : isPreviousNodeValid(nodes[0]);
|
|
879
|
+
if (!contentBeforeIsValid) {
|
|
880
|
+
return false;
|
|
881
|
+
}
|
|
882
|
+
const contentAfterIsValid = matchEnd < text.length ? isSeparator(text[matchEnd]) : isNextNodeValid(nodes[nodes.length - 1]);
|
|
883
|
+
return contentAfterIsValid;
|
|
884
|
+
}
|
|
885
|
+
function extractMatchingNodes(nodes, startIndex, endIndex) {
|
|
886
|
+
const unmodifiedBeforeNodes = [];
|
|
887
|
+
const matchingNodes = [];
|
|
888
|
+
const unmodifiedAfterNodes = [];
|
|
889
|
+
let matchingOffset = 0;
|
|
890
|
+
let currentOffset = 0;
|
|
891
|
+
const currentNodes = [...nodes];
|
|
892
|
+
while (currentNodes.length > 0) {
|
|
893
|
+
const currentNode = currentNodes[0];
|
|
894
|
+
const currentNodeText = currentNode.getTextContent();
|
|
895
|
+
const currentNodeLength = currentNodeText.length;
|
|
896
|
+
const currentNodeStart = currentOffset;
|
|
897
|
+
const currentNodeEnd = currentOffset + currentNodeLength;
|
|
898
|
+
if (currentNodeEnd <= startIndex) {
|
|
899
|
+
unmodifiedBeforeNodes.push(currentNode);
|
|
900
|
+
matchingOffset += currentNodeLength;
|
|
901
|
+
} else if (currentNodeStart >= endIndex) {
|
|
902
|
+
unmodifiedAfterNodes.push(currentNode);
|
|
903
|
+
} else {
|
|
904
|
+
matchingNodes.push(currentNode);
|
|
905
|
+
}
|
|
906
|
+
currentOffset += currentNodeLength;
|
|
907
|
+
currentNodes.shift();
|
|
908
|
+
}
|
|
909
|
+
return [matchingOffset, unmodifiedBeforeNodes, matchingNodes, unmodifiedAfterNodes];
|
|
910
|
+
}
|
|
911
|
+
function $createAutoLinkNode_(nodes, startIndex, endIndex, match) {
|
|
912
|
+
const linkNode = $createAutoLinkNode(match.url, match.attributes);
|
|
913
|
+
if (nodes.length === 1) {
|
|
914
|
+
let remainingTextNode = nodes[0];
|
|
915
|
+
let linkTextNode;
|
|
916
|
+
if (startIndex === 0) {
|
|
917
|
+
[linkTextNode, remainingTextNode] = remainingTextNode.splitText(endIndex);
|
|
918
|
+
} else {
|
|
919
|
+
[, linkTextNode, remainingTextNode] = remainingTextNode.splitText(startIndex, endIndex);
|
|
920
|
+
}
|
|
921
|
+
const textNode = $createTextNode(match.text);
|
|
922
|
+
textNode.setFormat(linkTextNode.getFormat());
|
|
923
|
+
textNode.setDetail(linkTextNode.getDetail());
|
|
924
|
+
textNode.setStyle(linkTextNode.getStyle());
|
|
925
|
+
linkNode.append(textNode);
|
|
926
|
+
linkTextNode.replace(linkNode);
|
|
927
|
+
return remainingTextNode;
|
|
928
|
+
} else if (nodes.length > 1) {
|
|
929
|
+
const firstTextNode = nodes[0];
|
|
930
|
+
let offset = firstTextNode.getTextContent().length;
|
|
931
|
+
let firstLinkTextNode;
|
|
932
|
+
if (startIndex === 0) {
|
|
933
|
+
firstLinkTextNode = firstTextNode;
|
|
934
|
+
} else {
|
|
935
|
+
[, firstLinkTextNode] = firstTextNode.splitText(startIndex);
|
|
936
|
+
}
|
|
937
|
+
const linkNodes = [];
|
|
938
|
+
let remainingTextNode;
|
|
939
|
+
for (let i = 1; i < nodes.length; i++) {
|
|
940
|
+
const currentNode = nodes[i];
|
|
941
|
+
const currentNodeText = currentNode.getTextContent();
|
|
942
|
+
const currentNodeLength = currentNodeText.length;
|
|
943
|
+
const currentNodeStart = offset;
|
|
944
|
+
const currentNodeEnd = offset + currentNodeLength;
|
|
945
|
+
if (currentNodeStart < endIndex) {
|
|
946
|
+
if (currentNodeEnd <= endIndex) {
|
|
947
|
+
linkNodes.push(currentNode);
|
|
948
|
+
} else {
|
|
949
|
+
const [linkTextNode, endNode] = currentNode.splitText(endIndex - currentNodeStart);
|
|
950
|
+
linkNodes.push(linkTextNode);
|
|
951
|
+
remainingTextNode = endNode;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
offset += currentNodeLength;
|
|
955
|
+
}
|
|
956
|
+
const selection = $getSelection();
|
|
957
|
+
const selectedTextNode = selection ? selection.getNodes().find($isTextNode) : undefined;
|
|
958
|
+
const textNode = $createTextNode(firstLinkTextNode.getTextContent());
|
|
959
|
+
textNode.setFormat(firstLinkTextNode.getFormat());
|
|
960
|
+
textNode.setDetail(firstLinkTextNode.getDetail());
|
|
961
|
+
textNode.setStyle(firstLinkTextNode.getStyle());
|
|
962
|
+
linkNode.append(textNode, ...linkNodes);
|
|
963
|
+
// it does not preserve caret position if caret was at the first text node
|
|
964
|
+
// so we need to restore caret position
|
|
965
|
+
if (selectedTextNode && selectedTextNode === firstLinkTextNode) {
|
|
966
|
+
if ($isRangeSelection(selection)) {
|
|
967
|
+
textNode.select(selection.anchor.offset, selection.focus.offset);
|
|
968
|
+
} else if ($isNodeSelection(selection)) {
|
|
969
|
+
textNode.select(0, textNode.getTextContent().length);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
firstLinkTextNode.replace(linkNode);
|
|
973
|
+
return remainingTextNode;
|
|
974
|
+
}
|
|
975
|
+
return undefined;
|
|
976
|
+
}
|
|
977
|
+
function $handleLinkCreation(nodes, matchers, onChange) {
|
|
978
|
+
let currentNodes = [...nodes];
|
|
979
|
+
const initialText = currentNodes.map(node => node.getTextContent()).join('');
|
|
980
|
+
let text = initialText;
|
|
981
|
+
let match;
|
|
982
|
+
let invalidMatchEnd = 0;
|
|
983
|
+
while ((match = findFirstMatch(text, matchers)) && match !== null) {
|
|
984
|
+
const matchStart = match.index;
|
|
985
|
+
const matchLength = match.length;
|
|
986
|
+
const matchEnd = matchStart + matchLength;
|
|
987
|
+
const isValid = isContentAroundIsValid(invalidMatchEnd + matchStart, invalidMatchEnd + matchEnd, initialText, currentNodes);
|
|
988
|
+
if (isValid) {
|
|
989
|
+
const [matchingOffset,, matchingNodes, unmodifiedAfterNodes] = extractMatchingNodes(currentNodes, invalidMatchEnd + matchStart, invalidMatchEnd + matchEnd);
|
|
990
|
+
const actualMatchStart = invalidMatchEnd + matchStart - matchingOffset;
|
|
991
|
+
const actualMatchEnd = invalidMatchEnd + matchEnd - matchingOffset;
|
|
992
|
+
const remainingTextNode = $createAutoLinkNode_(matchingNodes, actualMatchStart, actualMatchEnd, match);
|
|
993
|
+
currentNodes = remainingTextNode ? [remainingTextNode, ...unmodifiedAfterNodes] : unmodifiedAfterNodes;
|
|
994
|
+
onChange(match.url, null);
|
|
995
|
+
invalidMatchEnd = 0;
|
|
996
|
+
} else {
|
|
997
|
+
invalidMatchEnd += matchEnd;
|
|
998
|
+
}
|
|
999
|
+
text = text.substring(matchEnd);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
function handleLinkEdit(linkNode, matchers, onChange) {
|
|
1003
|
+
// Check children are simple text
|
|
1004
|
+
const children = linkNode.getChildren();
|
|
1005
|
+
const childrenLength = children.length;
|
|
1006
|
+
for (let i = 0; i < childrenLength; i++) {
|
|
1007
|
+
const child = children[i];
|
|
1008
|
+
if (!$isTextNode(child) || !child.isSimpleText()) {
|
|
1009
|
+
replaceWithChildren(linkNode);
|
|
1010
|
+
onChange(null, linkNode.getURL());
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// Check text content fully matches
|
|
1016
|
+
const text = linkNode.getTextContent();
|
|
1017
|
+
const match = findFirstMatch(text, matchers);
|
|
1018
|
+
if (match === null || match.text !== text) {
|
|
1019
|
+
replaceWithChildren(linkNode);
|
|
1020
|
+
onChange(null, linkNode.getURL());
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// Check neighbors
|
|
1025
|
+
if (!isPreviousNodeValid(linkNode) || !isNextNodeValid(linkNode)) {
|
|
1026
|
+
replaceWithChildren(linkNode);
|
|
1027
|
+
onChange(null, linkNode.getURL());
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
const url = linkNode.getURL();
|
|
1031
|
+
if (url !== match.url) {
|
|
1032
|
+
linkNode.setURL(match.url);
|
|
1033
|
+
onChange(match.url, url);
|
|
1034
|
+
}
|
|
1035
|
+
if (match.attributes) {
|
|
1036
|
+
const rel = linkNode.getRel();
|
|
1037
|
+
if (rel !== match.attributes.rel) {
|
|
1038
|
+
linkNode.setRel(match.attributes.rel || null);
|
|
1039
|
+
onChange(match.attributes.rel || null, rel);
|
|
1040
|
+
}
|
|
1041
|
+
const target = linkNode.getTarget();
|
|
1042
|
+
if (target !== match.attributes.target) {
|
|
1043
|
+
linkNode.setTarget(match.attributes.target || null);
|
|
1044
|
+
onChange(match.attributes.target || null, target);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
// Bad neighbors are edits in neighbor nodes that make AutoLinks incompatible.
|
|
1050
|
+
// Given the creation preconditions, these can only be simple text nodes.
|
|
1051
|
+
function handleBadNeighbors(textNode, matchers, onChange) {
|
|
1052
|
+
const previousSibling = textNode.getPreviousSibling();
|
|
1053
|
+
const nextSibling = textNode.getNextSibling();
|
|
1054
|
+
const text = textNode.getTextContent();
|
|
1055
|
+
if ($isAutoLinkNode(previousSibling) && !previousSibling.getIsUnlinked() && (!startsWithSeparator(text) || startsWithTLD(text, previousSibling.isEmailURI()))) {
|
|
1056
|
+
previousSibling.append(textNode);
|
|
1057
|
+
handleLinkEdit(previousSibling, matchers, onChange);
|
|
1058
|
+
onChange(null, previousSibling.getURL());
|
|
1059
|
+
}
|
|
1060
|
+
if ($isAutoLinkNode(nextSibling) && !nextSibling.getIsUnlinked() && !endsWithSeparator(text)) {
|
|
1061
|
+
replaceWithChildren(nextSibling);
|
|
1062
|
+
handleLinkEdit(nextSibling, matchers, onChange);
|
|
1063
|
+
onChange(null, nextSibling.getURL());
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
function replaceWithChildren(node) {
|
|
1067
|
+
const children = node.getChildren();
|
|
1068
|
+
const childrenLength = children.length;
|
|
1069
|
+
for (let j = childrenLength - 1; j >= 0; j--) {
|
|
1070
|
+
node.insertAfter(children[j]);
|
|
1071
|
+
}
|
|
1072
|
+
node.remove();
|
|
1073
|
+
return children.map(child => child.getLatest());
|
|
1074
|
+
}
|
|
1075
|
+
function getTextNodesToMatch(textNode) {
|
|
1076
|
+
// check if next siblings are simple text nodes till a node contains a space separator
|
|
1077
|
+
const textNodesToMatch = [textNode];
|
|
1078
|
+
let nextSibling = textNode.getNextSibling();
|
|
1079
|
+
while (nextSibling !== null && $isTextNode(nextSibling) && nextSibling.isSimpleText()) {
|
|
1080
|
+
textNodesToMatch.push(nextSibling);
|
|
1081
|
+
if (/[\s]/.test(nextSibling.getTextContent())) {
|
|
1082
|
+
break;
|
|
1083
|
+
}
|
|
1084
|
+
nextSibling = nextSibling.getNextSibling();
|
|
1085
|
+
}
|
|
1086
|
+
return textNodesToMatch;
|
|
1087
|
+
}
|
|
1088
|
+
const defaultConfig = {
|
|
1089
|
+
changeHandlers: [],
|
|
1090
|
+
matchers: []
|
|
1091
|
+
};
|
|
1092
|
+
function registerAutoLink(editor, config = defaultConfig) {
|
|
1093
|
+
const {
|
|
1094
|
+
matchers,
|
|
1095
|
+
changeHandlers
|
|
1096
|
+
} = config;
|
|
1097
|
+
const onChange = (url, prevUrl) => {
|
|
1098
|
+
for (const handler of changeHandlers) {
|
|
1099
|
+
handler(url, prevUrl);
|
|
1100
|
+
}
|
|
1101
|
+
};
|
|
1102
|
+
return mergeRegister(editor.registerNodeTransform(TextNode, textNode => {
|
|
1103
|
+
const parent = textNode.getParentOrThrow();
|
|
1104
|
+
const previous = textNode.getPreviousSibling();
|
|
1105
|
+
if ($isAutoLinkNode(parent) && !parent.getIsUnlinked()) {
|
|
1106
|
+
handleLinkEdit(parent, matchers, onChange);
|
|
1107
|
+
} else if (!$isLinkNode(parent)) {
|
|
1108
|
+
if (textNode.isSimpleText() && (startsWithSeparator(textNode.getTextContent()) || !$isAutoLinkNode(previous))) {
|
|
1109
|
+
const textNodesToMatch = getTextNodesToMatch(textNode);
|
|
1110
|
+
$handleLinkCreation(textNodesToMatch, matchers, onChange);
|
|
1111
|
+
}
|
|
1112
|
+
handleBadNeighbors(textNode, matchers, onChange);
|
|
1113
|
+
}
|
|
1114
|
+
}), editor.registerCommand(TOGGLE_LINK_COMMAND, payload => {
|
|
1115
|
+
const selection = $getSelection();
|
|
1116
|
+
if (payload !== null || !$isRangeSelection(selection)) {
|
|
1117
|
+
return false;
|
|
1118
|
+
}
|
|
1119
|
+
const nodes = selection.extract();
|
|
1120
|
+
nodes.forEach(node => {
|
|
1121
|
+
const parent = node.getParent();
|
|
1122
|
+
if ($isAutoLinkNode(parent)) {
|
|
1123
|
+
// invert the value
|
|
1124
|
+
parent.setIsUnlinked(!parent.getIsUnlinked());
|
|
1125
|
+
parent.markDirty();
|
|
1126
|
+
}
|
|
1127
|
+
});
|
|
1128
|
+
return false;
|
|
1129
|
+
}, COMMAND_PRIORITY_LOW));
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
/**
|
|
1133
|
+
* An extension to automatically create AutoLinkNode from text
|
|
1134
|
+
* that matches the configured matchers. No default implementation
|
|
1135
|
+
* is provided for any matcher, see {@link createLinkMatcherWithRegExp}
|
|
1136
|
+
* for a helper function to create a matcher from a RegExp, and the
|
|
1137
|
+
* Playground's [AutoLinkPlugin](https://github.com/facebook/lexical/blob/main/packages/lexical-playground/src/plugins/AutoLinkPlugin/index.tsx)
|
|
1138
|
+
* for some example RegExps that could be used.
|
|
1139
|
+
*
|
|
1140
|
+
* The given `matchers` and `changeHandlers` will be merged by
|
|
1141
|
+
* concatenating the configured arrays.
|
|
1142
|
+
*/
|
|
1143
|
+
const AutoLinkExtension = defineExtension({
|
|
1144
|
+
config: defaultConfig,
|
|
1145
|
+
dependencies: [LinkExtension],
|
|
1146
|
+
mergeConfig(config, overrides) {
|
|
1147
|
+
const merged = shallowMergeConfig(config, overrides);
|
|
1148
|
+
for (const k of ['matchers', 'changeHandlers']) {
|
|
1149
|
+
const v = overrides[k];
|
|
1150
|
+
if (Array.isArray(v)) {
|
|
1151
|
+
merged[k] = [...config[k], ...v];
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
return merged;
|
|
1155
|
+
},
|
|
1156
|
+
name: '@ekz/lexical-link/AutoLink',
|
|
1157
|
+
register: registerAutoLink
|
|
1158
|
+
});
|
|
1159
|
+
|
|
1160
|
+
/**
|
|
1161
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1162
|
+
*
|
|
1163
|
+
* This source code is licensed under the MIT license found in the
|
|
1164
|
+
* LICENSE file in the root directory of this source tree.
|
|
1165
|
+
*
|
|
1166
|
+
*/
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
/** @deprecated renamed to {@link $toggleLink} by @lexical/eslint-plugin rules-of-lexical */
|
|
1170
|
+
const toggleLink = $toggleLink;
|
|
1171
|
+
|
|
1172
|
+
export { $createAutoLinkNode, $createLinkNode, $isAutoLinkNode, $isLinkNode, $toggleLink, AutoLinkExtension, AutoLinkNode, ClickableLinkExtension, LinkExtension, LinkNode, TOGGLE_LINK_COMMAND, createLinkMatcherWithRegExp, formatUrl, registerAutoLink, registerClickableLink, registerLink, toggleLink };
|