feat:新建文件保存

This commit is contained in:
shixiaohua 2024-02-04 18:11:14 +08:00
parent 399fabd282
commit 15408649d0
4 changed files with 37 additions and 17 deletions

View File

@ -23,6 +23,8 @@ import {store} from "../../../redux/store";
import {SAVE} from "../../../utils/HotkeyConst";
import md5 from "md5"
import {isEmpty} from "../../../utils/ObjectUtils";
import {useDispatch} from "react-redux";
import {updatedSavedFile} from "../../../redux/tableBarItem_reducer";
function Placeholder() {
return <div className="editor-placeholder">Enter some rich text...</div>;
@ -87,6 +89,7 @@ export default function Hlexical(props) {
console.log("this.props.filePath:", props.filePath)
const [editorState, setEditorState] = useState("");
const [lastId ,setLastId]= useState("");
const dispstch = useDispatch();
function onChange(editorState) {
// Call toJSON on the EditorState object, which produces a serialization safe string
const editorStateJSON = editorState.toJSON();
@ -111,9 +114,9 @@ export default function Hlexical(props) {
if (!result.canceled){
const editorStateSave = {"editorState": JSON.parse(props.editorState)};
let resultSave = JSON.stringify(editorStateSave);
overWriteFile(result.filePath, resultSave)
overWriteFile(result.filePath, resultSave)
//
dispstch(updatedSavedFile({filePath:result.filePath}))
}
})
return
@ -137,7 +140,7 @@ export default function Hlexical(props) {
});
console.log("return unsubscribe();")
return ()=>unsubscribe();
},[props])
},[])
}

View File

@ -9,7 +9,7 @@ import './index.less'
import {store} from "../../redux/store";
import {isEmpty} from "../../utils/ObjectUtils";
import {useSelector, useDispatch} from "react-redux";
import {addTableBarItem, removeTableBarItem, setActiveKey} from "../../redux/tableBarItem_reducer"
import {addTableBarItem, removeTableBarItem, setActiveKey,updatedSavedFile} from "../../redux/tableBarItem_reducer"
const {Header, Sider, Content} = Layout;
const Note = () => {
@ -40,11 +40,11 @@ const Note = () => {
dispatch(addTableBarItem(
{
label: 'New Tab',
children: "",
// children: "",
key: newActiveKey,
activeKey:newActiveKey
}
));
dispatch(setActiveKey({"activeKey":newActiveKey}));
};
const remove = (targetKey) => {
console.log("remove = (targetKey):",targetKey)

View File

@ -1,21 +1,15 @@
import {configureStore} from '@reduxjs/toolkit'
import historyReducer from './historyRecord_reducer'
import redirectReducer from './redirectUrl_reducer'
import dirMessageReducer from './dirMessage_reducer'
import pushHotkeysReducer from "./pushHotkeys_reducer";
import tableBarItemReducer from "./tableBarItem_reducer";
import {electronStorage} from "../utils/LocalStorage";
import { persistStore, persistReducer } from 'redux-persist'
import {FLUSH, PAUSE, PERSIST, PURGE, REGISTER, REHYDRATE} from "redux-persist/es/constants";
// 持久化配置
const historyRecordPersistConfig = {
key: 'historyRecord',
storage: electronStorage()
}
const redirectUrlPersistConfig = {
key: 'redirectUrl',
storage: electronStorage()
}
const dirMessagePersistConfig = {
key: 'dirMessage',
storage: electronStorage()
@ -31,7 +25,6 @@ const tableBarItemPersistConfig = {
const historyRecordPersistedReducer = persistReducer(historyRecordPersistConfig, historyReducer)
const redirectUrlPersistedReducer = persistReducer(redirectUrlPersistConfig, redirectReducer)
const dirMessagePersistedReducer = persistReducer(dirMessagePersistConfig, dirMessageReducer)
const pushHotkeysPersistedReducer = persistReducer(pushHotkeysPersistConfig, pushHotkeysReducer)
const tableBarItemPersistedReducer = persistReducer(tableBarItemPersistConfig, tableBarItemReducer)
@ -39,7 +32,6 @@ const tableBarItemPersistedReducer = persistReducer(tableBarItemPersistConfig, t
export const store = configureStore({
reducer: {
historyRecord:historyRecordPersistedReducer,
redirectUrl:redirectUrlPersistedReducer,
dirMessage:dirMessagePersistedReducer,
pushHotkeys:pushHotkeysPersistedReducer,
tableBarItem:tableBarItemPersistedReducer,

View File

@ -1,6 +1,14 @@
import { createSlice } from '@reduxjs/toolkit'
import {isEmpty} from "../utils/ObjectUtils";
/**
* {
* label: e.node.title,
* children: e.node.key,
* key: e.node.key,
* activeKey:e.node.key
* }
*/
export const tableBarItemSlice = createSlice({
name: 'tableBarItem',
initialState: {
@ -24,17 +32,34 @@ export const tableBarItemSlice = createSlice({
if (action.payload.activeKey){
state.activeKey=action.payload.activeKey
}
}
,
},
setActiveKey: (state, action) => {
console.log("tableBarItemSlice:setActiveKey",action.payload)
if (action.payload.activeKey){
state.activeKey=action.payload.activeKey
}
},
updatedSavedFile:(state, action)=>{
console.log("tableBarItemSlice:updatedSavedFile",action.payload)
// 如果其中包含了相同的key则是文件覆盖。
if (state.data.filter(file=>file.key===action.payload.filePath).length>0){
state.data= state.data.filter(file=>file.key!==action.payload.filePath)
}
state.data.forEach(file=>{
if (file.key===state.activeKey){
file.children=action.payload.filePath;
file.key = action.payload.filePath;
let split = action.payload.filePath.split("/");
console.log("action.payload.filePath.split()",split)
file.label =split[split.length-1]
}
})
state.activeKey = action.payload.filePath
}
}
})
export const { addTableBarItem,
removeTableBarItem,
setActiveKey } = tableBarItemSlice.actions
setActiveKey,
updatedSavedFile} = tableBarItemSlice.actions
export default tableBarItemSlice.reducer