-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolbar.tsx
370 lines (347 loc) · 11.3 KB
/
Toolbar.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
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
'use client';
import type { Editor } from '@tiptap/react';
import {
AlignCenter,
AlignLeft,
AlignRight,
Bold,
Code,
ImageIcon,
Italic,
List,
ListOrdered,
PaintBucket,
Strikethrough,
Table as TableIcon,
Underline,
} from 'lucide-react';
import { useCallback, useState, type FC } from 'react';
import { Input } from '../ui/input';
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from '../ui/select';
import { Separator } from '../ui/separator';
import { Toggle } from '../ui/toggle';
import { ColorSelect } from './components/color-selector-select';
import { startImageUpload } from './plugins/upload-images';
interface ToolbarProps {
editor: Editor | null;
}
const Toolbar: FC<ToolbarProps> = ({ editor }) => {
if (!editor) {
return null;
}
const headings = [
{
name: 'Paragraph',
command: () =>
editor.chain().focus().toggleNode('paragraph', 'paragraph').run(),
// I feel like there has to be a more efficient way to do this – feel free to PR if you know how!
isActive: () =>
editor.isActive('paragraph') &&
!editor.isActive('bulletList') &&
!editor.isActive('orderedList'),
},
{
name: 'Heading 1',
command: () => editor.chain().focus().toggleHeading({ level: 1 }).run(),
isActive: () => editor.isActive('heading', { level: 1 }),
},
{
name: 'Heading 2',
command: () => editor.chain().focus().toggleHeading({ level: 2 }).run(),
isActive: () => editor.isActive('heading', { level: 2 }),
},
{
name: 'Heading 3',
command: () => editor.chain().focus().toggleHeading({ level: 3 }).run(),
isActive: () => editor.isActive('heading', { level: 3 }),
},
];
const [alignmentText, setAlignmentText] = useState('left');
const setLink = useCallback(() => {
const previousUrl = editor.getAttributes('link').href;
const url = window.prompt('URL', previousUrl);
// cancelled
if (url === null) {
return;
}
// empty
if (url === '') {
editor.chain().focus().extendMarkRange('link').unsetLink().run();
return;
}
// update link
editor.chain().focus().extendMarkRange('link').setLink({ href: url }).run();
}, [editor]);
return (
<div className="flex flex-wrap items-center px-2 py-1 space-x-2 border-b-2 sticky top-0 z-10 bg-background">
<Select
onValueChange={(value) => {
const selectedHeading = headings.find(
(heading) => heading.name === value,
);
selectedHeading?.command();
}}
>
<SelectTrigger className="flex items-center w-fit gap-2 border-none">
<SelectValue defaultValue="Paragraph" placeholder="Paragraph" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
{headings.map((heading, index) => (
<SelectItem key={heading.name} value={heading.name}>
{`${heading.name}heading`}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
<Separator orientation="vertical" className="h-8" />
<Toggle
size="sm"
pressed={editor.isActive('bold')}
onPressedChange={() => editor.chain().focus().toggleBold().run()}
>
<Bold size={16} />
</Toggle>
<Toggle
size="sm"
pressed={editor.isActive('italic')}
onPressedChange={() => editor.chain().focus().toggleItalic().run()}
>
<Italic size={16} />
</Toggle>
<Toggle
size="sm"
pressed={editor.isActive('underline')}
onPressedChange={() => editor.commands.toggleUnderline()}
>
<Underline size={16} />
</Toggle>
<Toggle
size="sm"
pressed={editor.isActive('strike')}
onPressedChange={() => editor.commands.toggleStrike()}
>
<Strikethrough size={16} />
</Toggle>
<Toggle
size="sm"
pressed={editor.isActive('code')}
onPressedChange={() => editor.chain().focus().toggleCode().run()}
>
<Code size={16} />
</Toggle>
<ColorSelect editor={editor} />
<Toggle size="sm">
<div className="relative">
<PaintBucket size={16} />
<Input
type="color"
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onInput={(event: any) =>
editor.chain().focus().setColor(event.target.value).run()
}
value={editor.getAttributes('textStyle').color}
data-testid="setColor"
className="absolute opacity-0 -bottom-3"
/>
</div>
</Toggle>
<Separator orientation="vertical" className="h-8" />
<Select
defaultValue={alignmentText}
onValueChange={(value) => {
setAlignmentText(value);
editor.chain().focus().setTextAlign(value).run();
}}
>
<SelectTrigger className="flex items-center border-none gap-1 p-2 px-2 h-fit focus:ring-0 text-sm font-medium rounded-md text-foreground hover:bg-muted active:bg-muted w-fit">
{alignmentText === 'left' ? (
<AlignLeft size={16} />
) : alignmentText === 'center' ? (
<AlignCenter size={16} />
) : (
<AlignRight size={16} />
)}
</SelectTrigger>
<SelectContent className="">
<SelectGroup>
<SelectItem value="left">
<AlignLeft size={16} />
</SelectItem>
<SelectItem value="center">
<AlignCenter size={16} />
</SelectItem>
<SelectItem value="right">
<AlignRight size={16} />
</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
{/* <DropdownMenu>
<DropdownMenuTrigger className="flex items-center gap-2">
<div className="">
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
className="bi bi-arrow-clockwise"
viewBox="0 0 16 16"
>
<path
fill-rule="evenodd"
d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z"
/>
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z" />
</svg>
</div>
<ChevronDown className="size-4" />
</DropdownMenuTrigger>
<DropdownMenuContent className="">
<DropdownMenuItem>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
className="bi bi-arrow-counterclockwise"
viewBox="0 0 16 16"
>
<path
fill-rule="evenodd"
d="M8 3a5 5 0 1 1-4.546 2.914.5.5 0 0 0-.908-.417A6 6 0 1 0 8 2v1z"
/>
<path d="M8 4.466V.534a.25.25 0 0 0-.41-.192L5.23 2.308a.25.25 0 0 0 0 .384l2.36 1.966A.25.25 0 0 0 8 4.466z" />
</svg>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu> */}
<Toggle
size="sm"
pressed={editor.isActive('bulletList')}
onPressedChange={() => editor.commands.toggleBulletList()}
>
<List size={16} />
</Toggle>
<Toggle
size="sm"
pressed={editor.isActive('orderedList')}
onPressedChange={() => editor.commands.toggleOrderedList()}
>
<ListOrdered size={16} />
</Toggle>
<Separator orientation="vertical" className="h-8" />
<Toggle
size="sm"
pressed={editor.isActive('link')}
onPressedChange={setLink}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
className="bi bi-link"
viewBox="0 0 16 16"
>
<path d="M6.354 5.5H4a3 3 0 0 0 0 6h3a3 3 0 0 0 2.83-4H9c-.086 0-.17.01-.25.031A2 2 0 0 1 7 10.5H4a2 2 0 1 1 0-4h1.535c.218-.376.495-.714.82-1z" />
<path d="M9 5.5a3 3 0 0 0-2.83 4h1.098A2 2 0 0 1 9 6.5h3a2 2 0 1 1 0 4h-1.535a4.02 4.02 0 0 1-.82 1H12a3 3 0 1 0 0-6H9z" />
</svg>
</Toggle>
<Toggle
size="sm"
pressed={editor.isActive('table')}
onClick={() =>
editor
.chain()
.focus()
.insertTable({ rows: 3, cols: 3, withHeaderRow: true })
.run()
}
>
<TableIcon size={16} />
</Toggle>
<div
className="hover:bg-muted p-2 flex items-center justify-center rounded-md"
onClick={() => {
if (!editor) return;
const range = {
from: editor.view.state.selection.from,
to: editor.view.state.selection.to,
};
editor.chain().focus().deleteRange(range).run();
// upload image
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.onchange = async () => {
if (input.files?.length) {
const file = input.files[0];
const pos = editor.view.state.selection.from;
startImageUpload(file, editor.view, pos);
}
};
input.click();
// input.multiple = true; // Allow multiple file selection
// input.onchange = async () => {
// if (input.files?.length) {
// const filesArray = Array.from(input.files);
// setDescriptionImages(filesArray); // Pass the array of files to setDescriptionImages
// }
// };
// input.click();
}}
onKeyDown={(event) => {
if (event.key === 'Enter') {
if (!editor) return;
const range = {
from: editor.view.state.selection.from,
to: editor.view.state.selection.to,
};
editor.chain().focus().deleteRange(range).run();
// upload image
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.onchange = async () => {
if (input.files?.length) {
const file = input.files[0];
const pos = editor.view.state.selection.from;
startImageUpload(file, editor.view, pos);
}
};
input.click();
// input.multiple = true; // Allow multiple file selection
// input.onchange = async () => {
// if (input.files?.length) {
// const filesArray = Array.from(input.files);
// setDescriptionImages(filesArray); // Pass the array of files to setDescriptionImages
// }
// };
// input.click();
}
}}
>
<ImageIcon size={16} />
</div>
{/* <Toggle
size="sm"
pressed={editor.isActive("heading")}
onPressedChange={() =>
editor.chain().focus().toggleHeading({ level: 2 }).run()
}
>
<Heading2 size={16} />
</Toggle> */}
</div>
);
};
export default Toolbar;