GH-3090 Add ability to paste image in activity body editor (#3119)

This commit is contained in:
Deepak Kumar 2023-12-26 22:22:59 +05:30 committed by GitHub
parent 00ab07ea62
commit 58a62e8d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,13 +83,50 @@ export const ActivityBodyEditor = ({
? JSON.parse(activity.body)
: undefined,
domAttributes: { editor: { class: 'editor' } },
onEditorContentChange: (editor) => {
onEditorContentChange: (editor: BlockNoteEditor) => {
debounceOnChange(JSON.stringify(editor.topLevelBlocks) ?? '');
},
slashMenuItems,
uploadFile: imagesActivated ? handleUploadAttachment : undefined,
onEditorReady: (editor: BlockNoteEditor) => {
editor.domElement.addEventListener('paste', handleImagePaste);
},
});
const handleImagePaste = async (event: ClipboardEvent) => {
const clipboardItems = event.clipboardData?.items;
if (clipboardItems) {
for (let i = 0; i < clipboardItems.length; i++) {
if (
clipboardItems[i].kind === 'file' &&
clipboardItems[i].type.match('^image/')
) {
const pastedFile = clipboardItems[i].getAsFile();
if (!pastedFile) {
return;
}
const imageUrl = await handleUploadAttachment(pastedFile);
if (imageUrl) {
editor?.insertBlocks(
[
{
type: 'image',
props: {
url: imageUrl,
},
},
],
editor?.getTextCursorPosition().block,
'after',
);
}
}
}
}
};
return (
<StyledBlockNoteStyledContainer>
<BlockEditor editor={editor} />