-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor-toolbar.tsx
More file actions
389 lines (360 loc) · 10.3 KB
/
editor-toolbar.tsx
File metadata and controls
389 lines (360 loc) · 10.3 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"use client";
import type { Editor } from "@tiptap/react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";
import {
Bold,
Italic,
Underline,
Strikethrough,
Code,
List,
ListOrdered,
Quote,
Minus,
Link,
Image,
Table,
AlignLeft,
AlignCenter,
AlignRight,
Undo2,
Redo2,
Highlighter,
Superscript,
Subscript,
type LucideIcon,
} from "lucide-react";
import { useState } from "react";
import { ImageUploadDialog } from "./image-upload-dialog";
interface EditorToolbarProps {
editor: Editor;
}
export function EditorToolbar({ editor }: EditorToolbarProps) {
const [imageDialogOpen, setImageDialogOpen] = useState(false);
const [linkDialogOpen, setLinkDialogOpen] = useState(false);
const [linkValue, setLinkValue] = useState("");
const hasActiveLink = editor.isActive("link");
function openLinkDialog() {
const previousUrl = editor.getAttributes("link").href;
setLinkValue(previousUrl || "https://");
setLinkDialogOpen(true);
}
function applyLink() {
const url = linkValue.trim();
if (!url) {
editor.chain().focus().extendMarkRange("link").unsetLink().run();
setLinkDialogOpen(false);
return;
}
try {
new URL(url);
} catch {
return;
}
editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run();
setLinkDialogOpen(false);
}
function clearLink() {
editor.chain().focus().extendMarkRange("link").unsetLink().run();
setLinkDialogOpen(false);
}
function insertImage(url: string, alt?: string) {
editor
.chain()
.focus()
.setImage({ src: url, alt: alt ?? "" })
.createParagraphNear()
.run();
}
function insertTable() {
editor
.chain()
.focus()
.insertTable({ rows: 3, cols: 3, withHeaderRow: true })
.run();
}
const currentHeading = editor.isActive("heading", { level: 1 })
? "h1"
: editor.isActive("heading", { level: 2 })
? "h2"
: editor.isActive("heading", { level: 3 })
? "h3"
: editor.isActive("heading", { level: 4 })
? "h4"
: "paragraph";
function setBlock(value: string) {
const chain = editor.chain().focus().clearNodes();
switch (value) {
case "paragraph":
chain.setParagraph().run();
break;
case "h1":
chain.setHeading({ level: 1 }).run();
break;
case "h2":
chain.setHeading({ level: 2 }).run();
break;
case "h3":
chain.setHeading({ level: 3 }).run();
break;
case "h4":
chain.setHeading({ level: 4 }).run();
break;
}
}
return (
<div className="flex flex-wrap items-center gap-0.5 border-b bg-slate-50/80 px-2 py-1.5 rounded-t-lg">
<div className="flex items-center rounded-lg border border-slate-200 bg-white p-0.5 shadow-xs">
{[
["paragraph", "P"],
["h1", "H1"],
["h2", "H2"],
["h3", "H3"],
["h4", "H4"],
].map(([value, label]) => (
<button
key={value}
type="button"
className={`flex h-7 min-w-9 items-center justify-center rounded-md px-2 text-[11px] font-semibold tracking-[0.08em] transition-colors ${
currentHeading === value
? "bg-slate-900 text-white"
: "text-slate-600 hover:bg-slate-100 hover:text-slate-900"
}`}
aria-pressed={currentHeading === value}
onPointerDown={(event) => {
event.preventDefault();
setBlock(value);
}}
onClick={(event) => {
event.preventDefault();
setBlock(value);
}}
>
{label}
</button>
))}
</div>
<ToolbarSep />
{/* Text formatting */}
<ToolbarButton
icon={Bold}
active={editor.isActive("bold")}
onClick={() => editor.chain().focus().toggleBold().run()}
tooltip="Bold (⌘B)"
/>
<ToolbarButton
icon={Italic}
active={editor.isActive("italic")}
onClick={() => editor.chain().focus().toggleItalic().run()}
tooltip="Italic (⌘I)"
/>
<ToolbarButton
icon={Underline}
active={editor.isActive("underline")}
onClick={() => editor.chain().focus().toggleUnderline().run()}
tooltip="Underline (⌘U)"
/>
<ToolbarButton
icon={Strikethrough}
active={editor.isActive("strike")}
onClick={() => editor.chain().focus().toggleStrike().run()}
tooltip="Strikethrough"
/>
<ToolbarButton
icon={Highlighter}
active={editor.isActive("highlight")}
onClick={() => editor.chain().focus().toggleHighlight().run()}
tooltip="Highlight"
/>
<ToolbarButton
icon={Superscript}
active={editor.isActive("superscript")}
onClick={() => editor.chain().focus().toggleSuperscript().run()}
tooltip="Superscript"
/>
<ToolbarButton
icon={Subscript}
active={editor.isActive("subscript")}
onClick={() => editor.chain().focus().toggleSubscript().run()}
tooltip="Subscript"
/>
<ToolbarSep />
{/* Lists */}
<ToolbarButton
icon={List}
active={editor.isActive("bulletList")}
onClick={() => editor.chain().focus().toggleBulletList().run()}
tooltip="Bullet List"
/>
<ToolbarButton
icon={ListOrdered}
active={editor.isActive("orderedList")}
onClick={() => editor.chain().focus().toggleOrderedList().run()}
tooltip="Ordered List"
/>
<ToolbarSep />
{/* Block elements */}
<ToolbarButton
icon={Quote}
active={editor.isActive("blockquote")}
onClick={() => editor.chain().focus().toggleBlockquote().run()}
tooltip="Blockquote"
/>
<ToolbarButton
icon={Code}
active={editor.isActive("codeBlock")}
onClick={() => editor.chain().focus().toggleCodeBlock().run()}
tooltip="Code Block"
/>
<ToolbarButton
icon={Minus}
active={false}
onClick={() => editor.chain().focus().setHorizontalRule().run()}
tooltip="Horizontal Rule"
/>
<ToolbarSep />
{/* Alignment */}
<ToolbarButton
icon={AlignLeft}
active={editor.isActive({ textAlign: "left" })}
onClick={() => editor.chain().focus().setTextAlign("left").run()}
tooltip="Align Left"
/>
<ToolbarButton
icon={AlignCenter}
active={editor.isActive({ textAlign: "center" })}
onClick={() => editor.chain().focus().setTextAlign("center").run()}
tooltip="Align Center"
/>
<ToolbarButton
icon={AlignRight}
active={editor.isActive({ textAlign: "right" })}
onClick={() => editor.chain().focus().setTextAlign("right").run()}
tooltip="Align Right"
/>
<ToolbarSep />
{/* Insert */}
<ToolbarButton
icon={Link}
active={editor.isActive("link")}
onClick={openLinkDialog}
tooltip="Insert Link"
/>
<ToolbarButton
icon={Image}
active={false}
onClick={() => setImageDialogOpen(true)}
tooltip="Insert Image"
/>
<ImageUploadDialog
open={imageDialogOpen}
onOpenChange={setImageDialogOpen}
onInsert={insertImage}
/>
<ToolbarButton
icon={Table}
active={editor.isActive("table")}
onClick={insertTable}
tooltip="Insert Table"
/>
<ToolbarSep />
<div className="ml-auto flex items-center gap-0.5">
<ToolbarButton
icon={Undo2}
active={false}
onClick={() => editor.chain().focus().undo().run()}
tooltip="Undo (⌘Z)"
disabled={!editor.can().undo()}
/>
<ToolbarButton
icon={Redo2}
active={false}
onClick={() => editor.chain().focus().redo().run()}
tooltip="Redo (⌘⇧Z)"
disabled={!editor.can().redo()}
/>
</div>
<Dialog open={linkDialogOpen} onOpenChange={setLinkDialogOpen}>
<DialogContent showCloseButton={false}>
<DialogHeader>
<DialogTitle>{hasActiveLink ? "Edit link" : "Insert link"}</DialogTitle>
<DialogDescription>
Add a full URL for the selected text.
</DialogDescription>
</DialogHeader>
<Input
type="url"
value={linkValue}
onChange={(e) => setLinkValue(e.target.value)}
placeholder="https://example.com"
autoFocus
/>
<DialogFooter>
{hasActiveLink && (
<Button type="button" variant="ghost" onClick={clearLink}>
Remove link
</Button>
)}
<Button
type="button"
variant="outline"
onClick={() => setLinkDialogOpen(false)}
>
Cancel
</Button>
<Button type="button" onClick={applyLink}>
Save link
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}
function ToolbarButton({
icon: Icon,
active,
onClick,
tooltip,
disabled = false,
}: {
icon: LucideIcon;
active: boolean;
onClick: () => void;
tooltip: string;
disabled?: boolean;
}) {
return (
<Tooltip>
<TooltipTrigger
type="button"
className={`inline-flex items-center justify-center h-7 w-7 rounded-md border-0 cursor-pointer ${active ? "bg-slate-200 text-slate-900" : "text-slate-500 bg-transparent hover:bg-slate-100"} ${disabled ? "opacity-50 pointer-events-none" : ""}`}
onClick={onClick}
disabled={disabled}
>
<Icon className="h-3.5 w-3.5" />
</TooltipTrigger>
<TooltipContent side="bottom" className="text-xs">
{tooltip}
</TooltipContent>
</Tooltip>
);
}
function ToolbarSep() {
return <Separator orientation="vertical" className="mx-1 h-5" />;
}