fix(editor): replace RichEditor with clean TipTap component; remove unused import
This commit is contained in:
parent
8cbc9a034a
commit
8f4fbb098f
@ -1,4 +1,4 @@
|
|||||||
import { Box, Button, Stack, TextField, Typography } from '@mui/material';
|
import { Box, Button, Stack, Typography } from '@mui/material';
|
||||||
import AdminLayout from '../layout/AdminLayout';
|
import AdminLayout from '../layout/AdminLayout';
|
||||||
import Recorder from '../features/recorder/Recorder';
|
import Recorder from '../features/recorder/Recorder';
|
||||||
import RichEditor from './RichEditor';
|
import RichEditor from './RichEditor';
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { useEffect } from 'react';
|
import { forwardRef, useEffect, useImperativeHandle } from 'react';
|
||||||
import { EditorContent, useEditor } from '@tiptap/react';
|
import { EditorContent, useEditor } from '@tiptap/react';
|
||||||
import StarterKit from '@tiptap/starter-kit';
|
import StarterKit from '@tiptap/starter-kit';
|
||||||
import Link from '@tiptap/extension-link';
|
import Link from '@tiptap/extension-link';
|
||||||
@ -6,38 +6,39 @@ import Placeholder from '@tiptap/extension-placeholder';
|
|||||||
import Image from '@tiptap/extension-image';
|
import Image from '@tiptap/extension-image';
|
||||||
import { Button, Stack } from '@mui/material';
|
import { Button, Stack } from '@mui/material';
|
||||||
|
|
||||||
export default function RichEditor({
|
export type RichEditorHandle = {
|
||||||
value,
|
insertImage: (src: string, alt?: string) => void;
|
||||||
onChange,
|
};
|
||||||
placeholder,
|
|
||||||
}: {
|
type Props = {
|
||||||
value: string;
|
value: string;
|
||||||
onChange: (html: string) => void;
|
onChange: (html: string) => void;
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
}) {
|
};
|
||||||
|
|
||||||
|
const RichEditor = forwardRef<RichEditorHandle, Props>(({ value, onChange, placeholder }, ref) => {
|
||||||
const editor = useEditor({
|
const editor = useEditor({
|
||||||
extensions: [
|
extensions: [
|
||||||
StarterKit,
|
StarterKit,
|
||||||
Link.configure({ openOnClick: true }),
|
Link.configure({ openOnClick: true }),
|
||||||
Placeholder.configure({ placeholder: placeholder || 'Write something…' }),
|
Placeholder.configure({ placeholder: placeholder ?? 'Write something…' }),
|
||||||
Image.configure({ inline: false, allowBase64: false }),
|
Image.configure({ inline: false, allowBase64: false }),
|
||||||
],
|
],
|
||||||
content: value || '',
|
content: value ?? '',
|
||||||
onUpdate: ({ editor }) => {
|
onUpdate: ({ editor }) => onChange(editor.getHTML()),
|
||||||
const html = editor.getHTML();
|
editorProps: { attributes: { class: 'tiptap-content' } },
|
||||||
onChange(html);
|
|
||||||
},
|
|
||||||
editorProps: {
|
|
||||||
attributes: {
|
|
||||||
class: 'tiptap-content',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// keep external value in sync if it changes (basic)
|
useImperativeHandle(ref, () => ({
|
||||||
|
insertImage: (src: string, alt?: string) => {
|
||||||
|
if (!editor) return;
|
||||||
|
editor.chain().focus().setImage({ src, alt }).run();
|
||||||
|
},
|
||||||
|
}), [editor]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (editor && value !== editor.getHTML()) {
|
if (editor && value !== editor.getHTML()) {
|
||||||
editor.commands.setContent(value || '', { emitUpdate: false });
|
editor.commands.setContent(value ?? '', { emitUpdate: false });
|
||||||
}
|
}
|
||||||
}, [value, editor]);
|
}, [value, editor]);
|
||||||
|
|
||||||
@ -45,7 +46,6 @@ export default function RichEditor({
|
|||||||
<div style={{ border: '1px solid #ddd', borderRadius: 6, padding: 8 }}>
|
<div style={{ border: '1px solid #ddd', borderRadius: 6, padding: 8 }}>
|
||||||
<Stack direction="row" spacing={1} sx={{ mb: 1 }}>
|
<Stack direction="row" spacing={1} sx={{ mb: 1 }}>
|
||||||
<Button size="small" variant="outlined" onClick={async () => {
|
<Button size="small" variant="outlined" onClick={async () => {
|
||||||
try {
|
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
const input = document.createElement('input');
|
const input = document.createElement('input');
|
||||||
input.type = 'file';
|
input.type = 'file';
|
||||||
@ -56,23 +56,17 @@ export default function RichEditor({
|
|||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append('image', file, file.name);
|
fd.append('image', file, file.name);
|
||||||
const res = await fetch('/api/media/image', { method: 'POST', body: fd });
|
const res = await fetch('/api/media/image', { method: 'POST', body: fd });
|
||||||
if (!res.ok) {
|
if (!res.ok) return console.error('Image upload failed', await res.text());
|
||||||
console.error('Image upload failed', await res.text());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
const url: string | undefined = data.url;
|
const url = data.url as string | undefined;
|
||||||
if (url) {
|
if (url) editor.chain().focus().setImage({ src: url, alt: file.name }).run();
|
||||||
editor.chain().focus().setImage({ src: url, alt: file.name }).run();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
input.click();
|
input.click();
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
}}>Insert Image</Button>
|
}}>Insert Image</Button>
|
||||||
</Stack>
|
</Stack>
|
||||||
<EditorContent editor={editor} />
|
<EditorContent editor={editor} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
export default RichEditor;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user