diff --git a/src/NodeRenderer/NodeTypes/wrapNode.js b/src/NodeRenderer/NodeTypes/wrapNode.js index 70b2d503..edf9d2ce 100644 --- a/src/NodeRenderer/NodeTypes/wrapNode.js +++ b/src/NodeRenderer/NodeTypes/wrapNode.js @@ -16,7 +16,7 @@ export default NodeComponent => (props) => { const { position } = __rg; const { id } = data; const [ x, y, k ] = graphContext.state.transform; - const selected = graphContext.state.selectedNodes.includes(id); + const selected = graphContext.state.selectedNodeIds.includes(id); const nodeClasses = cx('react-graph__node', { selected }); useEffect(() => { diff --git a/src/Selection/index.js b/src/Selection/index.js index e6cf73f3..81794caf 100644 --- a/src/Selection/index.js +++ b/src/Selection/index.js @@ -10,13 +10,14 @@ const initialRect = { y: 0, width: 0, height: 0, - draw: false + draw: false, + fixed: false }; export default () => { const selectionPane = useRef(null); const [rect, setRect] = useState(initialRect); - const { dispatch } = useContext(GraphContext); + const { dispatch, state } = useContext(GraphContext); useEffect(() => { function onMouseDown(evt) { @@ -56,8 +57,16 @@ export default () => { } function onMouseUp() { - setRect(initialRect); - dispatch(setSelection(false)); + setRect((r) => { + const nextRect = { + ...r, + fixed: true + }; + + dispatch(updateSelection(nextRect)); + + return nextRect; + }); } selectionPane.current.addEventListener('mousedown', onMouseDown); @@ -71,18 +80,21 @@ export default () => { }; }, []); + const selectionRect = rect.fixed ? state.selectedNodesBbox : rect; + console.log(selectionRect) + return (
- {rect.draw && ( + {selectionRect.draw && (
)} diff --git a/src/state/index.js b/src/state/index.js index 854e1fa9..debfcd07 100644 --- a/src/state/index.js +++ b/src/state/index.js @@ -19,14 +19,15 @@ export const initialState = { transform: [0, 0, 1], nodes: [], edges: [], - selectedNodes: [], + selectedNodeIds: [], + selectedNodesBbox: { x: 0, y: 0, width: 0, height: 0 }, d3Zoom: null, d3Selection: null, d3Initialised: false, selectionActive: false, - selection: {} + selection: {}, }; export const reducer = (state, action) => { @@ -73,8 +74,11 @@ export const reducer = (state, action) => { return state; } case UPDATE_SELECTION: { - const selectedNodes = getNodesInside(state.nodes, action.payload.selection, state.transform).map(n => n.data.id); - return { ...state, ...action.payload, selectedNodes }; + const selectedNodes = getNodesInside(state.nodes, action.payload.selection, state.transform); + const selectedNodesBbox = getBoundingBox(selectedNodes); + const selectedNodeIds = selectedNodes.map(n => n.data.id); + + return { ...state, ...action.payload, selectedNodeIds, selectedNodesBbox }; } case SET_NODES: case SET_EDGES: