@react-fabric/lexical 1.0.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.
Files changed (136) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/_virtual/prism.js +5 -0
  4. package/dist/cjs/editor/Lexical.module.css +1 -0
  5. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +432 -0
  6. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +21 -0
  7. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +27 -0
  8. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +1139 -0
  9. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +25 -0
  10. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +50 -0
  11. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +245 -0
  12. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +19 -0
  13. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +18 -0
  14. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +471 -0
  15. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +17 -0
  16. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +24 -0
  17. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +1275 -0
  18. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +29 -0
  19. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +29 -0
  20. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +789 -0
  21. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +29 -0
  22. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +26 -0
  23. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +976 -0
  24. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +35 -0
  25. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +35 -0
  26. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2792 -0
  27. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +23 -0
  28. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +55 -0
  29. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +888 -0
  30. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +35 -0
  31. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +66 -0
  32. package/dist/cjs/node_modules/@mdi/js/mdi.js +8 -0
  33. package/dist/cjs/node_modules/prismjs/components/prism-c.js +82 -0
  34. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +33 -0
  35. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +101 -0
  36. package/dist/cjs/node_modules/prismjs/components/prism-css.js +66 -0
  37. package/dist/cjs/node_modules/prismjs/components/prism-java.js +126 -0
  38. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +174 -0
  39. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +417 -0
  40. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +188 -0
  41. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +14 -0
  42. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +60 -0
  43. package/dist/cjs/node_modules/prismjs/components/prism-python.js +67 -0
  44. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +130 -0
  45. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +34 -0
  46. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +150 -0
  47. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +62 -0
  48. package/dist/cjs/node_modules/prismjs/prism.js +1951 -0
  49. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +221 -0
  50. package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +156 -0
  51. package/dist/cjs/packages/lexical/src/editor/theme.js +97 -0
  52. package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +451 -0
  53. package/dist/cjs/packages/lexical/src/index.js +7 -0
  54. package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +218 -0
  55. package/dist/cjs/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
  56. package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +20 -0
  57. package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
  58. package/dist/cjs/packages/lexical/src/utils/index.js +214 -0
  59. package/dist/esm/_virtual/prism.js +5 -0
  60. package/dist/esm/editor/Lexical.module.css +1 -0
  61. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +423 -0
  62. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +19 -0
  63. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +18 -0
  64. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +1121 -0
  65. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +21 -0
  66. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +32 -0
  67. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +244 -0
  68. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +18 -0
  69. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +17 -0
  70. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +463 -0
  71. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +17 -0
  72. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +16 -0
  73. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +1262 -0
  74. package/dist/esm/node_modules/@lexical/list/LexicalList.js +23 -0
  75. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +16 -0
  76. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +781 -0
  77. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +23 -0
  78. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +18 -0
  79. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +957 -0
  80. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +26 -0
  81. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +16 -0
  82. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2754 -0
  83. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +20 -0
  84. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +17 -0
  85. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +839 -0
  86. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +26 -0
  87. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +17 -0
  88. package/dist/esm/node_modules/@mdi/js/mdi.js +7 -0
  89. package/dist/esm/node_modules/prismjs/components/prism-c.js +82 -0
  90. package/dist/esm/node_modules/prismjs/components/prism-clike.js +33 -0
  91. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +101 -0
  92. package/dist/esm/node_modules/prismjs/components/prism-css.js +66 -0
  93. package/dist/esm/node_modules/prismjs/components/prism-java.js +126 -0
  94. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +174 -0
  95. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +417 -0
  96. package/dist/esm/node_modules/prismjs/components/prism-markup.js +188 -0
  97. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +14 -0
  98. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +60 -0
  99. package/dist/esm/node_modules/prismjs/components/prism-python.js +67 -0
  100. package/dist/esm/node_modules/prismjs/components/prism-rust.js +130 -0
  101. package/dist/esm/node_modules/prismjs/components/prism-sql.js +34 -0
  102. package/dist/esm/node_modules/prismjs/components/prism-swift.js +150 -0
  103. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +62 -0
  104. package/dist/esm/node_modules/prismjs/prism.js +1951 -0
  105. package/dist/esm/packages/lexical/src/components/StickyComponent.js +221 -0
  106. package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +156 -0
  107. package/dist/esm/packages/lexical/src/editor/theme.js +97 -0
  108. package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +450 -0
  109. package/dist/esm/packages/lexical/src/index.js +3 -0
  110. package/dist/esm/packages/lexical/src/nodes/StickyNode.js +216 -0
  111. package/dist/esm/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
  112. package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +20 -0
  113. package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
  114. package/dist/esm/packages/lexical/src/utils/index.js +211 -0
  115. package/dist/types/components/StickyComponent.d.ts +9 -0
  116. package/dist/types/editor/LexicalEditor.d.ts +13 -0
  117. package/dist/types/editor/theme.d.ts +70 -0
  118. package/dist/types/hooks/useFormatWatcher.d.ts +40 -0
  119. package/dist/types/index.d.ts +1 -0
  120. package/dist/types/nodes/StickyNode.d.ts +35 -0
  121. package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -0
  122. package/dist/types/plugins/StickyPlugin.d.ts +1 -0
  123. package/dist/types/plugins/ToolbarPlugin.d.ts +1 -0
  124. package/dist/types/utils/index.d.ts +57 -0
  125. package/package.json +55 -0
  126. package/src/components/StickyComponent.tsx +271 -0
  127. package/src/editor/Lexical.module.css +218 -0
  128. package/src/editor/LexicalEditor.tsx +222 -0
  129. package/src/editor/theme.ts +93 -0
  130. package/src/hooks/useFormatWatcher.ts +504 -0
  131. package/src/index.ts +24 -0
  132. package/src/nodes/StickyNode.tsx +249 -0
  133. package/src/plugins/DraggableBlockPlugin.tsx +418 -0
  134. package/src/plugins/StickyPlugin.ts +38 -0
  135. package/src/plugins/ToolbarPlugin.tsx +279 -0
  136. package/src/utils/index.ts +303 -0
