@webiny/lexical-nodes 0.0.0-unstable.06b2ede40f

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.
Files changed (81) hide show
  1. package/FontColorNode.d.ts +43 -0
  2. package/FontColorNode.js +122 -0
  3. package/FontColorNode.js.map +1 -0
  4. package/HeadingNode.d.ts +40 -0
  5. package/HeadingNode.js +195 -0
  6. package/HeadingNode.js.map +1 -0
  7. package/ImageNode.d.ts +56 -0
  8. package/ImageNode.js +164 -0
  9. package/ImageNode.js.map +1 -0
  10. package/LICENSE +21 -0
  11. package/LinkNode.d.ts +101 -0
  12. package/LinkNode.js +327 -0
  13. package/LinkNode.js.map +1 -0
  14. package/ListItemNode.d.ts +43 -0
  15. package/ListItemNode.js +363 -0
  16. package/ListItemNode.js.map +1 -0
  17. package/ListNode.d.ts +53 -0
  18. package/ListNode.js +248 -0
  19. package/ListNode.js.map +1 -0
  20. package/ParagraphNode.d.ts +39 -0
  21. package/ParagraphNode.js +157 -0
  22. package/ParagraphNode.js.map +1 -0
  23. package/QuoteNode.d.ts +38 -0
  24. package/QuoteNode.js +120 -0
  25. package/QuoteNode.js.map +1 -0
  26. package/README.md +6 -0
  27. package/components/ImageNode/ContentEditable.css +22 -0
  28. package/components/ImageNode/ContentEditable.d.ts +12 -0
  29. package/components/ImageNode/ContentEditable.js +19 -0
  30. package/components/ImageNode/ContentEditable.js.map +1 -0
  31. package/components/ImageNode/ImageComponent.css +43 -0
  32. package/components/ImageNode/ImageComponent.d.ts +18 -0
  33. package/components/ImageNode/ImageComponent.js +235 -0
  34. package/components/ImageNode/ImageComponent.js.map +1 -0
  35. package/components/ImageNode/ImageResizer.d.ts +24 -0
  36. package/components/ImageNode/ImageResizer.js +211 -0
  37. package/components/ImageNode/ImageResizer.js.map +1 -0
  38. package/components/ImageNode/Placeholder.css +20 -0
  39. package/components/ImageNode/Placeholder.d.ts +15 -0
  40. package/components/ImageNode/Placeholder.js +24 -0
  41. package/components/ImageNode/Placeholder.js.map +1 -0
  42. package/components/ImageNode/SharedHistoryContext.d.ts +10 -0
  43. package/components/ImageNode/SharedHistoryContext.js +19 -0
  44. package/components/ImageNode/SharedHistoryContext.js.map +1 -0
  45. package/generateInitialLexicalValue.d.ts +4 -0
  46. package/generateInitialLexicalValue.js +27 -0
  47. package/generateInitialLexicalValue.js.map +1 -0
  48. package/index.d.ts +19 -0
  49. package/index.js +51 -0
  50. package/index.js.map +1 -0
  51. package/package.json +39 -0
  52. package/prepareLexicalState.d.ts +2 -0
  53. package/prepareLexicalState.js +53 -0
  54. package/prepareLexicalState.js.map +1 -0
  55. package/types.d.ts +11 -0
  56. package/types.js +3 -0
  57. package/types.js.map +1 -0
  58. package/utils/clearNodeFormating.d.ts +2 -0
  59. package/utils/clearNodeFormating.js +23 -0
  60. package/utils/clearNodeFormating.js.map +1 -0
  61. package/utils/formatList.d.ts +19 -0
  62. package/utils/formatList.js +412 -0
  63. package/utils/formatList.js.map +1 -0
  64. package/utils/formatToHeading.d.ts +3 -0
  65. package/utils/formatToHeading.js +19 -0
  66. package/utils/formatToHeading.js.map +1 -0
  67. package/utils/formatToParagraph.d.ts +2 -0
  68. package/utils/formatToParagraph.js +13 -0
  69. package/utils/formatToParagraph.js.map +1 -0
  70. package/utils/formatToQuote.d.ts +2 -0
  71. package/utils/formatToQuote.js +19 -0
  72. package/utils/formatToQuote.js.map +1 -0
  73. package/utils/getStyleId.d.ts +11 -0
  74. package/utils/getStyleId.js +14 -0
  75. package/utils/getStyleId.js.map +1 -0
  76. package/utils/listNode.d.ts +21 -0
  77. package/utils/listNode.js +103 -0
  78. package/utils/listNode.js.map +1 -0
  79. package/utils/toggleLink.d.ts +8 -0
  80. package/utils/toggleLink.js +131 -0
  81. package/utils/toggleLink.js.map +1 -0
