@lexical/link 0.2.8 → 0.3.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.d.ts +4 -2
- package/LexicalLink.dev.js +145 -11
- package/LexicalLink.js.flow +2 -1
- package/LexicalLink.prod.js +7 -5
- package/package.json +3 -3
package/LexicalLink.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
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
|
-
|
|
7
|
+
|
|
8
8
|
*/
|
|
9
|
+
|
|
9
10
|
import type {
|
|
10
11
|
DOMConversionMap,
|
|
11
12
|
DOMConversionOutput,
|
|
@@ -53,3 +54,4 @@ export function $isAutoLinkNode(
|
|
|
53
54
|
): node is AutoLinkNode;
|
|
54
55
|
|
|
55
56
|
export var TOGGLE_LINK_COMMAND: LexicalCommand<string | null>;
|
|
57
|
+
export function toggleLink(url: null | string): void;
|
package/LexicalLink.dev.js
CHANGED
|
@@ -10,12 +10,12 @@ var utils = require('@lexical/utils');
|
|
|
10
10
|
var lexical = require('lexical');
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Copyright (c)
|
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
14
|
*
|
|
15
15
|
* This source code is licensed under the MIT license found in the
|
|
16
16
|
* LICENSE file in the root directory of this source tree.
|
|
17
17
|
*
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
*/
|
|
20
20
|
class LinkNode extends lexical.ElementNode {
|
|
21
21
|
static getType() {
|
|
@@ -38,10 +38,7 @@ class LinkNode extends lexical.ElementNode {
|
|
|
38
38
|
return element;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
updateDOM(
|
|
42
|
-
prevNode, dom, config) {
|
|
43
|
-
// $FlowFixMe: not sure how to fix this
|
|
44
|
-
const anchor = dom;
|
|
41
|
+
updateDOM(prevNode, anchor, config) {
|
|
45
42
|
const url = this.__url;
|
|
46
43
|
|
|
47
44
|
if (url !== prevNode.__url) {
|
|
@@ -60,6 +57,21 @@ class LinkNode extends lexical.ElementNode {
|
|
|
60
57
|
};
|
|
61
58
|
}
|
|
62
59
|
|
|
60
|
+
static importJSON(serializedNode) {
|
|
61
|
+
const node = $createLinkNode(serializedNode.url);
|
|
62
|
+
node.setFormat(serializedNode.format);
|
|
63
|
+
node.setIndent(serializedNode.indent);
|
|
64
|
+
node.setDirection(serializedNode.direction);
|
|
65
|
+
return node;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
exportJSON() {
|
|
69
|
+
return { ...super.exportJSON(),
|
|
70
|
+
type: 'link',
|
|
71
|
+
url: this.getURL()
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
63
75
|
getURL() {
|
|
64
76
|
return this.getLatest().__url;
|
|
65
77
|
}
|
|
@@ -103,7 +115,7 @@ function convertAnchorElement(domNode) {
|
|
|
103
115
|
let node = null;
|
|
104
116
|
|
|
105
117
|
if (domNode instanceof HTMLAnchorElement) {
|
|
106
|
-
node = $createLinkNode(domNode.href);
|
|
118
|
+
node = $createLinkNode(domNode.getAttribute('href'));
|
|
107
119
|
}
|
|
108
120
|
|
|
109
121
|
return {
|
|
@@ -116,19 +128,37 @@ function $createLinkNode(url) {
|
|
|
116
128
|
}
|
|
117
129
|
function $isLinkNode(node) {
|
|
118
130
|
return node instanceof LinkNode;
|
|
119
|
-
}
|
|
131
|
+
}
|
|
132
|
+
// Custom node type to override `canInsertTextAfter` that will
|
|
120
133
|
// allow typing within the link
|
|
121
|
-
|
|
122
134
|
class AutoLinkNode extends LinkNode {
|
|
123
135
|
static getType() {
|
|
124
136
|
return 'autolink';
|
|
125
|
-
}
|
|
126
|
-
|
|
137
|
+
}
|
|
127
138
|
|
|
128
139
|
static clone(node) {
|
|
129
140
|
return new AutoLinkNode(node.__url, node.__key);
|
|
130
141
|
}
|
|
131
142
|
|
|
143
|
+
static importJSON(serializedNode) {
|
|
144
|
+
const node = $createAutoLinkNode(serializedNode.url);
|
|
145
|
+
node.setFormat(serializedNode.format);
|
|
146
|
+
node.setIndent(serializedNode.indent);
|
|
147
|
+
node.setDirection(serializedNode.direction);
|
|
148
|
+
return node;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static importDOM() {
|
|
152
|
+
// TODO: Should link node should handle the import over autolink?
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
exportJSON() {
|
|
157
|
+
return { ...super.exportJSON(),
|
|
158
|
+
type: 'autolink'
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
132
162
|
insertNewAfter(selection) {
|
|
133
163
|
const element = this.getParentOrThrow().insertNewAfter(selection);
|
|
134
164
|
|
|
@@ -149,6 +179,109 @@ function $isAutoLinkNode(node) {
|
|
|
149
179
|
return node instanceof AutoLinkNode;
|
|
150
180
|
}
|
|
151
181
|
const TOGGLE_LINK_COMMAND = lexical.createCommand();
|
|
182
|
+
function toggleLink(url) {
|
|
183
|
+
const selection = lexical.$getSelection();
|
|
184
|
+
|
|
185
|
+
if (selection !== null) {
|
|
186
|
+
lexical.$setSelection(selection);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const sel = lexical.$getSelection();
|
|
190
|
+
|
|
191
|
+
if (sel !== null) {
|
|
192
|
+
const nodes = sel.extract();
|
|
193
|
+
|
|
194
|
+
if (url === null) {
|
|
195
|
+
// Remove LinkNodes
|
|
196
|
+
nodes.forEach(node => {
|
|
197
|
+
const parent = node.getParent();
|
|
198
|
+
|
|
199
|
+
if ($isLinkNode(parent)) {
|
|
200
|
+
const children = parent.getChildren();
|
|
201
|
+
|
|
202
|
+
for (let i = 0; i < children.length; i++) {
|
|
203
|
+
parent.insertBefore(children[i]);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
parent.remove();
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
} else {
|
|
210
|
+
// Add or merge LinkNodes
|
|
211
|
+
if (nodes.length === 1) {
|
|
212
|
+
const firstNode = nodes[0]; // if the first node is a LinkNode or if its
|
|
213
|
+
// parent is a LinkNode, we update the URL.
|
|
214
|
+
|
|
215
|
+
if ($isLinkNode(firstNode)) {
|
|
216
|
+
firstNode.setURL(url);
|
|
217
|
+
return;
|
|
218
|
+
} else {
|
|
219
|
+
const parent = firstNode.getParent();
|
|
220
|
+
|
|
221
|
+
if ($isLinkNode(parent)) {
|
|
222
|
+
// set parent to be the current linkNode
|
|
223
|
+
// so that other nodes in the same parent
|
|
224
|
+
// aren't handled separately below.
|
|
225
|
+
parent.setURL(url);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let prevParent = null;
|
|
232
|
+
let linkNode = null;
|
|
233
|
+
nodes.forEach(node => {
|
|
234
|
+
const parent = node.getParent();
|
|
235
|
+
|
|
236
|
+
if (parent === linkNode || parent === null || lexical.$isElementNode(node) && !node.isInline()) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if ($isLinkNode(parent)) {
|
|
241
|
+
linkNode = parent;
|
|
242
|
+
parent.setURL(url);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (!parent.is(prevParent)) {
|
|
247
|
+
prevParent = parent;
|
|
248
|
+
linkNode = $createLinkNode(url);
|
|
249
|
+
|
|
250
|
+
if ($isLinkNode(parent)) {
|
|
251
|
+
if (node.getPreviousSibling() === null) {
|
|
252
|
+
parent.insertBefore(linkNode);
|
|
253
|
+
} else {
|
|
254
|
+
parent.insertAfter(linkNode);
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
node.insertBefore(linkNode);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if ($isLinkNode(node)) {
|
|
262
|
+
if (node.is(linkNode)) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (linkNode !== null) {
|
|
267
|
+
const children = node.getChildren();
|
|
268
|
+
|
|
269
|
+
for (let i = 0; i < children.length; i++) {
|
|
270
|
+
linkNode.append(children[i]);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
node.remove();
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (linkNode !== null) {
|
|
279
|
+
linkNode.append(node);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
152
285
|
|
|
153
286
|
exports.$createAutoLinkNode = $createAutoLinkNode;
|
|
154
287
|
exports.$createLinkNode = $createLinkNode;
|
|
@@ -157,3 +290,4 @@ exports.$isLinkNode = $isLinkNode;
|
|
|
157
290
|
exports.AutoLinkNode = AutoLinkNode;
|
|
158
291
|
exports.LinkNode = LinkNode;
|
|
159
292
|
exports.TOGGLE_LINK_COMMAND = TOGGLE_LINK_COMMAND;
|
|
293
|
+
exports.toggleLink = toggleLink;
|
package/LexicalLink.js.flow
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
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.
|
|
@@ -52,3 +52,4 @@ declare export function $isAutoLinkNode(
|
|
|
52
52
|
): boolean %checks(node instanceof AutoLinkNode);
|
|
53
53
|
|
|
54
54
|
declare export var TOGGLE_LINK_COMMAND: LexicalCommand<string | null>;
|
|
55
|
+
declare export function toggleLink(url: null | string): void;
|
package/LexicalLink.prod.js
CHANGED
|
@@ -4,8 +4,10 @@
|
|
|
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
|
-
var
|
|
8
|
-
class
|
|
9
|
-
if(
|
|
10
|
-
class
|
|
11
|
-
exports.LinkNode=
|
|
7
|
+
'use strict';var h=require("@lexical/utils"),k=require("lexical");
|
|
8
|
+
class l extends k.ElementNode{static getType(){return"link"}static clone(a){return new l(a.__url,a.__key)}constructor(a,b){super(b);this.__url=a}createDOM(a){let b=document.createElement("a");b.href=this.__url;h.addClassNamesToElement(b,a.theme.link);return b}updateDOM(a,b){let f=this.__url;f!==a.__url&&(b.href=f);return!1}static importDOM(){return{a:()=>({conversion:m,priority:1})}}static importJSON(a){let b=n(a.url);b.setFormat(a.format);b.setIndent(a.indent);b.setDirection(a.direction);return b}exportJSON(){return{...super.exportJSON(),
|
|
9
|
+
type:"link",url:this.getURL()}}getURL(){return this.getLatest().__url}setURL(a){this.getWritable().__url=a}insertNewAfter(a){a=this.getParentOrThrow().insertNewAfter(a);if(k.$isElementNode(a)){let b=n(this.__url);a.append(b);return b}return null}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}}function m(a){let b=null;a instanceof HTMLAnchorElement&&(b=n(a.getAttribute("href")));return{node:b}}function n(a){return new l(a)}
|
|
10
|
+
function p(a){return a instanceof l}class r extends l{static getType(){return"autolink"}static clone(a){return new r(a.__url,a.__key)}static importJSON(a){let b=t(a.url);b.setFormat(a.format);b.setIndent(a.indent);b.setDirection(a.direction);return b}static importDOM(){return null}exportJSON(){return{...super.exportJSON(),type:"autolink"}}insertNewAfter(a){a=this.getParentOrThrow().insertNewAfter(a);if(k.$isElementNode(a)){let b=t(this.__url);a.append(b);return b}return null}}
|
|
11
|
+
function t(a){return new r(a)}let u=k.createCommand();exports.$createAutoLinkNode=t;exports.$createLinkNode=n;exports.$isAutoLinkNode=function(a){return a instanceof r};exports.$isLinkNode=p;exports.AutoLinkNode=r;exports.LinkNode=l;exports.TOGGLE_LINK_COMMAND=u;
|
|
12
|
+
exports.toggleLink=function(a){var b=k.$getSelection();null!==b&&k.$setSelection(b);b=k.$getSelection();if(null!==b)if(b=b.extract(),null===a)b.forEach(g=>{g=g.getParent();if(p(g)){let c=g.getChildren();for(let d=0;d<c.length;d++)g.insertBefore(c[d]);g.remove()}});else{if(1===b.length){var f=b[0];if(p(f)){f.setURL(a);return}f=f.getParent();if(p(f)){f.setURL(a);return}}let g=null,c=null;b.forEach(d=>{var e=d.getParent();if(e!==c&&null!==e&&(!k.$isElementNode(d)||d.isInline()))if(p(e))c=e,e.setURL(a);
|
|
13
|
+
else if(e.is(g)||(g=e,c=n(a),p(e)?null===d.getPreviousSibling()?e.insertBefore(c):e.insertAfter(c):d.insertBefore(c)),p(d)){if(!d.is(c)){if(null!==c){e=d.getChildren();for(let q=0;q<e.length;q++)c.append(e[q])}d.remove()}}else null!==c&&c.append(d)})}}
|
package/package.json
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
"link"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.
|
|
11
|
+
"version": "0.3.1",
|
|
12
12
|
"main": "LexicalLink.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.
|
|
14
|
+
"lexical": "0.3.1"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.
|
|
17
|
+
"@lexical/utils": "0.3.1"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|