@lexical/link 0.3.7 → 0.3.10
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 +184 -85
- package/LexicalLink.js.flow +35 -4
- package/LexicalLink.prod.js +9 -8
- package/index.d.ts +29 -5
- package/package.json +3 -3
package/LexicalLink.dev.js
CHANGED
|
@@ -22,28 +22,64 @@ class LinkNode extends lexical.ElementNode {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
static clone(node) {
|
|
25
|
-
return new LinkNode(node.__url,
|
|
25
|
+
return new LinkNode(node.__url, {
|
|
26
|
+
rel: node.__rel,
|
|
27
|
+
target: node.__target
|
|
28
|
+
}, node.__key);
|
|
26
29
|
}
|
|
27
30
|
|
|
28
|
-
constructor(url, key) {
|
|
31
|
+
constructor(url, attributes = {}, key) {
|
|
29
32
|
super(key);
|
|
33
|
+
const {
|
|
34
|
+
target = null,
|
|
35
|
+
rel = null
|
|
36
|
+
} = attributes;
|
|
30
37
|
this.__url = url;
|
|
38
|
+
this.__target = target;
|
|
39
|
+
this.__rel = rel;
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
createDOM(config) {
|
|
34
43
|
const element = document.createElement('a');
|
|
35
44
|
element.href = this.__url;
|
|
45
|
+
|
|
46
|
+
if (this.__target !== null) {
|
|
47
|
+
element.target = this.__target;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (this.__rel !== null) {
|
|
51
|
+
element.rel = this.__rel;
|
|
52
|
+
}
|
|
53
|
+
|
|
36
54
|
utils.addClassNamesToElement(element, config.theme.link);
|
|
37
55
|
return element;
|
|
38
56
|
}
|
|
39
57
|
|
|
40
58
|
updateDOM(prevNode, anchor, config) {
|
|
41
59
|
const url = this.__url;
|
|
60
|
+
const target = this.__target;
|
|
61
|
+
const rel = this.__rel;
|
|
42
62
|
|
|
43
63
|
if (url !== prevNode.__url) {
|
|
44
64
|
anchor.href = url;
|
|
45
65
|
}
|
|
46
66
|
|
|
67
|
+
if (target !== prevNode.__target) {
|
|
68
|
+
if (target) {
|
|
69
|
+
anchor.target = target;
|
|
70
|
+
} else {
|
|
71
|
+
anchor.removeAttribute('target');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (rel !== prevNode.__rel) {
|
|
76
|
+
if (rel) {
|
|
77
|
+
anchor.rel = rel;
|
|
78
|
+
} else {
|
|
79
|
+
anchor.removeAttribute('rel');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
47
83
|
return false;
|
|
48
84
|
}
|
|
49
85
|
|
|
@@ -57,7 +93,10 @@ class LinkNode extends lexical.ElementNode {
|
|
|
57
93
|
}
|
|
58
94
|
|
|
59
95
|
static importJSON(serializedNode) {
|
|
60
|
-
const node = $createLinkNode(serializedNode.url
|
|
96
|
+
const node = $createLinkNode(serializedNode.url, {
|
|
97
|
+
rel: serializedNode.rel,
|
|
98
|
+
target: serializedNode.target
|
|
99
|
+
});
|
|
61
100
|
node.setFormat(serializedNode.format);
|
|
62
101
|
node.setIndent(serializedNode.indent);
|
|
63
102
|
node.setDirection(serializedNode.direction);
|
|
@@ -66,6 +105,8 @@ class LinkNode extends lexical.ElementNode {
|
|
|
66
105
|
|
|
67
106
|
exportJSON() {
|
|
68
107
|
return { ...super.exportJSON(),
|
|
108
|
+
rel: this.getRel(),
|
|
109
|
+
target: this.getTarget(),
|
|
69
110
|
type: 'link',
|
|
70
111
|
url: this.getURL(),
|
|
71
112
|
version: 1
|
|
@@ -81,11 +122,32 @@ class LinkNode extends lexical.ElementNode {
|
|
|
81
122
|
writable.__url = url;
|
|
82
123
|
}
|
|
83
124
|
|
|
125
|
+
getTarget() {
|
|
126
|
+
return this.getLatest().__target;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
setTarget(target) {
|
|
130
|
+
const writable = this.getWritable();
|
|
131
|
+
writable.__target = target;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
getRel() {
|
|
135
|
+
return this.getLatest().__rel;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
setRel(rel) {
|
|
139
|
+
const writable = this.getWritable();
|
|
140
|
+
writable.__rel = rel;
|
|
141
|
+
}
|
|
142
|
+
|
|
84
143
|
insertNewAfter(selection) {
|
|
85
144
|
const element = this.getParentOrThrow().insertNewAfter(selection);
|
|
86
145
|
|
|
87
146
|
if (lexical.$isElementNode(element)) {
|
|
88
|
-
const linkNode = $createLinkNode(this.__url
|
|
147
|
+
const linkNode = $createLinkNode(this.__url, {
|
|
148
|
+
rel: this.__rel,
|
|
149
|
+
target: this.__target
|
|
150
|
+
});
|
|
89
151
|
element.append(linkNode);
|
|
90
152
|
return linkNode;
|
|
91
153
|
}
|
|
@@ -125,7 +187,10 @@ function convertAnchorElement(domNode) {
|
|
|
125
187
|
let node = null;
|
|
126
188
|
|
|
127
189
|
if (domNode instanceof HTMLAnchorElement) {
|
|
128
|
-
node = $createLinkNode(domNode.getAttribute('href') || ''
|
|
190
|
+
node = $createLinkNode(domNode.getAttribute('href') || '', {
|
|
191
|
+
rel: domNode.getAttribute('rel'),
|
|
192
|
+
target: domNode.getAttribute('target')
|
|
193
|
+
});
|
|
129
194
|
}
|
|
130
195
|
|
|
131
196
|
return {
|
|
@@ -133,8 +198,8 @@ function convertAnchorElement(domNode) {
|
|
|
133
198
|
};
|
|
134
199
|
}
|
|
135
200
|
|
|
136
|
-
function $createLinkNode(url) {
|
|
137
|
-
return new LinkNode(url);
|
|
201
|
+
function $createLinkNode(url, attributes) {
|
|
202
|
+
return new LinkNode(url, attributes);
|
|
138
203
|
}
|
|
139
204
|
function $isLinkNode(node) {
|
|
140
205
|
return node instanceof LinkNode;
|
|
@@ -147,11 +212,17 @@ class AutoLinkNode extends LinkNode {
|
|
|
147
212
|
}
|
|
148
213
|
|
|
149
214
|
static clone(node) {
|
|
150
|
-
return new AutoLinkNode(node.__url,
|
|
215
|
+
return new AutoLinkNode(node.__url, {
|
|
216
|
+
rel: node.__rel,
|
|
217
|
+
target: node.__target
|
|
218
|
+
}, node.__key);
|
|
151
219
|
}
|
|
152
220
|
|
|
153
221
|
static importJSON(serializedNode) {
|
|
154
|
-
const node = $createAutoLinkNode(serializedNode.url
|
|
222
|
+
const node = $createAutoLinkNode(serializedNode.url, {
|
|
223
|
+
rel: serializedNode.rel,
|
|
224
|
+
target: serializedNode.target
|
|
225
|
+
});
|
|
155
226
|
node.setFormat(serializedNode.format);
|
|
156
227
|
node.setIndent(serializedNode.indent);
|
|
157
228
|
node.setDirection(serializedNode.direction);
|
|
@@ -174,7 +245,10 @@ class AutoLinkNode extends LinkNode {
|
|
|
174
245
|
const element = this.getParentOrThrow().insertNewAfter(selection);
|
|
175
246
|
|
|
176
247
|
if (lexical.$isElementNode(element)) {
|
|
177
|
-
const linkNode = $createAutoLinkNode(this.__url
|
|
248
|
+
const linkNode = $createAutoLinkNode(this.__url, {
|
|
249
|
+
rel: this._rel,
|
|
250
|
+
target: this.__target
|
|
251
|
+
});
|
|
178
252
|
element.append(linkNode);
|
|
179
253
|
return linkNode;
|
|
180
254
|
}
|
|
@@ -183,117 +257,142 @@ class AutoLinkNode extends LinkNode {
|
|
|
183
257
|
}
|
|
184
258
|
|
|
185
259
|
}
|
|
186
|
-
function $createAutoLinkNode(url) {
|
|
187
|
-
return new AutoLinkNode(url);
|
|
260
|
+
function $createAutoLinkNode(url, attributes) {
|
|
261
|
+
return new AutoLinkNode(url, attributes);
|
|
188
262
|
}
|
|
189
263
|
function $isAutoLinkNode(node) {
|
|
190
264
|
return node instanceof AutoLinkNode;
|
|
191
265
|
}
|
|
192
266
|
const TOGGLE_LINK_COMMAND = lexical.createCommand();
|
|
193
|
-
function toggleLink(url) {
|
|
267
|
+
function toggleLink(url, attributes = {}) {
|
|
268
|
+
const {
|
|
269
|
+
target,
|
|
270
|
+
rel
|
|
271
|
+
} = attributes;
|
|
194
272
|
const selection = lexical.$getSelection();
|
|
195
273
|
|
|
196
|
-
if (selection
|
|
197
|
-
|
|
274
|
+
if (!lexical.$isRangeSelection(selection)) {
|
|
275
|
+
return;
|
|
198
276
|
}
|
|
199
277
|
|
|
200
|
-
const
|
|
278
|
+
const nodes = selection.extract();
|
|
201
279
|
|
|
202
|
-
if (
|
|
203
|
-
|
|
280
|
+
if (url === null) {
|
|
281
|
+
// Remove LinkNodes
|
|
282
|
+
nodes.forEach(node => {
|
|
283
|
+
const parent = node.getParent();
|
|
204
284
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
nodes.forEach(node => {
|
|
208
|
-
const parent = node.getParent();
|
|
285
|
+
if ($isLinkNode(parent)) {
|
|
286
|
+
const children = parent.getChildren();
|
|
209
287
|
|
|
210
|
-
|
|
211
|
-
|
|
288
|
+
for (let i = 0; i < children.length; i++) {
|
|
289
|
+
parent.insertBefore(children[i]);
|
|
290
|
+
}
|
|
212
291
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
292
|
+
parent.remove();
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
} else {
|
|
296
|
+
// Add or merge LinkNodes
|
|
297
|
+
if (nodes.length === 1) {
|
|
298
|
+
const firstNode = nodes[0]; // if the first node is a LinkNode or if its
|
|
299
|
+
// parent is a LinkNode, we update the URL, target and rel.
|
|
300
|
+
|
|
301
|
+
const linkNode = $isLinkNode(firstNode) ? firstNode : $getLinkAncestor(firstNode);
|
|
302
|
+
|
|
303
|
+
if (linkNode !== null) {
|
|
304
|
+
linkNode.setURL(url);
|
|
216
305
|
|
|
217
|
-
|
|
306
|
+
if (target !== undefined) {
|
|
307
|
+
linkNode.setTarget(target);
|
|
218
308
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
if (nodes.length === 1) {
|
|
223
|
-
const firstNode = nodes[0]; // if the first node is a LinkNode or if its
|
|
224
|
-
// parent is a LinkNode, we update the URL.
|
|
225
|
-
|
|
226
|
-
if ($isLinkNode(firstNode)) {
|
|
227
|
-
firstNode.setURL(url);
|
|
228
|
-
return;
|
|
229
|
-
} else {
|
|
230
|
-
const parent = firstNode.getParent();
|
|
231
|
-
|
|
232
|
-
if ($isLinkNode(parent)) {
|
|
233
|
-
// set parent to be the current linkNode
|
|
234
|
-
// so that other nodes in the same parent
|
|
235
|
-
// aren't handled separately below.
|
|
236
|
-
parent.setURL(url);
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
309
|
+
|
|
310
|
+
if (rel !== undefined) {
|
|
311
|
+
linkNode.setRel(rel);
|
|
239
312
|
}
|
|
313
|
+
|
|
314
|
+
return;
|
|
240
315
|
}
|
|
316
|
+
}
|
|
241
317
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
318
|
+
let prevParent = null;
|
|
319
|
+
let linkNode = null;
|
|
320
|
+
nodes.forEach(node => {
|
|
321
|
+
const parent = node.getParent();
|
|
246
322
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
323
|
+
if (parent === linkNode || parent === null || lexical.$isElementNode(node) && !node.isInline()) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
250
326
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
327
|
+
if ($isLinkNode(parent)) {
|
|
328
|
+
linkNode = parent;
|
|
329
|
+
parent.setURL(url);
|
|
256
330
|
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
|
|
331
|
+
if (target !== undefined) {
|
|
332
|
+
parent.setTarget(target);
|
|
333
|
+
}
|
|
260
334
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
parent.insertBefore(linkNode);
|
|
264
|
-
} else {
|
|
265
|
-
parent.insertAfter(linkNode);
|
|
266
|
-
}
|
|
267
|
-
} else {
|
|
268
|
-
node.insertBefore(linkNode);
|
|
269
|
-
}
|
|
335
|
+
if (rel !== undefined) {
|
|
336
|
+
parent.setRel(rel);
|
|
270
337
|
}
|
|
271
338
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
276
341
|
|
|
277
|
-
|
|
278
|
-
|
|
342
|
+
if (!parent.is(prevParent)) {
|
|
343
|
+
prevParent = parent;
|
|
344
|
+
linkNode = $createLinkNode(url, {
|
|
345
|
+
rel,
|
|
346
|
+
target
|
|
347
|
+
});
|
|
279
348
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
349
|
+
if ($isLinkNode(parent)) {
|
|
350
|
+
if (node.getPreviousSibling() === null) {
|
|
351
|
+
parent.insertBefore(linkNode);
|
|
352
|
+
} else {
|
|
353
|
+
parent.insertAfter(linkNode);
|
|
283
354
|
}
|
|
355
|
+
} else {
|
|
356
|
+
node.insertBefore(linkNode);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
284
359
|
|
|
285
|
-
|
|
360
|
+
if ($isLinkNode(node)) {
|
|
361
|
+
if (node.is(linkNode)) {
|
|
286
362
|
return;
|
|
287
363
|
}
|
|
288
364
|
|
|
289
365
|
if (linkNode !== null) {
|
|
290
|
-
|
|
366
|
+
const children = node.getChildren();
|
|
367
|
+
|
|
368
|
+
for (let i = 0; i < children.length; i++) {
|
|
369
|
+
linkNode.append(children[i]);
|
|
370
|
+
}
|
|
291
371
|
}
|
|
292
|
-
|
|
293
|
-
|
|
372
|
+
|
|
373
|
+
node.remove();
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (linkNode !== null) {
|
|
378
|
+
linkNode.append(node);
|
|
379
|
+
}
|
|
380
|
+
});
|
|
294
381
|
}
|
|
295
382
|
}
|
|
296
383
|
|
|
384
|
+
function $getLinkAncestor(node) {
|
|
385
|
+
return $getAncestor(node, ancestor => $isLinkNode(ancestor));
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function $getAncestor(node, predicate) {
|
|
389
|
+
let parent = node;
|
|
390
|
+
|
|
391
|
+
while (parent !== null && (parent = parent.getParent()) !== null && !predicate(parent));
|
|
392
|
+
|
|
393
|
+
return parent;
|
|
394
|
+
}
|
|
395
|
+
|
|
297
396
|
exports.$createAutoLinkNode = $createAutoLinkNode;
|
|
298
397
|
exports.$createLinkNode = $createLinkNode;
|
|
299
398
|
exports.$isAutoLinkNode = $isAutoLinkNode;
|
package/LexicalLink.js.flow
CHANGED
|
@@ -18,9 +18,18 @@ import {addClassNamesToElement} from '@lexical/utils';
|
|
|
18
18
|
import {$isElementNode, ElementNode} from 'lexical';
|
|
19
19
|
declare export class LinkNode extends ElementNode {
|
|
20
20
|
__url: string;
|
|
21
|
+
__target: null | string;
|
|
22
|
+
__rel: null | string;
|
|
21
23
|
static getType(): string;
|
|
22
24
|
static clone(node: LinkNode): LinkNode;
|
|
23
|
-
constructor(
|
|
25
|
+
constructor(
|
|
26
|
+
url: string,
|
|
27
|
+
attributes?: {
|
|
28
|
+
rel?: null | string,
|
|
29
|
+
target?: null | string,
|
|
30
|
+
},
|
|
31
|
+
key?: NodeKey,
|
|
32
|
+
): void;
|
|
24
33
|
createDOM(config: EditorConfig): HTMLElement;
|
|
25
34
|
updateDOM(
|
|
26
35
|
prevNode: LinkNode,
|
|
@@ -30,13 +39,23 @@ declare export class LinkNode extends ElementNode {
|
|
|
30
39
|
static importDOM(): DOMConversionMap | null;
|
|
31
40
|
getURL(): string;
|
|
32
41
|
setURL(url: string): void;
|
|
42
|
+
getTarget(): null | string;
|
|
43
|
+
setTarget(target: null | string): void;
|
|
44
|
+
getRel(): null | string;
|
|
45
|
+
setRel(rel: null | string): void;
|
|
33
46
|
insertNewAfter(selection: RangeSelection): null | ElementNode;
|
|
34
47
|
canInsertTextBefore(): false;
|
|
35
48
|
canInsertTextAfter(): false;
|
|
36
49
|
canBeEmpty(): false;
|
|
37
50
|
isInline(): true;
|
|
38
51
|
}
|
|
39
|
-
declare export function $createLinkNode(
|
|
52
|
+
declare export function $createLinkNode(
|
|
53
|
+
url: string,
|
|
54
|
+
attributes?: {
|
|
55
|
+
rel?: null | string,
|
|
56
|
+
target?: null | string,
|
|
57
|
+
},
|
|
58
|
+
): LinkNode;
|
|
40
59
|
declare export function $isLinkNode(
|
|
41
60
|
node: ?LexicalNode,
|
|
42
61
|
): boolean %checks(node instanceof LinkNode);
|
|
@@ -51,5 +70,17 @@ declare export function $isAutoLinkNode(
|
|
|
51
70
|
node: ?LexicalNode,
|
|
52
71
|
): boolean %checks(node instanceof AutoLinkNode);
|
|
53
72
|
|
|
54
|
-
declare export var TOGGLE_LINK_COMMAND: LexicalCommand<
|
|
55
|
-
|
|
73
|
+
declare export var TOGGLE_LINK_COMMAND: LexicalCommand<
|
|
74
|
+
| string
|
|
75
|
+
| {
|
|
76
|
+
url: string,
|
|
77
|
+
target?: null | string,
|
|
78
|
+
rel?: null | string,
|
|
79
|
+
}
|
|
80
|
+
| null,
|
|
81
|
+
>;
|
|
82
|
+
declare export function toggleLink(
|
|
83
|
+
url: null | string,
|
|
84
|
+
target?: null | string,
|
|
85
|
+
rel?: null | string,
|
|
86
|
+
): void;
|
package/LexicalLink.prod.js
CHANGED
|
@@ -4,11 +4,12 @@
|
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
'use strict';var l=require("@lexical/utils"),m=require("lexical");
|
|
8
|
+
class n extends m.ElementNode{static getType(){return"link"}static clone(a){return new n(a.__url,{rel:a.__rel,target:a.__target},a.__key)}constructor(a,b={},g){super(g);let {target:h=null,rel:d=null}=b;this.__url=a;this.__target=h;this.__rel=d}createDOM(a){let b=document.createElement("a");b.href=this.__url;null!==this.__target&&(b.target=this.__target);null!==this.__rel&&(b.rel=this.__rel);l.addClassNamesToElement(b,a.theme.link);return b}updateDOM(a,b){let g=this.__url,h=this.__target,d=this.__rel;
|
|
9
|
+
g!==a.__url&&(b.href=g);h!==a.__target&&(h?b.target=h:b.removeAttribute("target"));d!==a.__rel&&(d?b.rel=d:b.removeAttribute("rel"));return!1}static importDOM(){return{a:()=>({conversion:p,priority:1})}}static importJSON(a){let b=q(a.url,{rel:a.rel,target:a.target});b.setFormat(a.format);b.setIndent(a.indent);b.setDirection(a.direction);return b}exportJSON(){return{...super.exportJSON(),rel:this.getRel(),target:this.getTarget(),type:"link",url:this.getURL(),version:1}}getURL(){return this.getLatest().__url}setURL(a){this.getWritable().__url=
|
|
10
|
+
a}getTarget(){return this.getLatest().__target}setTarget(a){this.getWritable().__target=a}getRel(){return this.getLatest().__rel}setRel(a){this.getWritable().__rel=a}insertNewAfter(a){a=this.getParentOrThrow().insertNewAfter(a);if(m.$isElementNode(a)){let b=q(this.__url,{rel:this.__rel,target:this.__target});a.append(b);return b}return null}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(a,b){if(!m.$isRangeSelection(b))return!1;
|
|
11
|
+
a=b.anchor.getNode();let g=b.focus.getNode();return this.isParentOf(a)&&this.isParentOf(g)&&0<b.getTextContent().length}}function p(a){let b=null;a instanceof HTMLAnchorElement&&(b=q(a.getAttribute("href")||"",{rel:a.getAttribute("rel"),target:a.getAttribute("target")}));return{node:b}}function q(a,b){return new n(a,b)}function r(a){return a instanceof n}
|
|
12
|
+
class u extends n{static getType(){return"autolink"}static clone(a){return new u(a.__url,{rel:a.__rel,target:a.__target},a.__key)}static importJSON(a){let b=v(a.url,{rel:a.rel,target:a.target});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){a=this.getParentOrThrow().insertNewAfter(a);if(m.$isElementNode(a)){let b=v(this.__url,{rel:this._rel,target:this.__target});
|
|
13
|
+
a.append(b);return b}return null}}function v(a,b){return new u(a,b)}let w=m.createCommand();function x(a){return y(a,b=>r(b))}function y(a,b){for(;null!==a&&null!==(a=a.getParent())&&!b(a););return a}exports.$createAutoLinkNode=v;exports.$createLinkNode=q;exports.$isAutoLinkNode=function(a){return a instanceof u};exports.$isLinkNode=r;exports.AutoLinkNode=u;exports.LinkNode=n;exports.TOGGLE_LINK_COMMAND=w;
|
|
14
|
+
exports.toggleLink=function(a,b={}){let {target:g,rel:h}=b;b=m.$getSelection();if(m.$isRangeSelection(b))if(b=b.extract(),null===a)b.forEach(k=>{k=k.getParent();if(r(k)){let e=k.getChildren();for(let f=0;f<e.length;f++)k.insertBefore(e[f]);k.remove()}});else{if(1===b.length){var d=b[0];d=r(d)?d:x(d);if(null!==d){d.setURL(a);void 0!==g&&d.setTarget(g);void 0!==h&&d.setRel(h);return}}let k=null,e=null;b.forEach(f=>{var c=f.getParent();if(c!==e&&null!==c&&(!m.$isElementNode(f)||f.isInline()))if(r(c))e=
|
|
15
|
+
c,c.setURL(a),void 0!==g&&c.setTarget(g),void 0!==h&&c.setRel(h);else if(c.is(k)||(k=c,e=q(a,{rel:h,target:g}),r(c)?null===f.getPreviousSibling()?c.insertBefore(e):c.insertAfter(e):f.insertBefore(e)),r(f)){if(!f.is(e)){if(null!==e){c=f.getChildren();for(let t=0;t<c.length;t++)e.append(c[t])}f.remove()}}else null!==e&&e.append(f)})}}
|
package/index.d.ts
CHANGED
|
@@ -10,13 +10,20 @@ import { ElementNode, Spread } from 'lexical';
|
|
|
10
10
|
export declare type SerializedLinkNode = Spread<{
|
|
11
11
|
type: 'link';
|
|
12
12
|
url: string;
|
|
13
|
+
target?: null | string;
|
|
14
|
+
rel?: null | string;
|
|
13
15
|
version: 1;
|
|
14
16
|
}, SerializedElementNode>;
|
|
15
17
|
export declare class LinkNode extends ElementNode {
|
|
16
18
|
__url: string;
|
|
19
|
+
__target: null | string;
|
|
20
|
+
__rel: null | string;
|
|
17
21
|
static getType(): string;
|
|
18
22
|
static clone(node: LinkNode): LinkNode;
|
|
19
|
-
constructor(url: string,
|
|
23
|
+
constructor(url: string, attributes?: {
|
|
24
|
+
target?: null | string;
|
|
25
|
+
rel?: null | string;
|
|
26
|
+
}, key?: NodeKey);
|
|
20
27
|
createDOM(config: EditorConfig): HTMLAnchorElement;
|
|
21
28
|
updateDOM(prevNode: LinkNode, anchor: HTMLAnchorElement, config: EditorConfig): boolean;
|
|
22
29
|
static importDOM(): DOMConversionMap | null;
|
|
@@ -24,6 +31,10 @@ export declare class LinkNode extends ElementNode {
|
|
|
24
31
|
exportJSON(): SerializedLinkNode | SerializedAutoLinkNode;
|
|
25
32
|
getURL(): string;
|
|
26
33
|
setURL(url: string): void;
|
|
34
|
+
getTarget(): null | string;
|
|
35
|
+
setTarget(target: null | string): void;
|
|
36
|
+
getRel(): null | string;
|
|
37
|
+
setRel(rel: null | string): void;
|
|
27
38
|
insertNewAfter(selection: RangeSelection): null | ElementNode;
|
|
28
39
|
canInsertTextBefore(): false;
|
|
29
40
|
canInsertTextAfter(): false;
|
|
@@ -31,7 +42,10 @@ export declare class LinkNode extends ElementNode {
|
|
|
31
42
|
isInline(): true;
|
|
32
43
|
extractWithChild(child: LexicalNode, selection: RangeSelection | NodeSelection | GridSelection, destination: 'clone' | 'html'): boolean;
|
|
33
44
|
}
|
|
34
|
-
export declare function $createLinkNode(url: string
|
|
45
|
+
export declare function $createLinkNode(url: string, attributes?: {
|
|
46
|
+
target?: null | string;
|
|
47
|
+
rel?: null | string;
|
|
48
|
+
}): LinkNode;
|
|
35
49
|
export declare function $isLinkNode(node: LexicalNode | null | undefined): node is LinkNode;
|
|
36
50
|
export declare type SerializedAutoLinkNode = Spread<{
|
|
37
51
|
type: 'autolink';
|
|
@@ -45,7 +59,17 @@ export declare class AutoLinkNode extends LinkNode {
|
|
|
45
59
|
exportJSON(): SerializedAutoLinkNode;
|
|
46
60
|
insertNewAfter(selection: RangeSelection): null | ElementNode;
|
|
47
61
|
}
|
|
48
|
-
export declare function $createAutoLinkNode(url: string
|
|
62
|
+
export declare function $createAutoLinkNode(url: string, attributes?: {
|
|
63
|
+
target?: null | string;
|
|
64
|
+
rel?: null | string;
|
|
65
|
+
}): AutoLinkNode;
|
|
49
66
|
export declare function $isAutoLinkNode(node: LexicalNode | null | undefined): node is AutoLinkNode;
|
|
50
|
-
export declare const TOGGLE_LINK_COMMAND: LexicalCommand<string |
|
|
51
|
-
|
|
67
|
+
export declare const TOGGLE_LINK_COMMAND: LexicalCommand<string | {
|
|
68
|
+
url: string;
|
|
69
|
+
target?: string;
|
|
70
|
+
rel?: string;
|
|
71
|
+
} | null>;
|
|
72
|
+
export declare function toggleLink(url: null | string, attributes?: {
|
|
73
|
+
target?: null | string;
|
|
74
|
+
rel?: null | string;
|
|
75
|
+
}): void;
|
package/package.json
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
"link"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.3.
|
|
11
|
+
"version": "0.3.10",
|
|
12
12
|
"main": "LexicalLink.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.3.
|
|
14
|
+
"lexical": "0.3.10"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.3.
|
|
17
|
+
"@lexical/utils": "0.3.10"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|