feat:新建文件保存

This commit is contained in:
shixiaohua 2024-02-01 17:05:45 +08:00
parent 5feda5bd83
commit b58e3f62a2
5 changed files with 45 additions and 23 deletions

View File

@ -1,4 +1,4 @@
const {app, Menu, BrowserWindow} = require('electron') const {app, Menu, BrowserWindow,ipcMain,dialog} = require('electron')
const path = require('path') const path = require('path')
const {menuRebuild} = require('./src/comment/TopMenu.js') const {menuRebuild} = require('./src/comment/TopMenu.js')
const createWindow = () => { const createWindow = () => {
@ -60,6 +60,10 @@ app.whenReady().then(() => {
app.on('activate', () => { app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow() if (BrowserWindow.getAllWindows().length === 0) createWindow()
}) })
ipcMain.handle("saveFileWithName",(listen,args)=>{
console.log("saveFileWithName")
return dialog.showSaveDialog({"title":"保存文件",})
})
}) })
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常 // 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常
// 对应用程序和它们的菜单栏来说应该时刻保持激活状态, // 对应用程序和它们的菜单栏来说应该时刻保持激活状态,

View File

@ -62,7 +62,7 @@ const ItemTree = () => {
const [autoExpandParent, setAutoExpandParent] = useState(true); const [autoExpandParent, setAutoExpandParent] = useState(true);
useEffect(() => { useEffect(() => {
store.subscribe(() => { let unsubscribe = store.subscribe(() => {
let fileDirDate = store.getState().dirMessage.data; let fileDirDate = store.getState().dirMessage.data;
if (fileDirDate.length===0){ if (fileDirDate.length===0){
return return
@ -91,6 +91,7 @@ const ItemTree = () => {
console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData))) console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData)))
setDefaultValueState(Array.from(new Set(defaultData))) setDefaultValueState(Array.from(new Set(defaultData)))
}) })
return ()=>unsubscribe()
}, []) }, [])
const onExpand = (newExpandedKeys) => { const onExpand = (newExpandedKeys) => {

View File

@ -17,7 +17,7 @@ import {TRANSFORMERS} from "@lexical/markdown";
import "./index.less" import "./index.less"
import {useEffect, useId, useState} from 'react'; import {useEffect, useId, useState} from 'react';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {importFile, overWriteFile} from "../../../utils/File" import {importFile, overWriteFile, saveFileWithName} from "../../../utils/File"
import {CLEAR_HISTORY_COMMAND} from "lexical"; import {CLEAR_HISTORY_COMMAND} from "lexical";
import store from "../../../redux/store"; import store from "../../../redux/store";
import {SAVE} from "../../../utils/HotkeyConst"; import {SAVE} from "../../../utils/HotkeyConst";
@ -64,15 +64,17 @@ function OnChangePlugin({onChange}) {
function ImportFilePlugin(props) { function ImportFilePlugin(props) {
const [editor] = useLexicalComposerContext(); const [editor] = useLexicalComposerContext();
useEffect(() => { useEffect(() => {
importFile(props.filePath).then(value => { if (props.filePath){
const editorState = editor.parseEditorState( importFile(props.filePath).then(value => {
JSON.stringify(JSON.parse(value.toString()).editorState) const editorState = editor.parseEditorState(
); JSON.stringify(JSON.parse(value.toString()).editorState)
editor.setEditorState(editorState); );
editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined); editor.setEditorState(editorState);
}).catch(error => editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
console.error(error) }).catch(error =>
) console.error(error)
)
}
}, []) }, [])
} }
@ -96,23 +98,37 @@ export default function Hlexical(props) {
function SaveFilePlugin(props) { function SaveFilePlugin(props) {
useEffect(()=>{ useEffect(()=>{
let unsubscribe = store.subscribe(() => { let unsubscribe = store.subscribe(() => {
console.log("触发保存") let filePath =props.filePath;
console.log("props.editorState", props) console.log("触发保存filePath:",filePath)
if (isEmpty(props.editorState)) { if (isEmpty(props.editorState)) {
return return
} }
//
if (!filePath){
let saveDialogReturnValuePromise = saveFileWithName();
console.log("saveDialogReturnValuePromise",saveDialogReturnValuePromise)
saveDialogReturnValuePromise.then(result=>{
if (!result.canceled){
const editorStateSave = {"editorState": JSON.parse(props.editorState)};
let resultSave = JSON.stringify(editorStateSave);
overWriteFile(result.filePath, resultSave)
//
}
})
return
}
const editorStateSave = {"editorState": JSON.parse(props.editorState)}; const editorStateSave = {"editorState": JSON.parse(props.editorState)};
let resultSave = JSON.stringify(editorStateSave); let resultSave = JSON.stringify(editorStateSave);
console.log("store.getState().pushHotkeys", store.getState().pushHotkeys)
console.log("lastId", lastId) console.log("lastId", lastId)
if ((lastId === "" || lastId !== store.getState().pushHotkeys.id) && store.getState().pushHotkeys.data === SAVE) { if ((lastId === "" || lastId !== store.getState().pushHotkeys.id) && store.getState().pushHotkeys.data === SAVE) {
setLastId(store.getState().pushHotkeys.id); setLastId(store.getState().pushHotkeys.id);
importFile(props.filePath).then(value => { importFile(filePath).then(value => {
let save = 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) { if (save) {
console.log("保存重写") console.log("保存重写")
overWriteFile(props.filePath, resultSave) overWriteFile(filePath, resultSave)
} }
}).catch(error => }).catch(error =>
console.error(error) console.error(error)

View File

@ -48,7 +48,7 @@ const Note = () => {
const newPanes = [...items]; const newPanes = [...items];
newPanes.push({ newPanes.push({
label: 'New Tab', label: 'New Tab',
children: 'Content of new Tab', children: <div className="HlexicalName"><Hlexical /></div>,
key: newActiveKey, key: newActiveKey,
}); });
setItems(newPanes); setItems(newPanes);

View File

@ -1,12 +1,13 @@
import {CLEAR_HISTORY_COMMAND} from 'lexical';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
// import fileHelper from './fileHelper'
const fs = window.require("fs").promises const fs = window.require("fs").promises
const {ipcRenderer,dialog} = window.require('electron')
export async function importFile(pathName) { export async function importFile(pathName) {
return await fs.readFile(pathName) return await fs.readFile(pathName)
} }
export async function overWriteFile(filePath,jsonText) { export async function overWriteFile(filePath,jsonText) {
await fs.writeFile(filePath,jsonText,{"encoding":"utf-8"}) await fs.writeFile(filePath,jsonText,{"encoding":"utf-8"})
}
export async function saveFileWithName(){
return ipcRenderer.invoke("saveFileWithName" )
} }