feat(keyhandler): add onNodeRemove callback

This commit is contained in:
moklick
2019-07-24 16:44:26 +02:00
parent 7b32ee660c
commit 4be80dd985
8 changed files with 166 additions and 43 deletions
+14 -1
View File
@@ -1,7 +1,8 @@
import {
UPDATE_TRANSFORM, UPDATE_SIZE, SET_NODES, SET_EDGES,
UPDATE_NODE_DATA, UPDATE_NODE_POS, INIT_D3, FIT_VIEW,
UPDATE_SELECTION, SET_SELECTION, SET_NODES_SELECTION
UPDATE_SELECTION, SET_SELECTION, SET_NODES_SELECTION,
SET_SELECTED_NODES_IDS, REMOVE_NODES
} from './index';
export const updateTransform = (transform) => {
@@ -65,6 +66,18 @@ export const setNodesSelection = ({ isActive, selection }) => {
return { type: SET_NODES_SELECTION, payload: { nodesSelectionActive: isActive, selection } };
};
export const setSelectedNodesIds = (ids) => {
const idArray = Array.isArray(ids) ? ids : [ids];
return { type: SET_SELECTED_NODES_IDS, payload: { selectedNodeIds: idArray } };
};
export const removeNodes = (ids) => {
const idArray = Array.isArray(ids) ? ids : [ids];
return { type: REMOVE_NODES, payload: { ids: idArray } };
}
export const updateSelection = (selection) => {
return {
type: UPDATE_SELECTION,
+14
View File
@@ -13,6 +13,8 @@ export const FIT_VIEW = 'FIT_VIEW';
export const UPDATE_SELECTION = 'UPDATE_SELECTION';
export const SET_SELECTION = 'SET_SELECTION';
export const SET_NODES_SELECTION = 'SET_NODES_SELECTION';
export const SET_SELECTED_NODES_IDS = 'SET_SELECTED_NODES_IDS';
export const REMOVE_NODES = 'REMOVE_NODES';
export const initialState = {
width: 0,
@@ -98,12 +100,24 @@ export const reducer = (state, action) => {
return { ...state, ...action.payload, selectedNodesBbox };
}
case REMOVE_NODES: {
const { ids } = action.payload;
const nextEdges = state.edges.filter(e => !ids.includes(e.data.target) && !ids.includes(e.data.source));
const nextNodes = state.nodes.filter(n => !ids.includes(n.data.id));
console.log(ids,);
console.log( state.edges, nextEdges, );
console.log(state.nodes, nextNodes);
return { ...state, nodes: nextNodes, edges: nextEdges };
}
case SET_NODES:
case SET_EDGES:
case UPDATE_TRANSFORM:
case INIT_D3:
case UPDATE_SIZE:
case SET_SELECTION:
case SET_SELECTED_NODES_IDS:
return { ...state, ...action.payload };
default:
return state;