@webiny/lexical-nodes 6.3.0-beta.4 → 6.4.0-beta.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/FontColorNode.js +94 -119
- package/FontColorNode.js.map +1 -1
- package/HeadingNode.js +160 -183
- package/HeadingNode.js.map +1 -1
- package/ImageNode.js +101 -131
- package/ImageNode.js.map +1 -1
- package/LinkNode.js +228 -315
- package/LinkNode.js.map +1 -1
- package/ListItemNode.js +249 -320
- package/ListItemNode.js.map +1 -1
- package/ListNode.js +174 -223
- package/ListNode.js.map +1 -1
- package/ParagraphNode.js +119 -148
- package/ParagraphNode.js.map +1 -1
- package/QuoteNode.js +97 -102
- package/QuoteNode.js.map +1 -1
- package/components/ImageNode/ImageComponent.js +117 -147
- package/components/ImageNode/ImageComponent.js.map +1 -1
- package/components/ImageNode/ImageResizer.js +167 -194
- package/components/ImageNode/ImageResizer.js.map +1 -1
- package/generateInitialLexicalValue.js +20 -23
- package/generateInitialLexicalValue.js.map +1 -1
- package/index.js +38 -26
- package/index.js.map +1 -1
- package/package.json +4 -4
- package/prepareLexicalState.js +30 -43
- package/prepareLexicalState.js.map +1 -1
- package/types.js +0 -3
- package/utils/clearNodeFormating.js +14 -15
- package/utils/clearNodeFormating.js.map +1 -1
- package/utils/formatList.js +277 -368
- package/utils/formatList.js.map +1 -1
- package/utils/formatToHeading.js +6 -13
- package/utils/formatToHeading.js.map +1 -1
- package/utils/formatToParagraph.js +6 -7
- package/utils/formatToParagraph.js.map +1 -1
- package/utils/formatToQuote.js +6 -13
- package/utils/formatToQuote.js.map +1 -1
- package/utils/getStyleId.js +6 -11
- package/utils/getStyleId.js.map +1 -1
- package/utils/listNode.js +60 -84
- package/utils/listNode.js.map +1 -1
- package/utils/toggleLink.js +67 -118
- package/utils/toggleLink.js.map +1 -1
- package/types.js.map +0 -1
package/utils/toggleLink.js
CHANGED
|
@@ -1,131 +1,80 @@
|
|
|
1
1
|
import { $getSelection, $isElementNode, $isRangeSelection } from "lexical";
|
|
2
2
|
import { $createLinkNode, $isLinkNode } from "../LinkNode.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} = attributes;
|
|
16
|
-
const rel = attributes.rel === undefined ? "noreferrer" : attributes.rel;
|
|
17
|
-
const selection = $getSelection();
|
|
18
|
-
if (!$isRangeSelection(selection)) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
const nodes = selection.extract();
|
|
22
|
-
if (url === null) {
|
|
23
|
-
// Remove LinkNodes
|
|
24
|
-
nodes.forEach(node => {
|
|
25
|
-
const parent = node.getParent();
|
|
26
|
-
if ($isLinkNode(parent)) {
|
|
27
|
-
const children = parent.getChildren();
|
|
28
|
-
for (let i = 0; i < children.length; i++) {
|
|
29
|
-
parent.insertBefore(children[i]);
|
|
3
|
+
function toggleLink(url, attributes = {}) {
|
|
4
|
+
const { target, title, alt } = attributes;
|
|
5
|
+
const rel = void 0 === attributes.rel ? "noreferrer" : attributes.rel;
|
|
6
|
+
const selection = $getSelection();
|
|
7
|
+
if (!$isRangeSelection(selection)) return;
|
|
8
|
+
const nodes = selection.extract();
|
|
9
|
+
if (null === url) nodes.forEach((node)=>{
|
|
10
|
+
const parent = node.getParent();
|
|
11
|
+
if ($isLinkNode(parent)) {
|
|
12
|
+
const children = parent.getChildren();
|
|
13
|
+
for(let i = 0; i < children.length; i++)parent.insertBefore(children[i]);
|
|
14
|
+
parent.remove();
|
|
30
15
|
}
|
|
31
|
-
parent.remove();
|
|
32
|
-
}
|
|
33
16
|
});
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (rel !== null) {
|
|
47
|
-
linkNode.setRel(rel);
|
|
48
|
-
}
|
|
49
|
-
if (title !== undefined) {
|
|
50
|
-
linkNode.setTitle(title);
|
|
51
|
-
}
|
|
52
|
-
if (alt !== undefined) {
|
|
53
|
-
linkNode.setAlt(alt);
|
|
54
|
-
}
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
let prevParent = null;
|
|
59
|
-
let linkNode = null;
|
|
60
|
-
nodes.forEach(node => {
|
|
61
|
-
const parent = node.getParent();
|
|
62
|
-
if (parent === linkNode || parent === null || $isElementNode(node) && !node.isInline()) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if ($isLinkNode(parent)) {
|
|
66
|
-
linkNode = parent;
|
|
67
|
-
parent.setURL(url);
|
|
68
|
-
if (target !== undefined) {
|
|
69
|
-
parent.setTarget(target);
|
|
70
|
-
}
|
|
71
|
-
if (rel !== null) {
|
|
72
|
-
linkNode.setRel(rel);
|
|
17
|
+
else {
|
|
18
|
+
if (1 === nodes.length) {
|
|
19
|
+
const firstNode = nodes[0];
|
|
20
|
+
const linkNode = $isLinkNode(firstNode) ? firstNode : $getLinkAncestor(firstNode);
|
|
21
|
+
if (null !== linkNode) {
|
|
22
|
+
linkNode.setURL(url);
|
|
23
|
+
if (void 0 !== target) linkNode.setTarget(target);
|
|
24
|
+
if (null !== rel) linkNode.setRel(rel);
|
|
25
|
+
if (void 0 !== title) linkNode.setTitle(title);
|
|
26
|
+
if (void 0 !== alt) linkNode.setAlt(alt);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
73
29
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
30
|
+
let prevParent = null;
|
|
31
|
+
let linkNode = null;
|
|
32
|
+
nodes.forEach((node)=>{
|
|
33
|
+
const parent = node.getParent();
|
|
34
|
+
if (parent === linkNode || null === parent || $isElementNode(node) && !node.isInline()) return;
|
|
35
|
+
if ($isLinkNode(parent)) {
|
|
36
|
+
linkNode = parent;
|
|
37
|
+
parent.setURL(url);
|
|
38
|
+
if (void 0 !== target) parent.setTarget(target);
|
|
39
|
+
if (null !== rel) linkNode.setRel(rel);
|
|
40
|
+
if (void 0 !== title) linkNode.setTitle(title);
|
|
41
|
+
if (void 0 !== alt) linkNode.setAlt(alt);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!parent.is(prevParent)) {
|
|
45
|
+
prevParent = parent;
|
|
46
|
+
linkNode = $createLinkNode(url, {
|
|
47
|
+
rel,
|
|
48
|
+
target,
|
|
49
|
+
alt
|
|
50
|
+
});
|
|
51
|
+
if ($isLinkNode(parent)) if (null === node.getPreviousSibling()) parent.insertBefore(linkNode);
|
|
52
|
+
else parent.insertAfter(linkNode);
|
|
53
|
+
else node.insertBefore(linkNode);
|
|
54
|
+
}
|
|
55
|
+
if ($isLinkNode(node)) {
|
|
56
|
+
if (node.is(linkNode)) return;
|
|
57
|
+
if (null !== linkNode) {
|
|
58
|
+
const children = node.getChildren();
|
|
59
|
+
for(let i = 0; i < children.length; i++)linkNode.append(children[i]);
|
|
60
|
+
}
|
|
61
|
+
node.remove();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (null !== linkNode) linkNode.append(node);
|
|
88
65
|
});
|
|
89
|
-
|
|
90
|
-
if (node.getPreviousSibling() === null) {
|
|
91
|
-
parent.insertBefore(linkNode);
|
|
92
|
-
} else {
|
|
93
|
-
parent.insertAfter(linkNode);
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
node.insertBefore(linkNode);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
if ($isLinkNode(node)) {
|
|
100
|
-
if (node.is(linkNode)) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
if (linkNode !== null) {
|
|
104
|
-
const children = node.getChildren();
|
|
105
|
-
for (let i = 0; i < children.length; i++) {
|
|
106
|
-
linkNode.append(children[i]);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
node.remove();
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
if (linkNode !== null) {
|
|
113
|
-
linkNode.append(node);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
66
|
+
}
|
|
117
67
|
}
|
|
118
68
|
function $getLinkAncestor(node) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
return ancestor;
|
|
69
|
+
const ancestor = $getAncestor(node, $isLinkNode);
|
|
70
|
+
if (!ancestor) return null;
|
|
71
|
+
return ancestor;
|
|
124
72
|
}
|
|
125
73
|
function $getAncestor(node, predicate) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
74
|
+
let parent = node;
|
|
75
|
+
while(null !== parent && null !== (parent = parent.getParent()) && !predicate(parent));
|
|
76
|
+
return parent;
|
|
129
77
|
}
|
|
78
|
+
export { toggleLink };
|
|
130
79
|
|
|
131
80
|
//# sourceMappingURL=toggleLink.js.map
|
package/utils/toggleLink.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"utils/toggleLink.js","sources":["../../src/utils/toggleLink.ts"],"sourcesContent":["import type { ElementNode, LexicalNode } from \"lexical\";\nimport { $getSelection, $isElementNode, $isRangeSelection } from \"lexical\";\nimport type { LinkAttributes, LinkNode } from \"~/LinkNode.js\";\nimport { $createLinkNode, $isLinkNode } from \"~/LinkNode.js\";\n\n/**\n * Generates or updates a LinkNode. It can also delete a LinkNode if the URL is null,\n * but saves any children and brings them up to the parent node.\n * @param url - The URL the link directs to.\n * @param attributes - Optional HTML a tag attributes. { target, rel, title }\n */\nexport function toggleLink(url: null | string, attributes: LinkAttributes = {}): void {\n const { target, title, alt } = attributes;\n const rel = attributes.rel === undefined ? \"noreferrer\" : attributes.rel;\n const selection = $getSelection();\n\n if (!$isRangeSelection(selection)) {\n return;\n }\n const nodes = selection.extract();\n\n if (url === null) {\n // Remove LinkNodes\n nodes.forEach(node => {\n const parent = node.getParent();\n\n if ($isLinkNode(parent)) {\n const children = parent.getChildren();\n\n for (let i = 0; i < children.length; i++) {\n parent.insertBefore(children[i]);\n }\n\n parent.remove();\n }\n });\n } else {\n // Add or merge LinkNodes\n if (nodes.length === 1) {\n const firstNode = nodes[0];\n // if the first node is a LinkNode or if its\n // parent is a LinkNode, we update the URL, target and rel.\n const linkNode = $isLinkNode(firstNode) ? firstNode : $getLinkAncestor(firstNode);\n if (linkNode !== null) {\n linkNode.setURL(url);\n if (target !== undefined) {\n linkNode.setTarget(target);\n }\n if (rel !== null) {\n linkNode.setRel(rel);\n }\n if (title !== undefined) {\n linkNode.setTitle(title);\n }\n\n if (alt !== undefined) {\n linkNode.setAlt(alt);\n }\n return;\n }\n }\n\n let prevParent: ElementNode | LinkNode | null = null;\n let linkNode: LinkNode | null = null;\n\n nodes.forEach(node => {\n const parent = node.getParent();\n\n if (\n parent === linkNode ||\n parent === null ||\n ($isElementNode(node) && !node.isInline())\n ) {\n return;\n }\n\n if ($isLinkNode(parent)) {\n linkNode = parent;\n parent.setURL(url);\n if (target !== undefined) {\n parent.setTarget(target);\n }\n if (rel !== null) {\n linkNode.setRel(rel);\n }\n if (title !== undefined) {\n linkNode.setTitle(title);\n }\n if (alt !== undefined) {\n linkNode.setAlt(alt);\n }\n return;\n }\n\n if (!parent.is(prevParent)) {\n prevParent = parent;\n linkNode = $createLinkNode(url, { rel, target, alt });\n\n if ($isLinkNode(parent)) {\n if (node.getPreviousSibling() === null) {\n parent.insertBefore(linkNode);\n } else {\n parent.insertAfter(linkNode);\n }\n } else {\n node.insertBefore(linkNode);\n }\n }\n\n if ($isLinkNode(node)) {\n if (node.is(linkNode)) {\n return;\n }\n if (linkNode !== null) {\n const children = node.getChildren();\n\n for (let i = 0; i < children.length; i++) {\n linkNode.append(children[i]);\n }\n }\n\n node.remove();\n return;\n }\n\n if (linkNode !== null) {\n linkNode.append(node);\n }\n });\n }\n}\n\nfunction $getLinkAncestor(node: LexicalNode): LinkNode | null {\n const ancestor = $getAncestor(node, $isLinkNode);\n if (!ancestor) {\n return null;\n }\n\n return ancestor as LinkNode;\n}\n\nfunction $getAncestor<NodeType extends LexicalNode = LexicalNode>(\n node: LexicalNode,\n predicate: (ancestor: LexicalNode) => ancestor is NodeType\n): null | LexicalNode {\n let parent: null | LexicalNode = node;\n while (parent !== null && (parent = parent.getParent()) !== null && !predicate(parent)) {}\n return parent;\n}\n"],"names":["toggleLink","url","attributes","target","title","alt","rel","undefined","selection","$getSelection","$isRangeSelection","nodes","node","parent","$isLinkNode","children","i","firstNode","linkNode","$getLinkAncestor","prevParent","$isElementNode","$createLinkNode","ancestor","$getAncestor","predicate"],"mappings":";;AAWO,SAASA,WAAWC,GAAkB,EAAEC,aAA6B,CAAC,CAAC;IAC1E,MAAM,EAAEC,MAAM,EAAEC,KAAK,EAAEC,GAAG,EAAE,GAAGH;IAC/B,MAAMI,MAAMJ,AAAmBK,WAAnBL,WAAW,GAAG,GAAiB,eAAeA,WAAW,GAAG;IACxE,MAAMM,YAAYC;IAElB,IAAI,CAACC,kBAAkBF,YACnB;IAEJ,MAAMG,QAAQH,UAAU,OAAO;IAE/B,IAAIP,AAAQ,SAARA,KAEAU,MAAM,OAAO,CAACC,CAAAA;QACV,MAAMC,SAASD,KAAK,SAAS;QAE7B,IAAIE,YAAYD,SAAS;YACrB,MAAME,WAAWF,OAAO,WAAW;YAEnC,IAAK,IAAIG,IAAI,GAAGA,IAAID,SAAS,MAAM,EAAEC,IACjCH,OAAO,YAAY,CAACE,QAAQ,CAACC,EAAE;YAGnCH,OAAO,MAAM;QACjB;IACJ;SACG;QAEH,IAAIF,AAAiB,MAAjBA,MAAM,MAAM,EAAQ;YACpB,MA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a,SAAbA,UAAmB;oBACnB,MAAMH,WAAWH,KAAK,WAAW;oBAEjC,IAAK,IAAII,IAAI,GAAGA,IAAID,SAAS,MAAM,EAAEC,IACjCE,SAAS,MAAM,CAACH,QAAQ,CAACC,EAAE;gBAEnC;gBAEAJ,KAAK,MAAM;gBACX;YACJ;YAEA,IAAIM,AAAa,SAAbA,UACAA,SAAS,MAAM,CAACN;QAExB;IACJ;AACJ;AAEA,SAASO,iBAAiBP,IAAiB;IACvC,MAAMW,WAAWC,aAAaZ,MAAME;IACpC,IAAI,CAACS,UACD,OAAO;IAGX,OAAOA;AACX;AAEA,SAASC,aACLZ,IAAiB,EACjBa,SAA0D;IAE1D,IAAIZ,SAA6BD;IACjC,MAAOC,AAAW,SAAXA,UAAoBA,AAAiC,SAAjCA,CAAAA,SAASA,OAAO,SAAS,EAAC,KAAe,CAACY,UAAUZ;IAC/E,OAAOA;AACX"}
|
package/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["export type ThemeStyleType = \"typography\" | \"colors\" | \"fonts\";\n\nexport interface ThemeStyleValue {\n styleId: string;\n type: ThemeStyleType;\n}\n\nexport interface TypographyStylesNode {\n getStyleId: () => string | undefined;\n setStyleId: (id: string) => void;\n getClassName: () => string | undefined;\n setClassName: (className: string) => void;\n}\n"],"mappings":"","ignoreList":[]}
|