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
|
"use client"
import { useEditor, EditorContent } from "@tiptap/react"
import { BubbleMenu } from "@tiptap/react/menus"
import type { Editor } from "@tiptap/core"
import { Markdown } from "@tiptap/markdown"
import { useRef, useEffect, useCallback } from "react"
import { defaultExtensions } from "./extensions"
import { slashCommand } from "./suggestions"
import { Bold, Italic, Code } from "lucide-react"
import { useDebouncedCallback } from "use-debounce"
import { cn } from "@lib/utils"
const extensions = [...defaultExtensions, slashCommand, Markdown]
export function TextEditor({
content: initialContent,
onContentChange,
onSubmit,
}: {
content: string | undefined
onContentChange: (content: string) => void
onSubmit: () => void
}) {
const containerRef = useRef<HTMLDivElement>(null)
const editorRef = useRef<Editor | null>(null)
const onSubmitRef = useRef(onSubmit)
const hasUserEditedRef = useRef(false)
useEffect(() => {
onSubmitRef.current = onSubmit
}, [onSubmit])
const debouncedUpdates = useDebouncedCallback((editor: Editor) => {
if (!hasUserEditedRef.current) return
const json = editor.getJSON()
const markdown = editor.storage.markdown?.manager?.serialize(json) ?? ""
onContentChange?.(markdown)
}, 500)
const editor = useEditor({
extensions,
content: initialContent,
contentType: "markdown",
immediatelyRender: true,
onCreate: ({ editor }) => {
editorRef.current = editor
},
onUpdate: ({ editor }) => {
editorRef.current = editor
debouncedUpdates(editor)
},
editorProps: {
handleKeyDown: (_view, event) => {
if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
event.preventDefault()
onSubmitRef.current?.()
return true
}
hasUserEditedRef.current = true
return false
},
handleTextInput: () => {
hasUserEditedRef.current = true
return false
},
handlePaste: () => {
hasUserEditedRef.current = true
return false
},
handleDrop: () => {
hasUserEditedRef.current = true
return false
},
},
})
useEffect(() => {
if (editor && initialContent) {
hasUserEditedRef.current = false
editor.commands.setContent(initialContent, { contentType: "markdown" })
}
}, [editor, initialContent])
const handleClick = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
const target = e.target as HTMLElement
if (target.closest(".ProseMirror")) {
return
}
if (target.closest("button, a")) {
return
}
const proseMirror = containerRef.current?.querySelector(
".ProseMirror",
) as HTMLElement
if (proseMirror && editorRef.current) {
setTimeout(() => {
proseMirror.focus()
editorRef.current?.commands.focus("end")
}, 0)
}
},
[],
)
useEffect(() => {
return () => {
editor?.destroy()
}
}, [editor])
return (
<>
{/* biome-ignore lint/a11y/useSemanticElements: div is needed as container for editor, cannot use button */}
{/* biome-ignore lint/a11y/useKeyWithClickEvents: we need to use a div to get the focus on the editor */}
<div
role="button"
tabIndex={0}
ref={containerRef}
onClick={handleClick}
className="w-full h-full outline-none prose prose-invert max-w-none [&_.ProseMirror]:outline-none [&_.ProseMirror]:focus:outline-none [&_.ProseMirror-focused]:outline-none text-editor-prose cursor-text"
>
<EditorContent editor={editor} />
</div>
{editor && (
<BubbleMenu
editor={editor}
options={{ placement: "bottom-start", offset: 8 }}
>
<div className="flex items-center gap-1 rounded-[8px] bg-[#1b1f24] p-2 shadow-[0px_4px_20px_0px_rgba(0,0,0,0.25),inset_1px_1px_1px_0px_rgba(255,255,255,0.1)]">
<button
type="button"
onClick={() =>
editor.chain().focus().toggleBold().run()
}
className={cn(
"flex items-center justify-center rounded-[4px] p-1.5 hover:bg-[#2e353d] cursor-pointer text-[#fafafa]",
editor.isActive("bold") && "bg-[#2e353d]",
)}
>
<Bold size={16} />
</button>
<button
type="button"
onClick={() =>
editor.chain().focus().toggleItalic().run()
}
className={cn(
"flex items-center justify-center rounded-[4px] p-1.5 hover:bg-[#2e353d] cursor-pointer text-[#fafafa]",
editor.isActive("italic") && "bg-[#2e353d]",
)}
>
<Italic size={16} />
</button>
<button
type="button"
onClick={() =>
editor.chain().focus().toggleCode().run()
}
className={cn(
"flex items-center justify-center rounded-[4px] p-1.5 hover:bg-[#2e353d] cursor-pointer text-[#fafafa]",
editor.isActive("code") && "bg-[#2e353d]",
)}
>
<Code size={16} />
</button>
</div>
</BubbleMenu>
)}
</>
)
}
|