feat: add image handling capabilities to rich text editor with drag-and-drop support, resizing, and markdown integration
This commit is contained in:
49
src/editor/plugins/DragDropPastePlugin.tsx
Normal file
49
src/editor/plugins/DragDropPastePlugin.tsx
Normal file
@@ -0,0 +1,49 @@
|
||||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
||||
import { DRAG_DROP_PASTE } from '@lexical/rich-text';
|
||||
import { isMimeType, mediaFileReader } from '@lexical/utils';
|
||||
import { COMMAND_PRIORITY_LOW } from 'lexical';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { INSERT_IMAGE_COMMAND } from './ImagesPlugin';
|
||||
|
||||
const ACCEPTABLE_IMAGE_TYPES = [
|
||||
'image/',
|
||||
'image/heic',
|
||||
'image/heif',
|
||||
'image/gif',
|
||||
'image/webp',
|
||||
'image/png',
|
||||
'image/jpeg',
|
||||
'image/jpg',
|
||||
'image/svg+xml',
|
||||
];
|
||||
|
||||
export default function DragDropPastePlugin(): null {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
|
||||
useEffect(() => {
|
||||
return editor.registerCommand(
|
||||
DRAG_DROP_PASTE,
|
||||
(files) => {
|
||||
(async () => {
|
||||
const filesResult = await mediaFileReader(
|
||||
files,
|
||||
[ACCEPTABLE_IMAGE_TYPES].flatMap((x) => x),
|
||||
);
|
||||
for (const { file, result } of filesResult) {
|
||||
if (isMimeType(file, ACCEPTABLE_IMAGE_TYPES)) {
|
||||
editor.dispatchCommand(INSERT_IMAGE_COMMAND, {
|
||||
altText: file.name,
|
||||
src: result,
|
||||
});
|
||||
}
|
||||
}
|
||||
})();
|
||||
return true;
|
||||
},
|
||||
COMMAND_PRIORITY_LOW,
|
||||
);
|
||||
}, [editor]);
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user