@lexical/link 0.2.7 → 0.3.0

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 CHANGED
@@ -1,11 +1,12 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
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
- * @flow strict
7
+
8
8
  */
9
+
9
10
  import type {
10
11
  DOMConversionMap,
11
12
  DOMConversionOutput,
@@ -39,13 +40,18 @@ export declare class LinkNode extends ElementNode {
39
40
  }
40
41
  export function convertAnchorElement(domNode: Node): DOMConversionOutput;
41
42
  export function $createLinkNode(url: string): LinkNode;
42
- export function $isLinkNode(node: ?LexicalNode): node is LinkNode;
43
+ export function $isLinkNode(
44
+ node: LinkNode | LexicalNode | null | undefined,
45
+ ): node is LinkNode;
43
46
  export declare class AutoLinkNode extends LinkNode {
44
47
  static getType(): string;
45
48
  static clone(node: AutoLinkNode): AutoLinkNode;
46
49
  insertNewAfter(selection: RangeSelection): null | ElementNode;
47
50
  }
48
51
  export function $createAutoLinkNode(url: string): AutoLinkNode;
49
- export function $isAutoLinkNode(node: ?LexicalNode): node is AutoLinkNode;
52
+ export function $isAutoLinkNode(
53
+ node: LinkNode | LexicalNode | null | undefined,
54
+ ): node is AutoLinkNode;
50
55
 
51
56
  export var TOGGLE_LINK_COMMAND: LexicalCommand<string | null>;
57
+ export function toggleLink(url: null | string): void;
@@ -10,12 +10,12 @@ var utils = require('@lexical/utils');
10
10
  var lexical = require('lexical');
11
11
 
12
12
  /**
13
- * Copyright (c) Facebook, Inc. and its affiliates.
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( // $FlowFixMe: not sure how to fix this
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
- } // Custom node type to override `canInsertTextAfter` that will
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
- } // $FlowFixMe[incompatible-extend]
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,105 @@ 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 (linkNode !== null) {
263
+ const children = node.getChildren();
264
+
265
+ for (let i = 0; i < children.length; i++) {
266
+ linkNode.append(children[i]);
267
+ }
268
+ }
269
+
270
+ node.remove();
271
+ return;
272
+ }
273
+
274
+ if (linkNode !== null) {
275
+ linkNode.append(node);
276
+ }
277
+ });
278
+ }
279
+ }
280
+ }
152
281
 
153
282
  exports.$createAutoLinkNode = $createAutoLinkNode;
154
283
  exports.$createLinkNode = $createLinkNode;
@@ -157,3 +286,4 @@ exports.$isLinkNode = $isLinkNode;
157
286
  exports.AutoLinkNode = AutoLinkNode;
158
287
  exports.LinkNode = LinkNode;
159
288
  exports.TOGGLE_LINK_COMMAND = TOGGLE_LINK_COMMAND;
289
+ exports.toggleLink = toggleLink;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
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;
@@ -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 c=require("@lexical/utils"),d=require("lexical");
8
- class e extends d.ElementNode{static getType(){return"link"}static clone(a){return new e(a.__url,a.__key)}constructor(a,b){super(b);this.__url=a}createDOM(a){const b=document.createElement("a");b.href=this.__url;c.addClassNamesToElement(b,a.theme.link);return b}updateDOM(a,b){const g=this.__url;g!==a.__url&&(b.href=g);return!1}static importDOM(){return{a:()=>({conversion:f,priority:1})}}getURL(){return this.getLatest().__url}setURL(a){this.getWritable().__url=a}insertNewAfter(a){a=this.getParentOrThrow().insertNewAfter(a);
9
- if(d.$isElementNode(a)){const b=h(this.__url);a.append(b);return b}return null}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}}function f(a){let b=null;a instanceof HTMLAnchorElement&&(b=h(a.href));return{node:b}}function h(a){return new e(a)}
10
- class k extends e{static getType(){return"autolink"}static clone(a){return new k(a.__url,a.__key)}insertNewAfter(a){a=this.getParentOrThrow().insertNewAfter(a);if(d.$isElementNode(a)){const b=l(this.__url);a.append(b);return b}return null}}function l(a){return new k(a)}const m=d.createCommand();exports.$createAutoLinkNode=l;exports.$createLinkNode=h;exports.$isAutoLinkNode=function(a){return a instanceof k};exports.$isLinkNode=function(a){return a instanceof e};exports.AutoLinkNode=k;
11
- exports.LinkNode=e;exports.TOGGLE_LINK_COMMAND=m;
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(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.2.7",
11
+ "version": "0.3.0",
12
12
  "main": "LexicalLink.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.2.7"
14
+ "lexical": "0.3.0"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.2.7"
17
+ "@lexical/utils": "0.3.0"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",