@lexical/link 0.3.3 → 0.3.6

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.
@@ -15,7 +15,6 @@ var lexical = require('lexical');
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
-
19
18
  */
20
19
  class LinkNode extends lexical.ElementNode {
21
20
  static getType() {
@@ -68,7 +67,8 @@ class LinkNode extends lexical.ElementNode {
68
67
  exportJSON() {
69
68
  return { ...super.exportJSON(),
70
69
  type: 'link',
71
- url: this.getURL()
70
+ url: this.getURL(),
71
+ version: 1
72
72
  };
73
73
  }
74
74
 
@@ -109,13 +109,23 @@ class LinkNode extends lexical.ElementNode {
109
109
  return true;
110
110
  }
111
111
 
112
+ extractWithChild(child, selection, destination) {
113
+ if (!lexical.$isRangeSelection(selection)) {
114
+ return false;
115
+ }
116
+
117
+ const anchorNode = selection.anchor.getNode();
118
+ const focusNode = selection.focus.getNode();
119
+ return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && selection.getTextContent().length > 0;
120
+ }
121
+
112
122
  }
113
123
 
114
124
  function convertAnchorElement(domNode) {
115
125
  let node = null;
116
126
 
117
127
  if (domNode instanceof HTMLAnchorElement) {
118
- node = $createLinkNode(domNode.getAttribute('href'));
128
+ node = $createLinkNode(domNode.getAttribute('href') || '');
119
129
  }
120
130
 
121
131
  return {
@@ -155,7 +165,8 @@ class AutoLinkNode extends LinkNode {
155
165
 
156
166
  exportJSON() {
157
167
  return { ...super.exportJSON(),
158
- type: 'autolink'
168
+ type: 'autolink',
169
+ version: 1
159
170
  };
160
171
  }
161
172
 
@@ -4,10 +4,11 @@
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 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)})}}
7
+ 'use strict';var g=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;g.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(),version:1}}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}extractWithChild(a,b){if(!k.$isRangeSelection(b))return!1;a=b.anchor.getNode();let f=b.focus.getNode();return this.isParentOf(a)&&this.isParentOf(f)&&
10
+ 0<b.getTextContent().length}}function m(a){let b=null;a instanceof HTMLAnchorElement&&(b=n(a.getAttribute("href")||""));return{node:b}}function n(a){return new l(a)}function p(a){return a instanceof l}
11
+ 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",version:1}}insertNewAfter(a){a=this.getParentOrThrow().insertNewAfter(a);if(k.$isElementNode(a)){let b=t(this.__url);a.append(b);return b}return null}}function t(a){return new r(a)}let u=k.createCommand();
12
+ 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;
13
+ 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(h=>{h=h.getParent();if(p(h)){let c=h.getChildren();for(let d=0;d<c.length;d++)h.insertBefore(c[d]);h.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 h=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);
14
+ else if(e.is(h)||(h=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/index.d.ts ADDED
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { DOMConversionMap, EditorConfig, GridSelection, LexicalCommand, LexicalNode, NodeKey, NodeSelection, RangeSelection, SerializedElementNode } from 'lexical';
9
+ import { ElementNode, Spread } from 'lexical';
10
+ export declare type SerializedLinkNode = Spread<{
11
+ type: 'link';
12
+ url: string;
13
+ version: 1;
14
+ }, SerializedElementNode>;
15
+ export declare class LinkNode extends ElementNode {
16
+ __url: string;
17
+ static getType(): string;
18
+ static clone(node: LinkNode): LinkNode;
19
+ constructor(url: string, key?: NodeKey);
20
+ createDOM(config: EditorConfig): HTMLAnchorElement;
21
+ updateDOM(prevNode: LinkNode, anchor: HTMLAnchorElement, config: EditorConfig): boolean;
22
+ static importDOM(): DOMConversionMap | null;
23
+ static importJSON(serializedNode: SerializedLinkNode | SerializedAutoLinkNode): LinkNode;
24
+ exportJSON(): SerializedLinkNode | SerializedAutoLinkNode;
25
+ getURL(): string;
26
+ setURL(url: string): void;
27
+ insertNewAfter(selection: RangeSelection): null | ElementNode;
28
+ canInsertTextBefore(): false;
29
+ canInsertTextAfter(): false;
30
+ canBeEmpty(): false;
31
+ isInline(): true;
32
+ extractWithChild(child: LexicalNode, selection: RangeSelection | NodeSelection | GridSelection, destination: 'clone' | 'html'): boolean;
33
+ }
34
+ export declare function $createLinkNode(url: string): LinkNode;
35
+ export declare function $isLinkNode(node: LexicalNode | null | undefined): node is LinkNode;
36
+ export declare type SerializedAutoLinkNode = Spread<{
37
+ type: 'autolink';
38
+ version: 1;
39
+ }, SerializedLinkNode>;
40
+ export declare class AutoLinkNode extends LinkNode {
41
+ static getType(): string;
42
+ static clone(node: AutoLinkNode): AutoLinkNode;
43
+ static importJSON(serializedNode: SerializedAutoLinkNode): AutoLinkNode;
44
+ static importDOM(): null;
45
+ exportJSON(): SerializedAutoLinkNode;
46
+ insertNewAfter(selection: RangeSelection): null | ElementNode;
47
+ }
48
+ export declare function $createAutoLinkNode(url: string): AutoLinkNode;
49
+ export declare function $isAutoLinkNode(node: LexicalNode | null | undefined): node is AutoLinkNode;
50
+ export declare const TOGGLE_LINK_COMMAND: LexicalCommand<string | null>;
51
+ export declare function toggleLink(url: null | string): void;
package/package.json CHANGED
@@ -8,13 +8,13 @@
8
8
  "link"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.3.3",
11
+ "version": "0.3.6",
12
12
  "main": "LexicalLink.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.3.3"
14
+ "lexical": "0.3.6"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.3.3"
17
+ "@lexical/utils": "0.3.6"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
package/LexicalLink.d.ts DELETED
@@ -1,57 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
-
8
- */
9
-
10
- import type {
11
- DOMConversionMap,
12
- DOMConversionOutput,
13
- EditorConfig,
14
- LexicalNode,
15
- NodeKey,
16
- RangeSelection,
17
- LexicalCommand,
18
- } from 'lexical';
19
- import {ElementNode} from 'lexical';
20
-
21
- export declare class LinkNode extends ElementNode {
22
- __url: string;
23
- static getType(): string;
24
- static clone(node: LinkNode): LinkNode;
25
- constructor(url: string, key?: NodeKey);
26
- createDOM(config: EditorConfig): HTMLElement;
27
- updateDOM(
28
- prevNode: LinkNode,
29
- dom: HTMLElement,
30
- config: EditorConfig,
31
- ): boolean;
32
- static importDOM(): DOMConversionMap | null;
33
- getURL(): string;
34
- setURL(url: string): void;
35
- insertNewAfter(selection: RangeSelection): null | ElementNode;
36
- canInsertTextBefore(): false;
37
- canInsertTextAfter(): boolean;
38
- canBeEmpty(): false;
39
- isInline(): true;
40
- }
41
- export function convertAnchorElement(domNode: Node): DOMConversionOutput;
42
- export function $createLinkNode(url: string): LinkNode;
43
- export function $isLinkNode(
44
- node: LinkNode | LexicalNode | null | undefined,
45
- ): node is LinkNode;
46
- export declare class AutoLinkNode extends LinkNode {
47
- static getType(): string;
48
- static clone(node: AutoLinkNode): AutoLinkNode;
49
- insertNewAfter(selection: RangeSelection): null | ElementNode;
50
- }
51
- export function $createAutoLinkNode(url: string): AutoLinkNode;
52
- export function $isAutoLinkNode(
53
- node: LinkNode | LexicalNode | null | undefined,
54
- ): node is AutoLinkNode;
55
-
56
- export var TOGGLE_LINK_COMMAND: LexicalCommand<string | null>;
57
- export function toggleLink(url: null | string): void;