From 6350cf2179f08a7e2c33060c68176b1af3c9a946 Mon Sep 17 00:00:00 2001 From: yuanyxh <15766118362@139.com> Date: Tue, 21 May 2024 20:17:02 +0800 Subject: [PATCH] fix: md select error --- src/filehandle/md_editor/component/MDEditor.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/filehandle/md_editor/component/MDEditor.tsx b/src/filehandle/md_editor/component/MDEditor.tsx index 32facb5..fd0c302 100644 --- a/src/filehandle/md_editor/component/MDEditor.tsx +++ b/src/filehandle/md_editor/component/MDEditor.tsx @@ -1,4 +1,4 @@ -import { forwardRef, useImperativeHandle, useMemo, useRef } from 'react'; +import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'; import type { FH } from '@/filehandle/utils/fileManager'; @@ -159,6 +159,7 @@ const MDEditor = forwardRef(function MDEditor(p const editorContainerRef = useRef(null); const editorRef = useRef(); const mdStringRef = useRef(''); + const latestHandleRef = useRef(); uploadInfo = useMDStore().uploadInfo; @@ -168,12 +169,18 @@ const MDEditor = forwardRef(function MDEditor(p } })); - useMemo(() => { + useEffect(() => { + latestHandleRef.current = currentHandle; + currentHandle ?.getFile() .then((file) => file.text()) .then((markdown) => { createMDEditor(editorContainerRef.current!, markdown, onUpdate).then((value) => { + if (latestHandleRef.current && latestHandleRef.current !== currentHandle) { + return value.destroy(true); + } + editorRef.current && editorRef.current.destroy(true); editorRef.current = value;