feat: 保存文集

This commit is contained in:
shixiaohua 2024-01-28 11:48:48 +08:00
parent 1982910745
commit 615c7b30ba
3 changed files with 45 additions and 8 deletions

View File

@ -22,7 +22,7 @@ exports.menuRebuild = (mainWindow) => {
: []),
// { role: 'fileMenu' }
{
label: 'File',
label: '文件',
submenu: [
{
label: '打开目录',

View File

@ -64,7 +64,10 @@ const ItemTree = () => {
useEffect(() => {
store.subscribe(() => {
let fileDirDate = store.getState().dirMessage.data;
console.log("打开目录fileDirDate:", fileDirDate)
if (fileDirDate.length===0){
return
}
console.log("打开目录1fileDirDate:", fileDirDate)
for (let i = 0; i < fileDirDate.length; i++) {
const node = fileDirDate[i];
console.log("node:", node)

View File

@ -28,9 +28,10 @@ import {CLEAR_HISTORY_COMMAND} from "lexical";
import ActionPlugin from "../../../components/ActionPlug"
import {func} from "prop-types";
import store from "../../../redux/store";
import {useSelector} from 'react-redux'
import {SAVE} from "../../../utils/HotkeyConst";
import md5 from "md5"
import {isEmpty} from "../../../utils/ObjectUtils";
function Placeholder() {
return <div className="editor-placeholder">Enter some rich text...</div>;
@ -85,15 +86,22 @@ function ImportFilePlugin(props) {
}
function SaveFilePlugin(props) {
const [editor] = useLexicalComposerContext();
useEffect(() => {
store.subscribe(() => {
console.log("触发保存")
let data = store.getState().pushHotkeys.data;
const editorState = {"editorState":editor.getEditorState()};
console.log("props.editorState",props.editorState)
if (isEmpty(props.editorState)){
return
}
const editorState = {"editorState":JSON.parse(props.editorState)};
let resultSave = JSON.stringify(editorState);
console.log("data === SAVE",data,SAVE,data === SAVE)
if (data === SAVE) {
importFile(props.filePath).then(value => {
if (md5(resultSave)!==md5(JSON.stringify(JSON.parse(value.toString())))){
let save = md5(resultSave)!==md5(JSON.stringify(JSON.parse(value.toString())));
console.log("md5(resultSave)!==md5(JSON.stringify(JSON.parse(value.toString())))",resultSave,save)
if (save){
console.log("保存重写")
overWriteFile(props.filePath, resultSave)
}
@ -103,7 +111,7 @@ function SaveFilePlugin(props) {
}
})
}, editor)
}, props)
}
// JSON
@ -122,6 +130,32 @@ export default function Hlexical(props) {
setEditorState(JSON.stringify(editorStateJSON));
}
store.subscribe(() => {
console.log("触发保存")
let data = store.getState().pushHotkeys.data;
console.log("props.editorState",editorState)
if (isEmpty(editorState)){
return
}
const editorStateSave = {"editorState":JSON.parse(editorState)};
console.log("editorStateSave",editorStateSave)
let resultSave = JSON.stringify(editorStateSave);
console.log("data === SAVE",resultSave,data === SAVE)
if (data === SAVE) {
importFile(props.filePath).then(value => {
let save = md5(resultSave)!==md5(JSON.stringify(JSON.parse(value.toString())));
console.log("md5(resultSave)!==md5(JSON.stringify(JSON.parse(value.toString())))",resultSave,save)
if (save){
console.log("保存重写")
overWriteFile(props.filePath, resultSave)
}
}).catch(error =>
console.error(error)
)
}
})
return (
<LexicalComposer initialConfig={editorConfig}>
<div className="editor-container">
@ -145,7 +179,7 @@ export default function Hlexical(props) {
<MarkdownShortcutPlugin transformers={TRANSFORMERS}/>
<OnChangePlugin onChange={onChange}/>
<ImportFilePlugin filePath={props.filePath}/>
<SaveFilePlugin filePath={props.filePath}/>
{/*<SaveFilePlugin filePath={props.filePath} editorState={editorState} />*/}
{/*<ActionPlugin/>*/}
</div>
</div>