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 {isEmpty} from "../../../../utils/ObjectUtils";
import {CLEAR_HISTORY_COMMAND} from "lexical"; import {CLEAR_HISTORY_COMMAND} from "lexical";
import {TRANSFORMERS, $convertFromMarkdownString, $convertToMarkdownString,} from "@lexical/markdown"; 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 {updatedSavedFile} from "../../../../redux/tableBarItem_reducer";
import {useDispatch, useSelector} from "react-redux"; import {useDispatch, useSelector} from "react-redux";
import md5 from "md5" import md5 from "md5"
@ -19,10 +19,8 @@ const SaveFilePlugin=(props)=> {
const [editorState,setEditorState]=useState(); const [editorState,setEditorState]=useState();
function onChange(editorState) { function onChange(editorState) {
if (isEmpty(props.filePath)){
return if (!isEmpty(props.filePath)&&props.filePath.endsWith(".md")){
}
if (props.filePath.endsWith(".md")){
let read = editorState.read(() => $convertToMarkdownString(TRANSFORMERS)); let read = editorState.read(() => $convertToMarkdownString(TRANSFORMERS));
setEditorState(read) setEditorState(read)
}else { }else {
@ -40,7 +38,7 @@ const SaveFilePlugin=(props)=> {
return return
} }
let filePath = props.filePath; let filePath = props.filePath;
if (filePath !== activeKey) { if (!isEmpty(filePath)&&filePath !== activeKey) {
console.log("文件不同", filePath, activeKey) console.log("文件不同", filePath, activeKey)
return; return;
} }
@ -49,12 +47,7 @@ const SaveFilePlugin=(props)=> {
return return
} }
let resultSave; let resultSave;
if (props.filePath.endsWith(".md")){
resultSave=editorState
}else {
const editorStateSave = {"editorState": JSON.parse(editorState)};
resultSave = JSON.stringify(editorStateSave);
}
// 如果文件地址为空需要用户选择目录并设置文件。 // 如果文件地址为空需要用户选择目录并设置文件。
if (!filePath) { if (!filePath) {
@ -62,19 +55,31 @@ const SaveFilePlugin=(props)=> {
console.log("saveDialogReturnValuePromise", saveDialogReturnValuePromise) console.log("saveDialogReturnValuePromise", saveDialogReturnValuePromise)
saveDialogReturnValuePromise.then(result => { saveDialogReturnValuePromise.then(result => {
if (!result.canceled) { if (!result.canceled) {
let fileKey = getFileNameByPath(result.filePath) let filePath =result.filePath
if (isEmpty(fileKey)){ let fileExt = getFileExtByPath(result.filePath)
fileKey = fileKey+".lexical" 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 return
} }
if (props.filePath.endsWith(".md")){
resultSave=editorState
}else {
const editorStateSave = {"editorState": JSON.parse(editorState)};
resultSave = JSON.stringify(editorStateSave);
}
importFile(filePath).then(value => { importFile(filePath).then(value => {
let save let save
if (props.filePath.endsWith(".md")){ if (props.filePath.endsWith(".md")){