From b9d63dd313a65871c54364edf9242814a917b8a3 Mon Sep 17 00:00:00 2001 From: shixiaohua Date: Fri, 1 Mar 2024 17:46:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=87=E4=BB=B6=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Note/Hlexical/plugins/SaveFilePlugin.js | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/pages/Note/Hlexical/plugins/SaveFilePlugin.js b/src/pages/Note/Hlexical/plugins/SaveFilePlugin.js index 9049d2e..c8db020 100644 --- a/src/pages/Note/Hlexical/plugins/SaveFilePlugin.js +++ b/src/pages/Note/Hlexical/plugins/SaveFilePlugin.js @@ -4,7 +4,7 @@ import {importFile, overWriteFile, saveFileWithName} from "../../../../utils/Fil import {isEmpty} from "../../../../utils/ObjectUtils"; import {CLEAR_HISTORY_COMMAND} from "lexical"; import {TRANSFORMERS, $convertFromMarkdownString, $convertToMarkdownString,} from "@lexical/markdown"; -import {getFileNameByPath} from "../../../../utils/PathOperate"; +import {getFileExtByPath, getFileNameByPath} from "../../../../utils/PathOperate"; import {updatedSavedFile} from "../../../../redux/tableBarItem_reducer"; import {useDispatch, useSelector} from "react-redux"; import md5 from "md5" @@ -19,10 +19,8 @@ const SaveFilePlugin=(props)=> { const [editorState,setEditorState]=useState(); function onChange(editorState) { - if (isEmpty(props.filePath)){ - return - } - if (props.filePath.endsWith(".md")){ + + if (!isEmpty(props.filePath)&&props.filePath.endsWith(".md")){ let read = editorState.read(() => $convertToMarkdownString(TRANSFORMERS)); setEditorState(read) }else { @@ -40,7 +38,7 @@ const SaveFilePlugin=(props)=> { return } let filePath = props.filePath; - if (filePath !== activeKey) { + if (!isEmpty(filePath)&&filePath !== activeKey) { console.log("文件不同", filePath, activeKey) return; } @@ -49,12 +47,7 @@ const SaveFilePlugin=(props)=> { return } let resultSave; - if (props.filePath.endsWith(".md")){ - resultSave=editorState - }else { - const editorStateSave = {"editorState": JSON.parse(editorState)}; - resultSave = JSON.stringify(editorStateSave); - } + // 如果文件地址为空需要用户选择目录并设置文件。 if (!filePath) { @@ -62,19 +55,31 @@ const SaveFilePlugin=(props)=> { console.log("saveDialogReturnValuePromise", saveDialogReturnValuePromise) saveDialogReturnValuePromise.then(result => { if (!result.canceled) { - let fileKey = getFileNameByPath(result.filePath) - if (isEmpty(fileKey)){ - fileKey = fileKey+".lexical" + let filePath =result.filePath + let fileExt = getFileExtByPath(result.filePath) + if (isEmpty(fileExt)){ + filePath = filePath+".lexical" } - overWriteFile(fileKey, resultSave) + if (filePath.endsWith(".md")){ + resultSave=editorState + }else { + const editorStateSave = {"editorState": JSON.parse(editorState)}; + resultSave = JSON.stringify(editorStateSave); + } + overWriteFile(filePath, resultSave) // 修改当前文件名 - dispatch(updatedSavedFile({filePath: result.filePath})) + dispatch(updatedSavedFile({filePath: filePath})) // 文件目录更新 - dispatch() } }) return } + if (props.filePath.endsWith(".md")){ + resultSave=editorState + }else { + const editorStateSave = {"editorState": JSON.parse(editorState)}; + resultSave = JSON.stringify(editorStateSave); + } importFile(filePath).then(value => { let save if (props.filePath.endsWith(".md")){