Skip to content

Commit

Permalink
Add 'canMoveNode(INode)'.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoereth committed Jan 2, 2022
1 parent 3f37b6f commit e445882
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ All props are detailed below.
| `canDeleteSelected` | `func` | `false` | takes the place of `canDeleteNode` and `canDeleteEdge`. It accepts a `SelectionT` type as a parameter. It is called before a node or edge is deleted. The function should return a boolean.
| `canCreateEdge` | `func` | `false` | Called before an edge is created.|
| `canSwapEdge` | `func` | `false` | Called before an edge 'target' is swapped.
| `canMoveNode` | `func` | `false` | Called before a node is moved (e.g. by being dragged). |
| `afterRenderEdge` | `func` | `false` | Called after an edge is rendered. |
| `renderNode` | `func` | `false` | Called to render node geometry. |
| `renderNodeText` | `func` | `false` | Called to render the node text |
Expand Down
1 change: 1 addition & 0 deletions src/components/graph-view-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type IGraphViewProps = {
hoveredNode: INode | null,
swapEdge: IEdge
) => boolean,
canMoveNode?: (node: INode) => boolean,
onBackgroundClick?: (x: number, y: number, event: any) => void,
onCopySelected?: () => void,
onCreateEdge?: (sourceNode: INode, targetNode: INode) => void,
Expand Down
14 changes: 13 additions & 1 deletion src/components/graph-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class GraphView extends React.Component<IGraphViewProps, IGraphViewState> {
canCreateEdge: (startNode?: INode, endNode?: INode) => true,
canSwapEdge: () => true,
canDeleteSelected: () => true,
canMoveNode: (node: INode) => true,
allowMultiselect: true,
edgeArrowSize: 8,
gridSpacing: 36,
Expand Down Expand Up @@ -862,7 +863,14 @@ class GraphView extends React.Component<IGraphViewProps, IGraphViewState> {
}

handleNodeMove = (position: IPoint, nodeId: string, shiftKey: boolean) => {
const { canCreateEdge, readOnly, selected, nodeKey, onSelect } = this.props;
const {
canCreateEdge,
canMoveNode,
readOnly,
selected,
nodeKey,
onSelect,
} = this.props;
const { draggingEdge, nodesMap } = this.state;
const nodeMapNode: INodeMapNode | null = this.getNodeById(nodeId);

Expand All @@ -872,6 +880,10 @@ class GraphView extends React.Component<IGraphViewProps, IGraphViewState> {

const node = nodeMapNode.node;

if (canMoveNode && !canMoveNode(node)) {
return;
}

if (!shiftKey && !draggingEdge) {
const originalX = node.x || 0;
const originalY = node.y || 0;
Expand Down

0 comments on commit e445882

Please sign in to comment.