assistant-note/src/redux/tableBarItem_reducer.js

98 lines
3.8 KiB
JavaScript
Raw Normal View History

2024-02-02 01:40:28 -05:00
import { createSlice } from '@reduxjs/toolkit'
import {isEmpty} from "../utils/ObjectUtils";
2024-02-19 23:38:49 -05:00
import {getFileFullNameByPath} from "../utils/PathOperate";
2024-02-02 01:40:28 -05:00
2024-02-04 05:11:14 -05:00
/**
* {
* label: e.node.title,
* children: e.node.key,
* key: e.node.key,
* activeKey:e.node.key
* }
*/
2024-02-02 01:40:28 -05:00
export const tableBarItemSlice = createSlice({
name: 'tableBarItem',
initialState: {
type:"tableBarItem",
2024-02-03 20:29:00 -05:00
data: [],
2024-02-06 20:46:28 -05:00
activeKey:"",
expandedKeyList:[]
2024-02-02 01:40:28 -05:00
},
reducers: {
2024-02-03 20:29:00 -05:00
addTableBarItem: (state, action) => {
2024-02-06 03:03:26 -05:00
console.log("tableBarItemSlice:addTableBarItem", state, action)
2024-02-03 20:29:00 -05:00
if (state.data.filter(file=>file.key===action.payload.key).length===0){
2024-02-06 03:03:26 -05:00
state.data.push({"label":action.payload.label,"key":action.payload.key,"children":action.payload.children})
2024-02-03 20:29:00 -05:00
}
if (action.payload.activeKey){
state.activeKey=action.payload.activeKey
}
2024-02-03 20:29:00 -05:00
},
2024-02-03 22:08:49 -05:00
removeTableBarItem: (state, action) => {
console.log("tableBarItemSlice:removeTableBarItem",action.payload)
state.data=state.data.filter(file=>file.key!==action.payload)
if (action.payload.activeKey){
state.activeKey=action.payload.activeKey
}
2024-02-04 05:11:14 -05:00
},
setActiveKey: (state, action) => {
2024-02-03 22:08:49 -05:00
console.log("tableBarItemSlice:setActiveKey",action.payload)
if (action.payload.activeKey){
state.activeKey=action.payload.activeKey
2024-02-03 20:29:00 -05:00
}
2024-02-04 05:11:14 -05:00
},
2024-02-19 03:11:55 -05:00
updateFileName:(state,action)=>{
console.log("tableBarItemSlice:updateFileName", state, action)
let newFilePath = action.payload.newFilePath
let oldFilePath = action.payload.oldFilePath
// 查找旧文件并且修改文件信息
state.data.forEach(file => {
if (file.key === oldFilePath) {
file.key = newFilePath
file.children= newFilePath
2024-02-19 23:38:49 -05:00
file.label = getFileFullNameByPath(newFilePath)
2024-02-19 03:11:55 -05:00
}
})
if (state.activeKey===oldFilePath){
state.activeKey = newFilePath
}
},
2024-02-04 05:11:14 -05:00
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;
2024-02-19 23:38:49 -05:00
file.label = getFileFullNameByPath(action.payload.filePath)
2024-02-04 05:11:14 -05:00
}
})
state.activeKey = action.payload.filePath
2024-02-06 20:46:28 -05:00
},
addExpandedKeys:(state, action)=>{
state.expandedKeyList.push(...action.payload)
state.expandedKeyList=Array.from(new Set([...state.expandedKeyList]))
},
setExpandedKeys:(state, action)=>{
2024-02-21 00:28:03 -05:00
console.log("tableBarItemSlice:setExpandedKeys",action.payload)
2024-02-06 20:46:28 -05:00
state.expandedKeyList=action.payload
},
removeExpandedKeys:(state, action)=>{
state.expandedKeyList=state.expandedKeyList.filter(key=>key!==action.payload)
2024-02-02 01:40:28 -05:00
}
}
})
2024-02-03 22:08:49 -05:00
export const { addTableBarItem,
removeTableBarItem,
2024-02-04 05:11:14 -05:00
setActiveKey,
2024-02-06 20:46:28 -05:00
updatedSavedFile,
setExpandedKeys,
removeExpandedKeys,
2024-02-19 03:11:55 -05:00
addExpandedKeys,
updateFileName
2024-02-06 20:46:28 -05:00
} = tableBarItemSlice.actions
2024-02-02 01:40:28 -05:00
export default tableBarItemSlice.reducer