-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add DragNodeWithForce Behaviors
- Loading branch information
1 parent
cf4c9b9
commit 89f7696
Showing
4 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { useEffect } from 'react'; | ||
import GraphinContext from '../GraphinContext'; | ||
|
||
import { IG6GraphEvent } from '@antv/g6'; | ||
|
||
export interface DragNodeWithForceProps { | ||
/** | ||
* @description 琚嫋鎷界殑鑺傜偣锛屾槸鍚﹁嚜鍔ㄥ浐瀹氫綇 | ||
* @description.en-US Whether the dragged node is automatically fixed | ||
* @default false | ||
*/ | ||
autoPin?: boolean; | ||
} | ||
const DragNodeWithForce = (props: DragNodeWithForceProps) => { | ||
const { graph, layout } = React.useContext(GraphinContext); | ||
const { autoPin } = props; | ||
|
||
useEffect(() => { | ||
const { instance } = layout; | ||
const { simulation, type } = instance; | ||
console.log(instance, layout); | ||
const handleNodeDragStart = () => { | ||
if (simulation) { | ||
simulation.stop(); | ||
} | ||
}; | ||
const handleNodeDragEnd = (e: IG6GraphEvent) => { | ||
if (type !== 'graphin-force') { | ||
return; | ||
} | ||
|
||
if (e.item) { | ||
console.log('e.item', instance); | ||
const nodeModel = e.item.get('model'); | ||
nodeModel.x = e.x; | ||
nodeModel.y = e.y; | ||
nodeModel.layout = { | ||
...nodeModel.layout, | ||
force: { | ||
mass: autoPin ? 1000000 : null, | ||
}, | ||
}; | ||
const drageNodes = [nodeModel]; | ||
simulation.restart(drageNodes, graph); | ||
graph.refreshPositions(); | ||
} | ||
}; | ||
|
||
graph.on('node:dragstart', handleNodeDragStart); | ||
graph.on('node:dragend', handleNodeDragEnd); | ||
return () => { | ||
graph.off('node:dragstart', handleNodeDragStart); | ||
graph.off('node:dragend', handleNodeDragEnd); | ||
}; | ||
}, [graph, autoPin]); | ||
return null; | ||
}; | ||
|
||
export default DragNodeWithForce; |
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