-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(edges): only redraw moved edges
- Loading branch information
Showing
9 changed files
with
436 additions
and
403 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,78 @@ | ||
import React, { PureComponent } from 'react'; | ||
import React, { memo, useContext } from 'react'; | ||
|
||
import { Consumer } from '../GraphContext'; | ||
import { GraphContext } from '../GraphContext'; | ||
import ConnectionLine from '../ConnectionLine'; | ||
|
||
class EdgeRenderer extends PureComponent { | ||
renderEdge(e, nodes, onElementClick) { | ||
const edgeType = e.type || 'default'; | ||
const sourceNode = nodes.find(n => n.id === e.source); | ||
const targetNode = nodes.find(n => n.id === e.target); | ||
function renderEdge(e, props, graphContext) { | ||
const edgeType = e.type || 'default'; | ||
const sourceNode = graphContext.state.nodes.find(n => n.id === e.source); | ||
const targetNode = graphContext.state.nodes.find(n => n.id === e.target); | ||
|
||
if (!sourceNode) { | ||
throw new Error(`couldn't create edge for source id: ${e.source}`); | ||
} | ||
if (!sourceNode) { | ||
throw new Error(`couldn't create edge for source id: ${e.source}`); | ||
} | ||
|
||
if (!targetNode) { | ||
throw new Error(`couldn't create edge for target id: ${e.target}`); | ||
} | ||
if (!targetNode) { | ||
throw new Error(`couldn't create edge for target id: ${e.target}`); | ||
} | ||
|
||
const EdgeComponent = this.props.edgeTypes[edgeType] || this.props.edgeTypes.default; | ||
const EdgeComponent = props.edgeTypes[edgeType] || props.edgeTypes.default; | ||
|
||
return ( | ||
<EdgeComponent | ||
key={`${e.source}-${e.target}`} | ||
sourceNode={sourceNode} | ||
targetNode={targetNode} | ||
onClick={onElementClick} | ||
{...e} | ||
/> | ||
); | ||
} | ||
return ( | ||
<EdgeComponent | ||
key={`${e.source}-${e.target}`} | ||
type={e.type} | ||
sourceNode={sourceNode} | ||
targetNode={targetNode} | ||
onClick={props.onElementClick} | ||
selectedElements={graphContext.state.selectedElements} | ||
dispatch={graphContext.dispatch} | ||
animated={e.animated} | ||
style={e.style} | ||
source={e.source} | ||
target={e.target} | ||
sourceNodeX={sourceNode.__rg.position.x} | ||
sourceNodeY={sourceNode.__rg.position.y} | ||
targetNodeX={targetNode.__rg.position.x} | ||
targeteNodeY={targetNode.__rg.position.y} | ||
/> | ||
); | ||
} | ||
|
||
render() { | ||
const { | ||
width, height, onElementClick, | ||
connectionLineStyle, connectionLineType | ||
} = this.props; | ||
const EdgeRenderer = memo((props) => { | ||
const graphContext = useContext(GraphContext); | ||
const { | ||
width, height, connectionLineStyle, connectionLineType | ||
} = props; | ||
|
||
if (!width) { | ||
return null; | ||
} | ||
if (!width) { | ||
return null; | ||
} | ||
|
||
const { transform, edges, nodes, connectionSourceId, connectionPosition } = graphContext.state; | ||
const transformStyle = `translate(${transform[0]},${transform[1]}) scale(${transform[2]})`; | ||
|
||
return ( | ||
<Consumer> | ||
{({ state }) => ( | ||
<svg | ||
width={width} | ||
height={height} | ||
className="react-graph__edges" | ||
> | ||
<g | ||
transform={`translate(${state.transform[0]},${state.transform[1]}) scale(${state.transform[2]})`} | ||
> | ||
{state.edges.map(e => this.renderEdge(e, state.nodes, onElementClick))} | ||
{state.connectionSourceId && ( | ||
<ConnectionLine | ||
nodes={state.nodes} | ||
connectionSourceId={state.connectionSourceId} | ||
connectionPosition={state.connectionPosition} | ||
transform={state.transform} | ||
connectionLineStyle={connectionLineStyle} | ||
connectionLineType={connectionLineType} | ||
/> | ||
)} | ||
</g> | ||
</svg> | ||
return ( | ||
<svg | ||
width={width} | ||
height={height} | ||
className="react-graph__edges" | ||
> | ||
<g transform={transformStyle}> | ||
{edges.map(e => renderEdge(e, props, graphContext))} | ||
{connectionSourceId && ( | ||
<ConnectionLine | ||
nodes={nodes} | ||
connectionSourceId={connectionSourceId} | ||
connectionPosition={connectionPosition} | ||
transform={transform} | ||
connectionLineStyle={connectionLineStyle} | ||
connectionLineType={connectionLineType} | ||
/> | ||
)} | ||
</Consumer> | ||
); | ||
} | ||
} | ||
</g> | ||
</svg> | ||
); | ||
}); | ||
|
||
export default EdgeRenderer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
import React from 'react'; | ||
import React, { memo, useContext } from 'react'; | ||
|
||
import BaseHandle from './BaseHandle'; | ||
import NodeIdContext from '../NodeIdContext' | ||
import { GraphContext } from '../../GraphContext'; | ||
|
||
export default props => <BaseHandle source {...props} />; | ||
export default memo((props) => { | ||
const nodeId = useContext(NodeIdContext); | ||
const { dispatch, onConnect } = useContext(GraphContext); | ||
|
||
return ( | ||
<BaseHandle | ||
source | ||
nodeId={nodeId} | ||
dispatch={dispatch} | ||
onConnect={onConnect} | ||
{...props} | ||
/> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
import React from 'react'; | ||
import React, { memo, useContext } from 'react'; | ||
|
||
import BaseHandle from './BaseHandle'; | ||
import { GraphContext } from '../../GraphContext'; | ||
import NodeIdContext from '../NodeIdContext' | ||
|
||
export default props => <BaseHandle target {...props} />; | ||
export default memo((props) => { | ||
const nodeId = useContext(NodeIdContext); | ||
const { dispatch, onConnect } = useContext(GraphContext); | ||
|
||
return ( | ||
<BaseHandle | ||
target | ||
nodeId={nodeId} | ||
dispatch={dispatch} | ||
onConnect={onConnect} | ||
{...props} | ||
/> | ||
); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters