forked from sjdemartini/mui-tiptap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editor.tsx
254 lines (232 loc) · 10.5 KB
/
Editor.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
import Lock from "@mui/icons-material/Lock";
import LockOpen from "@mui/icons-material/LockOpen";
import TextFields from "@mui/icons-material/TextFields";
import { Box, Button, Stack, Typography } from "@mui/material";
import type { EditorOptions } from "@tiptap/core";
import { useCallback, useRef, useState } from "react";
import {
LinkBubbleMenu,
MenuButton,
RichTextEditor,
RichTextReadOnly,
TableBubbleMenu,
insertImages,
type RichTextEditorRef,
} from "../";
import EditorMenuControls from "./EditorMenuControls";
import useExtensions from "./useExtensions";
const exampleContent =
'<h2 style="text-align: center">Hey there 👋</h2><p>This is a <em>basic</em> example of <code>mui-tiptap</code>, which combines <a target="_blank" rel="noopener noreferrer nofollow" href="https://tiptap.dev/">Tiptap</a> with customizable <a target="_blank" rel="noopener noreferrer nofollow" href="https://mui.com/">MUI (Material-UI)</a> styles, plus a suite of additional components and extensions! Sure, there are <strong>all <em>kinds</em> of <s>text</s> <u>formatting</u> options</strong> you’d probably expect from a rich text editor. But wait until you see the <span data-type="mention" data-id="15" data-label="Axl Rose">@Axl Rose</span> mentions and lists:</p><ul><li><p>That’s a bullet list with one …</p></li><li><p>… or two list items.</p></li></ul><p>Isn’t that great? And all of that is editable. <strong><span style="color: #ff9900">But wait, </span><span style="color: #403101"><mark data-color="#ffd699" style="background-color: #ffd699; color: inherit">there’s more!</mark></span></strong> Let’s try a code block:</p><pre><code class="language-css">body {\n display: none;\n}</code></pre><p></p><p>That’s only the tip of the iceberg. Feel free to add and resize images:</p><img height="auto" src="https://picsum.photos/600/400" alt="random image" width="350" style="aspect-ratio: 3 / 2"><p></p><p>Organize information in tables:</p><table><tbody><tr><th colspan="1" rowspan="1"><p>Name</p></th><th colspan="1" rowspan="1"><p>Role</p></th><th colspan="1" rowspan="1"><p>Team</p></th></tr><tr><td colspan="1" rowspan="1"><p>Alice</p></td><td colspan="1" rowspan="1"><p>PM</p></td><td colspan="1" rowspan="1"><p>Internal tools</p></td></tr><tr><td colspan="1" rowspan="1"><p>Bob</p></td><td colspan="1" rowspan="1"><p>Software</p></td><td colspan="1" rowspan="1"><p>Infrastructure</p></td></tr></tbody></table><p></p><p>Or write down your groceries:</p><ul data-type="taskList"><li data-checked="true" data-type="taskItem"><label><input type="checkbox" checked="checked"><span></span></label><div><p>Milk</p></div></li><li data-checked="false" data-type="taskItem"><label><input type="checkbox"><span></span></label><div><p>Eggs</p></div></li><li data-checked="false" data-type="taskItem"><label><input type="checkbox"><span></span></label><div><p>Sriracha</p></div></li></ul><blockquote><p>Wow, that’s amazing. Good work! 👏 <br>— Mom</p></blockquote><p>Give it a try and click around!</p>';
function fileListToImageFiles(fileList: FileList): File[] {
// You may want to use a package like attr-accept
// (https://www.npmjs.com/package/attr-accept) to restrict to certain file
// types.
return Array.from(fileList).filter((file) => {
const mimeType = (file.type || "").toLowerCase();
return mimeType.startsWith("image/");
});
}
export default function Editor() {
const extensions = useExtensions({
placeholder: "Add your own content here...",
});
const rteRef = useRef<RichTextEditorRef>(null);
const [isEditable, setIsEditable] = useState(true);
const [showMenuBar, setShowMenuBar] = useState(true);
const handleNewImageFiles = useCallback(
(files: File[], insertPosition?: number): void => {
if (!rteRef.current?.editor) {
return;
}
// For the sake of a demo, we don't have a server to upload the files to,
// so we'll instead convert each one to a local "temporary" object URL.
// This will not persist properly in a production setting. You should
// instead upload the image files to your server, or perhaps convert the
// images to bas64 if you would like to encode the image data directly
// into the editor content, though that can make the editor content very
// large. You will probably want to use the same upload function here as
// for the MenuButtonImageUpload `onUploadFiles` prop.
const attributesForImageFiles = files.map((file) => ({
src: URL.createObjectURL(file),
alt: file.name,
}));
insertImages({
images: attributesForImageFiles,
editor: rteRef.current.editor,
position: insertPosition,
});
},
[]
);
// Allow for dropping images into the editor
const handleDrop: NonNullable<EditorOptions["editorProps"]["handleDrop"]> =
useCallback(
(view, event, _slice, _moved) => {
if (!(event instanceof DragEvent) || !event.dataTransfer) {
return false;
}
const imageFiles = fileListToImageFiles(event.dataTransfer.files);
if (imageFiles.length > 0) {
const insertPosition = view.posAtCoords({
left: event.clientX,
top: event.clientY,
})?.pos;
handleNewImageFiles(imageFiles, insertPosition);
// Return true to treat the event as handled. We call preventDefault
// ourselves for good measure.
event.preventDefault();
return true;
}
return false;
},
[handleNewImageFiles]
);
// Allow for pasting images
const handlePaste: NonNullable<EditorOptions["editorProps"]["handlePaste"]> =
useCallback(
(_view, event, _slice) => {
if (!event.clipboardData) {
return false;
}
const pastedImageFiles = fileListToImageFiles(
event.clipboardData.files
);
if (pastedImageFiles.length > 0) {
handleNewImageFiles(pastedImageFiles);
// Return true to mark the paste event as handled. This can for
// instance prevent redundant copies of the same image showing up,
// like if you right-click and copy an image from within the editor
// (in which case it will be added to the clipboard both as a file and
// as HTML, which Tiptap would otherwise separately parse.)
return true;
}
// We return false here to allow the standard paste-handler to run.
return false;
},
[handleNewImageFiles]
);
const [submittedContent, setSubmittedContent] = useState("");
return (
<>
<Box
sx={{
// An example of how editor styles can be overridden. In this case,
// setting where the scroll anchors to when jumping to headings. The
// scroll margin isn't built in since it will likely vary depending on
// where the editor itself is rendered (e.g. if there's a sticky nav
// bar on your site).
"& .ProseMirror": {
"& h1, & h2, & h3, & h4, & h5, & h6": {
scrollMarginTop: showMenuBar ? 50 : 0,
},
},
}}
>
<RichTextEditor
ref={rteRef}
extensions={extensions}
content={exampleContent}
editable={isEditable}
editorProps={{
handleDrop: handleDrop,
handlePaste: handlePaste,
}}
renderControls={() => <EditorMenuControls />}
RichTextFieldProps={{
// The "outlined" variant is the default (shown here only as
// example), but can be changed to "standard" to remove the outlined
// field border from the editor
variant: "outlined",
MenuBarProps: {
hide: !showMenuBar,
},
// Below is an example of adding a toggle within the outlined field
// for showing/hiding the editor menu bar, and a "submit" button for
// saving/viewing the HTML content
footer: (
<Stack
direction="row"
spacing={2}
sx={{
borderTopStyle: "solid",
borderTopWidth: 1,
borderTopColor: (theme) => theme.palette.divider,
py: 1,
px: 1.5,
}}
>
<MenuButton
value="formatting"
tooltipLabel={
showMenuBar ? "Hide formatting" : "Show formatting"
}
size="small"
onClick={() =>
setShowMenuBar((currentState) => !currentState)
}
selected={showMenuBar}
IconComponent={TextFields}
/>
<MenuButton
value="formatting"
tooltipLabel={
isEditable
? "Prevent edits (use read-only mode)"
: "Allow edits"
}
size="small"
onClick={() => setIsEditable((currentState) => !currentState)}
selected={!isEditable}
IconComponent={isEditable ? Lock : LockOpen}
/>
<Button
variant="contained"
size="small"
onClick={() => {
setSubmittedContent(
rteRef.current?.editor?.getHTML() ?? ""
);
}}
>
Save
</Button>
</Stack>
),
}}
>
{() => (
<>
<LinkBubbleMenu />
<TableBubbleMenu />
</>
)}
</RichTextEditor>
</Box>
<Typography variant="h5" sx={{ mt: 5 }}>
Saved result:
</Typography>
{submittedContent ? (
<>
<pre style={{ marginTop: 10, overflow: "auto", maxWidth: "100%" }}>
<code>{submittedContent}</code>
</pre>
<Box mt={3}>
<Typography variant="overline" sx={{ mb: 2 }}>
Read-only saved snapshot:
</Typography>
<RichTextReadOnly
content={submittedContent}
extensions={extensions}
/>
</Box>
</>
) : (
<>
Press “Save” above to show the HTML markup for the editor content.
Typically you’d use a similar <code>editor.getHTML()</code> approach
to save your data in a form.
</>
)}
</>
);
}