From 5fcf2dc4f7b5aead564b8bc36e5785a7715834eb Mon Sep 17 00:00:00 2001 From: yuanyxh <15766118362@139.com> Date: Sun, 19 May 2024 09:57:56 +0800 Subject: [PATCH] fix: fix md handle error --- .../md_editor/component/MDContent.tsx | 4 +- .../md_editor/component/MDEditor.tsx | 45 +++++++------------ 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/filehandle/md_editor/component/MDContent.tsx b/src/filehandle/md_editor/component/MDContent.tsx index bd93701..88bb9ef 100644 --- a/src/filehandle/md_editor/component/MDContent.tsx +++ b/src/filehandle/md_editor/component/MDContent.tsx @@ -55,6 +55,8 @@ export const MDContent = forwardRef( () => ({ confirm() { return new Promise((resolve, reject) => { + if (!changed) return resolve(true); + confirm({ title: '温馨提示', content: '是否保存更改?如果不保存,您的更改会丢失。', @@ -80,7 +82,7 @@ export const MDContent = forwardRef( }); } }), - [currentHandle] + [currentHandle, changed] ); const handleSelect = async (handle: FH) => { diff --git a/src/filehandle/md_editor/component/MDEditor.tsx b/src/filehandle/md_editor/component/MDEditor.tsx index 47ebb1e..875355a 100644 --- a/src/filehandle/md_editor/component/MDEditor.tsx +++ b/src/filehandle/md_editor/component/MDEditor.tsx @@ -163,12 +163,11 @@ const MDEditor = forwardRef( function MDEditor(props, ref) { const { currentHandle, changed, onChanged, onSave } = props; - uploadInfo = useMDStore().uploadInfo; - const editorContainerRef = useRef(null); const editorRef = useRef(); const mdStringRef = useRef(''); - const creatingRef = useRef(false); + + uploadInfo = useMDStore().uploadInfo; useImperativeHandle(ref, () => ({ getMarkdown() { @@ -177,32 +176,20 @@ const MDEditor = forwardRef( })); useMemo(() => { - if (creatingRef.current) { - return void 0; - } - - if (editorRef.current) { - editorRef.current.destroy(true); - } - - if (currentHandle) { - creatingRef.current = true; - - currentHandle - .getFile() - .then((file) => file.text()) - .then((markdown) => { - createMDEditor(editorContainerRef.current!, markdown, onUpdate) - .then((value) => { - editorRef.current = value; - mdStringRef.current = markdown; - onChanged(false); - }) - .finally(() => { - creatingRef.current = false; - }); - }); - } + currentHandle + ?.getFile() + .then((file) => file.text()) + .then((markdown) => { + createMDEditor(editorContainerRef.current!, markdown, onUpdate).then( + (value) => { + editorRef.current && editorRef.current.destroy(true); + + editorRef.current = value; + mdStringRef.current = markdown; + onChanged(false); + } + ); + }); return () => { editorRef.current?.destroy(true);