@react-fabric/lexical 1.0.0-beta.8 → 1.0.1

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 (148) hide show
  1. package/{src/editor/Lexical.module.css → css/_lexical.css} +79 -13
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/{packages/lexical/src/editor → editor}/LexicalEditor.js +98 -45
  4. package/dist/cjs/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +147 -140
  5. package/dist/cjs/i18n/ar/lexical.json.js +30 -0
  6. package/dist/cjs/i18n/en/lexical.json.js +30 -0
  7. package/{src/plugins/StickyPlugin.ts → dist/cjs/i18n/index.js} +10 -15
  8. package/dist/cjs/index.js +36 -0
  9. package/dist/cjs/plugins/CommentPlugin.js +481 -0
  10. package/dist/cjs/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +17 -20
  11. package/dist/cjs/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +58 -37
  12. package/dist/esm/{packages/lexical/src/editor → editor}/LexicalEditor.js +101 -48
  13. package/dist/esm/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +95 -88
  14. package/dist/esm/i18n/ar/lexical.json.js +25 -0
  15. package/dist/esm/i18n/en/lexical.json.js +25 -0
  16. package/dist/esm/i18n/index.js +33 -0
  17. package/dist/esm/index.js +34 -0
  18. package/dist/esm/plugins/CommentPlugin.js +481 -0
  19. package/dist/esm/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +15 -18
  20. package/dist/esm/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +59 -38
  21. package/dist/types/editor/LexicalEditor.d.ts +33 -4
  22. package/dist/types/hooks/useFormatWatcher.d.ts +7 -4
  23. package/dist/types/i18n/index.d.ts +1 -0
  24. package/dist/types/index.d.ts +27 -0
  25. package/dist/types/plugins/CommentPlugin.d.ts +5 -0
  26. package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -2
  27. package/dist/types/plugins/ToolbarPlugin.d.ts +1 -1
  28. package/dist/types/utils/index.d.ts +3 -6
  29. package/package.json +18 -18
  30. package/src/editor/LexicalEditor.tsx +95 -55
  31. package/src/hooks/useFormatWatcher.ts +50 -74
  32. package/src/i18n/ar/lexical.json +18 -0
  33. package/src/i18n/en/lexical.json +18 -0
  34. package/src/i18n/index.ts +28 -0
  35. package/src/index.ts +24 -17
  36. package/src/plugins/CommentPlugin.tsx +523 -0
  37. package/src/plugins/DraggableBlockPlugin.tsx +23 -68
  38. package/src/plugins/ToolbarPlugin.tsx +83 -131
  39. package/src/utils/index.ts +12 -108
  40. package/dist/cjs/_virtual/prism.js +0 -5
  41. package/dist/cjs/editor/Lexical.module.css +0 -1
  42. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -432
  43. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -21
  44. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -27
  45. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +0 -1139
  46. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +0 -25
  47. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +0 -50
  48. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +0 -245
  49. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +0 -19
  50. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +0 -18
  51. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +0 -471
  52. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +0 -17
  53. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +0 -24
  54. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +0 -1275
  55. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +0 -29
  56. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +0 -29
  57. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -789
  58. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +0 -29
  59. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -26
  60. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -976
  61. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +0 -35
  62. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -35
  63. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +0 -2792
  64. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +0 -23
  65. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +0 -55
  66. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -888
  67. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +0 -35
  68. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -66
  69. package/dist/cjs/node_modules/@mdi/js/mdi.js +0 -8
  70. package/dist/cjs/node_modules/prismjs/components/prism-c.js +0 -82
  71. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +0 -33
  72. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +0 -101
  73. package/dist/cjs/node_modules/prismjs/components/prism-css.js +0 -66
  74. package/dist/cjs/node_modules/prismjs/components/prism-java.js +0 -126
  75. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +0 -174
  76. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +0 -417
  77. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +0 -188
  78. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +0 -14
  79. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +0 -60
  80. package/dist/cjs/node_modules/prismjs/components/prism-python.js +0 -67
  81. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +0 -130
  82. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +0 -34
  83. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +0 -150
  84. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +0 -62
  85. package/dist/cjs/node_modules/prismjs/prism.js +0 -1951
  86. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +0 -221
  87. package/dist/cjs/packages/lexical/src/index.js +0 -7
  88. package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +0 -218
  89. package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +0 -20
  90. package/dist/esm/_virtual/prism.js +0 -5
  91. package/dist/esm/editor/Lexical.module.css +0 -1
  92. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -423
  93. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -19
  94. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -18
  95. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +0 -1121
  96. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +0 -21
  97. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +0 -32
  98. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +0 -244
  99. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +0 -18
  100. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +0 -17
  101. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +0 -463
  102. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +0 -17
  103. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +0 -16
  104. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +0 -1262
  105. package/dist/esm/node_modules/@lexical/list/LexicalList.js +0 -23
  106. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +0 -16
  107. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -781
  108. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +0 -23
  109. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -18
  110. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -957
  111. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +0 -26
  112. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -16
  113. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +0 -2754
  114. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +0 -20
  115. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +0 -17
  116. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -839
  117. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +0 -26
  118. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -17
  119. package/dist/esm/node_modules/@mdi/js/mdi.js +0 -7
  120. package/dist/esm/node_modules/prismjs/components/prism-c.js +0 -82
  121. package/dist/esm/node_modules/prismjs/components/prism-clike.js +0 -33
  122. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +0 -101
  123. package/dist/esm/node_modules/prismjs/components/prism-css.js +0 -66
  124. package/dist/esm/node_modules/prismjs/components/prism-java.js +0 -126
  125. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +0 -174
  126. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +0 -417
  127. package/dist/esm/node_modules/prismjs/components/prism-markup.js +0 -188
  128. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +0 -14
  129. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +0 -60
  130. package/dist/esm/node_modules/prismjs/components/prism-python.js +0 -67
  131. package/dist/esm/node_modules/prismjs/components/prism-rust.js +0 -130
  132. package/dist/esm/node_modules/prismjs/components/prism-sql.js +0 -34
  133. package/dist/esm/node_modules/prismjs/components/prism-swift.js +0 -150
  134. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +0 -62
  135. package/dist/esm/node_modules/prismjs/prism.js +0 -1951
  136. package/dist/esm/packages/lexical/src/components/StickyComponent.js +0 -221
  137. package/dist/esm/packages/lexical/src/index.js +0 -3
  138. package/dist/esm/packages/lexical/src/nodes/StickyNode.js +0 -216
  139. package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +0 -20
  140. package/dist/types/components/StickyComponent.d.ts +0 -9
  141. package/dist/types/nodes/StickyNode.d.ts +0 -35
  142. package/dist/types/plugins/StickyPlugin.d.ts +0 -1
  143. package/src/components/StickyComponent.tsx +0 -271
  144. package/src/nodes/StickyNode.tsx +0 -249
  145. /package/dist/cjs/{packages/lexical/src/editor → editor}/theme.js +0 -0
  146. /package/dist/cjs/{packages/lexical/src/utils → utils}/index.js +0 -0
  147. /package/dist/esm/{packages/lexical/src/editor → editor}/theme.js +0 -0
  148. /package/dist/esm/{packages/lexical/src/utils → utils}/index.js +0 -0
