/
Tiptap.tsx
203 lines (171 loc) 路 5.7 KB
/
Tiptap.tsx
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
import { useEffect, useRef, useState } from 'react'
import { BubbleMenu, EditorContent, JSONContent, useEditor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import { Comment } from '@sereneinserenade/tiptap-comment-extension'
import { v4 } from 'uuid'
import { CommentIcon } from './CommentIcon'
import './Tiptap.scss'
const content: JSONContent = {
type: 'doc',
content: [
{
type: 'heading',
attrs: {
level: 1,
},
content: [
{
type: 'text',
text: 'Hello World!',
},
],
},
],
}
interface Comment {
id: string
content: string
replies: Comment[]
createdAt: Date
}
const getNewComment = (content: string): Comment => {
return {
id: `a${v4()}a`,
content,
replies: [],
createdAt: new Date()
}
}
const Tiptap = () => {
const [comments, setComments] = useState<Comment[]>([])
const [activeCommentId, setActiveCommentId] = useState<string | null>(null)
const commentsSectionRef = useRef<HTMLDivElement | null>(null)
const focusCommentWithActiveId = (id: string) => {
if (!commentsSectionRef.current) return
const commentInput = commentsSectionRef.current.querySelector<HTMLInputElement>(`input#${id}`)
if (!commentInput) return
commentInput.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'center'
})
}
useEffect(
() => {
if (!activeCommentId) return
focusCommentWithActiveId(activeCommentId)
}
, [activeCommentId]
)
const extensions = [
StarterKit,
Comment.configure(
{
HTMLAttributes: {
class: 'my-comment'
},
onCommentActivated: (commentId) => {
setActiveCommentId(commentId)
if (commentId) setTimeout(() => focusCommentWithActiveId(commentId))
}
}
)
]
const editor = useEditor(
{
extensions,
content,
}
)
const setComment = () => {
const newComment = getNewComment('')
setComments([...comments, newComment])
editor?.commands.setComment(newComment.id)
setActiveCommentId(newComment.id)
setTimeout(focusCommentWithActiveId)
}
return (
<div className='flex gap-2 p-8'>
<EditorContent editor={editor} className='prose prose-invert' />
{
editor && (
<>
<section className='flex flex-col gap-2 p-2 border rounded-lg w-96 border-slate-200' ref={commentsSectionRef}>
{
comments.length ? (
comments.map(comment => (
<div
key={comment.id}
className={`flex flex-col gap-4 p-2 border rounded-lg border-slate-400 ${comment.id === activeCommentId ? 'border-blue-400 border-2' : ''} box-border`}
>
<span className='flex items-end gap-2'>
<a href='https://github.com/sereneinserenade' className='font-semibold border-b border-blue-200'>
sereneinserenade
</a>
<span className='text-xs text-slate-400'>
{comment.createdAt.toLocaleDateString()}
</span>
</span>
<input
value={comment.content || ''}
disabled={comment.id !== activeCommentId}
className={`p-2 rounded-lg text-inherit bg-transparent focus:outline-none ${comment.id === activeCommentId ? 'bg-slate-600' : ''}`}
id={comment.id}
onInput={
(event) => {
const value = (event.target as HTMLInputElement).value
setComments(comments.map(comment => {
if (comment.id === activeCommentId) {
return {
...comment,
content: value
}
}
return comment
}))
}
}
onKeyDown={
(event) => {
if (event.key !== 'Enter') return
setActiveCommentId(null)
}
}
/>
{
comment.id === activeCommentId && (
<button
className='rounded-md bg-white/10 px-2.5 py-1.5 text-sm font-semibold text-white shadow-sm hover:bg-white/20'
onClick={() => {
setActiveCommentId(null)
editor.commands.focus()
}}
>
Save
</button>
)
}
</div>
))
) : (
<span className='pt-8 text-center text-slate-400'>
No comments yet
</span>
)
}
</section>
<BubbleMenu editor={editor} className='p-1 border rounded-lg border-slate-400'>
<button
className='rounded-md bg-white/10 px-2.5 py-1.5 text-sm font-semibold text-white shadow-sm hover:bg-white/20'
onClick={setComment}
>
<CommentIcon />
</button>
</BubbleMenu>
</>
)
}
</div>
)
}
export default Tiptap