From b58e3f62a2746919b91e15085575dae1973b1e71 Mon Sep 17 00:00:00 2001 From: shixiaohua Date: Thu, 1 Feb 2024 17:05:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=BB=BA=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 6 +++- src/components/ItemTree/index.jsx | 3 +- src/pages/Note/Hlexical/index.jsx | 48 ++++++++++++++++++++----------- src/pages/Note/index.jsx | 2 +- src/utils/File/index.jsx | 9 +++--- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/main.js b/main.js index f07c0c0..9d59803 100644 --- a/main.js +++ b/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 外,当所有窗口都被关闭的时候退出程序。 因此, 通常 // 对应用程序和它们的菜单栏来说应该时刻保持激活状态, diff --git a/src/components/ItemTree/index.jsx b/src/components/ItemTree/index.jsx index 902e1f0..6cef545 100644 --- a/src/components/ItemTree/index.jsx +++ b/src/components/ItemTree/index.jsx @@ -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) => { diff --git a/src/pages/Note/Hlexical/index.jsx b/src/pages/Note/Hlexical/index.jsx index 9d68c67..5768b94 100644 --- a/src/pages/Note/Hlexical/index.jsx +++ b/src/pages/Note/Hlexical/index.jsx @@ -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,15 +64,17 @@ function OnChangePlugin({onChange}) { function ImportFilePlugin(props) { const [editor] = useLexicalComposerContext(); useEffect(() => { - importFile(props.filePath).then(value => { - const editorState = editor.parseEditorState( - JSON.stringify(JSON.parse(value.toString()).editorState) - ); - editor.setEditorState(editorState); - editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined); - }).catch(error => - console.error(error) - ) + if (props.filePath){ + importFile(props.filePath).then(value => { + const editorState = editor.parseEditorState( + JSON.stringify(JSON.parse(value.toString()).editorState) + ); + editor.setEditorState(editorState); + editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined); + }).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) diff --git a/src/pages/Note/index.jsx b/src/pages/Note/index.jsx index a9b504a..0314c22 100644 --- a/src/pages/Note/index.jsx +++ b/src/pages/Note/index.jsx @@ -48,7 +48,7 @@ const Note = () => { const newPanes = [...items]; newPanes.push({ label: 'New Tab', - children: 'Content of new Tab', + children:
, key: newActiveKey, }); setItems(newPanes); diff --git a/src/utils/File/index.jsx b/src/utils/File/index.jsx index f820397..654722e 100644 --- a/src/utils/File/index.jsx +++ b/src/utils/File/index.jsx @@ -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 {ipcRenderer,dialog} = window.require('electron') export async function importFile(pathName) { return await fs.readFile(pathName) } export async function overWriteFile(filePath,jsonText) { await fs.writeFile(filePath,jsonText,{"encoding":"utf-8"}) +} + +export async function saveFileWithName(){ + return ipcRenderer.invoke("saveFileWithName" ) } \ No newline at end of file