-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomment.ts
325 lines (250 loc) · 8.18 KB
/
comment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import {
NodeKey,
EditorConfig,
LexicalNode,
$isElementNode,
ElementNode,
DOMConversionMap,
DOMConversionOutput,
RangeSelection,
CommandListenerEditorPriority,
LexicalCommand,
createCommand,
$getSelection,
$setSelection
} from 'lexical'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { addClassNamesToElement, mergeRegister } from '@lexical/utils'
import { useEffect } from 'react'
export interface Comment {
userName: string,
time: string,
content: string,
}
export interface CommentInstance {
uuid: string
textContent: string
comments: Comment[]
}
const selectElementText = (el: EventTarget) => {
const range = document.createRange()
range.selectNode(el as HTMLSpanElement)
const sel = window.getSelection()
sel?.removeAllRanges()
sel?.addRange(range)
}
class CommentNode extends ElementNode {
__commentInstance: CommentInstance;
static getType(): string {
return 'comment';
}
static clone(node: CommentNode): CommentNode {
return new CommentNode(node.__commentInstance, node.__key);
}
constructor(commentInstance: CommentInstance, key?: NodeKey) {
super(key);
this.__commentInstance = commentInstance;
}
createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement {
const element = document.createElement('span');
element.setAttribute('data-comment-instance', JSON.stringify(this.__commentInstance))
// element.addEventListener('click', (e) => e.target && selectElementText(e.target))
addClassNamesToElement(element, config.theme.comment as string);
return element;
}
updateDOM<EditorContext>(prevNode: CommentNode, dom: HTMLElement, config: EditorConfig<EditorContext>): boolean {
const commentSpan: HTMLSpanElement = dom;
const [prevInstance, currentInstance] = [JSON.stringify(prevNode.__commentInstance), JSON.stringify(this.__commentInstance)]
if (prevInstance !== currentInstance) commentSpan.setAttribute('data-comment-instance', currentInstance)
return false;
}
static importDOM(): DOMConversionMap | null {
return {
span: (node: Node) => ({
conversion: convertCommentSpan,
priority: 1,
}),
};
}
getComment(): CommentInstance {
const self = this.getLatest();
return (self as CommentNode).__commentInstance
}
setComment(commentInstance: CommentInstance): void {
const writable = this.getWritable();
(writable as CommentNode).__commentInstance = commentInstance;
}
insertNewAfter(selection: RangeSelection): null | ElementNode {
const element = this.getParentOrThrow().insertNewAfter(selection);
if ($isElementNode(element)) {
const commentNode = $createCommentNode(this.__commentInstance);
(element as ElementNode)?.append(commentNode);
return commentNode;
}
return null;
}
canInsertTextBefore(): false {
return false;
}
canInsertTextAfter(): boolean {
return false;
}
canBeEmpty(): false {
return false;
}
isInline(): true {
return true;
}
}
function convertCommentSpan(domNode: Node): DOMConversionOutput {
let node = null;
if (domNode instanceof HTMLSpanElement) {
const commentInstance = domNode.getAttribute('data-comment-instance')
if (commentInstance) {
const jsonCommentInstance = JSON.parse(commentInstance as string)
node = $createCommentNode(jsonCommentInstance);
}
}
return { node };
}
function $createCommentNode(commentInstance: CommentInstance): CommentNode {
return new CommentNode(commentInstance);
}
function $isCommentNode(node: LexicalNode): boolean {
return node instanceof CommentNode;
}
const UPDATE_COMMENT_COMMAND: LexicalCommand<CommentInstance | null> = createCommand();
const SET_COMMENT_COMMAND: LexicalCommand<CommentInstance | null> = createCommand();
const EditorPriority: CommandListenerEditorPriority = 0;
function setCommentInstance(commentInstance: CommentInstance | null) {
const selection = $getSelection();
if (selection !== null) $setSelection(selection)
const sel = $getSelection();
// IT should look like this
// TODO: figure out what could be solution of the problem that occur when sel.getTextContent() is included
// if (sel !== null && sel.getTextContent()) {
if (sel !== null) {
const nodes = sel.extract();
if (commentInstance === null) {
// Remove CommentNodes
nodes.forEach((node) => {
const parent = node.getParent();
if (parent && $isCommentNode(parent)) {
const children = parent.getChildren();
for (let i = 0; i < children.length; i += 1) parent.insertBefore(children[i])
parent.remove();
}
});
} else {
// Add or merge CommentNodes
if (nodes.length === 1) {
const firstNode = nodes[0];
// if the first node is a CommentNode or if its
// parent is a CommentNode, we update the commentInstance.
if ($isCommentNode(firstNode)) {
(firstNode as CommentNode).setComment(commentInstance);
return;
} else {
const parent = firstNode.getParent();
if (parent && $isCommentNode(parent)) {
// set parent to be the current CommentNode
// so that other nodes in the same parent
// aren't handled separately below.
(parent as CommentNode).setComment(commentInstance);
return;
}
}
}
let prevParent: any = null;
let commentNode: any = null;
nodes.forEach((node) => {
const parent = node.getParent();
if (
parent === commentNode ||
parent === null ||
($isElementNode(node) && !(node as ElementNode).isInline())
) {
return;
}
if (!parent.is(prevParent)) {
prevParent = parent;
commentNode = $createCommentNode(commentInstance);
if ($isCommentNode(parent)) {
if (node.getPreviousSibling() === null) {
parent.insertBefore(commentNode);
} else {
parent.insertAfter(commentNode);
}
} else {
node.insertBefore(commentNode);
}
}
if ($isCommentNode(node)) {
if (commentNode !== null) {
const children = (node as CommentNode).getChildren();
for (let i = 0; i < children.length; i++) commentNode.append(children[i])
}
node.remove();
return;
}
if (commentNode !== null) {
commentNode.append(node);
}
});
}
}
}
function updateCommentInstance(commentInstance: CommentInstance | null) {
// const [editor] = useLexicalComposerContext()
if (!commentInstance) return
const selection = $getSelection();
if (selection !== null) $setSelection(selection)
const sel = $getSelection()
if (sel !== null) {
const nodes = sel.extract()
for (const node of nodes) {
const parent = node.getParent()
let commentNode: CommentNode | undefined = undefined;
if (parent && $isCommentNode(parent)) commentNode = parent as CommentNode
else if (node && $isCommentNode(node)) commentNode = node as CommentNode
const foundNodeWithSameUuid = commentNode?.__commentInstance.uuid === commentInstance.uuid || false
if (foundNodeWithSameUuid) commentNode?.setComment(commentInstance)
}
}
}
export default function CommentPlugin(): null {
const [editor] = useLexicalComposerContext();
useEffect(() => {
if (!editor.hasNodes([CommentNode])) {
throw new Error('CommentPlugin: CommentNode not registered on editor');
}
}, [editor]);
useEffect(() => {
return mergeRegister(
editor.registerCommand(
SET_COMMENT_COMMAND,
(payload: CommentInstance) => {
setCommentInstance(payload);
return true;
},
EditorPriority,
),
editor.registerCommand(
UPDATE_COMMENT_COMMAND,
(payload: CommentInstance) => {
updateCommentInstance(payload);
return true;
},
EditorPriority,
),
)
}, [editor]);
return null;
}
export {
CommentNode,
$createCommentNode,
$isCommentNode,
SET_COMMENT_COMMAND,
UPDATE_COMMENT_COMMAND,
}