@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
@@ -0,0 +1,523 @@
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 { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
25
+ import {
26
+ Badge,
27
+ Button,
28
+ Card,
29
+ Content,
30
+ Flyout,
31
+ Footer,
32
+ Icon,
33
+ Modal,
34
+ useOverlayService,
35
+ type ModalProps,
36
+ } from "@react-fabric/core";
37
+ import { DateDuration } from "@react-fabric/date";
38
+ import { Textarea } from "@react-fabric/form";
39
+ import { cn, compareValues } from "@react-fabric/utilities";
40
+ import {
41
+ $getNearestNodeFromDOMNode,
42
+ $getNodeByKey,
43
+ $getRoot,
44
+ $getState,
45
+ $isElementNode,
46
+ $setState,
47
+ createState,
48
+ type LexicalEditor,
49
+ } from "lexical";
50
+ import { useEffect, useEffectEvent, useRef, useState } from "react";
51
+ import { createPortal } from "react-dom";
52
+ import { useTranslation } from "react-i18next";
53
+ import { Point, Rect, isHTMLElement } from "../utils";
54
+
55
+ const SPACE = 4;
56
+
57
+ const Downward = 1;
58
+ const Upward = -1;
59
+ const Indeterminate = 0;
60
+
61
+ let prevIndex = Infinity;
62
+
63
+ function getCurrentIndex(keysLength: number): number {
64
+ if (keysLength === 0) {
65
+ return Infinity;
66
+ }
67
+ if (prevIndex >= 0 && prevIndex < keysLength) {
68
+ return prevIndex;
69
+ }
70
+
71
+ return Math.floor(keysLength / 2);
72
+ }
73
+
74
+ function getTopLevelNodeKeys(editor: LexicalEditor): string[] {
75
+ return editor.getEditorState().read(() => $getRoot().getChildrenKeys());
76
+ }
77
+
78
+ function getBlockElement(anchorElem: HTMLElement, editor: LexicalEditor, event: MouseEvent): HTMLElement | null {
79
+ const anchorElementRect = anchorElem.getBoundingClientRect();
80
+ const topLevelNodeKeys = getTopLevelNodeKeys(editor);
81
+
82
+ let blockElem: HTMLElement | null = null;
83
+
84
+ editor.getEditorState().read(() => {
85
+ let index = getCurrentIndex(topLevelNodeKeys.length);
86
+ let direction = Indeterminate;
87
+
88
+ while (index >= 0 && index < topLevelNodeKeys.length) {
89
+ const key = topLevelNodeKeys[index];
90
+ const elem = editor.getElementByKey(key);
91
+ if (elem === null) {
92
+ break;
93
+ }
94
+ const point = new Point(event.x, event.y);
95
+ const domRect = Rect.fromDOM(elem);
96
+ const { marginTop, marginBottom } = window.getComputedStyle(elem);
97
+
98
+ const rect = domRect.generateNewRect({
99
+ bottom: domRect.bottom + parseFloat(marginBottom),
100
+ left: anchorElementRect.left,
101
+ right: anchorElementRect.right,
102
+ top: domRect.top - parseFloat(marginTop),
103
+ });
104
+
105
+ const {
106
+ result,
107
+ reason: { isOnTopSide, isOnBottomSide },
108
+ } = rect.contains(point);
109
+
110
+ if (result) {
111
+ blockElem = elem;
112
+ prevIndex = index;
113
+ break;
114
+ }
115
+
116
+ if (direction === Indeterminate) {
117
+ if (isOnTopSide) {
118
+ direction = Upward;
119
+ } else if (isOnBottomSide) {
120
+ direction = Downward;
121
+ } else {
122
+ // stop search block element
123
+ direction = Infinity;
124
+ }
125
+ }
126
+
127
+ index += direction;
128
+ }
129
+ });
130
+
131
+ return blockElem;
132
+ }
133
+
134
+ interface ICommentEntry {
135
+ username: string;
136
+ timestamp: string;
137
+ comment: string;
138
+ }
139
+
140
+ interface IComment extends ICommentEntry {
141
+ lastUpdate: string;
142
+ replies: ICommentEntry[];
143
+ }
144
+
145
+ const commentState = createState<"comment", IComment | null>("comment", {
146
+ parse: (v) => (typeof v === "string" ? (JSON.parse(v) as IComment) : typeof v === "object" ? (v as IComment) : null),
147
+ });
148
+
149
+ function setMenuPosition(targetElem: HTMLElement | null, floatingElem: HTMLElement, anchorElem: HTMLElement) {
150
+ if (!targetElem) {
151
+ floatingElem.style.opacity = "0";
152
+ floatingElem.style.transform = "translate(-10000px, -10000px)";
153
+ return;
154
+ }
155
+
156
+ const targetRect = targetElem.getBoundingClientRect();
157
+ const targetStyle = window.getComputedStyle(targetElem);
158
+ const floatingElemRect = floatingElem.getBoundingClientRect();
159
+ const anchorElementRect = anchorElem.getBoundingClientRect();
160
+
161
+ const top = targetRect.top + (parseInt(targetStyle.lineHeight, 10) - floatingElemRect.height) / 2 - anchorElementRect.top;
162
+
163
+ const left = SPACE;
164
+
165
+ floatingElem.style.opacity = "1";
166
+ floatingElem.style.transform = `translate(-${left}px, ${top}px)`;
167
+ }
168
+
169
+ function useCommentMenu(
170
+ editor: LexicalEditor,
171
+ anchorElem: HTMLElement,
172
+ username: string,
173
+ openForm: (props: { sample: string }) => Promise<string>,
174
+ ): React.ReactElement {
175
+ const scrollerElem = anchorElem.parentElement;
176
+
177
+ const menuRef = useRef<HTMLDivElement>(null);
178
+ const [draggableBlockElem, setDraggableBlockElem] = useState<HTMLElement | null>(null);
179
+
180
+ useEffect(() => {
181
+ function onMouseMove(event: MouseEvent) {
182
+ const target = event.target as HTMLElement;
183
+ if (!isHTMLElement(target)) {
184
+ setDraggableBlockElem(null);
185
+ return;
186
+ }
187
+
188
+ const _draggableBlockElem = getBlockElement(anchorElem, editor, event);
189
+ editor.update(() => {
190
+ const node = _draggableBlockElem && $getNearestNodeFromDOMNode(_draggableBlockElem);
191
+ if (node) {
192
+ const comment = $getState(node, commentState);
193
+ setDraggableBlockElem(comment === null ? _draggableBlockElem : null);
194
+ } else {
195
+ setDraggableBlockElem(null);
196
+ }
197
+ });
198
+ }
199
+
200
+ function onMouseLeave() {
201
+ setDraggableBlockElem(null);
202
+ }
203
+
204
+ scrollerElem?.addEventListener("mousemove", onMouseMove);
205
+ scrollerElem?.addEventListener("mouseleave", onMouseLeave);
206
+
207
+ return () => {
208
+ scrollerElem?.removeEventListener("mousemove", onMouseMove);
209
+ scrollerElem?.removeEventListener("mouseleave", onMouseLeave);
210
+ };
211
+ }, [scrollerElem, anchorElem, editor]);
212
+
213
+ useEffect(() => {
214
+ if (menuRef.current) {
215
+ setMenuPosition(draggableBlockElem, menuRef.current, anchorElem);
216
+ }
217
+ }, [anchorElem, draggableBlockElem]);
218
+
219
+ const addComment = () => {
220
+ editor.update(() => {
221
+ const node = draggableBlockElem && $getNearestNodeFromDOMNode(draggableBlockElem);
222
+
223
+ if (node && $isElementNode(node)) {
224
+ draggableBlockElem.scrollIntoView({
225
+ behavior: "smooth",
226
+ block: "center",
227
+ });
228
+ void openForm({ sample: node.getTextContent().substring(0, 128) }).then((comment) => {
229
+ editor.update(() => {
230
+ if (comment)
231
+ $setState(node, commentState, {
232
+ username,
233
+ comment,
234
+ timestamp: new Date().toISOString(),
235
+ lastUpdate: new Date().toISOString(),
236
+ replies: [],
237
+ });
238
+ });
239
+ return true;
240
+ });
241
+ }
242
+ });
243
+ };
244
+
245
+ return createPortal(
246
+ <>
247
+ <div
248
+ role="none"
249
+ ref={menuRef}
250
+ onClick={addComment}
251
+ className="rounded px-0.5 py-px select-none opacity-0 absolute right-0 top-0 will-change-transform cursor-pointer hover:bg-primary-50"
252
+ >
253
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" className="w-4 h-4 opacity-30 pointer-events-none">
254
+ <title>comment-plus-outline</title>
255
+ <path d="M9,22A1,1 0 0,1 8,21V18H4A2,2 0 0,1 2,16V4C2,2.89 2.9,2 4,2H20A2,2 0 0,1 22,4V16A2,2 0 0,1 20,18H13.9L10.2,21.71C10,21.9 9.75,22 9.5,22V22H9M10,16V19.08L13.08,16H20V4H4V16H10M11,6H13V9H16V11H13V14H11V11H8V9H11V6Z" />
256
+ </svg>
257
+ </div>
258
+ </>,
259
+ anchorElem,
260
+ );
261
+ }
262
+
263
+ function CommentCard({
264
+ sample,
265
+ nodeKey,
266
+ comment,
267
+ active,
268
+ onSelect,
269
+ onReply,
270
+ }: {
271
+ sample: string;
272
+ nodeKey: string;
273
+ comment: IComment;
274
+ active: boolean;
275
+ onSelect: (key: string) => void;
276
+ onReply: (key: string, reply: string) => void;
277
+ }) {
278
+ const { t } = useTranslation("lexical");
279
+ const [reply, setReply] = useState<string>();
280
+ return (
281
+ <Card data-key={nodeKey} selected={active} className="mb-2" onClick={() => onSelect(nodeKey)}>
282
+ <Content>
283
+ <div className="line-clamp-2 mb-4">{sample}</div>
284
+ <div className="text-xs truncate mb-2 text-tint-500">
285
+ <div className="font-medium">{comment.username}</div>
286
+ <DateDuration date={comment.timestamp} />
287
+ </div>
288
+ <div className="text-sm">{comment.comment}</div>
289
+ {comment.replies?.length > 0 && (
290
+ <div className="bg-alternate ps-2 border-s-2 border-s-tint-200">
291
+ {comment.replies.map((reply, idx) => (
292
+ // eslint-disable-next-line @eslint-react/no-array-index-key
293
+ <div key={idx} className="px-2 py-1">
294
+ <div className="text-xs truncate mb-2 text-tint-500">
295
+ <div className="font-medium">{reply.username}</div>
296
+ <DateDuration date={reply.timestamp} />
297
+ </div>
298
+ <div className="text-sm">{reply.comment}</div>
299
+ </div>
300
+ ))}
301
+ </div>
302
+ )}
303
+ </Content>
304
+ <Footer>
305
+ <div className="p-px text-xs bg-default">
306
+ <Textarea
307
+ placeholder={t("reply") + "..."}
308
+ rows={1}
309
+ value={reply ?? ""}
310
+ onChange={setReply}
311
+ onEnterPressed={() => reply && (onReply(nodeKey, reply), setReply(undefined))}
312
+ decorateEnd={
313
+ <Button
314
+ size="sm"
315
+ variant="link"
316
+ icon="icon-[mdi--send]"
317
+ aria-label="reply"
318
+ onClick={() => reply && (onReply(nodeKey, reply), setReply(undefined))}
319
+ />
320
+ }
321
+ />
322
+ </div>
323
+ </Footer>
324
+ </Card>
325
+ );
326
+ }
327
+
328
+ const CommentFlyout = ({
329
+ comments,
330
+ activeKey,
331
+ onClose,
332
+ onSelect,
333
+ onReply,
334
+ }: ModalProps<{
335
+ activeKey?: string;
336
+ onSelect: (key: string) => void;
337
+ onReply: (key: string, reply: string) => void;
338
+ comments: Array<{ sample: string; nodeKey: string; comment: IComment }>;
339
+ }>) => {
340
+ const contentRef = useRef<HTMLDivElement>(null);
341
+ useEffect(() => {
342
+ if (activeKey) {
343
+ const tmr = setTimeout(() => {
344
+ contentRef.current?.querySelector(`[data-key="${activeKey}"]`)?.scrollIntoView({
345
+ behavior: "smooth",
346
+ block: "center",
347
+ });
348
+ }, 100);
349
+
350
+ return () => {
351
+ clearTimeout(tmr);
352
+ };
353
+ }
354
+ }, [activeKey]);
355
+ return (
356
+ <Flyout
357
+ onClose={onClose}
358
+ title="Comments"
359
+ icon="icon-[mdi--comment-text-multiple]"
360
+ hideMask
361
+ align="end"
362
+ size="sm"
363
+ classNames={{ titlebar: "border-b border-primary-500 text-sm font-medium" }}
364
+ >
365
+ <Content ref={contentRef}>
366
+ {comments.map((props) => (
367
+ <CommentCard
368
+ key={props.nodeKey}
369
+ {...props}
370
+ active={activeKey === props.nodeKey}
371
+ onSelect={onSelect}
372
+ onReply={onReply}
373
+ />
374
+ ))}
375
+ </Content>
376
+ </Flyout>
377
+ );
378
+ };
379
+
380
+ function CommentForm({ onClose, sample }: ModalProps & { sample: string }) {
381
+ const { t } = useTranslation("lexical");
382
+ const [comment, setComment] = useState<string>();
383
+ return (
384
+ <Modal onClose={onClose} size="sm" title={t("addComment")}>
385
+ <Content className="grid text-sm">
386
+ <div className="px-2 py-1 line-clamp-2">{sample}</div>
387
+ <Textarea
388
+ placeholder={t("addComment") + "..."}
389
+ rows={4}
390
+ {...{ autoFocus: true }}
391
+ value={comment}
392
+ onChange={setComment}
393
+ onEnterPressed={() => comment && onClose?.(comment)}
394
+ decorateEnd={
395
+ <Button
396
+ size="sm"
397
+ variant="link"
398
+ icon="icon-[mdi--send]"
399
+ aria-label="add comment"
400
+ onClick={() => comment && onClose?.(comment)}
401
+ />
402
+ }
403
+ />
404
+ </Content>
405
+ </Modal>
406
+ );
407
+ }
408
+
409
+ function CommentHandler(editor: LexicalEditor, anchorElem: HTMLElement, username: string) {
410
+ const [comments, setComments] = useState<Array<{ sample: string; nodeKey: string; comment: IComment }>>(() => []);
411
+ const [renderFlyout, openFlyout, closeFlyout] = useOverlayService(CommentFlyout);
412
+ const [renderModal, openModal] = useOverlayService(CommentForm);
413
+
414
+ const scrollIntoView = (key: string) => {
415
+ const el = editor.getElementByKey(key);
416
+ if (el) {
417
+ el.scrollIntoView({
418
+ behavior: "smooth",
419
+ block: "center",
420
+ });
421
+ el.classList.add("editor-comment-hilight");
422
+ setTimeout(() => {
423
+ el.classList.remove("editor-comment-hilight");
424
+ }, 1000);
425
+ }
426
+ };
427
+ const addReply = (key: string, reply: string) => {
428
+ editor.update(() => {
429
+ const node = $getNodeByKey(key);
430
+ if (node) {
431
+ const comment = $getState(node, commentState);
432
+ if (comment) {
433
+ comment.lastUpdate = new Date().toISOString();
434
+ comment.replies.unshift({
435
+ username,
436
+ comment: reply,
437
+ timestamp: new Date().toISOString(),
438
+ });
439
+ $setState(node, commentState, comment);
440
+ }
441
+ }
442
+ });
443
+ };
444
+ const handleOpenFlyout = useEffectEvent((activeKey?: string) => {
445
+ if (!activeKey && closeFlyout) {
446
+ closeFlyout(null);
447
+ }
448
+ void openFlyout({ activeKey });
449
+ });
450
+
451
+ useEffect(() => {
452
+ return editor.registerUpdateListener(({ editorState }) => {
453
+ const comments: Array<{ sample: string; nodeKey: string; comment: IComment }> = [];
454
+ editorState.read(() => {
455
+ $getRoot()
456
+ .getChildren()
457
+ .forEach((node) => {
458
+ const comment = $getState(node, commentState);
459
+ const element = editor.getElementByKey(node.getKey());
460
+ element?.classList.remove("editor-has-comment");
461
+ if (comment !== null && element) {
462
+ element.classList.add("editor-has-comment");
463
+ // eslint-disable-next-line @eslint-react/web-api-no-leaked-event-listener
464
+ element.addEventListener("click", (e) => {
465
+ if (e.offsetX < 0) {
466
+ handleOpenFlyout(node.getKey());
467
+ }
468
+ });
469
+ comments.push({
470
+ nodeKey: node.getKey(),
471
+ sample: node.getTextContent().substring(0, 128),
472
+ comment,
473
+ });
474
+ }
475
+ });
476
+ });
477
+ setComments(comments.sort(compareValues("desc", "comment.lastUpdate")));
478
+ });
479
+ }, [editor]);
480
+ return (
481
+ <>
482
+ {useCommentMenu(editor, anchorElem, username, openModal)}
483
+ <div className="fixed inset-x-0 bottom-0 max-w-[21cm] mx-auto flex justify-end">
484
+ {comments.length > 0 && (
485
+ <Badge value={comments.length} placement="top-start">
486
+ <button
487
+ type="button"
488
+ className="size-fit cursor-pointer"
489
+ onClick={() => {
490
+ void openFlyout();
491
+ }}
492
+ >
493
+ <Icon
494
+ rounded
495
+ icon="icon-[mdi--comment-text-multiple]"
496
+ className={cn(
497
+ "text-2xl outline p-2 -me-4",
498
+ closeFlyout ? "bg-primary-50 text-primary-500" : "bg-dimmed text-muted hover:text-dimmed",
499
+ )}
500
+ />
501
+ </button>
502
+ </Badge>
503
+ )}
504
+ </div>
505
+ {renderFlyout({ onSelect: scrollIntoView, onReply: addReply, comments })}
506
+ {renderModal()}
507
+ </>
508
+ );
509
+ }
510
+
511
+ export const CommentPlugin = ({
512
+ anchorElem = document.body,
513
+ isPublishMode = true,
514
+ username,
515
+ }: {
516
+ anchorElem?: HTMLElement;
517
+ isPublishMode: boolean;
518
+ username: string;
519
+ }): React.ReactElement => {
520
+ const [editor] = useLexicalComposerContext();
521
+ if (isPublishMode) return <></>;
522
+ return CommentHandler(editor, anchorElem, username);
523
+ };