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