From 4bebbf3e1d3ccad7b430f28410be773b0436ef7f Mon Sep 17 00:00:00 2001 From: Zeng1998 <1129142694@qq.com> Date: Sun, 11 Dec 2022 19:17:39 +0800 Subject: [PATCH] feat: enable paste multiple resources (#729) * feat: enable paste multiple resources * update * update --- web/src/components/MemoEditor.tsx | 39 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/web/src/components/MemoEditor.tsx b/web/src/components/MemoEditor.tsx index fc9bf022..0054d9f2 100644 --- a/web/src/components/MemoEditor.tsx +++ b/web/src/components/MemoEditor.tsx @@ -151,35 +151,34 @@ const MemoEditor = () => { } }; + const uploadMultiFiles = async (files: FileList) => { + const uploadedResourceList: Resource[] = []; + for (const file of files) { + const resource = await handleUploadResource(file); + if (resource) { + uploadedResourceList.push(resource); + if (editorState.editMemoId) { + await upsertMemoResource(editorState.editMemoId, resource.id); + } + } + } + if (uploadedResourceList.length > 0) { + const resourceList = editorStateService.getState().resourceList; + editorStateService.setResourceList([...resourceList, ...uploadedResourceList]); + } + }; + const handleDropEvent = async (event: React.DragEvent) => { if (event.dataTransfer && event.dataTransfer.files.length > 0) { event.preventDefault(); - const uploadResourceList: Resource[] = []; - for (const file of event.dataTransfer.files) { - const resource = await handleUploadResource(file); - if (resource) { - uploadResourceList.push(resource); - if (editorState.editMemoId) { - await upsertMemoResource(editorState.editMemoId, resource.id); - } - } - } - if (uploadResourceList.length > 0) { - const resourceList = editorStateService.getState().resourceList; - editorStateService.setResourceList([...resourceList, ...uploadResourceList]); - } + await uploadMultiFiles(event.dataTransfer.files); } }; const handlePasteEvent = async (event: React.ClipboardEvent) => { if (event.clipboardData && event.clipboardData.files.length > 0) { event.preventDefault(); - const file = event.clipboardData.files[0]; - const resource = await handleUploadResource(file); - if (resource) { - const resourceList = editorStateService.getState().resourceList; - editorStateService.setResourceList([...resourceList, resource]); - } + await uploadMultiFiles(event.clipboardData.files); } };