feat:文件保存

This commit is contained in:
shixiaohua 2024-03-01 17:46:40 +08:00
parent 91b72e671f
commit b9d63dd313
1 changed files with 23 additions and 18 deletions

View File

@ -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")){