// 选中上一个
selectPrevComponent: (state: ComponentStateType) => {
const { selectId, componentList } = state
const noHiddenComponentList = componentList.filter(c => !c.isHidden)
const selectedIndex = noHiddenComponentList.findIndex(c => c.fe_id === selectId)
if (selectedIndex < 0) return // 未选中组件
if (selectedIndex <= 0) return // 已经选中了第一个
state.selectId = noHiddenComponentList[selectedIndex - 1].fe_id
},
// 选中下一个
selectNextComponent: (state: ComponentStateType) => {
const { selectId, componentList } = state
const noHiddenComponentList = componentList.filter(c => !c.isHidden)
const selectedIndex = noHiddenComponentList.findIndex(c => c.fe_id === selectId)
if (selectedIndex < 0) return // 未选中组件
if (selectedIndex + 1 === noHiddenComponentList.length) return // 已经选中了最后一个
state.selectId = noHiddenComponentList[selectedIndex + 1].fe_id
}
在reducer 中过滤一下componentList 就可以了