@@ -0,0 +1,93 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ export const LexicalTheme = {
25
+ ltr: "ltr",
26
+ rtl: "rtl",
27
+ paragraph: "editor-paragraph",
28
+ quote: "editor-quote",
29
+ heading: {
30
+ h1: "editor-heading-h1",
31
+ h2: "editor-heading-h2",
32
+ h3: "editor-heading-h3",
33
+ h4: "editor-heading-h4",
34
+ h5: "editor-heading-h5",
35
+ h6: "editor-heading-h6",
36
+ },
37
+ list: {
38
+ nested: {
39
+ listitem: "editor-nested-listitem",
40
+ },
41
+ ol: "editor-list-ol",
42
+ ul: "editor-list-ul",
43
+ listitem: "editor-listItem",
44
+ listitemChecked: "editor-listItemChecked",
45
+ listitemUnchecked: "editor-listItemUnchecked",
46
+ },
47
+ hashtag: "editor-hashtag",
48
+ image: "editor-image",
49
+ link: "editor-link",
50
+ text: {
51
+ bold: "editor-textBold",
52
+ code: "editor-textCode",
53
+ italic: "editor-textItalic",
54
+ strikethrough: "editor-textStrikethrough",
55
+ subscript: "editor-textSubscript",
56
+ superscript: "editor-textSuperscript",
57
+ underline: "editor-textUnderline",
58
+ underlineStrikethrough: "editor-textUnderlineStrikethrough",
59
+ },
60
+ code: "editor-code",
61
+ codeHighlight: {
62
+ atrule: "editor-tokenAttr",
63
+ attr: "editor-tokenAttr",
64
+ boolean: "editor-tokenProperty",
65
+ builtin: "editor-tokenSelector",
66
+ cdata: "editor-tokenComment",
67
+ char: "editor-tokenSelector",
68
+ class: "editor-tokenFunction",
69
+ "class-name": "editor-tokenFunction",
70
+ comment: "editor-tokenComment",
71
+ constant: "editor-tokenProperty",
72
+ deleted: "editor-tokenProperty",
73
+ doctype: "editor-tokenComment",
74
+ entity: "editor-tokenOperator",
75
+ function: "editor-tokenFunction",
76
+ important: "editor-tokenVariable",
77
+ inserted: "editor-tokenSelector",
78
+ keyword: "editor-tokenAttr",
79
+ namespace: "editor-tokenVariable",
80
+ number: "editor-tokenProperty",
81
+ operator: "editor-tokenOperator",
82
+ prolog: "editor-tokenComment",
83
+ property: "editor-tokenProperty",
84
+ punctuation: "editor-tokenPunctuation",
85
+ regex: "editor-tokenVariable",
86
+ selector: "editor-tokenSelector",
87
+ string: "editor-tokenSelector",
88
+ symbol: "editor-tokenProperty",
89
+ tag: "editor-tokenProperty",
90
+ url: "editor-tokenOperator",
91
+ variable: "editor-tokenVariable",
92
+ },
93
+ };
@@ -0,0 +1,504 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { $createCodeNode, $isCodeNode, CODE_LANGUAGE_MAP } from "@lexical/code";
25
+ import { $isLinkNode } from "@lexical/link";
26
+ import {
27
+ $isListNode,
28
+ INSERT_CHECK_LIST_COMMAND,
29
+ INSERT_ORDERED_LIST_COMMAND,
30
+ INSERT_UNORDERED_LIST_COMMAND,
31
+ ListNode,
32
+ REMOVE_LIST_COMMAND,
33
+ } from "@lexical/list";
34
+ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
35
+ import {
36
+ $createHeadingNode,
37
+ $createQuoteNode,
38
+ $isHeadingNode,
39
+ $isQuoteNode,
40
+ type HeadingTagType,
41
+ } from "@lexical/rich-text";
42
+ import {
43
+ $getSelectionStyleValueForProperty,
44
+ $isAtNodeEnd,
45
+ $isParentElementRTL,
46
+ $patchStyleText,
47
+ $setBlocksType,
48
+ } from "@lexical/selection";
49
+ import { $isTableNode } from "@lexical/table";
50
+ import {
51
+ $findMatchingParent,
52
+ $getNearestBlockElementAncestorOrThrow,
53
+ $getNearestNodeOfType,
54
+ mergeRegister,
55
+ } from "@lexical/utils";
56
+ import {
57
+ $createParagraphNode,
58
+ $getSelection,
59
+ $isRangeSelection,
60
+ $isRootOrShadowRoot,
61
+ $isTextNode,
62
+ CAN_REDO_COMMAND,
63
+ CAN_UNDO_COMMAND,
64
+ COMMAND_PRIORITY_CRITICAL,
65
+ FOCUS_COMMAND,
66
+ INDENT_CONTENT_COMMAND,
67
+ OUTDENT_CONTENT_COMMAND,
68
+ SELECTION_CHANGE_COMMAND,
69
+ type ElementNode,
70
+ type NodeKey,
71
+ type RangeSelection,
72
+ type TextNode,
73
+ } from "lexical";
74
+ import { useCallback, useEffect, useState, useTransition } from "react";
75
+
76
+ const blockTypeToBlockName = {
77
+ bullet: "Bulleted List",
78
+ check: "Check List",
79
+ code: "Code Block",
80
+ h1: "Heading 1",
81
+ h2: "Heading 2",
82
+ h3: "Heading 3",
83
+ h4: "Heading 4",
84
+ h5: "Heading 5",
85
+ h6: "Heading 6",
86
+ number: "Numbered List",
87
+ paragraph: "Normal",
88
+ quote: "Quote",
89
+ };
90
+ const rootTypeToRootName = {
91
+ root: "Root",
92
+ table: "Table",
93
+ };
94
+
95
+ export function getSelectedNode(
96
+ selection: RangeSelection,
97
+ ): TextNode | ElementNode {
98
+ const anchor = selection.anchor;
99
+ const focus = selection.focus;
100
+ const anchorNode = selection.anchor.getNode();
101
+ const focusNode = selection.focus.getNode();
102
+ if (anchorNode === focusNode) {
103
+ return anchorNode;
104
+ }
105
+ const isBackward = selection.isBackward();
106
+ if (isBackward) {
107
+ return $isAtNodeEnd(focus) ? anchorNode : focusNode;
108
+ } else {
109
+ return $isAtNodeEnd(anchor) ? anchorNode : focusNode;
110
+ }
111
+ }
112
+
113
+ export const useFormatWatcher = () => {
114
+ const [, startTransition] = useTransition();
115
+ const [editor] = useLexicalComposerContext();
116
+ const [activeEditor, setActiveEditor] = useState(editor);
117
+ const [blockType, setBlockType] =
118
+ useState<keyof typeof blockTypeToBlockName>("paragraph");
119
+ const [rootType, setRootType] =
120
+ useState<keyof typeof rootTypeToRootName>("root");
121
+ const [selectedElementKey, setSelectedElementKey] = useState<NodeKey | null>(
122
+ null,
123
+ );
124
+ const [fontSize, setFontSize] = useState<string>("16");
125
+ const [fontColor, setFontColor] = useState<string>("#000");
126
+ const [bgColor, setBgColor] = useState<string>("#fff");
127
+ const [fontFamily, setFontFamily] = useState<string>("Arial");
128
+ const [isLink, setIsLink] = useState(false);
129
+ const [isBold, setIsBold] = useState(false);
130
+ const [isItalic, setIsItalic] = useState(false);
131
+ const [isUnderline, setIsUnderline] = useState(false);
132
+ const [isStrikethrough, setIsStrikethrough] = useState(false);
133
+ const [isSubscript, setIsSubscript] = useState(false);
134
+ const [isSuperscript, setIsSuperscript] = useState(false);
135
+ const [isCode, setIsCode] = useState(false);
136
+ const [canUndo, setCanUndo] = useState(false);
137
+ const [canRedo, setCanRedo] = useState(false);
138
+ const [isRTL, setIsRTL] = useState(false);
139
+ const [codeLanguage, setCodeLanguage] = useState<string>("");
140
+ const [isEditable, setIsEditable] = useState(() => editor.isEditable());
141
+
142
+ const $updateToolbar = useCallback(() => {
143
+ const selection = $getSelection();
144
+ if ($isRangeSelection(selection)) {
145
+ const anchorNode = selection.anchor.getNode();
146
+ let element =
147
+ anchorNode.getKey() === "root"
148
+ ? anchorNode
149
+ : $findMatchingParent(anchorNode, (e) => {
150
+ const parent = e.getParent();
151
+ return parent !== null && $isRootOrShadowRoot(parent);
152
+ });
153
+
154
+ if (element === null) {
155
+ element = anchorNode.getTopLevelElementOrThrow();
156
+ }
157
+
158
+ const elementKey = element.getKey();
159
+ const elementDOM = activeEditor.getElementByKey(elementKey);
160
+
161
+ // Update text format
162
+ setIsBold(selection.hasFormat("bold"));
163
+ setIsItalic(selection.hasFormat("italic"));
164
+ setIsUnderline(selection.hasFormat("underline"));
165
+ setIsStrikethrough(selection.hasFormat("strikethrough"));
166
+ setIsSubscript(selection.hasFormat("subscript"));
167
+ setIsSuperscript(selection.hasFormat("superscript"));
168
+ setIsCode(selection.hasFormat("code"));
169
+ setIsRTL($isParentElementRTL(selection));
170
+
171
+ // Update links
172
+ const node = getSelectedNode(selection);
173
+ const parent = node.getParent();
174
+ if ($isLinkNode(parent) || $isLinkNode(node)) {
175
+ setIsLink(true);
176
+ } else {
177
+ setIsLink(false);
178
+ }
179
+
180
+ const tableNode = $findMatchingParent(node, $isTableNode);
181
+ if ($isTableNode(tableNode)) {
182
+ setRootType("table");
183
+ } else {
184
+ setRootType("root");
185
+ }
186
+
187
+ if (elementDOM !== null) {
188
+ setSelectedElementKey(elementKey);
189
+ if ($isListNode(element)) {
190
+ const parentList = $getNearestNodeOfType<ListNode>(
191
+ anchorNode,
192
+ ListNode,
193
+ );
194
+ const type = parentList
195
+ ? parentList.getListType()
196
+ : element.getListType();
197
+ setBlockType(type);
198
+ } else {
199
+ const type = $isHeadingNode(element)
200
+ ? element.getTag()
201
+ : element.getType();
202
+ if (type in blockTypeToBlockName) {
203
+ setBlockType(type as keyof typeof blockTypeToBlockName);
204
+ }
205
+ if ($isCodeNode(element)) {
206
+ const language = element.getLanguage();
207
+ setCodeLanguage(
208
+ language ? CODE_LANGUAGE_MAP[language] || language : "",
209
+ );
210
+ return;
211
+ }
212
+ }
213
+ }
214
+ // Handle buttons
215
+ setFontSize(
216
+ parseInt(
217
+ $getSelectionStyleValueForProperty(selection, "font-size", "16"),
218
+ ) + "",
219
+ );
220
+ setFontColor(
221
+ $getSelectionStyleValueForProperty(selection, "color", "#000"),
222
+ );
223
+ setBgColor(
224
+ $getSelectionStyleValueForProperty(
225
+ selection,
226
+ "background-color",
227
+ "#fff",
228
+ ),
229
+ );
230
+ setFontFamily(
231
+ $getSelectionStyleValueForProperty(selection, "font-family", "inherit"),
232
+ );
233
+ }
234
+ }, [activeEditor]);
235
+
236
+ useEffect(() => {
237
+ return editor.registerCommand(
238
+ FOCUS_COMMAND,
239
+ (_payload, newEditor) => {
240
+ setActiveEditor(newEditor);
241
+ return false;
242
+ },
243
+ COMMAND_PRIORITY_CRITICAL,
244
+ );
245
+ }, [editor]);
246
+
247
+ useEffect(() => {
248
+ return activeEditor.registerCommand(
249
+ SELECTION_CHANGE_COMMAND,
250
+ (_payload, newEditor) => {
251
+ $updateToolbar();
252
+ return false;
253
+ },
254
+ COMMAND_PRIORITY_CRITICAL,
255
+ );
256
+ }, [activeEditor, $updateToolbar]);
257
+
258
+ useEffect(() => {
259
+ return mergeRegister(
260
+ editor.registerEditableListener((editable) => {
261
+ setIsEditable(editable);
262
+ }),
263
+ activeEditor.registerUpdateListener(({ editorState }) => {
264
+ editorState.read(() => {
265
+ $updateToolbar();
266
+ });
267
+ }),
268
+ activeEditor.registerCommand<boolean>(
269
+ CAN_UNDO_COMMAND,
270
+ (payload) => {
271
+ setCanUndo(payload);
272
+ return false;
273
+ },
274
+ COMMAND_PRIORITY_CRITICAL,
275
+ ),
276
+ activeEditor.registerCommand<boolean>(
277
+ CAN_REDO_COMMAND,
278
+ (payload) => {
279
+ setCanRedo(payload);
280
+ return false;
281
+ },
282
+ COMMAND_PRIORITY_CRITICAL,
283
+ ),
284
+ );
285
+ }, [$updateToolbar, activeEditor, editor]);
286
+
287
+ const applyStyleText = useCallback(
288
+ (styles: Record<string, string>) => {
289
+ setTimeout(() => {
290
+ editor.update(() => {
291
+ const selection = $getSelection();
292
+ if ($isRangeSelection(selection)) {
293
+ $patchStyleText(selection, styles);
294
+ }
295
+ });
296
+ }, 100);
297
+ },
298
+ [editor],
299
+ );
300
+
301
+ const formatBulletList = () => {
302
+ setTimeout(() => {
303
+ if (blockType !== "bullet") {
304
+ editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, undefined);
305
+ } else {
306
+ editor.dispatchCommand(REMOVE_LIST_COMMAND, undefined);
307
+ }
308
+ }, 100);
309
+ };
310
+
311
+ const formatCheckList = () => {
312
+ setTimeout(() => {
313
+ if (blockType !== "check") {
314
+ editor.dispatchCommand(INSERT_CHECK_LIST_COMMAND, undefined);
315
+ } else {
316
+ editor.dispatchCommand(REMOVE_LIST_COMMAND, undefined);
317
+ }
318
+ }, 100);
319
+ };
320
+
321
+ const formatNumberedList = () => {
322
+ setTimeout(() => {
323
+ if (blockType !== "number") {
324
+ editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, undefined);
325
+ } else {
326
+ editor.dispatchCommand(REMOVE_LIST_COMMAND, undefined);
327
+ }
328
+ }, 100);
329
+ };
330
+
331
+ const formatNormal = useCallback(() => {
332
+ setTimeout(() => {
333
+ activeEditor.update(() => {
334
+ const selection = $getSelection();
335
+ if ($isRangeSelection(selection)) {
336
+ $setBlocksType(selection, () => $createParagraphNode());
337
+ }
338
+ });
339
+ }, 100);
340
+ }, [activeEditor, blockType]);
341
+
342
+ const formatQuote = useCallback(() => {
343
+ if (blockType !== "quote") {
344
+ setTimeout(() => {
345
+ activeEditor.update(() => {
346
+ const selection = $getSelection();
347
+ if ($isRangeSelection(selection)) {
348
+ $setBlocksType(selection, () => $createQuoteNode());
349
+ }
350
+ });
351
+ }, 100);
352
+ }
353
+ }, [activeEditor, blockType]);
354
+
355
+ const formatBlock = useCallback(() => {
356
+ if (blockType !== "code") {
357
+ setTimeout(() => {
358
+ activeEditor.update(() => {
359
+ const selection = $getSelection();
360
+ if ($isRangeSelection(selection)) {
361
+ $setBlocksType(selection, () => $createCodeNode());
362
+ }
363
+ });
364
+ }, 100);
365
+ }
366
+ }, [activeEditor, blockType]);
367
+
368
+ const formatHeading = useCallback(
369
+ (headingSize: HeadingTagType) => {
370
+ if (blockType !== headingSize) {
371
+ setTimeout(() => {
372
+ activeEditor.update(() => {
373
+ const selection = $getSelection();
374
+ if ($isRangeSelection(selection)) {
375
+ $setBlocksType(selection, () => $createHeadingNode(headingSize));
376
+ }
377
+ });
378
+ }, 100);
379
+ }
380
+ },
381
+ [activeEditor, blockType],
382
+ );
383
+
384
+ const indent = useCallback(() => {
385
+ activeEditor.dispatchCommand(INDENT_CONTENT_COMMAND, undefined);
386
+ }, [activeEditor]);
387
+
388
+ const outdent = useCallback(() => {
389
+ activeEditor.dispatchCommand(OUTDENT_CONTENT_COMMAND, undefined);
390
+ }, [activeEditor]);
391
+
392
+ const clearFormatting = useCallback(() => {
393
+ activeEditor.update(() => {
394
+ const selection = $getSelection();
395
+ if ($isRangeSelection(selection)) {
396
+ const anchor = selection.anchor;
397
+ const focus = selection.focus;
398
+ const nodes = selection.getNodes();
399
+
400
+ if (anchor.key === focus.key && anchor.offset === focus.offset) {
401
+ return;
402
+ }
403
+ setFontSize("16");
404
+ setBgColor("#fff");
405
+ setFontColor("#000");
406
+ nodes.forEach((node, idx) => {
407
+ // We split the first and last node by the selection
408
+ // So that we don't format unselected text inside those nodes
409
+ if ($isTextNode(node)) {
410
+ if (idx === 0 && anchor.offset !== 0) {
411
+ node = node.splitText(anchor.offset)[1] || node;
412
+ }
413
+ if (idx === nodes.length - 1 && $isTextNode(node)) {
414
+ node = node.splitText(focus.offset)[0] || node;
415
+ }
416
+ if ($isTextNode(node)) {
417
+ if (node.__style !== "") {
418
+ node.setStyle("");
419
+ }
420
+ if (node.__format !== 0) {
421
+ node.setFormat(0);
422
+ $getNearestBlockElementAncestorOrThrow(node).setFormat("");
423
+ }
424
+ }
425
+ } else if ($isHeadingNode(node) || $isQuoteNode(node)) {
426
+ node.replace($createParagraphNode(), true);
427
+ }
428
+ });
429
+ }
430
+ });
431
+ }, [activeEditor]);
432
+
433
+ const onSizeChange = useCallback(
434
+ (value: string) => {
435
+ startTransition(() => setFontColor(value));
436
+ applyStyleText({ "font-size": +value + "px" });
437
+ },
438
+ [applyStyleText],
439
+ );
440
+
441
+ const onFontColorSelect = useCallback(
442
+ (value: string) => {
443
+ startTransition(() => setFontColor(value));
444
+ applyStyleText({ color: value });
445
+ },
446
+ [applyStyleText],
447
+ );
448
+
449
+ const onBgColorSelect = useCallback(
450
+ (value: string) => {
451
+ startTransition(() => setBgColor(value));
452
+ applyStyleText({ "background-color": value });
453
+ },
454
+ [applyStyleText],
455
+ );
456
+
457
+ // @ts-expect-error ignore
458
+ window.$getSelection = async () => {
459
+ return await new Promise((resolve) => {
460
+ activeEditor.update(() => {
461
+ const selection = $getSelection();
462
+ resolve(selection);
463
+ });
464
+ });
465
+ };
466
+
467
+ return {
468
+ editor,
469
+ blockType,
470
+ rootType,
471
+ selectedElementKey,
472
+ fontSize,
473
+ fontColor,
474
+ bgColor,
475
+ fontFamily,
476
+ isLink,
477
+ isBold,
478
+ isItalic,
479
+ isUnderline,
480
+ isStrikethrough,
481
+ isSubscript,
482
+ isSuperscript,
483
+ isCode,
484
+ canUndo,
485
+ canRedo,
486
+ isRTL,
487
+ isEditable,
488
+ codeLanguage,
489
+ activeEditor,
490
+ indent,
491
+ outdent,
492
+ clearFormatting,
493
+ onFontColorSelect,
494
+ onBgColorSelect,
495
+ formatQuote,
496
+ onSizeChange,
497
+ formatBlock,
498
+ formatNormal,
499
+ formatHeading,
500
+ formatBulletList,
501
+ formatCheckList,
502
+ formatNumberedList,
503
+ };
504
+ };
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ export { LexicalEditor } from "./editor/LexicalEditor";