feat:新建文件保存
This commit is contained in:
parent
5feda5bd83
commit
b58e3f62a2
6
main.js
6
main.js
|
@ -1,4 +1,4 @@
|
|||
const {app, Menu, BrowserWindow} = require('electron')
|
||||
const {app, Menu, BrowserWindow,ipcMain,dialog} = require('electron')
|
||||
const path = require('path')
|
||||
const {menuRebuild} = require('./src/comment/TopMenu.js')
|
||||
const createWindow = () => {
|
||||
|
@ -60,6 +60,10 @@ app.whenReady().then(() => {
|
|||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||||
})
|
||||
ipcMain.handle("saveFileWithName",(listen,args)=>{
|
||||
console.log("saveFileWithName")
|
||||
return dialog.showSaveDialog({"title":"保存文件",})
|
||||
})
|
||||
})
|
||||
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常
|
||||
// 对应用程序和它们的菜单栏来说应该时刻保持激活状态,
|
||||
|
|
|
@ -62,7 +62,7 @@ const ItemTree = () => {
|
|||
const [autoExpandParent, setAutoExpandParent] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
store.subscribe(() => {
|
||||
let unsubscribe = store.subscribe(() => {
|
||||
let fileDirDate = store.getState().dirMessage.data;
|
||||
if (fileDirDate.length===0){
|
||||
return
|
||||
|
@ -91,6 +91,7 @@ const ItemTree = () => {
|
|||
console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData)))
|
||||
setDefaultValueState(Array.from(new Set(defaultData)))
|
||||
})
|
||||
return ()=>unsubscribe()
|
||||
}, [])
|
||||
|
||||
const onExpand = (newExpandedKeys) => {
|
||||
|
|
|
@ -17,7 +17,7 @@ import {TRANSFORMERS} from "@lexical/markdown";
|
|||
import "./index.less"
|
||||
import {useEffect, useId, useState} from 'react';
|
||||
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 store from "../../../redux/store";
|
||||
import {SAVE} from "../../../utils/HotkeyConst";
|
||||
|
@ -64,6 +64,7 @@ function OnChangePlugin({onChange}) {
|
|||
function ImportFilePlugin(props) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
useEffect(() => {
|
||||
if (props.filePath){
|
||||
importFile(props.filePath).then(value => {
|
||||
const editorState = editor.parseEditorState(
|
||||
JSON.stringify(JSON.parse(value.toString()).editorState)
|
||||
|
@ -73,6 +74,7 @@ function ImportFilePlugin(props) {
|
|||
}).catch(error =>
|
||||
console.error(error)
|
||||
)
|
||||
}
|
||||
}, [])
|
||||
}
|
||||
|
||||
|
@ -96,23 +98,37 @@ export default function Hlexical(props) {
|
|||
function SaveFilePlugin(props) {
|
||||
useEffect(()=>{
|
||||
let unsubscribe = store.subscribe(() => {
|
||||
console.log("触发保存")
|
||||
console.log("props.editorState", props)
|
||||
let filePath =props.filePath;
|
||||
console.log("触发保存filePath:",filePath)
|
||||
if (isEmpty(props.editorState)) {
|
||||
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)};
|
||||
let resultSave = JSON.stringify(editorStateSave);
|
||||
console.log("store.getState().pushHotkeys", store.getState().pushHotkeys)
|
||||
console.log("lastId", lastId)
|
||||
if ((lastId === "" || lastId !== store.getState().pushHotkeys.id) && store.getState().pushHotkeys.data === SAVE) {
|
||||
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())));
|
||||
console.log("md5(resultSave)!==md5(JSON.stringify(JSON.parse(value.toString())))", resultSave, save)
|
||||
if (save) {
|
||||
console.log("保存重写")
|
||||
overWriteFile(props.filePath, resultSave)
|
||||
overWriteFile(filePath, resultSave)
|
||||
}
|
||||
}).catch(error =>
|
||||
console.error(error)
|
||||
|
|
|
@ -48,7 +48,7 @@ const Note = () => {
|
|||
const newPanes = [...items];
|
||||
newPanes.push({
|
||||
label: 'New Tab',
|
||||
children: 'Content of new Tab',
|
||||
children: <div className="HlexicalName"><Hlexical /></div>,
|
||||
key: newActiveKey,
|
||||
});
|
||||
setItems(newPanes);
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
import {CLEAR_HISTORY_COMMAND} from 'lexical';
|
||||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
||||
// import fileHelper from './fileHelper'
|
||||
const fs = window.require("fs").promises
|
||||
|
||||
const {ipcRenderer,dialog} = window.require('electron')
|
||||
export async function importFile(pathName) {
|
||||
return await fs.readFile(pathName)
|
||||
}
|
||||
|
@ -10,3 +7,7 @@ export async function importFile(pathName) {
|
|||
export async function overWriteFile(filePath,jsonText) {
|
||||
await fs.writeFile(filePath,jsonText,{"encoding":"utf-8"})
|
||||
}
|
||||
|
||||
export async function saveFileWithName(){
|
||||
return ipcRenderer.invoke("saveFileWithName" )
|
||||
}
|
Loading…
Reference in New Issue