2024-02-22 21:46:42 -05:00
|
|
|
|
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
|
|
|
import {Fragment, useEffect, useState} from "react";
|
|
|
|
|
import {importFile, overWriteFile, saveFileWithName} from "../../../../utils/File";
|
|
|
|
|
import {isEmpty} from "../../../../utils/ObjectUtils";
|
|
|
|
|
import {CLEAR_HISTORY_COMMAND} from "lexical";
|
|
|
|
|
import {TRANSFORMERS, $convertFromMarkdownString, $convertToMarkdownString,} from "@lexical/markdown";
|
2024-03-04 04:38:31 -05:00
|
|
|
|
import {getFileExtByPath, getFileFullNameByPath, getFileNameByPath} from "../../../../utils/PathOperate";
|
2024-02-22 21:46:42 -05:00
|
|
|
|
import {updatedSavedFile} from "../../../../redux/tableBarItem_reducer";
|
|
|
|
|
import {useDispatch, useSelector} from "react-redux";
|
|
|
|
|
import md5 from "md5"
|
|
|
|
|
import {message} from "antd";
|
|
|
|
|
const {ipcRenderer} = window.require('electron')
|
2024-02-29 04:00:51 -05:00
|
|
|
|
import "./ToobarPlugin.less"
|
2024-03-13 04:21:03 -04:00
|
|
|
|
import {newFileAdd, updateFileMd5} from "../../../../redux/dirMessage_reducer";
|
2024-02-25 20:24:03 -05:00
|
|
|
|
const SaveFilePlugin=(props)=> {
|
2024-02-22 21:46:42 -05:00
|
|
|
|
let activeKey = useSelector(state => state.tableBarItem.activeKey);
|
|
|
|
|
const dispatch = useDispatch();
|
2024-02-27 03:54:04 -05:00
|
|
|
|
// const [messageApi,contextHolder] = message.useMessage();
|
2024-02-22 21:46:42 -05:00
|
|
|
|
const [editor] = useLexicalComposerContext();
|
|
|
|
|
const [editorState,setEditorState]=useState();
|
|
|
|
|
|
|
|
|
|
function onChange(editorState) {
|
2024-03-01 04:46:40 -05:00
|
|
|
|
|
|
|
|
|
if (!isEmpty(props.filePath)&&props.filePath.endsWith(".md")){
|
2024-02-22 21:46:42 -05:00
|
|
|
|
let read = editorState.read(() => $convertToMarkdownString(TRANSFORMERS));
|
|
|
|
|
setEditorState(read)
|
|
|
|
|
}else {
|
|
|
|
|
// Call toJSON on the EditorState object, which produces a serialization safe string
|
|
|
|
|
const editorStateJSON = editorState.toJSON();
|
|
|
|
|
console.log('onChange-editorStateJSON')
|
|
|
|
|
// However, we still have a JavaScript object, so we need to convert it to an actual string with JSON.stringify
|
|
|
|
|
setEditorState(JSON.stringify(editorStateJSON));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const saveFile = (event, args) => {
|
|
|
|
|
console.log("event,args:", event, args)
|
|
|
|
|
if (args !== "CTRL+S") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let filePath = props.filePath;
|
2024-03-01 04:46:40 -05:00
|
|
|
|
if (!isEmpty(filePath)&&filePath !== activeKey) {
|
2024-02-22 21:46:42 -05:00
|
|
|
|
console.log("文件不同", filePath, activeKey)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log("触发保存filePath:", filePath)
|
|
|
|
|
if (isEmpty(editorState)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let resultSave;
|
2024-03-01 04:46:40 -05:00
|
|
|
|
|
2024-02-22 21:46:42 -05:00
|
|
|
|
|
|
|
|
|
// 如果文件地址为空需要用户选择目录并设置文件。
|
|
|
|
|
if (!filePath) {
|
|
|
|
|
let saveDialogReturnValuePromise = saveFileWithName();
|
|
|
|
|
console.log("saveDialogReturnValuePromise", saveDialogReturnValuePromise)
|
|
|
|
|
saveDialogReturnValuePromise.then(result => {
|
|
|
|
|
if (!result.canceled) {
|
2024-03-01 04:46:40 -05:00
|
|
|
|
let filePath =result.filePath
|
|
|
|
|
let fileExt = getFileExtByPath(result.filePath)
|
|
|
|
|
if (isEmpty(fileExt)){
|
|
|
|
|
filePath = filePath+".lexical"
|
|
|
|
|
}
|
|
|
|
|
if (filePath.endsWith(".md")){
|
|
|
|
|
resultSave=editorState
|
|
|
|
|
}else {
|
|
|
|
|
const editorStateSave = {"editorState": JSON.parse(editorState)};
|
|
|
|
|
resultSave = JSON.stringify(editorStateSave);
|
2024-02-22 21:46:42 -05:00
|
|
|
|
}
|
2024-03-01 04:46:40 -05:00
|
|
|
|
overWriteFile(filePath, resultSave)
|
2024-02-22 21:46:42 -05:00
|
|
|
|
// 修改当前文件名
|
2024-03-01 04:46:40 -05:00
|
|
|
|
dispatch(updatedSavedFile({filePath: filePath}))
|
2024-02-22 21:46:42 -05:00
|
|
|
|
// 文件目录更新
|
2024-03-13 04:21:03 -04:00
|
|
|
|
dispatch(newFileAdd({fileName:getFileFullNameByPath(filePath),dirFlag:false,children:[],filePath: filePath,
|
|
|
|
|
fileId:filePath,fileMd5:md5(resultSave)}))
|
2024-02-22 21:46:42 -05:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-01 04:46:40 -05:00
|
|
|
|
if (props.filePath.endsWith(".md")){
|
|
|
|
|
resultSave=editorState
|
|
|
|
|
}else {
|
|
|
|
|
const editorStateSave = {"editorState": JSON.parse(editorState)};
|
|
|
|
|
resultSave = JSON.stringify(editorStateSave);
|
|
|
|
|
}
|
2024-03-13 04:21:03 -04:00
|
|
|
|
|
|
|
|
|
// 后期不再读取文件,直接读取文件MD5
|
2024-02-22 21:46:42 -05:00
|
|
|
|
importFile(filePath).then(value => {
|
2024-03-26 20:46:34 -04:00
|
|
|
|
let save =true;
|
2024-03-14 02:27:51 -04:00
|
|
|
|
let oldFileMd5
|
|
|
|
|
let newFileMd5 =md5(resultSave)
|
2024-04-09 21:21:50 -04:00
|
|
|
|
if (!isEmpty(value)){
|
2024-03-26 20:46:34 -04:00
|
|
|
|
if (props.filePath.endsWith(".md")){
|
|
|
|
|
// editorState
|
|
|
|
|
oldFileMd5 = md5(value.toString());
|
|
|
|
|
save = (isEmpty(value)) || newFileMd5 !== oldFileMd5;
|
|
|
|
|
}else {
|
|
|
|
|
oldFileMd5 = md5(JSON.stringify(JSON.parse(value.toString())));
|
|
|
|
|
save = (isEmpty(value)) || newFileMd5 !== oldFileMd5;
|
|
|
|
|
}
|
2024-02-22 21:46:42 -05:00
|
|
|
|
}
|
|
|
|
|
if (save) {
|
2024-03-13 04:21:03 -04:00
|
|
|
|
overWriteFile(filePath, resultSave).then(()=>{
|
|
|
|
|
console.log("保存成功"+ filePath)
|
|
|
|
|
// 修改文件Md5
|
|
|
|
|
dispatch(updateFileMd5({filePath,"fileMd5":newFileMd5}))
|
|
|
|
|
// messageApi.open({type:"success",content:"保存成功:" + filePath,duration:1})
|
|
|
|
|
message.success("保存成功:" + filePath)
|
|
|
|
|
})
|
2024-02-22 21:46:42 -05:00
|
|
|
|
}
|
|
|
|
|
}).catch(error =>
|
|
|
|
|
console.error(error)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
ipcRenderer.on("pushHotkeys", saveFile);
|
|
|
|
|
let updateListener = editor.registerUpdateListener(({editorState}) => {
|
|
|
|
|
console.log("SaveFilePlugin:editorState",editorState)
|
|
|
|
|
onChange(editorState);
|
|
|
|
|
})
|
|
|
|
|
return () => {
|
|
|
|
|
console.log("销毁取消监听");
|
|
|
|
|
updateListener()
|
|
|
|
|
ipcRenderer.removeListener("pushHotkeys", saveFile)
|
|
|
|
|
};
|
|
|
|
|
},[editor,onChange]
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-02-25 20:24:03 -05:00
|
|
|
|
export default SaveFilePlugin
|