package/ListNode.js ADDED
@@ -0,0 +1,248 @@
1
+ import { ElementNode } from "lexical";
2
+ import { findTypographyStyleByHtmlTag } from "@webiny/lexical-theme";
3
+ import { addClassNamesToElement, removeClassNamesFromElement } from "@lexical/utils";
4
+ import { $getListDepth, wrapInListItem } from "./utils/listNode";
5
+ import { $isListItemNode } from "./ListItemNode";
6
+ export class ListNode extends ElementNode {
7
+ /** @internal */
8
+
9
+ /** @internal */
10
+
11
+ /** @internal */
12
+
13
+ constructor(listType, options = {}) {
14
+ const {
15
+ styleId,
16
+ key,
17
+ className,
18
+ start
19
+ } = options;
20
+ super(key);
21
+ this.__styleId = styleId ?? "";
22
+ this.__className = className;
23
+ const _listType = TAG_TO_WEBINY_LIST_TYPE[listType] || listType;
24
+ this.__listType = _listType;
25
+ this.__tag = _listType === "number" ? "ol" : "ul";
26
+ this.__start = start || 1;
27
+ }
28
+ static getType() {
29
+ return "wby-list";
30
+ }
31
+ createDOM(config) {
32
+ const tag = this.__tag;
33
+ const element = document.createElement(tag);
34
+ if (this.__start !== 1) {
35
+ element.setAttribute("start", String(this.__start));
36
+ }
37
+ this.updateElementWithThemeClasses(element, config.theme);
38
+
39
+ // @ts-expect-error Internal field.
40
+ element.__lexicalListType = this.__listType;
41
+ const theme = config.theme;
42
+ setListThemeClassNames(element, theme, this, this.__styleId);
43
+ return element;
44
+ }
45
+ exportDOM(editor) {
46
+ const base = super.exportDOM(editor);
47
+ const element = base.element;
48
+ if (element && this.__className) {
49
+ element.classList.add(this.__className);
50
+ }
51
+ return {
52
+ ...base,
53
+ element
54
+ };
55
+ }
56
+ static clone(node) {
57
+ return new ListNode(node.getListType(), {
58
+ className: node.getClassName(),
59
+ styleId: node.getStyleId(),
60
+ start: node.getStart(),
61
+ key: node.getKey()
62
+ });
63
+ }
64
+ getStyleId() {
65
+ return this.__styleId;
66
+ }
67
+ setStyleId(styleId) {
68
+ this.__styleId = styleId ?? "";
69
+ }
70
+ setClassName(className) {
71
+ this.__className = className;
72
+ }
73
+ getClassName() {
74
+ return this.__className;
75
+ }
76
+ static importJSON(serializedNode) {
77
+ const node = $createListNode(serializedNode.listType,
78
+ // `styleId` is for backwards compatibility
79
+ serializedNode.styleId ?? serializedNode.styleId, serializedNode.start);
80
+ node.setFormat(serializedNode.format);
81
+ node.setIndent(serializedNode.indent);
82
+ node.setDirection(serializedNode.direction);
83
+ node.setClassName(serializedNode.className);
84
+ return node;
85
+ }
86
+ exportJSON() {
87
+ return {
88
+ ...super.exportJSON(),
89
+ styleId: this.__styleId,
90
+ className: this.__className,
91
+ listType: this.getListType(),
92
+ start: this.getStart(),
93
+ tag: this.getTag(),
94
+ type: "wby-list"
95
+ };
96
+ }
97
+ static importDomConversionMap() {
98
+ return {
99
+ conversion: convertListNode,
100
+ priority: 0
101
+ };
102
+ }
103
+ static importDOM() {
104
+ return {
105
+ ol: () => {
106
+ return this.importDomConversionMap();
107
+ },
108
+ ul: () => {
109
+ return this.importDomConversionMap();
110
+ }
111
+ };
112
+ }
113
+ updateDOM(prevNode, dom, config) {
114
+ if (prevNode.__tag !== this.__tag) {
115
+ return true;
116
+ }
117
+ setListThemeClassNames(dom, config.theme, this, this.__styleId);
118
+ return false;
119
+ }
120
+ extractWithChild(child) {
121
+ return $isListItemNode(child);
122
+ }
123
+ getListType() {
124
+ return this.__listType;
125
+ }
126
+ getStart() {
127
+ return this.__start;
128
+ }
129
+ getTag() {
130
+ return this.__tag;
131
+ }
132
+ updateElementWithThemeClasses(element, theme) {
133
+ if (!theme?.emotionMap) {
134
+ return element;
135
+ }
136
+ if (!this.__styleId || !this.__className) {
137
+ this.setDefaultTypography(theme.emotionMap);
138
+ }
139
+ if (this.__className) {
140
+ addClassNamesToElement(element, this.__className);
141
+ }
142
+ return element;
143
+ }
144
+ setDefaultTypography(themeEmotionMap) {
145
+ const typographyStyle = findTypographyStyleByHtmlTag(this.getTag(), themeEmotionMap);
146
+ if (typographyStyle) {
147
+ this.__styleId = typographyStyle.id;
148
+ this.__className = typographyStyle.className;
149
+ }
150
+ }
151
+ }
152
+ function setListThemeClassNames(dom, editorTheme, node, styleId) {
153
+ const editorThemeClasses = editorTheme;
154
+ const classesToAdd = [];
155
+ const classesToRemove = [];
156
+ const listTheme = editorThemeClasses.list;
157
+ const emotionMap = editorTheme?.emotionMap || {};
158
+ if (listTheme !== undefined) {
159
+ const listLevelsClassNames = listTheme[`${node.__tag}Depth`] || [];
160
+ const listDepth = $getListDepth(node) - 1;
161
+ const normalizedListDepth = listDepth % listLevelsClassNames.length;
162
+ const listLevelClassName = listLevelsClassNames[normalizedListDepth];
163
+ const listClassName = `${listTheme[node.__tag]} ${emotionMap[styleId]?.className ?? ""}`;
164
+ let nestedListClassName;
165
+ const nestedListTheme = listTheme.nested;
166
+ if (nestedListTheme !== undefined && nestedListTheme.list) {
167
+ nestedListClassName = nestedListTheme.list;
168
+ }
169
+ if (listClassName) {
170
+ classesToAdd.push(listClassName);
171
+ }
172
+ if (listLevelClassName !== undefined) {
173
+ const listItemClasses = listLevelClassName.split(" ");
174
+ classesToAdd.push(...listItemClasses);
175
+ for (let i = 0; i < listLevelsClassNames.length; i++) {
176
+ if (i !== normalizedListDepth) {
177
+ classesToRemove.push(node.__tag + i);
178
+ }
179
+ }
180
+ }
181
+ if (nestedListClassName !== undefined) {
182
+ const nestedListItemClasses = nestedListClassName.split(" ");
183
+ if (listDepth > 1) {
184
+ classesToAdd.push(...nestedListItemClasses);
185
+ } else {
186
+ classesToRemove.push(...nestedListItemClasses);
187
+ }
188
+ }
189
+ }
190
+ if (classesToRemove.length > 0) {
191
+ removeClassNamesFromElement(dom, ...classesToRemove);
192
+ }
193
+ if (classesToAdd.length > 0) {
194
+ addClassNamesToElement(dom, ...classesToAdd);
195
+ }
196
+ }
197
+
198
+ /*
199
+ * This function normalizes the children of a ListNode after the conversion from HTML,
200
+ * ensuring that they are all ListItemNodes and contain either a single nested ListNode
201
+ * or some other inline content.
202
+ */
203
+ function normalizeChildren(nodes) {
204
+ const normalizedListItems = [];
205
+ for (let i = 0; i < nodes.length; i++) {
206
+ const node = nodes[i];
207
+ if ($isListItemNode(node)) {
208
+ normalizedListItems.push(node);
209
+ node.getChildren().forEach(child => {
210
+ if ($isListNode(child)) {
211
+ normalizedListItems.push(wrapInListItem(child));
212
+ }
213
+ });
214
+ } else {
215
+ normalizedListItems.push(wrapInListItem(node));
216
+ }
217
+ }
218
+ return normalizedListItems;
219
+ }
220
+ function convertListNode(domNode) {
221
+ const nodeName = domNode.nodeName.toLowerCase();
222
+ let node = null;
223
+ if (nodeName === "ol") {
224
+ node = $createListNode("number");
225
+ } else if (nodeName === "ul") {
226
+ node = $createListNode("bullet");
227
+ }
228
+ return {
229
+ // @ts-expect-error
230
+ after: normalizeChildren,
231
+ node
232
+ };
233
+ }
234
+ const TAG_TO_WEBINY_LIST_TYPE = {
235
+ ol: "number",
236
+ ul: "bullet"
237
+ };
238
+ export function $createListNode(listType, styleId, start = 1) {
239
+ return new ListNode(listType, {
240
+ start,
241
+ styleId
242
+ });
243
+ }
244
+ export function $isListNode(node) {
245
+ return node instanceof ListNode;
246
+ }
247
+
248
+ //# sourceMappingURL=ListNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["ElementNode","findTypographyStyleByHtmlTag","addClassNamesToElement","removeClassNamesFromElement","$getListDepth","wrapInListItem","$isListItemNode","ListNode","constructor","listType","options","styleId","key","className","start","__styleId","__className","_listType","TAG_TO_WEBINY_LIST_TYPE","__listType","__tag","__start","getType","createDOM","config","tag","element","document","createElement","setAttribute","String","updateElementWithThemeClasses","theme","__lexicalListType","setListThemeClassNames","exportDOM","editor","base","classList","add","clone","node","getListType","getClassName","getStyleId","getStart","getKey","setStyleId","setClassName","importJSON","serializedNode","$createListNode","setFormat","format","setIndent","indent","setDirection","direction","exportJSON","getTag","type","importDomConversionMap","conversion","convertListNode","priority","importDOM","ol","ul","updateDOM","prevNode","dom","extractWithChild","child","emotionMap","setDefaultTypography","themeEmotionMap","typographyStyle","id","editorTheme","editorThemeClasses","classesToAdd","classesToRemove","listTheme","list","undefined","listLevelsClassNames","listDepth","normalizedListDepth","length","listLevelClassName","listClassName","nestedListClassName","nestedListTheme","nested","push","listItemClasses","split","i","nestedListItemClasses","normalizeChildren","nodes","normalizedListItems","getChildren","forEach","$isListNode","domNode","nodeName","toLowerCase","after"],"sources":["ListNode.ts"],"sourcesContent":["import type {\n DOMConversion,\n DOMConversionMap,\n DOMConversionOutput,\n DOMExportOutput,\n EditorConfig,\n LexicalEditor,\n LexicalNode,\n NodeKey,\n SerializedElementNode,\n Spread\n} from \"lexical\";\nimport { ElementNode } from \"lexical\";\nimport type { EditorTheme, ThemeEmotionMap } from \"@webiny/lexical-theme\";\nimport { findTypographyStyleByHtmlTag } from \"@webiny/lexical-theme\";\nimport { addClassNamesToElement, removeClassNamesFromElement } from \"@lexical/utils\";\nimport type { ListNodeTagType } from \"@lexical/list/LexicalListNode\";\nimport { $getListDepth, wrapInListItem } from \"~/utils/listNode\";\nimport type { ListItemNode } from \"./ListItemNode\";\nimport { $isListItemNode } from \"./ListItemNode\";\nimport type { ListType } from \"@lexical/list\";\nimport type { TypographyStylesNode } from \"~/types\";\n\nexport type SerializedWebinyListNode = Spread<\n {\n styleId?: string;\n className?: string;\n listType: ListType;\n start: number;\n tag: ListNodeTagType;\n type: \"wby-list\";\n },\n SerializedElementNode\n>;\n\ntype ListNodeOptions = {\n styleId?: string;\n className?: string;\n start?: number;\n key?: NodeKey;\n};\n\nexport class ListNode extends ElementNode implements TypographyStylesNode {\n /** @internal */\n __tag: ListNodeTagType;\n /** @internal */\n __start: number;\n /** @internal */\n __listType: ListType;\n\n private __styleId: string;\n private __className: string | undefined;\n\n constructor(listType: ListType, options: ListNodeOptions = {}) {\n const { styleId, key, className, start } = options;\n super(key);\n this.__styleId = styleId ?? \"\";\n this.__className = className;\n const _listType = TAG_TO_WEBINY_LIST_TYPE[listType] || listType;\n this.__listType = _listType;\n this.__tag = _listType === \"number\" ? \"ol\" : \"ul\";\n this.__start = start || 1;\n }\n\n static override getType() {\n return \"wby-list\";\n }\n\n override createDOM(config: EditorConfig): HTMLElement {\n const tag = this.__tag;\n const element = document.createElement(tag);\n\n if (this.__start !== 1) {\n element.setAttribute(\"start\", String(this.__start));\n }\n\n this.updateElementWithThemeClasses(element, config.theme as EditorTheme);\n\n // @ts-expect-error Internal field.\n element.__lexicalListType = this.__listType;\n const theme = config.theme as EditorTheme;\n setListThemeClassNames(element, theme, this, this.__styleId);\n return element;\n }\n\n override exportDOM(editor: LexicalEditor): DOMExportOutput {\n const base = super.exportDOM(editor);\n\n const element = base.element as HTMLElement;\n if (element && this.__className) {\n element.classList.add(this.__className);\n }\n\n return { ...base, element };\n }\n\n static override clone(node: ListNode): ListNode {\n return new ListNode(node.getListType(), {\n className: node.getClassName(),\n styleId: node.getStyleId(),\n start: node.getStart(),\n key: node.getKey()\n });\n }\n\n getStyleId(): string | undefined {\n return this.__styleId;\n }\n\n setStyleId(styleId: string | undefined) {\n this.__styleId = styleId ?? \"\";\n }\n\n setClassName(className: string | undefined) {\n this.__className = className;\n }\n\n getClassName(): string | undefined {\n return this.__className;\n }\n\n static override importJSON(serializedNode: SerializedWebinyListNode): ListNode {\n const node = $createListNode(\n serializedNode.listType,\n // `styleId` is for backwards compatibility\n serializedNode.styleId ?? serializedNode.styleId,\n serializedNode.start\n );\n node.setFormat(serializedNode.format);\n node.setIndent(serializedNode.indent);\n node.setDirection(serializedNode.direction);\n\n node.setClassName(serializedNode.className);\n\n return node;\n }\n\n override exportJSON(): SerializedWebinyListNode {\n return {\n ...super.exportJSON(),\n styleId: this.__styleId,\n className: this.__className,\n listType: this.getListType(),\n start: this.getStart(),\n tag: this.getTag(),\n type: \"wby-list\"\n };\n }\n\n static importDomConversionMap(): DOMConversion<HTMLElement> | null {\n return {\n conversion: convertListNode,\n priority: 0\n };\n }\n\n static override importDOM(): DOMConversionMap | null {\n return {\n ol: () => {\n return this.importDomConversionMap();\n },\n ul: () => {\n return this.importDomConversionMap();\n }\n };\n }\n\n override updateDOM(prevNode: ListNode, dom: HTMLElement, config: EditorConfig): boolean {\n if (prevNode.__tag !== this.__tag) {\n return true;\n }\n\n setListThemeClassNames(dom, config.theme as EditorTheme, this, this.__styleId);\n return false;\n }\n\n override extractWithChild(child: LexicalNode): boolean {\n return $isListItemNode(child);\n }\n\n public getListType(): ListType {\n return this.__listType;\n }\n\n public getStart(): number {\n return this.__start;\n }\n\n private getTag(): ListNodeTagType {\n return this.__tag;\n }\n\n protected updateElementWithThemeClasses(element: HTMLElement, theme: EditorTheme): HTMLElement {\n if (!theme?.emotionMap) {\n return element;\n }\n\n if (!this.__styleId || !this.__className) {\n this.setDefaultTypography(theme.emotionMap);\n }\n\n if (this.__className) {\n addClassNamesToElement(element, this.__className);\n }\n\n return element;\n }\n\n private setDefaultTypography(themeEmotionMap: ThemeEmotionMap) {\n const typographyStyle = findTypographyStyleByHtmlTag(this.getTag(), themeEmotionMap);\n if (typographyStyle) {\n this.__styleId = typographyStyle.id;\n this.__className = typographyStyle.className;\n }\n }\n}\n\nfunction setListThemeClassNames(\n dom: HTMLElement,\n editorTheme: EditorTheme,\n node: ListNode,\n styleId: string\n): void {\n const editorThemeClasses = editorTheme;\n const classesToAdd = [];\n const classesToRemove = [];\n const listTheme = editorThemeClasses.list;\n const emotionMap = editorTheme?.emotionMap || {};\n if (listTheme !== undefined) {\n const listLevelsClassNames = listTheme[`${node.__tag}Depth`] || [];\n const listDepth = $getListDepth(node) - 1;\n const normalizedListDepth = listDepth % listLevelsClassNames.length;\n const listLevelClassName = listLevelsClassNames[normalizedListDepth];\n const listClassName = `${listTheme[node.__tag]} ${emotionMap[styleId]?.className ?? \"\"}`;\n let nestedListClassName;\n const nestedListTheme = listTheme.nested;\n\n if (nestedListTheme !== undefined && nestedListTheme.list) {\n nestedListClassName = nestedListTheme.list;\n }\n\n if (listClassName) {\n classesToAdd.push(listClassName);\n }\n\n if (listLevelClassName !== undefined) {\n const listItemClasses = listLevelClassName.split(\" \");\n classesToAdd.push(...listItemClasses);\n for (let i = 0; i < listLevelsClassNames.length; i++) {\n if (i !== normalizedListDepth) {\n classesToRemove.push(node.__tag + i);\n }\n }\n }\n\n if (nestedListClassName !== undefined) {\n const nestedListItemClasses = nestedListClassName.split(\" \");\n\n if (listDepth > 1) {\n classesToAdd.push(...nestedListItemClasses);\n } else {\n classesToRemove.push(...nestedListItemClasses);\n }\n }\n }\n\n if (classesToRemove.length > 0) {\n removeClassNamesFromElement(dom, ...classesToRemove);\n }\n\n if (classesToAdd.length > 0) {\n addClassNamesToElement(dom, ...classesToAdd);\n }\n}\n\n/*\n * This function normalizes the children of a ListNode after the conversion from HTML,\n * ensuring that they are all ListItemNodes and contain either a single nested ListNode\n * or some other inline content.\n */\nfunction normalizeChildren(nodes: Array<ListNode>): Array<ListItemNode> {\n const normalizedListItems: Array<ListItemNode> = [];\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n if ($isListItemNode(node)) {\n normalizedListItems.push(node);\n node.getChildren().forEach(child => {\n if ($isListNode(child)) {\n normalizedListItems.push(wrapInListItem(child));\n }\n });\n } else {\n normalizedListItems.push(wrapInListItem(node));\n }\n }\n return normalizedListItems;\n}\n\nfunction convertListNode(domNode: Node): DOMConversionOutput {\n const nodeName = domNode.nodeName.toLowerCase();\n let node = null;\n\n if (nodeName === \"ol\") {\n node = $createListNode(\"number\");\n } else if (nodeName === \"ul\") {\n node = $createListNode(\"bullet\");\n }\n\n return {\n // @ts-expect-error\n after: normalizeChildren,\n node\n };\n}\n\nconst TAG_TO_WEBINY_LIST_TYPE: Record<string, ListType> = {\n ol: \"number\",\n ul: \"bullet\"\n};\n\nexport function $createListNode(listType: ListType, styleId?: string, start = 1): ListNode {\n return new ListNode(listType, {\n start,\n styleId\n });\n}\n\nexport function $isListNode(node: LexicalNode | null | undefined): node is ListNode {\n return node instanceof ListNode;\n}\n"],"mappings":"AAYA,SAASA,WAAW,QAAQ,SAAS;AAErC,SAASC,4BAA4B,QAAQ,uBAAuB;AACpE,SAASC,sBAAsB,EAAEC,2BAA2B,QAAQ,gBAAgB;AAEpF,SAASC,aAAa,EAAEC,cAAc;AAEtC,SAASC,eAAe;AAuBxB,OAAO,M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CAAC;EACpC,CAAC,MAAM,IAAIuD,QAAQ,KAAK,IAAI,EAAE;IAC1BjE,IAAI,GAAGU,eAAe,CAAC,QAAQ,CAAC;EACpC;EAEA,OAAO;IACH;IACAyD,KAAK,EAAET,iBAAiB;IACxB1D;EACJ,CAAC;AACL;AAEA,MAAMvB,uBAAiD,GAAG;EACtDgD,EAAE,EAAE,QAAQ;EACZC,EAAE,EAAE;AACR,CAAC;AAED,OAAO,SAAShB,eAAeA,CAAC1C,QAAkB,EAAEE,OAAgB,EAAEG,KAAK,GAAG,CAAC,EAAY;EACvF,OAAO,IAAIP,QAAQ,CAACE,QAAQ,EAAE;IAC1BK,KAAK;IACLH;EACJ,CAAC,CAAC;AACN;AAEA,OAAO,SAAS6F,WAAWA,CAAC/D,IAAoC,EAAoB;EAChF,OAAOA,IAAI,YAAYlC,QAAQ;AACnC","ignoreList":[]}
@@ -0,0 +1,39 @@
1
+ import type { DOMConversionMap, LexicalNode, NodeKey, SerializedParagraphNode as SerializedBaseParagraphNode, Spread, LexicalEditor, DOMExportOutput, RangeSelection } from "lexical";
2
+ import { ParagraphNode as BaseParagraphNode } from "lexical";
3
+ import type { EditorConfig } from "lexical";
4
+ import type { EditorTheme } from "@webiny/lexical-theme";
5
+ import type { TypographyStylesNode, ThemeStyleValue } from "./types";
6
+ export type SerializeParagraphNode = Spread<{
7
+ styles?: ThemeStyleValue[];
8
+ styleId?: string;
9
+ className?: string;
10
+ type: "wby-paragraph";
11
+ }, SerializedBaseParagraphNode>;
12
+ interface ParagraphNodeOptions {
13
+ className?: string;
14
+ styleId?: string;
15
+ key?: NodeKey;
16
+ }
17
+ export declare class ParagraphNode extends BaseParagraphNode implements TypographyStylesNode {
18
+ private __styleId;
19
+ private __className;
20
+ constructor(options?: ParagraphNodeOptions);
21
+ getStyleId(): string | undefined;
22
+ setStyleId(styleId: string | undefined): void;
23
+ setClassName(className: string | undefined): void;
24
+ getClassName(): string | undefined;
25
+ static getType(): string;
26
+ static clone(node: ParagraphNode): ParagraphNode;
27
+ insertNewAfter(rangeSelection: RangeSelection, restoreSelection: boolean): ParagraphNode;
28
+ createDOM(config: EditorConfig): HTMLElement;
29
+ exportDOM(editor: LexicalEditor): DOMExportOutput;
30
+ updateDOM(prevNode: ParagraphNode, dom: HTMLElement, config: EditorConfig): boolean;
31
+ static importDOM(): DOMConversionMap | null;
32
+ static importJSON(serializedNode: SerializeParagraphNode): BaseParagraphNode;
33
+ exportJSON(): SerializeParagraphNode;
34
+ protected updateElementWithThemeClasses(element: HTMLElement, theme: EditorTheme): HTMLElement;
35
+ private setDefaultTypography;
36
+ }
37
+ export declare function $createParagraphNode(styleId?: string): ParagraphNode;
38
+ export declare function $isParagraphNode(node: LexicalNode | null | undefined): node is ParagraphNode;
39
+ export {};
@@ -0,0 +1,157 @@
1
+ import { ParagraphNode as BaseParagraphNode } from "lexical";
2
+ import { findTypographyStyleByHtmlTag } from "@webiny/lexical-theme";
3
+ import { addClassNamesToElement } from "@lexical/utils";
4
+ import { getStyleId } from "./utils/getStyleId";
5
+ function convertParagraphElement(element) {
6
+ const node = $createParagraphNode();
7
+ if (element.style) {
8
+ node.setFormat(element.style.textAlign);
9
+ }
10
+ return {
11
+ node
12
+ };
13
+ }
14
+ export class ParagraphNode extends BaseParagraphNode {
15
+ constructor(options = {}) {
16
+ const {
17
+ styleId,
18
+ key,
19
+ className
20
+ } = options;
21
+ super(key);
22
+ this.__styleId = styleId;
23
+ this.__className = className;
24
+ }
25
+ getStyleId() {
26
+ return this.__styleId;
27
+ }
28
+ setStyleId(styleId) {
29
+ this.__styleId = styleId;
30
+ }
31
+ setClassName(className) {
32
+ this.__className = className;
33
+ }
34
+ getClassName() {
35
+ return this.__className;
36
+ }
37
+ static getType() {
38
+ return "wby-paragraph";
39
+ }
40
+ static clone(node) {
41
+ return new ParagraphNode({
42
+ styleId: node.getStyleId(),
43
+ className: node.getClassName(),
44
+ key: node.__key
45
+ });
46
+ }
47
+ insertNewAfter(rangeSelection, restoreSelection) {
48
+ const newElement = $createParagraphNode();
49
+ newElement.setTextFormat(rangeSelection.format);
50
+ newElement.setTextStyle(rangeSelection.style);
51
+ const direction = this.getDirection();
52
+ newElement.setDirection(direction);
53
+ newElement.setFormat(this.getFormatType());
54
+ newElement.setStyle(this.getStyle());
55
+ this.insertAfter(newElement, restoreSelection);
56
+ return newElement;
57
+ }
58
+ createDOM(config) {
59
+ const element = super.createDOM(config);
60
+ return this.updateElementWithThemeClasses(element, config.theme);
61
+ }
62
+ exportDOM(editor) {
63
+ const base = super.exportDOM(editor);
64
+ const element = base.element;
65
+ if (element && this.__className) {
66
+ element.classList.add(this.__className);
67
+ }
68
+ return {
69
+ ...base,
70
+ element
71
+ };
72
+ }
73
+ updateDOM(prevNode, dom, config) {
74
+ const prevTypoStyleId = prevNode.getStyleId();
75
+ const nextTypoStyleId = this.getStyleId();
76
+ if (!nextTypoStyleId) {
77
+ this.updateElementWithThemeClasses(dom, config.theme);
78
+ return false;
79
+ }
80
+ if (prevTypoStyleId !== nextTypoStyleId && nextTypoStyleId) {
81
+ this.updateElementWithThemeClasses(dom, config.theme);
82
+ }
83
+ // Returning false tells Lexical that this node does not need its
84
+ // DOM element replacing with a new copy from createDOM.
85
+ return false;
86
+ }
87
+
88
+ /*
89
+ * On copy/paste event this method will be executed in and create a node
90
+ * */
91
+ static importDOM() {
92
+ return {
93
+ p: () => ({
94
+ conversion: convertParagraphElement,
95
+ priority: 0
96
+ })
97
+ };
98
+ }
99
+
100
+ /*
101
+ * Serialize the JSON data back into a node
102
+ */
103
+ static importJSON(serializedNode) {
104
+ const node = $createParagraphNode();
105
+ node.setFormat(serializedNode.format);
106
+ node.setIndent(serializedNode.indent);
107
+ node.setDirection(serializedNode.direction);
108
+ const styleId = getStyleId({
109
+ styleId: serializedNode.styleId,
110
+ styles: serializedNode.styles
111
+ });
112
+ node.setStyleId(styleId);
113
+ node.setClassName(serializedNode.className);
114
+ return node;
115
+ }
116
+
117
+ /*
118
+ * Serialize the node to JSON data representation.
119
+ * */
120
+ exportJSON() {
121
+ return {
122
+ ...super.exportJSON(),
123
+ styleId: this.__styleId,
124
+ className: this.__className,
125
+ type: "wby-paragraph"
126
+ };
127
+ }
128
+ updateElementWithThemeClasses(element, theme) {
129
+ if (!theme?.emotionMap) {
130
+ return element;
131
+ }
132
+ if (!this.__styleId || !this.__className) {
133
+ this.setDefaultTypography(theme.emotionMap);
134
+ }
135
+ if (this.__className) {
136
+ addClassNamesToElement(element, this.__className);
137
+ }
138
+ return element;
139
+ }
140
+ setDefaultTypography(themeEmotionMap) {
141
+ const typographyStyle = findTypographyStyleByHtmlTag("p", themeEmotionMap);
142
+ if (typographyStyle) {
143
+ this.__styleId = typographyStyle.id;
144
+ this.__className = typographyStyle.className;
145
+ }
146
+ }
147
+ }
148
+ export function $createParagraphNode(styleId) {
149
+ return new ParagraphNode({
150
+ styleId
151
+ });
152
+ }
153
+ export function $isParagraphNode(node) {
154
+ return node instanceof ParagraphNode;
155
+ }
156
+
157
+ //# sourceMappingURL=ParagraphNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["ParagraphNode","BaseParagraphNode","findTypographyStyleByHtmlTag","addClassNamesToElement","getStyleId","convertParagraphElement","element","node","$createParagraphNode","style","setFormat","textAlign","constructor","options","styleId","key","className","__styleId","__className","setStyleId","setClassName","getClassName","getType","clone","__key","insertNewAfter","rangeSelection","restoreSelection","newElement","setTextFormat","format","setTextStyle","direction","getDirection","setDirection","getFormatType","setStyle","getStyle","insertAfter","createDOM","config","updateElementWithThemeClasses","theme","exportDOM","editor","base","classList","add","updateDOM","prevNode","dom","prevTypoStyleId","nextTypoStyleId","importDOM","p","conversion","priority","importJSON","serializedNode","setIndent","indent","styles","exportJSON","type","emotionMap","setDefaultTypography","themeEmotionMap","typographyStyle","id","$isParagraphNode"],"sources":["ParagraphNode.ts"],"sourcesContent":["import type {\n DOMConversionMap,\n DOMConversionOutput,\n ElementFormatType,\n LexicalNode,\n NodeKey,\n SerializedParagraphNode as SerializedBaseParagraphNode,\n Spread,\n LexicalEditor,\n DOMExportOutput,\n RangeSelection\n} from \"lexical\";\nimport { ParagraphNode as BaseParagraphNode } from \"lexical\";\nimport type { EditorConfig } from \"lexical\";\nimport type { EditorTheme, ThemeEmotionMap } from \"@webiny/lexical-theme\";\nimport { findTypographyStyleByHtmlTag } from \"@webiny/lexical-theme\";\nimport { addClassNamesToElement } from \"@lexical/utils\";\nimport type { TypographyStylesNode, ThemeStyleValue } from \"~/types\";\nimport { getStyleId } from \"./utils/getStyleId\";\n\nfunction convertParagraphElement(element: HTMLElement): DOMConversionOutput {\n const node = $createParagraphNode();\n if (element.style) {\n node.setFormat(element.style.textAlign as ElementFormatType);\n }\n\n return { node };\n}\n\nexport type SerializeParagraphNode = Spread<\n {\n styles?: ThemeStyleValue[];\n styleId?: string;\n className?: string;\n type: \"wby-paragraph\";\n },\n SerializedBaseParagraphNode\n>;\n\ninterface ParagraphNodeOptions {\n className?: string;\n styleId?: string;\n key?: NodeKey;\n}\n\nexport class ParagraphNode extends BaseParagraphNode implements TypographyStylesNode {\n private __styleId: string | undefined;\n private __className: string | undefined;\n\n constructor(options: ParagraphNodeOptions = {}) {\n const { styleId, key, className } = options;\n super(key);\n\n this.__styleId = styleId;\n this.__className = className;\n }\n\n getStyleId(): string | undefined {\n return this.__styleId;\n }\n\n setStyleId(styleId: string | undefined) {\n this.__styleId = styleId;\n }\n\n setClassName(className: string | undefined) {\n this.__className = className;\n }\n\n getClassName(): string | undefined {\n return this.__className;\n }\n\n static override getType(): string {\n return \"wby-paragraph\";\n }\n\n static override clone(node: ParagraphNode): ParagraphNode {\n return new ParagraphNode({\n styleId: node.getStyleId(),\n className: node.getClassName(),\n key: node.__key\n });\n }\n\n override insertNewAfter(\n rangeSelection: RangeSelection,\n restoreSelection: boolean\n ): ParagraphNode {\n const newElement = $createParagraphNode();\n newElement.setTextFormat(rangeSelection.format);\n newElement.setTextStyle(rangeSelection.style);\n const direction = this.getDirection();\n newElement.setDirection(direction);\n newElement.setFormat(this.getFormatType());\n newElement.setStyle(this.getStyle());\n this.insertAfter(newElement, restoreSelection);\n return newElement;\n }\n\n override createDOM(config: EditorConfig): HTMLElement {\n const element = super.createDOM(config);\n return this.updateElementWithThemeClasses(element, config.theme as EditorTheme);\n }\n\n override exportDOM(editor: LexicalEditor): DOMExportOutput {\n const base = super.exportDOM(editor);\n\n const element = base.element as HTMLElement;\n if (element && this.__className) {\n element.classList.add(this.__className);\n }\n\n return { ...base, element };\n }\n\n override updateDOM(prevNode: ParagraphNode, dom: HTMLElement, config: EditorConfig): boolean {\n const prevTypoStyleId = prevNode.getStyleId();\n const nextTypoStyleId = this.getStyleId();\n\n if (!nextTypoStyleId) {\n this.updateElementWithThemeClasses(dom, config.theme as EditorTheme);\n return false;\n }\n\n if (prevTypoStyleId !== nextTypoStyleId && nextTypoStyleId) {\n this.updateElementWithThemeClasses(dom, config.theme as EditorTheme);\n }\n // Returning false tells Lexical that this node does not need its\n // DOM element replacing with a new copy from createDOM.\n return false;\n }\n\n /*\n * On copy/paste event this method will be executed in and create a node\n * */\n static override importDOM(): DOMConversionMap | null {\n return {\n p: () => ({\n conversion: convertParagraphElement,\n priority: 0\n })\n };\n }\n\n /*\n * Serialize the JSON data back into a node\n */\n static override importJSON(serializedNode: SerializeParagraphNode): BaseParagraphNode {\n const node = $createParagraphNode();\n node.setFormat(serializedNode.format);\n node.setIndent(serializedNode.indent);\n node.setDirection(serializedNode.direction);\n\n const styleId = getStyleId({\n styleId: serializedNode.styleId,\n styles: serializedNode.styles\n });\n\n node.setStyleId(styleId);\n node.setClassName(serializedNode.className);\n return node;\n }\n\n /*\n * Serialize the node to JSON data representation.\n * */\n override exportJSON(): SerializeParagraphNode {\n return {\n ...super.exportJSON(),\n styleId: this.__styleId,\n className: this.__className,\n type: \"wby-paragraph\"\n };\n }\n\n protected updateElementWithThemeClasses(element: HTMLElement, theme: EditorTheme): HTMLElement {\n if (!theme?.emotionMap) {\n return element;\n }\n\n if (!this.__styleId || !this.__className) {\n this.setDefaultTypography(theme.emotionMap);\n }\n\n if (this.__className) {\n addClassNamesToElement(element, this.__className);\n }\n\n return element;\n }\n\n private setDefaultTypography(themeEmotionMap: ThemeEmotionMap) {\n const typographyStyle = findTypographyStyleByHtmlTag(\"p\", themeEmotionMap);\n if (typographyStyle) {\n this.__styleId = typographyStyle.id;\n this.__className = typographyStyle.className;\n }\n }\n}\n\nexport function $createParagraphNode(styleId?: string): ParagraphNode {\n return new ParagraphNode({ styleId });\n}\n\nexport function $isParagraphNode(node: LexicalNode | null | undefined): node is ParagraphNode {\n return node instanceof ParagraphNode;\n}\n"],"mappings":"AAYA,SAASA,aAAa,IAAIC,iBAAiB,QAAQ,SAAS;AAG5D,SAASC,4BAA4B,QAAQ,uBAAuB;AACpE,SAASC,sBAAsB,QAAQ,gBAAgB;AAEvD,SAASC,UAAU;AAEnB,SAASC,uBAAuBA,CAACC,OAAoB,EAAuB;EACxE,MAAMC,IAAI,GAAGC,oBAAoB,CAAC,CAAC;EACnC,IAAIF,OAAO,CAACG,KAAK,EAAE;IACfF,IAAI,CAACG,SAAS,CAACJ,OAAO,CAACG,KAAK,CAACE,SAA8B,CAAC;EAChE;EAEA,OAAO;IAAEJ;EAAK,CAAC;AACnB;AAkBA,OAAO,MAAMP,aAAa,SAASC,iBAAiB,CAAiC;EAIjFW,WAAWA,CAACC,OAA6B,GAAG,CAAC,CAAC,EAAE;IAC5C,MAAM;MAAEC,OAAO;MAAEC,GAAG;MAAEC;IAAU,CAAC,GAAGH,OAAO;IAC3C,KAAK,CAACE,GAAG,CAAC;IAEV,IAAI,CAACE,SAAS,GAAGH,OAAO;IACxB,IAAI,CAACI,WAAW,GAAGF,SAAS;EAChC;EAEAZ,UAAUA,CAAA,EAAuB;IAC7B,OAAO,IAAI,CAACa,SAAS;EACzB;EAEAE,UAAUA,CAACL,OAA2B,EAAE;IACpC,IAAI,CAACG,SAAS,GAAGH,OAAO;EAC5B;EAEAM,YAAYA,CAACJ,SAA6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A,IAAI,YAAYP,aAAa;AACxC","ignoreList":[]}
package/QuoteNode.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import type { DOMConversion, DOMConversionMap, DOMExportOutput, EditorConfig, LexicalEditor, LexicalNode, NodeKey, Spread } from "lexical";
2
+ import type { EditorTheme } from "@webiny/lexical-theme";
3
+ import type { SerializedQuoteNode as BaseSerializedQuoteNode } from "@lexical/rich-text";
4
+ import { QuoteNode as BaseQuoteNode } from "@lexical/rich-text";
5
+ import type { ThemeStyleValue, TypographyStylesNode } from "./types";
6
+ export type SerializedQuoteNode = Spread<{
7
+ styleId?: string;
8
+ styles?: ThemeStyleValue[];
9
+ className?: string;
10
+ type: "wby-quote";
11
+ }, BaseSerializedQuoteNode>;
12
+ interface QuoteNodeOptions {
13
+ className?: string;
14
+ styleId?: string;
15
+ key?: NodeKey;
16
+ }
17
+ export declare class QuoteNode extends BaseQuoteNode implements TypographyStylesNode {
18
+ private __styleId;
19
+ private __className;
20
+ constructor(options?: QuoteNodeOptions);
21
+ getStyleId(): string | undefined;
22
+ setStyleId(styleId: string | undefined): void;
23
+ setClassName(className: string | undefined): void;
24
+ getClassName(): string | undefined;
25
+ private setDefaultTypography;
26
+ static getType(): string;
27
+ static clone(node: QuoteNode): QuoteNode;
28
+ createDOM(config: EditorConfig): HTMLElement;
29
+ exportDOM(editor: LexicalEditor): DOMExportOutput;
30
+ static importDomConversionMap(): DOMConversion<HTMLElement> | null;
31
+ static importDOM(): DOMConversionMap | null;
32
+ static importJSON(serializedNode: SerializedQuoteNode): QuoteNode;
33
+ exportJSON(): SerializedQuoteNode;
34
+ protected updateElementWithThemeClasses(element: HTMLElement, theme: EditorTheme): HTMLElement;
35
+ }
36
+ export declare function $createQuoteNode(styleId?: string, key?: NodeKey): QuoteNode;
37
+ export declare function $isQuoteNode(node: LexicalNode | null | undefined): node is QuoteNode;
38
+ export {};
package/QuoteNode.js ADDED
@@ -0,0 +1,120 @@
1
+ import { $applyNodeReplacement } from "lexical";
2
+ import { findTypographyStyleByHtmlTag } from "@webiny/lexical-theme";
3
+ import { addClassNamesToElement } from "@lexical/utils";
4
+ import { QuoteNode as BaseQuoteNode } from "@lexical/rich-text";
5
+ import { getStyleId } from "./utils/getStyleId";
6
+ export class QuoteNode extends BaseQuoteNode {
7
+ constructor(options = {}) {
8
+ super(options.key);
9
+ this.__styleId = options?.styleId;
10
+ this.__className = options?.className;
11
+ }
12
+ getStyleId() {
13
+ return this.__styleId;
14
+ }
15
+ setStyleId(styleId) {
16
+ this.__styleId = styleId;
17
+ }
18
+ setClassName(className) {
19
+ this.__className = className;
20
+ }
21
+ getClassName() {
22
+ return this.__className;
23
+ }
24
+ setDefaultTypography(themeEmotionMap) {
25
+ // For some time in v5 we had `quoteblock` as tag name :facepalm: We must not break it.
26
+ const typographyStyle = findTypographyStyleByHtmlTag(["blockquote", "quoteblock"], themeEmotionMap);
27
+ if (typographyStyle) {
28
+ this.__styleId = typographyStyle.id;
29
+ this.__className = typographyStyle.className;
30
+ }
31
+ }
32
+ static getType() {
33
+ return "wby-quote";
34
+ }
35
+ static clone(node) {
36
+ return new QuoteNode({
37
+ styleId: node.getStyleId(),
38
+ className: node.getClassName(),
39
+ key: node.getKey()
40
+ });
41
+ }
42
+ createDOM(config) {
43
+ const element = super.createDOM(config);
44
+ return this.updateElementWithThemeClasses(element, config.theme);
45
+ }
46
+ exportDOM(editor) {
47
+ const base = super.exportDOM(editor);
48
+ const element = base.element;
49
+ if (element && this.__className) {
50
+ element.classList.add(this.__className);
51
+ }
52
+ return {
53
+ ...base,
54
+ element
55
+ };
56
+ }
57
+ static importDomConversionMap() {
58
+ return {
59
+ conversion: convertBlockquoteElement,
60
+ priority: 0
61
+ };
62
+ }
63
+ static importDOM() {
64
+ return {
65
+ blockquote: () => {
66
+ return this.importDomConversionMap();
67
+ }
68
+ };
69
+ }
70
+ static importJSON(serializedNode) {
71
+ const node = $createQuoteNode();
72
+ node.setFormat(serializedNode.format);
73
+ node.setIndent(serializedNode.indent);
74
+ node.setDirection(serializedNode.direction);
75
+ const styleId = getStyleId({
76
+ styleId: serializedNode.styleId,
77
+ styles: serializedNode.styles
78
+ });
79
+ node.setStyleId(styleId);
80
+ node.setClassName(serializedNode.className);
81
+ return node;
82
+ }
83
+ exportJSON() {
84
+ return {
85
+ ...super.exportJSON(),
86
+ type: "wby-quote",
87
+ className: this.__className,
88
+ styleId: this.__styleId
89
+ };
90
+ }
91
+ updateElementWithThemeClasses(element, theme) {
92
+ if (!theme?.emotionMap) {
93
+ return element;
94
+ }
95
+ if (!this.__styleId || !this.__className) {
96
+ this.setDefaultTypography(theme.emotionMap);
97
+ }
98
+ if (this.__className) {
99
+ addClassNamesToElement(element, this.__className);
100
+ }
101
+ return element;
102
+ }
103
+ }
104
+ function convertBlockquoteElement() {
105
+ const node = $createQuoteNode();
106
+ return {
107
+ node
108
+ };
109
+ }
110
+ export function $createQuoteNode(styleId, key) {
111
+ return $applyNodeReplacement(new QuoteNode({
112
+ styleId,
113
+ key
114
+ }));
115
+ }
116
+ export function $isQuoteNode(node) {
117
+ return node instanceof QuoteNode;
118
+ }
119
+
120
+ //# sourceMappingURL=QuoteNode.js.map