@lexical/link 0.8.1 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LexicalLink.dev.js +75 -7
- package/package.json +3 -3
package/LexicalLink.dev.js
CHANGED
|
@@ -10,6 +10,7 @@ var utils = require('@lexical/utils');
|
|
|
10
10
|
var lexical = require('lexical');
|
|
11
11
|
|
|
12
12
|
/** @module @lexical/link */
|
|
13
|
+
|
|
13
14
|
/** @noInheritDoc */
|
|
14
15
|
class LinkNode extends lexical.ElementNode {
|
|
15
16
|
/** @internal */
|
|
@@ -17,16 +18,17 @@ class LinkNode extends lexical.ElementNode {
|
|
|
17
18
|
/** @internal */
|
|
18
19
|
|
|
19
20
|
/** @internal */
|
|
20
|
-
|
|
21
21
|
static getType() {
|
|
22
22
|
return 'link';
|
|
23
23
|
}
|
|
24
|
+
|
|
24
25
|
static clone(node) {
|
|
25
26
|
return new LinkNode(node.__url, {
|
|
26
27
|
rel: node.__rel,
|
|
27
28
|
target: node.__target
|
|
28
29
|
}, node.__key);
|
|
29
30
|
}
|
|
31
|
+
|
|
30
32
|
constructor(url, attributes = {}, key) {
|
|
31
33
|
super(key);
|
|
32
34
|
const {
|
|
@@ -37,25 +39,32 @@ class LinkNode extends lexical.ElementNode {
|
|
|
37
39
|
this.__target = target;
|
|
38
40
|
this.__rel = rel;
|
|
39
41
|
}
|
|
42
|
+
|
|
40
43
|
createDOM(config) {
|
|
41
44
|
const element = document.createElement('a');
|
|
42
45
|
element.href = this.__url;
|
|
46
|
+
|
|
43
47
|
if (this.__target !== null) {
|
|
44
48
|
element.target = this.__target;
|
|
45
49
|
}
|
|
50
|
+
|
|
46
51
|
if (this.__rel !== null) {
|
|
47
52
|
element.rel = this.__rel;
|
|
48
53
|
}
|
|
54
|
+
|
|
49
55
|
utils.addClassNamesToElement(element, config.theme.link);
|
|
50
56
|
return element;
|
|
51
57
|
}
|
|
58
|
+
|
|
52
59
|
updateDOM(prevNode, anchor, config) {
|
|
53
60
|
const url = this.__url;
|
|
54
61
|
const target = this.__target;
|
|
55
62
|
const rel = this.__rel;
|
|
63
|
+
|
|
56
64
|
if (url !== prevNode.__url) {
|
|
57
65
|
anchor.href = url;
|
|
58
66
|
}
|
|
67
|
+
|
|
59
68
|
if (target !== prevNode.__target) {
|
|
60
69
|
if (target) {
|
|
61
70
|
anchor.target = target;
|
|
@@ -63,6 +72,7 @@ class LinkNode extends lexical.ElementNode {
|
|
|
63
72
|
anchor.removeAttribute('target');
|
|
64
73
|
}
|
|
65
74
|
}
|
|
75
|
+
|
|
66
76
|
if (rel !== prevNode.__rel) {
|
|
67
77
|
if (rel) {
|
|
68
78
|
anchor.rel = rel;
|
|
@@ -70,8 +80,10 @@ class LinkNode extends lexical.ElementNode {
|
|
|
70
80
|
anchor.removeAttribute('rel');
|
|
71
81
|
}
|
|
72
82
|
}
|
|
83
|
+
|
|
73
84
|
return false;
|
|
74
85
|
}
|
|
86
|
+
|
|
75
87
|
static importDOM() {
|
|
76
88
|
return {
|
|
77
89
|
a: node => ({
|
|
@@ -80,6 +92,7 @@ class LinkNode extends lexical.ElementNode {
|
|
|
80
92
|
})
|
|
81
93
|
};
|
|
82
94
|
}
|
|
95
|
+
|
|
83
96
|
static importJSON(serializedNode) {
|
|
84
97
|
const node = $createLinkNode(serializedNode.url, {
|
|
85
98
|
rel: serializedNode.rel,
|
|
@@ -90,9 +103,9 @@ class LinkNode extends lexical.ElementNode {
|
|
|
90
103
|
node.setDirection(serializedNode.direction);
|
|
91
104
|
return node;
|
|
92
105
|
}
|
|
106
|
+
|
|
93
107
|
exportJSON() {
|
|
94
|
-
return {
|
|
95
|
-
...super.exportJSON(),
|
|
108
|
+
return { ...super.exportJSON(),
|
|
96
109
|
rel: this.getRel(),
|
|
97
110
|
target: this.getTarget(),
|
|
98
111
|
type: 'link',
|
|
@@ -100,29 +113,37 @@ class LinkNode extends lexical.ElementNode {
|
|
|
100
113
|
version: 1
|
|
101
114
|
};
|
|
102
115
|
}
|
|
116
|
+
|
|
103
117
|
getURL() {
|
|
104
118
|
return this.getLatest().__url;
|
|
105
119
|
}
|
|
120
|
+
|
|
106
121
|
setURL(url) {
|
|
107
122
|
const writable = this.getWritable();
|
|
108
123
|
writable.__url = url;
|
|
109
124
|
}
|
|
125
|
+
|
|
110
126
|
getTarget() {
|
|
111
127
|
return this.getLatest().__target;
|
|
112
128
|
}
|
|
129
|
+
|
|
113
130
|
setTarget(target) {
|
|
114
131
|
const writable = this.getWritable();
|
|
115
132
|
writable.__target = target;
|
|
116
133
|
}
|
|
134
|
+
|
|
117
135
|
getRel() {
|
|
118
136
|
return this.getLatest().__rel;
|
|
119
137
|
}
|
|
138
|
+
|
|
120
139
|
setRel(rel) {
|
|
121
140
|
const writable = this.getWritable();
|
|
122
141
|
writable.__rel = rel;
|
|
123
142
|
}
|
|
143
|
+
|
|
124
144
|
insertNewAfter(selection, restoreSelection = true) {
|
|
125
145
|
const element = this.getParentOrThrow().insertNewAfter(selection, restoreSelection);
|
|
146
|
+
|
|
126
147
|
if (lexical.$isElementNode(element)) {
|
|
127
148
|
const linkNode = $createLinkNode(this.__url, {
|
|
128
149
|
rel: this.__rel,
|
|
@@ -131,33 +152,44 @@ class LinkNode extends lexical.ElementNode {
|
|
|
131
152
|
element.append(linkNode);
|
|
132
153
|
return linkNode;
|
|
133
154
|
}
|
|
155
|
+
|
|
134
156
|
return null;
|
|
135
157
|
}
|
|
158
|
+
|
|
136
159
|
canInsertTextBefore() {
|
|
137
160
|
return false;
|
|
138
161
|
}
|
|
162
|
+
|
|
139
163
|
canInsertTextAfter() {
|
|
140
164
|
return false;
|
|
141
165
|
}
|
|
166
|
+
|
|
142
167
|
canBeEmpty() {
|
|
143
168
|
return false;
|
|
144
169
|
}
|
|
170
|
+
|
|
145
171
|
isInline() {
|
|
146
172
|
return true;
|
|
147
173
|
}
|
|
174
|
+
|
|
148
175
|
extractWithChild(child, selection, destination) {
|
|
149
176
|
if (!lexical.$isRangeSelection(selection)) {
|
|
150
177
|
return false;
|
|
151
178
|
}
|
|
179
|
+
|
|
152
180
|
const anchorNode = selection.anchor.getNode();
|
|
153
181
|
const focusNode = selection.focus.getNode();
|
|
154
182
|
return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && selection.getTextContent().length > 0;
|
|
155
183
|
}
|
|
184
|
+
|
|
156
185
|
}
|
|
186
|
+
|
|
157
187
|
function convertAnchorElement(domNode) {
|
|
158
188
|
let node = null;
|
|
189
|
+
|
|
159
190
|
if (utils.isHTMLAnchorElement(domNode)) {
|
|
160
191
|
const content = domNode.textContent;
|
|
192
|
+
|
|
161
193
|
if (content !== null && content !== '') {
|
|
162
194
|
node = $createLinkNode(domNode.getAttribute('href') || '', {
|
|
163
195
|
rel: domNode.getAttribute('rel'),
|
|
@@ -165,10 +197,12 @@ function convertAnchorElement(domNode) {
|
|
|
165
197
|
});
|
|
166
198
|
}
|
|
167
199
|
}
|
|
200
|
+
|
|
168
201
|
return {
|
|
169
202
|
node
|
|
170
203
|
};
|
|
171
204
|
}
|
|
205
|
+
|
|
172
206
|
function $createLinkNode(url, attributes) {
|
|
173
207
|
return lexical.$applyNodeReplacement(new LinkNode(url, attributes));
|
|
174
208
|
}
|
|
@@ -181,12 +215,14 @@ class AutoLinkNode extends LinkNode {
|
|
|
181
215
|
static getType() {
|
|
182
216
|
return 'autolink';
|
|
183
217
|
}
|
|
218
|
+
|
|
184
219
|
static clone(node) {
|
|
185
220
|
return new AutoLinkNode(node.__url, {
|
|
186
221
|
rel: node.__rel,
|
|
187
222
|
target: node.__target
|
|
188
223
|
}, node.__key);
|
|
189
224
|
}
|
|
225
|
+
|
|
190
226
|
static importJSON(serializedNode) {
|
|
191
227
|
const node = $createAutoLinkNode(serializedNode.url, {
|
|
192
228
|
rel: serializedNode.rel,
|
|
@@ -197,19 +233,22 @@ class AutoLinkNode extends LinkNode {
|
|
|
197
233
|
node.setDirection(serializedNode.direction);
|
|
198
234
|
return node;
|
|
199
235
|
}
|
|
236
|
+
|
|
200
237
|
static importDOM() {
|
|
201
238
|
// TODO: Should link node should handle the import over autolink?
|
|
202
239
|
return null;
|
|
203
240
|
}
|
|
241
|
+
|
|
204
242
|
exportJSON() {
|
|
205
|
-
return {
|
|
206
|
-
...super.exportJSON(),
|
|
243
|
+
return { ...super.exportJSON(),
|
|
207
244
|
type: 'autolink',
|
|
208
245
|
version: 1
|
|
209
246
|
};
|
|
210
247
|
}
|
|
248
|
+
|
|
211
249
|
insertNewAfter(selection, restoreSelection = true) {
|
|
212
250
|
const element = this.getParentOrThrow().insertNewAfter(selection, restoreSelection);
|
|
251
|
+
|
|
213
252
|
if (lexical.$isElementNode(element)) {
|
|
214
253
|
const linkNode = $createAutoLinkNode(this.__url, {
|
|
215
254
|
rel: this._rel,
|
|
@@ -218,8 +257,10 @@ class AutoLinkNode extends LinkNode {
|
|
|
218
257
|
element.append(linkNode);
|
|
219
258
|
return linkNode;
|
|
220
259
|
}
|
|
260
|
+
|
|
221
261
|
return null;
|
|
222
262
|
}
|
|
263
|
+
|
|
223
264
|
}
|
|
224
265
|
function $createAutoLinkNode(url, attributes) {
|
|
225
266
|
return lexical.$applyNodeReplacement(new AutoLinkNode(url, attributes));
|
|
@@ -234,64 +275,82 @@ function toggleLink(url, attributes = {}) {
|
|
|
234
275
|
} = attributes;
|
|
235
276
|
const rel = attributes.rel === undefined ? 'noopener' : attributes.rel;
|
|
236
277
|
const selection = lexical.$getSelection();
|
|
278
|
+
|
|
237
279
|
if (!lexical.$isRangeSelection(selection)) {
|
|
238
280
|
return;
|
|
239
281
|
}
|
|
282
|
+
|
|
240
283
|
const nodes = selection.extract();
|
|
284
|
+
|
|
241
285
|
if (url === null) {
|
|
242
286
|
// Remove LinkNodes
|
|
243
287
|
nodes.forEach(node => {
|
|
244
288
|
const parent = node.getParent();
|
|
289
|
+
|
|
245
290
|
if ($isLinkNode(parent)) {
|
|
246
291
|
const children = parent.getChildren();
|
|
292
|
+
|
|
247
293
|
for (let i = 0; i < children.length; i++) {
|
|
248
294
|
parent.insertBefore(children[i]);
|
|
249
295
|
}
|
|
296
|
+
|
|
250
297
|
parent.remove();
|
|
251
298
|
}
|
|
252
299
|
});
|
|
253
300
|
} else {
|
|
254
301
|
// Add or merge LinkNodes
|
|
255
302
|
if (nodes.length === 1) {
|
|
256
|
-
const firstNode = nodes[0];
|
|
257
|
-
// if the first node is a LinkNode or if its
|
|
303
|
+
const firstNode = nodes[0]; // if the first node is a LinkNode or if its
|
|
258
304
|
// parent is a LinkNode, we update the URL, target and rel.
|
|
305
|
+
|
|
259
306
|
const linkNode = $isLinkNode(firstNode) ? firstNode : $getLinkAncestor(firstNode);
|
|
307
|
+
|
|
260
308
|
if (linkNode !== null) {
|
|
261
309
|
linkNode.setURL(url);
|
|
310
|
+
|
|
262
311
|
if (target !== undefined) {
|
|
263
312
|
linkNode.setTarget(target);
|
|
264
313
|
}
|
|
314
|
+
|
|
265
315
|
if (rel !== null) {
|
|
266
316
|
linkNode.setRel(rel);
|
|
267
317
|
}
|
|
318
|
+
|
|
268
319
|
return;
|
|
269
320
|
}
|
|
270
321
|
}
|
|
322
|
+
|
|
271
323
|
let prevParent = null;
|
|
272
324
|
let linkNode = null;
|
|
273
325
|
nodes.forEach(node => {
|
|
274
326
|
const parent = node.getParent();
|
|
327
|
+
|
|
275
328
|
if (parent === linkNode || parent === null || lexical.$isElementNode(node) && !node.isInline()) {
|
|
276
329
|
return;
|
|
277
330
|
}
|
|
331
|
+
|
|
278
332
|
if ($isLinkNode(parent)) {
|
|
279
333
|
linkNode = parent;
|
|
280
334
|
parent.setURL(url);
|
|
335
|
+
|
|
281
336
|
if (target !== undefined) {
|
|
282
337
|
parent.setTarget(target);
|
|
283
338
|
}
|
|
339
|
+
|
|
284
340
|
if (rel !== null) {
|
|
285
341
|
linkNode.setRel(rel);
|
|
286
342
|
}
|
|
343
|
+
|
|
287
344
|
return;
|
|
288
345
|
}
|
|
346
|
+
|
|
289
347
|
if (!parent.is(prevParent)) {
|
|
290
348
|
prevParent = parent;
|
|
291
349
|
linkNode = $createLinkNode(url, {
|
|
292
350
|
rel,
|
|
293
351
|
target
|
|
294
352
|
});
|
|
353
|
+
|
|
295
354
|
if ($isLinkNode(parent)) {
|
|
296
355
|
if (node.getPreviousSibling() === null) {
|
|
297
356
|
parent.insertBefore(linkNode);
|
|
@@ -302,31 +361,40 @@ function toggleLink(url, attributes = {}) {
|
|
|
302
361
|
node.insertBefore(linkNode);
|
|
303
362
|
}
|
|
304
363
|
}
|
|
364
|
+
|
|
305
365
|
if ($isLinkNode(node)) {
|
|
306
366
|
if (node.is(linkNode)) {
|
|
307
367
|
return;
|
|
308
368
|
}
|
|
369
|
+
|
|
309
370
|
if (linkNode !== null) {
|
|
310
371
|
const children = node.getChildren();
|
|
372
|
+
|
|
311
373
|
for (let i = 0; i < children.length; i++) {
|
|
312
374
|
linkNode.append(children[i]);
|
|
313
375
|
}
|
|
314
376
|
}
|
|
377
|
+
|
|
315
378
|
node.remove();
|
|
316
379
|
return;
|
|
317
380
|
}
|
|
381
|
+
|
|
318
382
|
if (linkNode !== null) {
|
|
319
383
|
linkNode.append(node);
|
|
320
384
|
}
|
|
321
385
|
});
|
|
322
386
|
}
|
|
323
387
|
}
|
|
388
|
+
|
|
324
389
|
function $getLinkAncestor(node) {
|
|
325
390
|
return $getAncestor(node, ancestor => $isLinkNode(ancestor));
|
|
326
391
|
}
|
|
392
|
+
|
|
327
393
|
function $getAncestor(node, predicate) {
|
|
328
394
|
let parent = node;
|
|
395
|
+
|
|
329
396
|
while (parent !== null && (parent = parent.getParent()) !== null && !predicate(parent));
|
|
397
|
+
|
|
330
398
|
return parent;
|
|
331
399
|
}
|
|
332
400
|
|
package/package.json
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
"link"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.
|
|
11
|
+
"version": "0.9.1",
|
|
12
12
|
"main": "LexicalLink.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.
|
|
14
|
+
"lexical": "0.9.1"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.
|
|
17
|
+
"@lexical/utils": "0.9.1"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|