assistant-note/src/redux/tableBarItem_reducer.js

97 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createSlice } from '@reduxjs/toolkit'
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:[],
leftTableOfContents:""
// document.querySelectorAll("[data-node-key='/media/shixiaohua/homedisk/work/202402/note使用记录.lexical']")[0].setAttribute('style','background:black')
},
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)
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
},
editLeftTableOfContents:(state, action)=>{
state.leftTableOfContents=action.payload.leftTableOfContents
}
}
})
export const { addTableBarItem,
removeTableBarItem,
setActiveKey,
updatedSavedFile,
setExpandedKeys,
addExpandedKeys,
updateFileName,
editLeftTableOfContents
} = tableBarItemSlice.actions
export default tableBarItemSlice.reducer