@lexical/link 0.12.2 → 0.12.3
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/LexicalLink.dev.js +29 -115
- package/LexicalLink.prod.js +10 -10
- package/index.d.ts +1 -1
- package/package.json +3 -3
package/LexicalLink.dev.js
CHANGED
|
@@ -11,8 +11,8 @@ var lexical = require('lexical');
|
|
|
11
11
|
|
|
12
12
|
/** @module @lexical/link */
|
|
13
13
|
const SUPPORTED_URL_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'sms:', 'tel:']);
|
|
14
|
-
/** @noInheritDoc */
|
|
15
14
|
|
|
15
|
+
/** @noInheritDoc */
|
|
16
16
|
class LinkNode extends lexical.ElementNode {
|
|
17
17
|
/** @internal */
|
|
18
18
|
|
|
@@ -21,10 +21,10 @@ class LinkNode extends lexical.ElementNode {
|
|
|
21
21
|
/** @internal */
|
|
22
22
|
|
|
23
23
|
/** @internal */
|
|
24
|
+
|
|
24
25
|
static getType() {
|
|
25
26
|
return 'link';
|
|
26
27
|
}
|
|
27
|
-
|
|
28
28
|
static clone(node) {
|
|
29
29
|
return new LinkNode(node.__url, {
|
|
30
30
|
rel: node.__rel,
|
|
@@ -32,7 +32,6 @@ class LinkNode extends lexical.ElementNode {
|
|
|
32
32
|
title: node.__title
|
|
33
33
|
}, node.__key);
|
|
34
34
|
}
|
|
35
|
-
|
|
36
35
|
constructor(url, attributes = {}, key) {
|
|
37
36
|
super(key);
|
|
38
37
|
const {
|
|
@@ -45,37 +44,29 @@ class LinkNode extends lexical.ElementNode {
|
|
|
45
44
|
this.__rel = rel;
|
|
46
45
|
this.__title = title;
|
|
47
46
|
}
|
|
48
|
-
|
|
49
47
|
createDOM(config) {
|
|
50
48
|
const element = document.createElement('a');
|
|
51
49
|
element.href = this.sanitizeUrl(this.__url);
|
|
52
|
-
|
|
53
50
|
if (this.__target !== null) {
|
|
54
51
|
element.target = this.__target;
|
|
55
52
|
}
|
|
56
|
-
|
|
57
53
|
if (this.__rel !== null) {
|
|
58
54
|
element.rel = this.__rel;
|
|
59
55
|
}
|
|
60
|
-
|
|
61
56
|
if (this.__title !== null) {
|
|
62
57
|
element.title = this.__title;
|
|
63
58
|
}
|
|
64
|
-
|
|
65
59
|
utils.addClassNamesToElement(element, config.theme.link);
|
|
66
60
|
return element;
|
|
67
61
|
}
|
|
68
|
-
|
|
69
62
|
updateDOM(prevNode, anchor, config) {
|
|
70
63
|
const url = this.__url;
|
|
71
64
|
const target = this.__target;
|
|
72
65
|
const rel = this.__rel;
|
|
73
66
|
const title = this.__title;
|
|
74
|
-
|
|
75
67
|
if (url !== prevNode.__url) {
|
|
76
68
|
anchor.href = url;
|
|
77
69
|
}
|
|
78
|
-
|
|
79
70
|
if (target !== prevNode.__target) {
|
|
80
71
|
if (target) {
|
|
81
72
|
anchor.target = target;
|
|
@@ -83,7 +74,6 @@ class LinkNode extends lexical.ElementNode {
|
|
|
83
74
|
anchor.removeAttribute('target');
|
|
84
75
|
}
|
|
85
76
|
}
|
|
86
|
-
|
|
87
77
|
if (rel !== prevNode.__rel) {
|
|
88
78
|
if (rel) {
|
|
89
79
|
anchor.rel = rel;
|
|
@@ -91,7 +81,6 @@ class LinkNode extends lexical.ElementNode {
|
|
|
91
81
|
anchor.removeAttribute('rel');
|
|
92
82
|
}
|
|
93
83
|
}
|
|
94
|
-
|
|
95
84
|
if (title !== prevNode.__title) {
|
|
96
85
|
if (title) {
|
|
97
86
|
anchor.title = title;
|
|
@@ -99,10 +88,8 @@ class LinkNode extends lexical.ElementNode {
|
|
|
99
88
|
anchor.removeAttribute('title');
|
|
100
89
|
}
|
|
101
90
|
}
|
|
102
|
-
|
|
103
91
|
return false;
|
|
104
92
|
}
|
|
105
|
-
|
|
106
93
|
static importDOM() {
|
|
107
94
|
return {
|
|
108
95
|
a: node => ({
|
|
@@ -111,7 +98,6 @@ class LinkNode extends lexical.ElementNode {
|
|
|
111
98
|
})
|
|
112
99
|
};
|
|
113
100
|
}
|
|
114
|
-
|
|
115
101
|
static importJSON(serializedNode) {
|
|
116
102
|
const node = $createLinkNode(serializedNode.url, {
|
|
117
103
|
rel: serializedNode.rel,
|
|
@@ -123,23 +109,21 @@ class LinkNode extends lexical.ElementNode {
|
|
|
123
109
|
node.setDirection(serializedNode.direction);
|
|
124
110
|
return node;
|
|
125
111
|
}
|
|
126
|
-
|
|
127
112
|
sanitizeUrl(url) {
|
|
128
113
|
try {
|
|
129
|
-
const parsedUrl = new URL(url);
|
|
130
|
-
|
|
114
|
+
const parsedUrl = new URL(url);
|
|
115
|
+
// eslint-disable-next-line no-script-url
|
|
131
116
|
if (!SUPPORTED_URL_PROTOCOLS.has(parsedUrl.protocol)) {
|
|
132
117
|
return 'about:blank';
|
|
133
118
|
}
|
|
134
|
-
} catch {
|
|
119
|
+
} catch (_unused) {
|
|
135
120
|
return url;
|
|
136
121
|
}
|
|
137
|
-
|
|
138
122
|
return url;
|
|
139
123
|
}
|
|
140
|
-
|
|
141
124
|
exportJSON() {
|
|
142
|
-
return {
|
|
125
|
+
return {
|
|
126
|
+
...super.exportJSON(),
|
|
143
127
|
rel: this.getRel(),
|
|
144
128
|
target: this.getTarget(),
|
|
145
129
|
title: this.getTitle(),
|
|
@@ -148,94 +132,69 @@ class LinkNode extends lexical.ElementNode {
|
|
|
148
132
|
version: 1
|
|
149
133
|
};
|
|
150
134
|
}
|
|
151
|
-
|
|
152
135
|
getURL() {
|
|
153
136
|
return this.getLatest().__url;
|
|
154
137
|
}
|
|
155
|
-
|
|
156
138
|
setURL(url) {
|
|
157
139
|
const writable = this.getWritable();
|
|
158
140
|
writable.__url = url;
|
|
159
141
|
}
|
|
160
|
-
|
|
161
142
|
getTarget() {
|
|
162
143
|
return this.getLatest().__target;
|
|
163
144
|
}
|
|
164
|
-
|
|
165
145
|
setTarget(target) {
|
|
166
146
|
const writable = this.getWritable();
|
|
167
147
|
writable.__target = target;
|
|
168
148
|
}
|
|
169
|
-
|
|
170
149
|
getRel() {
|
|
171
150
|
return this.getLatest().__rel;
|
|
172
151
|
}
|
|
173
|
-
|
|
174
152
|
setRel(rel) {
|
|
175
153
|
const writable = this.getWritable();
|
|
176
154
|
writable.__rel = rel;
|
|
177
155
|
}
|
|
178
|
-
|
|
179
156
|
getTitle() {
|
|
180
157
|
return this.getLatest().__title;
|
|
181
158
|
}
|
|
182
|
-
|
|
183
159
|
setTitle(title) {
|
|
184
160
|
const writable = this.getWritable();
|
|
185
161
|
writable.__title = title;
|
|
186
162
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
title: this.__title
|
|
196
|
-
});
|
|
197
|
-
element.append(linkNode);
|
|
198
|
-
return linkNode;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return null;
|
|
163
|
+
insertNewAfter(_, restoreSelection = true) {
|
|
164
|
+
const linkNode = $createLinkNode(this.__url, {
|
|
165
|
+
rel: this.__rel,
|
|
166
|
+
target: this.__target,
|
|
167
|
+
title: this.__title
|
|
168
|
+
});
|
|
169
|
+
this.insertAfter(linkNode, restoreSelection);
|
|
170
|
+
return linkNode;
|
|
202
171
|
}
|
|
203
|
-
|
|
204
172
|
canInsertTextBefore() {
|
|
205
173
|
return false;
|
|
206
174
|
}
|
|
207
|
-
|
|
208
175
|
canInsertTextAfter() {
|
|
209
176
|
return false;
|
|
210
177
|
}
|
|
211
|
-
|
|
212
178
|
canBeEmpty() {
|
|
213
179
|
return false;
|
|
214
180
|
}
|
|
215
|
-
|
|
216
181
|
isInline() {
|
|
217
182
|
return true;
|
|
218
183
|
}
|
|
219
|
-
|
|
220
184
|
extractWithChild(child, selection, destination) {
|
|
221
185
|
if (!lexical.$isRangeSelection(selection)) {
|
|
222
186
|
return false;
|
|
223
187
|
}
|
|
224
|
-
|
|
225
188
|
const anchorNode = selection.anchor.getNode();
|
|
226
189
|
const focusNode = selection.focus.getNode();
|
|
227
190
|
return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && selection.getTextContent().length > 0;
|
|
228
191
|
}
|
|
229
|
-
|
|
230
192
|
}
|
|
231
|
-
|
|
232
193
|
function convertAnchorElement(domNode) {
|
|
233
194
|
let node = null;
|
|
234
|
-
|
|
235
195
|
if (utils.isHTMLAnchorElement(domNode)) {
|
|
236
196
|
const content = domNode.textContent;
|
|
237
|
-
|
|
238
|
-
if (content !== null && content !== '') {
|
|
197
|
+
if (content !== null && content !== '' || domNode.children.length > 0) {
|
|
239
198
|
node = $createLinkNode(domNode.getAttribute('href') || '', {
|
|
240
199
|
rel: domNode.getAttribute('rel'),
|
|
241
200
|
target: domNode.getAttribute('target'),
|
|
@@ -243,28 +202,26 @@ function convertAnchorElement(domNode) {
|
|
|
243
202
|
});
|
|
244
203
|
}
|
|
245
204
|
}
|
|
246
|
-
|
|
247
205
|
return {
|
|
248
206
|
node
|
|
249
207
|
};
|
|
250
208
|
}
|
|
209
|
+
|
|
251
210
|
/**
|
|
252
211
|
* Takes a URL and creates a LinkNode.
|
|
253
212
|
* @param url - The URL the LinkNode should direct to.
|
|
254
213
|
* @param attributes - Optional HTML a tag attributes { target, rel, title }
|
|
255
214
|
* @returns The LinkNode.
|
|
256
215
|
*/
|
|
257
|
-
|
|
258
|
-
|
|
259
216
|
function $createLinkNode(url, attributes) {
|
|
260
217
|
return lexical.$applyNodeReplacement(new LinkNode(url, attributes));
|
|
261
218
|
}
|
|
219
|
+
|
|
262
220
|
/**
|
|
263
221
|
* Determines if node is a LinkNode.
|
|
264
222
|
* @param node - The node to be checked.
|
|
265
223
|
* @returns true if node is a LinkNode, false otherwise.
|
|
266
224
|
*/
|
|
267
|
-
|
|
268
225
|
function $isLinkNode(node) {
|
|
269
226
|
return node instanceof LinkNode;
|
|
270
227
|
}
|
|
@@ -274,7 +231,6 @@ class AutoLinkNode extends LinkNode {
|
|
|
274
231
|
static getType() {
|
|
275
232
|
return 'autolink';
|
|
276
233
|
}
|
|
277
|
-
|
|
278
234
|
static clone(node) {
|
|
279
235
|
return new AutoLinkNode(node.__url, {
|
|
280
236
|
rel: node.__rel,
|
|
@@ -282,7 +238,6 @@ class AutoLinkNode extends LinkNode {
|
|
|
282
238
|
title: node.__title
|
|
283
239
|
}, node.__key);
|
|
284
240
|
}
|
|
285
|
-
|
|
286
241
|
static importJSON(serializedNode) {
|
|
287
242
|
const node = $createAutoLinkNode(serializedNode.url, {
|
|
288
243
|
rel: serializedNode.rel,
|
|
@@ -294,36 +249,32 @@ class AutoLinkNode extends LinkNode {
|
|
|
294
249
|
node.setDirection(serializedNode.direction);
|
|
295
250
|
return node;
|
|
296
251
|
}
|
|
297
|
-
|
|
298
252
|
static importDOM() {
|
|
299
253
|
// TODO: Should link node should handle the import over autolink?
|
|
300
254
|
return null;
|
|
301
255
|
}
|
|
302
|
-
|
|
303
256
|
exportJSON() {
|
|
304
|
-
return {
|
|
257
|
+
return {
|
|
258
|
+
...super.exportJSON(),
|
|
305
259
|
type: 'autolink',
|
|
306
260
|
version: 1
|
|
307
261
|
};
|
|
308
262
|
}
|
|
309
|
-
|
|
310
263
|
insertNewAfter(selection, restoreSelection = true) {
|
|
311
264
|
const element = this.getParentOrThrow().insertNewAfter(selection, restoreSelection);
|
|
312
|
-
|
|
313
265
|
if (lexical.$isElementNode(element)) {
|
|
314
266
|
const linkNode = $createAutoLinkNode(this.__url, {
|
|
315
|
-
rel: this.
|
|
267
|
+
rel: this.__rel,
|
|
316
268
|
target: this.__target,
|
|
317
269
|
title: this.__title
|
|
318
270
|
});
|
|
319
271
|
element.append(linkNode);
|
|
320
272
|
return linkNode;
|
|
321
273
|
}
|
|
322
|
-
|
|
323
274
|
return null;
|
|
324
275
|
}
|
|
325
|
-
|
|
326
276
|
}
|
|
277
|
+
|
|
327
278
|
/**
|
|
328
279
|
* Takes a URL and creates an AutoLinkNode. AutoLinkNodes are generally automatically generated
|
|
329
280
|
* during typing, which is especially useful when a button to generate a LinkNode is not practical.
|
|
@@ -331,27 +282,26 @@ class AutoLinkNode extends LinkNode {
|
|
|
331
282
|
* @param attributes - Optional HTML a tag attributes. { target, rel, title }
|
|
332
283
|
* @returns The LinkNode.
|
|
333
284
|
*/
|
|
334
|
-
|
|
335
285
|
function $createAutoLinkNode(url, attributes) {
|
|
336
286
|
return lexical.$applyNodeReplacement(new AutoLinkNode(url, attributes));
|
|
337
287
|
}
|
|
288
|
+
|
|
338
289
|
/**
|
|
339
290
|
* Determines if node is an AutoLinkNode.
|
|
340
291
|
* @param node - The node to be checked.
|
|
341
292
|
* @returns true if node is an AutoLinkNode, false otherwise.
|
|
342
293
|
*/
|
|
343
|
-
|
|
344
294
|
function $isAutoLinkNode(node) {
|
|
345
295
|
return node instanceof AutoLinkNode;
|
|
346
296
|
}
|
|
347
297
|
const TOGGLE_LINK_COMMAND = lexical.createCommand('TOGGLE_LINK_COMMAND');
|
|
298
|
+
|
|
348
299
|
/**
|
|
349
300
|
* Generates or updates a LinkNode. It can also delete a LinkNode if the URL is null,
|
|
350
301
|
* but saves any children and brings them up to the parent node.
|
|
351
302
|
* @param url - The URL the link directs to.
|
|
352
303
|
* @param attributes - Optional HTML a tag attributes. { target, rel, title }
|
|
353
304
|
*/
|
|
354
|
-
|
|
355
305
|
function toggleLink(url, attributes = {}) {
|
|
356
306
|
const {
|
|
357
307
|
target,
|
|
@@ -359,90 +309,71 @@ function toggleLink(url, attributes = {}) {
|
|
|
359
309
|
} = attributes;
|
|
360
310
|
const rel = attributes.rel === undefined ? 'noreferrer' : attributes.rel;
|
|
361
311
|
const selection = lexical.$getSelection();
|
|
362
|
-
|
|
363
312
|
if (!lexical.$isRangeSelection(selection)) {
|
|
364
313
|
return;
|
|
365
314
|
}
|
|
366
|
-
|
|
367
315
|
const nodes = selection.extract();
|
|
368
|
-
|
|
369
316
|
if (url === null) {
|
|
370
317
|
// Remove LinkNodes
|
|
371
318
|
nodes.forEach(node => {
|
|
372
319
|
const parent = node.getParent();
|
|
373
|
-
|
|
374
320
|
if ($isLinkNode(parent)) {
|
|
375
321
|
const children = parent.getChildren();
|
|
376
|
-
|
|
377
322
|
for (let i = 0; i < children.length; i++) {
|
|
378
323
|
parent.insertBefore(children[i]);
|
|
379
324
|
}
|
|
380
|
-
|
|
381
325
|
parent.remove();
|
|
382
326
|
}
|
|
383
327
|
});
|
|
384
328
|
} else {
|
|
385
329
|
// Add or merge LinkNodes
|
|
386
330
|
if (nodes.length === 1) {
|
|
387
|
-
const firstNode = nodes[0];
|
|
331
|
+
const firstNode = nodes[0];
|
|
332
|
+
// if the first node is a LinkNode or if its
|
|
388
333
|
// parent is a LinkNode, we update the URL, target and rel.
|
|
389
|
-
|
|
390
|
-
const linkNode = $isLinkNode(firstNode) ? firstNode : $getLinkAncestor(firstNode);
|
|
391
|
-
|
|
334
|
+
const linkNode = utils.$getAncestor(firstNode, $isLinkNode);
|
|
392
335
|
if (linkNode !== null) {
|
|
393
336
|
linkNode.setURL(url);
|
|
394
|
-
|
|
395
337
|
if (target !== undefined) {
|
|
396
338
|
linkNode.setTarget(target);
|
|
397
339
|
}
|
|
398
|
-
|
|
399
340
|
if (rel !== null) {
|
|
400
341
|
linkNode.setRel(rel);
|
|
401
342
|
}
|
|
402
|
-
|
|
403
343
|
if (title !== undefined) {
|
|
404
344
|
linkNode.setTitle(title);
|
|
405
345
|
}
|
|
406
|
-
|
|
407
346
|
return;
|
|
408
347
|
}
|
|
409
348
|
}
|
|
410
|
-
|
|
411
349
|
let prevParent = null;
|
|
412
350
|
let linkNode = null;
|
|
413
351
|
nodes.forEach(node => {
|
|
414
352
|
const parent = node.getParent();
|
|
415
|
-
|
|
416
353
|
if (parent === linkNode || parent === null || lexical.$isElementNode(node) && !node.isInline()) {
|
|
417
354
|
return;
|
|
418
355
|
}
|
|
419
|
-
|
|
420
356
|
if ($isLinkNode(parent)) {
|
|
421
357
|
linkNode = parent;
|
|
422
358
|
parent.setURL(url);
|
|
423
|
-
|
|
424
359
|
if (target !== undefined) {
|
|
425
360
|
parent.setTarget(target);
|
|
426
361
|
}
|
|
427
|
-
|
|
428
362
|
if (rel !== null) {
|
|
429
363
|
linkNode.setRel(rel);
|
|
430
364
|
}
|
|
431
|
-
|
|
432
365
|
if (title !== undefined) {
|
|
433
366
|
linkNode.setTitle(title);
|
|
434
367
|
}
|
|
435
|
-
|
|
436
368
|
return;
|
|
437
369
|
}
|
|
438
|
-
|
|
439
370
|
if (!parent.is(prevParent)) {
|
|
440
371
|
prevParent = parent;
|
|
441
372
|
linkNode = $createLinkNode(url, {
|
|
442
373
|
rel,
|
|
443
|
-
target
|
|
374
|
+
target,
|
|
375
|
+
title
|
|
444
376
|
});
|
|
445
|
-
|
|
446
377
|
if ($isLinkNode(parent)) {
|
|
447
378
|
if (node.getPreviousSibling() === null) {
|
|
448
379
|
parent.insertBefore(linkNode);
|
|
@@ -453,24 +384,19 @@ function toggleLink(url, attributes = {}) {
|
|
|
453
384
|
node.insertBefore(linkNode);
|
|
454
385
|
}
|
|
455
386
|
}
|
|
456
|
-
|
|
457
387
|
if ($isLinkNode(node)) {
|
|
458
388
|
if (node.is(linkNode)) {
|
|
459
389
|
return;
|
|
460
390
|
}
|
|
461
|
-
|
|
462
391
|
if (linkNode !== null) {
|
|
463
392
|
const children = node.getChildren();
|
|
464
|
-
|
|
465
393
|
for (let i = 0; i < children.length; i++) {
|
|
466
394
|
linkNode.append(children[i]);
|
|
467
395
|
}
|
|
468
396
|
}
|
|
469
|
-
|
|
470
397
|
node.remove();
|
|
471
398
|
return;
|
|
472
399
|
}
|
|
473
|
-
|
|
474
400
|
if (linkNode !== null) {
|
|
475
401
|
linkNode.append(node);
|
|
476
402
|
}
|
|
@@ -478,18 +404,6 @@ function toggleLink(url, attributes = {}) {
|
|
|
478
404
|
}
|
|
479
405
|
}
|
|
480
406
|
|
|
481
|
-
function $getLinkAncestor(node) {
|
|
482
|
-
return $getAncestor(node, $isLinkNode);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
function $getAncestor(node, predicate) {
|
|
486
|
-
let parent = node;
|
|
487
|
-
|
|
488
|
-
while (parent !== null && (parent = parent.getParent()) !== null && !predicate(parent));
|
|
489
|
-
|
|
490
|
-
return parent;
|
|
491
|
-
}
|
|
492
|
-
|
|
493
407
|
exports.$createAutoLinkNode = $createAutoLinkNode;
|
|
494
408
|
exports.$createLinkNode = $createLinkNode;
|
|
495
409
|
exports.$isAutoLinkNode = $isAutoLinkNode;
|
package/LexicalLink.prod.js
CHANGED
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
'use strict';var
|
|
8
|
-
class
|
|
9
|
-
a.theme.link);return b}updateDOM(a,b){let
|
|
10
|
-
return b}sanitizeUrl(a){try{let b=new URL(a);if(!
|
|
11
|
-
a}insertNewAfter(a,b=!0){a=
|
|
12
|
-
function
|
|
13
|
-
class
|
|
14
|
-
(b=
|
|
15
|
-
exports.toggleLink=function(a,b={}){let {target:
|
|
16
|
-
if(g!==
|
|
7
|
+
'use strict';var l=require("@lexical/utils"),m=require("lexical");let n=new Set(["http:","https:","mailto:","sms:","tel:"]);
|
|
8
|
+
class p extends m.ElementNode{static getType(){return"link"}static clone(a){return new p(a.__url,{rel:a.__rel,target:a.__target,title:a.__title},a.__key)}constructor(a,b={},e){super(e);let {target:h=null,rel:k=null,title:f=null}=b;this.__url=a;this.__target=h;this.__rel=k;this.__title=f}createDOM(a){let b=document.createElement("a");b.href=this.sanitizeUrl(this.__url);null!==this.__target&&(b.target=this.__target);null!==this.__rel&&(b.rel=this.__rel);null!==this.__title&&(b.title=this.__title);l.addClassNamesToElement(b,
|
|
9
|
+
a.theme.link);return b}updateDOM(a,b){let e=this.__url,h=this.__target,k=this.__rel,f=this.__title;e!==a.__url&&(b.href=e);h!==a.__target&&(h?b.target=h:b.removeAttribute("target"));k!==a.__rel&&(k?b.rel=k:b.removeAttribute("rel"));f!==a.__title&&(f?b.title=f:b.removeAttribute("title"));return!1}static importDOM(){return{a:()=>({conversion:q,priority:1})}}static importJSON(a){let b=r(a.url,{rel:a.rel,target:a.target,title:a.title});b.setFormat(a.format);b.setIndent(a.indent);b.setDirection(a.direction);
|
|
10
|
+
return b}sanitizeUrl(a){try{let b=new URL(a);if(!n.has(b.protocol))return"about:blank"}catch(b){}return a}exportJSON(){return{...super.exportJSON(),rel:this.getRel(),target:this.getTarget(),title:this.getTitle(),type:"link",url:this.getURL(),version:1}}getURL(){return this.getLatest().__url}setURL(a){this.getWritable().__url=a}getTarget(){return this.getLatest().__target}setTarget(a){this.getWritable().__target=a}getRel(){return this.getLatest().__rel}setRel(a){this.getWritable().__rel=a}getTitle(){return this.getLatest().__title}setTitle(a){this.getWritable().__title=
|
|
11
|
+
a}insertNewAfter(a,b=!0){a=r(this.__url,{rel:this.__rel,target:this.__target,title:this.__title});this.insertAfter(a,b);return a}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(a,b){if(!m.$isRangeSelection(b))return!1;a=b.anchor.getNode();let e=b.focus.getNode();return this.isParentOf(a)&&this.isParentOf(e)&&0<b.getTextContent().length}}
|
|
12
|
+
function q(a){let b=null;if(l.isHTMLAnchorElement(a)){let e=a.textContent;if(null!==e&&""!==e||0<a.children.length)b=r(a.getAttribute("href")||"",{rel:a.getAttribute("rel"),target:a.getAttribute("target"),title:a.getAttribute("title")})}return{node:b}}function r(a,b){return m.$applyNodeReplacement(new p(a,b))}function t(a){return a instanceof p}
|
|
13
|
+
class v extends p{static getType(){return"autolink"}static clone(a){return new v(a.__url,{rel:a.__rel,target:a.__target,title:a.__title},a.__key)}static importJSON(a){let b=w(a.url,{rel:a.rel,target:a.target,title:a.title});b.setFormat(a.format);b.setIndent(a.indent);b.setDirection(a.direction);return b}static importDOM(){return null}exportJSON(){return{...super.exportJSON(),type:"autolink",version:1}}insertNewAfter(a,b=!0){a=this.getParentOrThrow().insertNewAfter(a,b);return m.$isElementNode(a)?
|
|
14
|
+
(b=w(this.__url,{rel:this.__rel,target:this.__target,title:this.__title}),a.append(b),b):null}}function w(a,b){return m.$applyNodeReplacement(new v(a,b))}let x=m.createCommand("TOGGLE_LINK_COMMAND");exports.$createAutoLinkNode=w;exports.$createLinkNode=r;exports.$isAutoLinkNode=function(a){return a instanceof v};exports.$isLinkNode=t;exports.AutoLinkNode=v;exports.LinkNode=p;exports.TOGGLE_LINK_COMMAND=x;
|
|
15
|
+
exports.toggleLink=function(a,b={}){let {target:e,title:h}=b,k=void 0===b.rel?"noreferrer":b.rel;b=m.$getSelection();if(m.$isRangeSelection(b))if(b=b.extract(),null===a)b.forEach(f=>{f=f.getParent();if(t(f)){let d=f.getChildren();for(let c=0;c<d.length;c++)f.insertBefore(d[c]);f.remove()}});else{if(1===b.length){let c=l.$getAncestor(b[0],t);if(null!==c){c.setURL(a);void 0!==e&&c.setTarget(e);null!==k&&c.setRel(k);void 0!==h&&c.setTitle(h);return}}let f=null,d=null;b.forEach(c=>{var g=c.getParent();
|
|
16
|
+
if(g!==d&&null!==g&&(!m.$isElementNode(c)||c.isInline()))if(t(g))d=g,g.setURL(a),void 0!==e&&g.setTarget(e),null!==k&&d.setRel(k),void 0!==h&&d.setTitle(h);else if(g.is(f)||(f=g,d=r(a,{rel:k,target:e,title:h}),t(g)?null===c.getPreviousSibling()?g.insertBefore(d):g.insertAfter(d):c.insertBefore(d)),t(c)){if(!c.is(d)){if(null!==d){g=c.getChildren();for(let u=0;u<g.length;u++)d.append(g[u])}c.remove()}}else null!==d&&d.append(c)})}}
|
package/index.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ export declare class LinkNode extends ElementNode {
|
|
|
43
43
|
setRel(rel: null | string): void;
|
|
44
44
|
getTitle(): null | string;
|
|
45
45
|
setTitle(title: null | string): void;
|
|
46
|
-
insertNewAfter(
|
|
46
|
+
insertNewAfter(_: RangeSelection, restoreSelection?: boolean): null | ElementNode;
|
|
47
47
|
canInsertTextBefore(): false;
|
|
48
48
|
canInsertTextAfter(): false;
|
|
49
49
|
canBeEmpty(): false;
|
package/package.json
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
"link"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.12.
|
|
11
|
+
"version": "0.12.3",
|
|
12
12
|
"main": "LexicalLink.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.12.
|
|
14
|
+
"lexical": "0.12.3"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.12.
|
|
17
|
+
"@lexical/utils": "0.12.3"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|