import { createSlice } from '@reduxjs/toolkit' import {isEmpty} from "../utils/ObjectUtils"; import {getFileFullNameByPath} from "../utils/PathOperate"; /** * { * label: e.node.title, * children: e.node.key, * key: e.node.key, * activeKey:e.node.key * } */ export const tableBarItemSlice = createSlice({ name: 'tableBarItem', initialState: { type:"tableBarItem", data: [], activeKey:"", expandedKeyList:[] }, reducers: { addTableBarItem: (state, action) => { console.log("tableBarItemSlice:addTableBarItem", state, action) if (state.data.filter(file=>file.key===action.payload.key).length===0){ state.data.push({"label":action.payload.label,"key":action.payload.key,"children":action.payload.children}) } if (action.payload.activeKey){ state.activeKey=action.payload.activeKey } }, 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 } }, setActiveKey: (state, action) => { console.log("tableBarItemSlice:setActiveKey",action.payload) if (action.payload.activeKey){ state.activeKey=action.payload.activeKey } }, 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 file.label = getFileFullNameByPath(newFilePath) } }) if (state.activeKey===oldFilePath){ state.activeKey = newFilePath } }, 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; file.label = getFileFullNameByPath(action.payload.filePath) } }) state.activeKey = action.payload.filePath }, addExpandedKeys:(state, action)=>{ state.expandedKeyList.push(...action.payload) state.expandedKeyList=Array.from(new Set([...state.expandedKeyList])) }, setExpandedKeys:(state, action)=>{ console.log("tableBarItemSlice:setExpandedKeys",action.payload) state.expandedKeyList=action.payload }, removeExpandedKeys:(state, action)=>{ state.expandedKeyList=state.expandedKeyList.filter(key=>key!==action.payload) } } }) export const { addTableBarItem, removeTableBarItem, setActiveKey, updatedSavedFile, setExpandedKeys, removeExpandedKeys, addExpandedKeys, updateFileName } = tableBarItemSlice.actions export default tableBarItemSlice.reducer