feat(nodesselection): hide on click

This commit is contained in:
moklick
2019-07-24 15:43:18 +02:00
parent ac8ac49d1d
commit 7b32ee660c
2 changed files with 20 additions and 21 deletions
+19 -18
View File
@@ -8,19 +8,19 @@ import NodeRenderer from '../NodeRenderer';
import EdgeRenderer from '../EdgeRenderer'; import EdgeRenderer from '../EdgeRenderer';
import UserSelection from '../UserSelection'; import UserSelection from '../UserSelection';
import NodesSelection from '../NodesSelection'; import NodesSelection from '../NodesSelection';
import { updateTransform, updateSize, initD3, fitView } from '../state/actions'; import { updateTransform, updateSize, initD3, fitView, setNodesSelection } from '../state/actions';
import { useKeyPress } from '../hooks'; import { useKeyPress } from '../hooks';
const d3ZoomInstance = d3Zoom.zoom().scaleExtent([0.5, 2]); const d3ZoomInstance = d3Zoom.zoom().scaleExtent([0.5, 2]);
const GraphView = (props) => { const GraphView = (props) => {
const zoomPane = useRef(null); const zoomPane = useRef(null);
const graphContext = useContext(GraphContext); const { state, dispatch } = useContext(GraphContext);
const shiftPressed = useKeyPress('Shift'); const shiftPressed = useKeyPress('Shift');
useEffect(() => { useEffect(() => {
const selection = select(zoomPane.current).call(d3ZoomInstance); const selection = select(zoomPane.current).call(d3ZoomInstance);
graphContext.dispatch(initD3({ zoom: d3ZoomInstance, selection })); dispatch(initD3({ zoom: d3ZoomInstance, selection }));
}, []); }, []);
useEffect(() => { useEffect(() => {
@@ -32,53 +32,54 @@ const GraphView = (props) => {
return false; return false;
} }
graphContext.dispatch(updateTransform(event.transform)); dispatch(updateTransform(event.transform));
props.onMove(); props.onMove();
}); });
if (graphContext.state.d3Selection) { if (state.d3Selection) {
// we need to restore the graph transform otherwise d3 zoom transform and graph transform are not synced // we need to restore the graph transform otherwise d3 zoom transform and graph transform are not synced
const graphTransform = d3Zoom.zoomIdentity const graphTransform = d3Zoom.zoomIdentity
.translate(graphContext.state.transform[0], graphContext.state.transform[1]) .translate(state.transform[0], state.transform[1])
.scale(graphContext.state.transform[2]); .scale(state.transform[2]);
graphContext.state.d3Selection.call(graphContext.state.d3Zoom.transform, graphTransform); state.d3Selection.call(state.d3Zoom.transform, graphTransform);
} }
} }
}, [shiftPressed]); }, [shiftPressed]);
useEffect( useEffect(
() => graphContext.dispatch(updateSize(props.size)), () => dispatch(updateSize(props.size)),
[props.size.width, props.size.height] [props.size.width, props.size.height]
); );
useEffect(() => { useEffect(() => {
if (graphContext.state.d3Initialised) { if (state.d3Initialised) {
props.onLoad({ props.onLoad({
nodes: graphContext.state.nodes, nodes: state.nodes,
edges: graphContext.state.edges, edges: state.edges,
fitView: () => graphContext.dispatch(fitView()) fitView: () => dispatch(fitView())
}); });
} }
}, [graphContext.state.d3Initialised]); }, [state.d3Initialised]);
useEffect(() => { useEffect(() => {
props.onChange({ props.onChange({
nodes: graphContext.state.nodes, nodes: state.nodes,
edges: graphContext.state.edges, edges: state.edges,
}); });
}); });
return ( return (
<div className="react-graph__renderer"> <div className="react-graph__renderer">
<NodeRenderer nodeTypes={props.nodeTypes} /> <NodeRenderer nodeTypes={props.nodeTypes} />
<EdgeRenderer width={graphContext.state.width} height={graphContext.state.height} /> <EdgeRenderer width={state.width} height={state.height} />
{shiftPressed && <UserSelection />} {shiftPressed && <UserSelection />}
{graphContext.state.nodesSelectionActive && <NodesSelection />} {state.nodesSelectionActive && <NodesSelection />}
<div <div
className="react-graph__zoompane" className="react-graph__zoompane"
onClick={() => dispatch(setNodesSelection({ isActive: false }))}
ref={zoomPane} ref={zoomPane}
/> />
</div> </div>
+1 -3
View File
@@ -1,11 +1,10 @@
import React, { useContext } from 'react'; import React, { useContext } from 'react';
import { GraphContext } from '../GraphContext'; import { GraphContext } from '../GraphContext';
import { setNodesSelection } from '../state/actions';
export default () => { export default () => {
const graphContext = useContext(GraphContext); const graphContext = useContext(GraphContext);
const { state, dispatch } = graphContext; const { state } = graphContext;
return ( return (
<div <div
@@ -13,7 +12,6 @@ export default () => {
style={{ style={{
transform: `translate(${state.transform[0]}px,${state.transform[1]}px) scale(${state.transform[2]})` transform: `translate(${state.transform[0]}px,${state.transform[1]}px) scale(${state.transform[2]})`
}} }}
onClick={() => dispatch(setNodesSelection({ isActive: false }))}
> >
<div <div
className="react-graph__nodesselection-rect" className="react-graph__nodesselection-rect"