From 12351b900790fbf036699114d3cbc377600127a0 Mon Sep 17 00:00:00 2001 From: Innei Date: Sun, 4 Feb 2024 01:15:50 +0800 Subject: [PATCH] fix: reset initial data Signed-off-by: Innei --- .../ui/editor/Milkdown/plugins/Excalidraw.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/components/ui/editor/Milkdown/plugins/Excalidraw.tsx b/src/components/ui/editor/Milkdown/plugins/Excalidraw.tsx index ef0bb4c8da..729f607919 100644 --- a/src/components/ui/editor/Milkdown/plugins/Excalidraw.tsx +++ b/src/components/ui/editor/Milkdown/plugins/Excalidraw.tsx @@ -179,7 +179,7 @@ const ExcalidrawBoard: FC = () => { const nodeCtx = useNodeViewContext() const content = nodeCtx.node.attrs.value - const [initialContent] = useState(content) + const [initialContent, resetInitialContent] = useState(content) const [forceUpdate, key] = useForceUpdate() useEffect(() => { @@ -204,6 +204,10 @@ const ExcalidrawBoard: FC = () => { const [editorOption, setEditorOption] = useAtom(excalidrawOptionAtom) const excalidrawRef = useRef(null) + const alreadyUploadValueFileMap = useRef( + {} as Record, + ).current + const getFinalSaveValue = async (): Promise => { if (editorOption.delta) { const currentData = valueRef.current @@ -250,12 +254,19 @@ const ExcalidrawBoard: FC = () => { const currentData = valueRef.current if (!currentData) return + const hasUploaded = alreadyUploadValueFileMap[currentData] + if (hasUploaded) { + return hasUploaded + } + const file = new File([currentData], 'file.excalidraw', {}) toast.info('正在上传文件') const result = await uploadFileToServer(FileTypeEnum.File, file) toast.success('上传成功') - return `ref:file/${result.name}` + const refName = `ref:file/${result.name}` + alreadyUploadValueFileMap[currentData] = refName + return refName } } return ( @@ -307,6 +318,7 @@ const ExcalidrawBoard: FC = () => { const value = await getFinalSaveValue() if (!value) return nodeCtx.setAttrs({ value }) + resetInitialContent(value) }} >