@@ -35,19 +35,20 @@ import { TablePlugin } from "@lexical/react/LexicalTablePlugin";
35
35
  import { HeadingNode, QuoteNode } from "@lexical/rich-text";
36
36
  import { TableCellNode, TableNode, TableRowNode } from "@lexical/table";
37
37
  import { ErrorBoundary, Header, ThemeProvider } from "@react-fabric/core";
38
- import { debounce } from "@react-fabric/utilities";
39
- import classNames from "classnames";
40
- import { type LexicalEditor as LXE } from "lexical";
38
+ import { cn, debounce, EMPTY_ARRAY } from "@react-fabric/utilities";
39
+ import type { SerializedEditorState, SerializedLexicalNode, UpdateListenerPayload } from "lexical";
40
+ import { type Klass, type LexicalNode, type LexicalNodeReplacement, type LexicalEditor as LXE } from "lexical";
41
+ import type { PropsWithChildren } from "react";
41
42
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
42
- import { StickyNode } from "../nodes/StickyNode";
43
+ import { CommentPlugin } from "../plugins/CommentPlugin";
43
44
  import { DraggableBlockPlugin } from "../plugins/DraggableBlockPlugin";
44
- import { StickyPlugin } from "../plugins/StickyPlugin";
45
- import classes from "./Lexical.module.css";
46
- import { LexicalTheme } from "./theme";
47
45
  import { ToolbarPlugin } from "../plugins/ToolbarPlugin";
