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