use-panel-interactions.ts 964 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import type { MouseEvent } from 'react'
  2. import { useCallback } from 'react'
  3. import { useWorkflowStore } from '../store'
  4. export const usePanelInteractions = () => {
  5. const workflowStore = useWorkflowStore()
  6. const handlePaneContextMenu = useCallback((e: MouseEvent) => {
  7. e.preventDefault()
  8. const container = document.querySelector('#workflow-container')
  9. const { x, y } = container!.getBoundingClientRect()
  10. workflowStore.setState({
  11. panelMenu: {
  12. top: e.clientY - y,
  13. left: e.clientX - x,
  14. },
  15. })
  16. }, [workflowStore])
  17. const handlePaneContextmenuCancel = useCallback(() => {
  18. workflowStore.setState({
  19. panelMenu: undefined,
  20. })
  21. }, [workflowStore])
  22. const handleNodeContextmenuCancel = useCallback(() => {
  23. workflowStore.setState({
  24. nodeMenu: undefined,
  25. })
  26. }, [workflowStore])
  27. return {
  28. handlePaneContextMenu,
  29. handlePaneContextmenuCancel,
  30. handleNodeContextmenuCancel,
  31. }
  32. }