46
+ import { LexicalTheme } from "./theme";
48
47
 
49
48
  export interface EditorProps {
50
- value?: string | AnyObject;
49
+ value?: string | SerializedEditorState<SerializedLexicalNode>;
50
+
51
+ isRtl?: boolean;
51
52
 
52
53
  readOnly?: boolean;
53
54
  /**
@@ -55,24 +56,77 @@ export interface EditorProps {
55
56
  */
56
57
  publishMode?: boolean;
57
58
 
58
- header?: string;
59
- footer?: string;
59
+ header?: React.ReactElement;
60
+ footer?: React.ReactElement;
61
+ coverPage?: React.ReactElement;
62
+
63
+ /**
64
+ * Username is used for comment plugin to identify the comment author. It can be used to conditionally render delete option for comments.
65
+ */
66
+ username?: string;
67
+
68
+ nodes?: ReadonlyArray<Klass<LexicalNode> | LexicalNodeReplacement>;
60
69
 
61
70
  onChange?: (value: KeyValue) => void;
62
71
  onDirty?: (isDirty: boolean) => void;
63
72
  }
64
73
 
65
- export const LexicalEditor = ({
74
+ function Wrapper({
75
+ children,
76
+ publishMode = false,
77
+ readOnly = false,
78
+ }: PropsWithChildren<{ publishMode?: boolean; readOnly?: boolean }>) {
79
+ return !publishMode ? (
80
+ <div className="lexical-scroller area-content overflow-auto">
81
+ {!readOnly && (
82
+ <div className={cn("fabric-lexicalToolbar", "sticky top-0 z-1")}>
83
+ <Header className="bg-default">
84
+ <ToolbarPlugin />
85
+ </Header>
86
+ </div>
87
+ )}
88
+ {children}
89
+ </div>
90
+ ) : (
91
+ children
92
+ );
93
+ }
94
+
95
+ /**
96
+ * LexicalEditor component is a rich text editor built using Lexical.
97
+ * It supports various features such as code highlighting, lists, tables, and sticky notes.
98
+ * It provides a customizable toolbar and handles editor state changes.
99
+ * It also supports drag-and-drop functionality for blocks.
100
+ * It can be used in both read-only and editable modes, and supports publishing mode for exporting content.
101
+ * It uses a custom theme and provides error handling for Lexical updates.
102
+ *
103
+ * @example
104
+ * ```jsx
105
+ * <LexicalEditor
106
+ * value={initialValue}
107
+ * readOnly={false}
108
+ * publishMode={false}
109
+ * onChange={(value) => console.log("Editor value changed:", value)}
110
+ * onDirty={(isDirty) => console.log("Editor is dirty:", isDirty)}
111
+ * />
112
+ * ```
113
+ * @see {@link https://lexical.dev/docs/}
114
+ */
115
+ export function LexicalEditor({
66
116
  value,
67
117
  readOnly,
68
118
  publishMode,
119
+ header,
120
+ footer,
121
+ coverPage,
122
+ isRtl,
123
+ username = "Guest",
124
+ nodes = EMPTY_ARRAY,
69
125
  onChange,
70
126
  onDirty,
71
- }: EditorProps) => {
72
- const editorRef = useRef<LXE>();
73
- const [editorContainer, setEditorContainer] = useState<HTMLDivElement | null>(
74
- null,
75
- );
127
+ }: EditorProps) {
128
+ const editorRef = useRef<LXE>(null);
129
+ const [editorContainer, setEditorContainer] = useState<HTMLDivElement | null>(null);
76
130
 
77
131
  // Catch any errors that occur during Lexical updates and log them
78
132
  // or throw them as needed. If you don't throw them, Lexical will
@@ -82,9 +136,8 @@ export const LexicalEditor = ({
82
136
  };
83
137
 
84
138
  const initEditor = useCallback((editor: LXE) => {
85
- let dispose: AnyObject;
86
139
  editorRef.current = editor;
87
- const handler = debounce(({ editorState, dirtyElements, dirtyLeaves }) => {
140
+ const handler = debounce(({ editorState, dirtyElements }: UpdateListenerPayload) => {
88
141
  editorState.read(() => {
89
142
  if (dirtyElements.size > 0) {
90
143
  onDirty?.(true);
@@ -110,27 +163,26 @@ export const LexicalEditor = ({
110
163
  // COMMAND_PRIORITY_HIGH,
111
164
  // ),
112
165
  // );
113
-
114
166
  return () => {
115
- dispose?.();
116
167
  handle?.();
117
168
  // cb?.();
118
169
  };
170
+ // eslint-disable-next-line @eslint-react/exhaustive-deps
119
171
  }, []);
120
172
 
121
173
  useEffect(() => {
122
- editorRef.current?.setEditorState(
123
- editorRef.current?.parseEditorState(value ?? ""),
124
- );
174
+ editorRef.current?.setEditorState(editorRef.current?.parseEditorState(value ?? ""));
125
175
  }, [value]);
126
176
 
127
177
  useEffect(() => {
128
178
  editorRef.current?.registerRootListener((rootElement) => {
129
- rootElement &&
130
- setTimeout(
131
- () => (rootElement.contentEditable = `${!readOnly && !publishMode}`),
132
- 100,
133
- );
179
+ if (rootElement) {
180
+ const tmr = setTimeout(() => (rootElement.contentEditable = `${!readOnly && !publishMode}`), 100);
181
+
182
+ return () => {
183
+ clearTimeout(tmr);
184
+ };
185
+ }
134
186
  });
135
187
  }, [readOnly, publishMode]);
136
188
 
@@ -150,33 +202,14 @@ export const LexicalEditor = ({
150
202
  TableCellNode,
151
203
  TableNode,
152
204
  TableRowNode,
153
- StickyNode,
205
+ ...nodes,
154
206
  ],
155
207
  onError,
156
208
  }),
209
+ // eslint-disable-next-line @eslint-react/exhaustive-deps
157
210
  [],
158
211
  );
159
212
 
160
- const Wrapper = useCallback(
161
- ({ children }: KeyValue) => {
162
- return !publishMode ? (
163
- <div className="lexical-scroller area-content overflow-auto">
164
- <div
165
- className={classNames(classes.lexicalToolbar, "sticky top-0 z-1")}
166
- >
167
- <Header className="bg-base">
168
- <ToolbarPlugin />
169
- </Header>
170
- </div>
171
- {children}
172
- </div>
173
- ) : (
174
- children
175
- );
176
- },
177
- [publishMode],
178
- );
179
-
180
213
  return (
181
214
  <ErrorBoundary>
182
215
  <LexicalComposer initialConfig={initialConfig}>
@@ -184,17 +217,23 @@ export const LexicalEditor = ({
184
217
  <ThemeProvider colorScheme="light">
185
218
  <div
186
219
  role="none"
187
- className={classNames(
188
- classes.lexicalEditor,
220
+ className={cn(
221
+ "fabric-lexicalEditor",
189
222
  "lexical-container relative bg-white min-h-full mx-auto shadow-md print:shadow-none",
190
223
  )}
224
+ dir={isRtl ? "rtl" : "ltr"}
191
225
  onDragOver={(e) => e.stopPropagation()}
192
226
  onKeyDown={(e) => e.stopPropagation()}
193
227
  ref={setEditorContainer}
194
228
  >
229
+ {!publishMode && header && <div className="editor-header">{header}</div>}
230
+ {coverPage && <div className="editor-cover px-[2cm] py-[1cm]">{coverPage}</div>}
195
231
  <RichTextPlugin
196
232
  contentEditable={
197
- <ContentEditable className="lexical-editor px-[2cm] py-[1cm] min-h-screen" />
233
+ <ContentEditable
234
+ readOnly={!!readOnly || publishMode}
235
+ className="lexical-editor min-h-screen px-[2cm] py-[1cm]"
236
+ />
198
237
  }
199
238
  placeholder={
200
239
  <div className="text-muted overflow-hidden absolute mt-6 top-[1cm] left-[2cm] select-none pointer-events-none">
@@ -207,16 +246,17 @@ export const LexicalEditor = ({
207
246
  <CheckListPlugin />
208
247
  <TablePlugin />
209
248
  <HistoryPlugin />
210
- <StickyPlugin />
211
249
  <AutoFocusPlugin defaultSelection="rootStart" />
212
250
  {/** Custom plugins **/}
251
+ {editorContainer && <DraggableBlockPlugin anchorElem={editorContainer} isEditable={!readOnly && !publishMode} />}
213
252
  {editorContainer && (
214
- <DraggableBlockPlugin anchorElem={editorContainer} />
253
+ <CommentPlugin username={username} anchorElem={editorContainer} isPublishMode={!!publishMode} />
215
254
  )}
255
+ {!publishMode && footer && <div className="editor-footer">{footer}</div>}
216
256
  </div>
217
257
  </ThemeProvider>
218
258
  </Wrapper>
219
259
  </LexicalComposer>
220
260
  </ErrorBoundary>
221
261
  );
222
- };
262
+ }
@@ -32,13 +32,7 @@ import {
32
32
  REMOVE_LIST_COMMAND,
33
33
  } from "@lexical/list";
34
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";
35
+ import { $createHeadingNode, $createQuoteNode, $isHeadingNode, $isQuoteNode, type HeadingTagType } from "@lexical/rich-text";
42
36
  import {
43
37
  $getSelectionStyleValueForProperty,
44
38
  $isAtNodeEnd,
@@ -53,16 +47,18 @@ import {
53
47
  $getNearestNodeOfType,
54
48
  mergeRegister,
55
49
  } from "@lexical/utils";
50
+ import { isNil } from "@react-fabric/utilities";
51
+ import type { BaseSelection } from "lexical";
56
52
  import {
57
53
  $createParagraphNode,
58
54
  $getSelection,
59
55
  $isRangeSelection,
60
56
  $isRootOrShadowRoot,
61
57
  $isTextNode,
58
+ $setSelection,
62
59
  CAN_REDO_COMMAND,
63
60
  CAN_UNDO_COMMAND,
64
61
  COMMAND_PRIORITY_CRITICAL,
65
- FOCUS_COMMAND,
66
62
  INDENT_CONTENT_COMMAND,
67
63
  OUTDENT_CONTENT_COMMAND,
68
64
  SELECTION_CHANGE_COMMAND,
@@ -71,7 +67,7 @@ import {
71
67
  type RangeSelection,
72
68
  type TextNode,
73
69
  } from "lexical";
74
- import { useCallback, useEffect, useState, useTransition } from "react";
70
+ import { useCallback, useEffect, useState } from "react";
75
71
 
76
72
  const blockTypeToBlockName = {
77
73
  bullet: "Bulleted List",
@@ -87,14 +83,9 @@ const blockTypeToBlockName = {
87
83
  paragraph: "Normal",
88
84
  quote: "Quote",
89
85
  };
90
- const rootTypeToRootName = {
91
- root: "Root",
92
- table: "Table",
93
- };
86
+ type RootTypeToRootName = "root" | "table";
94
87
 
95
- export function getSelectedNode(
96
- selection: RangeSelection,
97
- ): TextNode | ElementNode {
88
+ export function getSelectedNode(selection: RangeSelection): TextNode | ElementNode {
98
89
  const anchor = selection.anchor;
99
90
  const focus = selection.focus;
100
91
  const anchorNode = selection.anchor.getNode();
@@ -111,20 +102,17 @@ export function getSelectedNode(
111
102
  }
112
103
 
113
104
  export const useFormatWatcher = () => {
114
- const [, startTransition] = useTransition();
115
105
  const [editor] = useLexicalComposerContext();
116
106
  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
- );
107
+ const [currentSelection, setCurrentSelection] = useState<BaseSelection | null>();
108
+ const [blockType, setBlockType] = useState<keyof typeof blockTypeToBlockName>("paragraph");
109
+ const [rootType, setRootType] = useState<RootTypeToRootName>("root");
110
+ const [selectedElementKey, setSelectedElementKey] = useState<NodeKey | null>(null);
124
111
  const [fontSize, setFontSize] = useState<string>("16");
125
112
  const [fontColor, setFontColor] = useState<string>("#000");
126
113
  const [bgColor, setBgColor] = useState<string>("#fff");
127
114
  const [fontFamily, setFontFamily] = useState<string>("Arial");
115
+ const [align, setAlign] = useState("");
128
116
  const [isLink, setIsLink] = useState(false);
129
117
  const [isBold, setIsBold] = useState(false);
130
118
  const [isItalic, setIsItalic] = useState(false);
@@ -166,7 +154,9 @@ export const useFormatWatcher = () => {
166
154
  setIsSubscript(selection.hasFormat("subscript"));
167
155
  setIsSuperscript(selection.hasFormat("superscript"));
168
156
  setIsCode(selection.hasFormat("code"));
169
- setIsRTL($isParentElementRTL(selection));
157
+ editor.read(() => {
158
+ setIsRTL($isParentElementRTL(selection));
159
+ });
170
160
 
171
161
  // Update links
172
162
  const node = getSelectedNode(selection);
@@ -186,73 +176,62 @@ export const useFormatWatcher = () => {
186
176
 
187
177
  if (elementDOM !== null) {
188
178
  setSelectedElementKey(elementKey);
179
+ if (elementDOM) setAlign(getComputedStyle(elementDOM).textAlign);
189
180
  if ($isListNode(element)) {
190
- const parentList = $getNearestNodeOfType<ListNode>(
191
- anchorNode,
192
- ListNode,
193
- );
194
- const type = parentList
195
- ? parentList.getListType()
196
- : element.getListType();
181
+ const parentList = $getNearestNodeOfType<ListNode>(anchorNode, ListNode);
182
+ const type = parentList ? parentList.getListType() : element.getListType();
197
183
  setBlockType(type);
198
184
  } else {
199
- const type = $isHeadingNode(element)
200
- ? element.getTag()
201
- : element.getType();
185
+ const type = $isHeadingNode(element) ? element.getTag() : element.getType();
202
186
  if (type in blockTypeToBlockName) {
203
187
  setBlockType(type as keyof typeof blockTypeToBlockName);
204
188
  }
205
189
  if ($isCodeNode(element)) {
206
190
  const language = element.getLanguage();
207
- setCodeLanguage(
208
- language ? CODE_LANGUAGE_MAP[language] || language : "",
209
- );
191
+ setCodeLanguage(language ? CODE_LANGUAGE_MAP[language] || language : "");
210
192
  return;
211
193
  }
212
194
  }
213
195
  }
214
196
  // Handle buttons
215
- setFontSize(
216
- parseInt(
217
- $getSelectionStyleValueForProperty(selection, "font-size", "16"),
218
- ) + "",
219
- );
197
+ setFontSize(parseInt($getSelectionStyleValueForProperty(selection, "font-size", "16")) + "");
220
198
  setFontColor(
221
- $getSelectionStyleValueForProperty(selection, "color", "#000"),
199
+ $getSelectionStyleValueForProperty(
200
+ selection,
201
+ "color",
202
+ // @ts-expect-error ignore
203
+ new String("#000"),
204
+ ),
222
205
  );
223
206
  setBgColor(
224
207
  $getSelectionStyleValueForProperty(
225
208
  selection,
226
209
  "background-color",
227
- "#fff",
210
+ // @ts-expect-error ignore
211
+ new String("#fff"),
228
212
  ),
229
213
  );
230
- setFontFamily(
231
- $getSelectionStyleValueForProperty(selection, "font-family", "inherit"),
232
- );
214
+ setFontFamily($getSelectionStyleValueForProperty(selection, "font-family", "inherit"));
233
215
  }
234
- }, [activeEditor]);
216
+ }, [activeEditor, editor]);
235
217
 
236
218
  useEffect(() => {
237
219
  return editor.registerCommand(
238
- FOCUS_COMMAND,
220
+ SELECTION_CHANGE_COMMAND,
239
221
  (_payload, newEditor) => {
240
222
  setActiveEditor(newEditor);
223
+ setCurrentSelection($getSelection());
224
+ $updateToolbar();
241
225
  return false;
242
226
  },
243
227
  COMMAND_PRIORITY_CRITICAL,
244
228
  );
245
- }, [editor]);
229
+ }, [editor, $updateToolbar]);
246
230
 
247
231
  useEffect(() => {
248
- return activeEditor.registerCommand(
249
- SELECTION_CHANGE_COMMAND,
250
- (_payload, newEditor) => {
251
- $updateToolbar();
252
- return false;
253
- },
254
- COMMAND_PRIORITY_CRITICAL,
255
- );
232
+ activeEditor.getEditorState().read(() => {
233
+ $updateToolbar();
234
+ });
256
235
  }, [activeEditor, $updateToolbar]);
257
236
 
258
237
  useEffect(() => {
@@ -286,16 +265,15 @@ export const useFormatWatcher = () => {
286
265
 
287
266
  const applyStyleText = useCallback(
288
267
  (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);
268
+ // activeEditor.focus();
269
+ activeEditor.update(() => {
270
+ if (!isNil(currentSelection)) {
271
+ $setSelection(currentSelection.clone());
272
+ $patchStyleText(currentSelection, styles);
273
+ }
274
+ });
297
275
  },
298
- [editor],
276
+ [activeEditor, currentSelection],
299
277
  );
300
278
 
301
279
  const formatBulletList = () => {
@@ -337,7 +315,7 @@ export const useFormatWatcher = () => {
337
315
  }
338
316
  });
339
317
  }, 100);
340
- }, [activeEditor, blockType]);
318
+ }, [activeEditor]);
341
319
 
342
320
  const formatQuote = useCallback(() => {
343
321
  if (blockType !== "quote") {
@@ -432,23 +410,20 @@ export const useFormatWatcher = () => {
432
410
 
433
411
  const onSizeChange = useCallback(
434
412
  (value: string) => {
435
- startTransition(() => setFontColor(value));
436
413
  applyStyleText({ "font-size": +value + "px" });
437
414
  },
438
415
  [applyStyleText],
439
416
  );
440
417
 
441
418
  const onFontColorSelect = useCallback(
442
- (value: string) => {
443
- startTransition(() => setFontColor(value));
419
+ (value: string = "#000000") => {
444
420
  applyStyleText({ color: value });
445
421
  },
446
422
  [applyStyleText],
447
423
  );
448
424
 
449
425
  const onBgColorSelect = useCallback(
450
- (value: string) => {
451
- startTransition(() => setBgColor(value));
426
+ (value: string = "#ffffff") => {
452
427
  applyStyleText({ "background-color": value });
453
428
  },
454
429
  [applyStyleText],
@@ -484,6 +459,7 @@ export const useFormatWatcher = () => {
484
459
  canUndo,
485
460
  canRedo,
486
461
  isRTL,
462
+ align,
487
463
  isEditable,
488
464
  codeLanguage,
489
465
  activeEditor,
@@ -0,0 +1,18 @@
1
+ {
2
+ "addComment": "أضف تعليقا",
3
+ "reply": "رد",
4
+ "tool": {
5
+ "alignCenter": "محاذاة المركز",
6
+ "alignEnd": "محاذاة النهاية",
7
+ "alignJustify": "محاذاة وضبط",
8
+ "alignStart": "محاذاة البداية",
9
+ "bold": "عريض",
10
+ "clear": "مسح التنسيق",
11
+ "hilight": "تسليط الضوء",
12
+ "indentIn": "المسافة البادئة في",
13
+ "indentOut": "مسافة بادئة للخارج",
14
+ "italic": "مائل",
15
+ "strikethrough": "يتوسطه خط",
16
+ "underline": "تسطير"
17
+ }
18
+ }
@@ -0,0 +1,18 @@
1
+ {
2
+ "tool": {
3
+ "alignStart": "Align Start",
4
+ "alignCenter": "Align Center",
5
+ "alignEnd": "Align End",
6
+ "alignJustify": "Align Justify",
7
+ "bold": "Bold",
8
+ "italic": "Italic",
9
+ "underline": "Underline",
10
+ "strikethrough": "Strikethrough",
11
+ "indentOut": "Indent Out",
12
+ "indentIn": "Indent In",
13
+ "hilight": "Highlight",
14
+ "clear": "Clear Formatting"
15
+ },
16
+ "addComment": "Add comment",
17
+ "reply": "Reply"
18
+ }
@@ -0,0 +1,28 @@
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 { addTranslationBundle } from "@react-fabric/core";
25
+ import ar from "./ar/lexical.json";
26
+ import en from "./en/lexical.json";
27
+
28
+ addTranslationBundle("lexical", { en, ar });
package/src/index.ts CHANGED
@@ -1,24 +1,31 @@
1
- /*
1
+ /**
2
2
  * React Fabric
3
- * @version: 1.0.0
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
4
6
  *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
5
13
  *
6
- * The MIT License (MIT)
7
- * Copyright (c) 2024 Adarsh Pastakia
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
8
16
  *
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.
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
22
24
  */
23
25
 
26
+ export const LEXICAL_PACKAGE = "@react-fabric/lexical";
27
+ export const LEXICAL_VERSION = "1.0.0";
28
+
29
+ import "./i18n";
30
+
24
31
  export { LexicalEditor } from "./editor/LexicalEditor";