-
Notifications
You must be signed in to change notification settings - Fork 2
/
TextareaWYSIWYG.tsx
375 lines (348 loc) · 10.5 KB
/
TextareaWYSIWYG.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
371
372
373
374
375
/**
* This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course.
* © Copyright Utrecht University (Department of Information and Computing Sciences)
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* This file contains a custom TextareaWYSIWYG component that provides rich text editing functionality.
* It utilizes 'tiptap' and 'react-hook-form' to create a feature-rich textarea component for various use cases.
* The MenuBar component is also defined here to handle toolbar actions and styling.
*/
import React, { useCallback, useEffect, useState } from 'react';
import { Toggle } from '@/src/components/ui/Toggle';
import Link from '@tiptap/extension-link';
import Placeholder from '@tiptap/extension-placeholder';
import { Editor, EditorContent, useEditor } from '@tiptap/react';
import StarterKit from '@tiptap/starter-kit';
import clsx from 'clsx';
import ReactDOM from 'react-dom';
import { FieldError, FieldValues } from 'react-hook-form';
import {
FaBold,
FaCompressAlt,
FaExpandAlt,
FaItalic,
FaLink,
FaListOl,
FaListUl,
FaUnlink,
} from 'react-icons/fa';
type MenuBarProps = {
disabled: boolean;
editor: Editor | null;
isExpanded: boolean;
setIsExpanded: React.Dispatch<React.SetStateAction<boolean>>;
fullScreen?: boolean;
};
/**
* The MenuBar component represents the toolbar for the TextareaWYSIWYG component.
* It contains buttons for applying formatting and other actions.
*
* @param props - The properties for the MenuBar component.
* @param {boolean} props.disabled - Indicates whether the editor is disabled.
* @param {Editor} props.editor - The 'tiptap' Editor instance.
* @param {boolean} props.isExpanded - Indicates whether the editor is in fullscreen mode.
* @param {React.SetStateAction<boolean>} props.setIsExpanded - A function to toggle the editor's fullscreen mode.
* @param {boolean} [props.fullScreen=false] - Optional flag to indicate if the menu bar should be displayed in fullscreen mode.
* @returns A MenuBar React element.
*/
const MenuBar: React.FC<MenuBarProps> = ({
editor,
disabled,
isExpanded,
setIsExpanded,
fullScreen = false,
}) => {
const setLink = useCallback(() => {
const previousUrl = editor?.getAttributes('link').href;
const url = window.prompt('URL', previousUrl);
if (url === null) {
return;
}
if (url === '') {
editor?.chain().focus().extendMarkRange('link').unsetLink().run();
return;
}
if (url.indexOf('http://') === -1 && url.indexOf('https://') === -1) {
editor
?.chain()
.focus()
.extendMarkRange('link')
.setLink({ href: `http://${url}` })
.run();
} else {
editor
?.chain()
.focus()
.extendMarkRange('link')
.setLink({ href: url })
.run();
}
}, [editor]);
if (!editor) {
return null;
}
return (
<div
className={clsx(
'flex w-full flex-wrap items-center justify-between bg-accent px-4 py-2',
fullScreen && 'sticky top-0 z-10',
disabled && 'bg-muted'
)}
>
<div className="flex flex-wrap space-x-1.5">
<Toggle
//isActive={editor.isActive('bold')}
disabled={disabled}
onClick={() => editor.chain().focus().toggleBold().run()}
>
<FaBold />
</Toggle>
<Toggle
//isActive={editor.isActive('italic')}
disabled={disabled}
onClick={() => editor.chain().focus().toggleItalic().run()}
>
<FaItalic />
</Toggle>
<Toggle
//isActive={editor.isActive('link')}
disabled={disabled}
onClick={setLink}
>
<FaLink />
</Toggle>
<Toggle
//isActive={editor.isActive('link')}
disabled={!editor.isActive('link') || disabled}
onClick={() => editor.chain().focus().unsetLink().run()}
>
<FaUnlink />
</Toggle>
<Toggle
//isActive={editor.isActive('bulletList')}
disabled={disabled}
onClick={() => editor.chain().focus().toggleBulletList().run()}
>
<FaListUl />
</Toggle>
<Toggle
//isActive={editor.isActive('orderedList')}
disabled={disabled}
//
onClick={() => editor.chain().focus().toggleOrderedList().run()}
>
<FaListOl />
</Toggle>
</div>
<Toggle disabled={disabled} onClick={() => setIsExpanded(!isExpanded)}>
{isExpanded ? <FaCompressAlt /> : <FaExpandAlt />}
</Toggle>
</div>
);
};
/**
* The TextareaWYSIWYG component represents a styled WYSIWYG textarea element with rich text editing functionality.
* It supports error handling with 'react-hook-form' and can be easily integrated into forms.
*
* @template T - The generic type parameter for FieldValues.
* @param {Object} props - The properties for the TextareaWYSIWYG component.
* @param {string} [props.placeholder=''] - Optional placeholder text for the textarea.
* @param {boolean} [props.disabled=false] - Optional flag to disable the editor.
* @param {(html: string) => void} [props.onBlur] - Optional callback to handle the onBlur event.
* @param {(html: string) => void} [props.onChange] - Optional callback to handle the onChange event.
* @param {string} [props.name='editor'] - Optional name attribute for the textarea.
* @param {string} [props.value=''] - Optional initial value for the textarea.
* @param {FieldError} [props.error] - Optional 'react-hook-form' FieldError object to handle error states.
* @param {() => void} props.setError - A function to set the error state for the textarea.
* @param {() => void} props.clearErrors - A function to clear the error state for the textarea.
* @returns A TextareaWYSIWYG React element.
*/
export type TextareaWYSIWYGProps<T extends FieldValues> = {
placeholder?: string;
disabled?: boolean;
onBlur?: (html: string) => void;
onChange?: (html: string) => void;
name?: string;
value?: string;
error?: FieldError;
setError: () => void;
clearErrors: () => void;
isRequired?: boolean;
};
export const TextareaWYSIWYG = <T extends FieldValues>({
placeholder = '',
disabled = false,
onBlur,
onChange,
name = 'editor',
value = '',
error,
setError,
clearErrors,
isRequired = false,
}: TextareaWYSIWYGProps<T>) => {
// State for handling expanded mode
const [isExpanded, setIsExpanded] = useState(false);
// State for handling focus state
const [isFocused, setIsFocused] = useState(false);
// Initialize tiptap editor
const editor = useEditor(
{
content: value,
editable: !disabled,
extensions: [
StarterKit,
Link,
Placeholder.configure({
placeholder,
}),
],
// Handle onBlur event
onBlur: ({ editor }) => {
if (onBlur) {
onBlur(editor.getHTML());
}
if (isRequired) {
if (isEmptyContent(editor.getHTML())) {
setError();
} else {
clearErrors();
}
}
setIsFocused(false);
},
onUpdate: ({ editor }) => {
if (onChange) {
onChange(editor.getHTML());
}
},
onFocus: () => {
setIsFocused(true);
},
},
[disabled]
);
// Helper function to check if content is empty
const isEmptyContent = (content: string) => {
const strippedContent = content.replace(/<\/?[^>]+(>|$)/g, '').trim();
return strippedContent === '';
};
// Update editor content and handle onChange event
useEffect(() => {
if (editor && value !== editor.getHTML()) {
editor.commands.setContent(value);
}
if (editor && onChange) {
onChange(editor.getHTML());
}
}, [editor, onChange, value]);
// Handle expanded mode and create portal if necessary
if (isExpanded) {
// Handle escape key to exit fullscreen mode
document.onkeydown = (e) => {
if (e.key === 'Escape' && isExpanded) {
setIsExpanded(!isExpanded);
}
};
// Create and render portal for fullscreen mode
let portalNode = document.querySelector('#fullscreen-editor');
if (!portalNode) {
const div = document.createElement('div');
div.id = 'fullscreen-editor';
document.body.appendChild(div);
portalNode = div;
}
const fullScreenEditor = (
<EditorComponent
isExpanded={isExpanded}
disabled={disabled}
error={error}
isFocused={isFocused}
name={name}
editor={editor}
setIsExpanded={setIsExpanded}
/>
);
return ReactDOM.createPortal(fullScreenEditor, portalNode);
}
return (
<EditorComponent
isExpanded={isExpanded}
disabled={disabled}
error={error}
isFocused={isFocused}
name={name}
editor={editor}
setIsExpanded={setIsExpanded}
/>
);
};
const EditorWrapper: React.FC<{
isExpanded: boolean;
disabled: boolean;
error?: FieldError;
isFocused: boolean;
children: React.ReactNode;
}> = ({ isExpanded, disabled, error, isFocused, children }) => {
return (
<div
className={clsx(
isExpanded
? 'fixed top-0 flex h-screen w-screen flex-col overflow-auto bg-highlight text-highlight-foreground'
: 'group w-full overflow-auto rounded-md bg-transparent',
disabled
? 'cursor-not-allowed border-input opacity-50'
: error
? 'border border-destructive ring-destructive'
: 'border border-input ring-primary/50',
isFocused && 'outline-none ring-2 ring-offset-2 ring-offset-background'
)}
>
{children}
</div>
);
};
const EditorComponent = ({
isExpanded,
setIsExpanded,
name,
disabled,
error,
isFocused,
editor,
}: {
isExpanded: boolean;
setIsExpanded: React.Dispatch<React.SetStateAction<boolean>>;
name: string;
disabled: boolean;
error?: FieldError;
isFocused: boolean;
editor: Editor | null;
}) => {
return (
<EditorWrapper
isExpanded={isExpanded}
disabled={disabled}
error={error}
isFocused={isFocused}
>
<div className="sticky top-0 z-10 flex flex-wrap justify-between">
<MenuBar
disabled={disabled}
editor={editor}
fullScreen
isExpanded={isExpanded}
setIsExpanded={setIsExpanded}
/>
</div>
<EditorContent
name={name}
editor={editor}
className="styled-editor-content"
/>
</EditorWrapper>
);
};