feat(editor): 💫 Can pan graph with mouse + click
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Flex, FlexProps, useEventListener } from '@chakra-ui/react'
|
import { Flex, FlexProps, useEventListener } from '@chakra-ui/react'
|
||||||
import React, { useRef, useMemo, useEffect } from 'react'
|
import React, { useRef, useMemo, useEffect, useState } from 'react'
|
||||||
import { blockWidth, useGraph } from 'contexts/GraphContext'
|
import { blockWidth, useGraph } from 'contexts/GraphContext'
|
||||||
import { BlockNode } from './Nodes/BlockNode/BlockNode'
|
import { BlockNode } from './Nodes/BlockNode/BlockNode'
|
||||||
import { useStepDnd } from 'contexts/GraphDndContext'
|
import { useStepDnd } from 'contexts/GraphDndContext'
|
||||||
@@ -36,6 +36,7 @@ export const Graph = ({
|
|||||||
`translate(${graphPosition.x}px, ${graphPosition.y}px) scale(${graphPosition.scale})`,
|
`translate(${graphPosition.x}px, ${graphPosition.y}px) scale(${graphPosition.scale})`,
|
||||||
[graphPosition]
|
[graphPosition]
|
||||||
)
|
)
|
||||||
|
const [isMouseDown, setIsMouseDown] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editorContainerRef.current = document.getElementById(
|
editorContainerRef.current = document.getElementById(
|
||||||
@@ -60,8 +61,8 @@ export const Graph = ({
|
|||||||
y: graphPosition.y - e.deltaY,
|
y: graphPosition.y - e.deltaY,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
useEventListener('wheel', handleMouseWheel, graphContainerRef.current)
|
|
||||||
|
|
||||||
|
const handleGlobalMouseUp = () => setIsMouseDown(false)
|
||||||
const handleMouseUp = (e: MouseEvent) => {
|
const handleMouseUp = (e: MouseEvent) => {
|
||||||
if (!typebot) return
|
if (!typebot) return
|
||||||
if (!draggedStep && !draggedStepType) return
|
if (!draggedStep && !draggedStepType) return
|
||||||
@@ -80,19 +81,39 @@ export const Graph = ({
|
|||||||
setDraggedStep(undefined)
|
setDraggedStep(undefined)
|
||||||
setDraggedStepType(undefined)
|
setDraggedStepType(undefined)
|
||||||
}
|
}
|
||||||
useEventListener('mouseup', handleMouseUp, graphContainerRef.current)
|
|
||||||
|
|
||||||
const handleMouseDown = (e: MouseEvent) => {
|
const handleCaptureMouseDown = (e: MouseEvent) => {
|
||||||
const isRightClick = e.button === 2
|
const isRightClick = e.button === 2
|
||||||
if (isRightClick) e.stopPropagation()
|
if (isRightClick) e.stopPropagation()
|
||||||
}
|
}
|
||||||
useEventListener('mousedown', handleMouseDown, undefined, { capture: true })
|
|
||||||
|
|
||||||
const handleClick = () => setOpenedStepId(undefined)
|
const handleClick = () => setOpenedStepId(undefined)
|
||||||
useEventListener('click', handleClick, editorContainerRef.current)
|
|
||||||
|
|
||||||
|
const handleMouseDown = () => setIsMouseDown(true)
|
||||||
|
const handleMouseMove = (event: React.MouseEvent) => {
|
||||||
|
if (!isMouseDown) return
|
||||||
|
const { movementX, movementY } = event
|
||||||
|
setGraphPosition({
|
||||||
|
x: graphPosition.x + movementX,
|
||||||
|
y: graphPosition.y + movementY,
|
||||||
|
scale: 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
useEventListener('wheel', handleMouseWheel, graphContainerRef.current)
|
||||||
|
useEventListener('mousedown', handleCaptureMouseDown, undefined, {
|
||||||
|
capture: true,
|
||||||
|
})
|
||||||
|
useEventListener('mouseup', handleMouseUp, graphContainerRef.current)
|
||||||
|
useEventListener('mouseup', handleGlobalMouseUp)
|
||||||
|
useEventListener('click', handleClick, editorContainerRef.current)
|
||||||
return (
|
return (
|
||||||
<Flex ref={graphContainerRef} {...props}>
|
<Flex
|
||||||
|
ref={graphContainerRef}
|
||||||
|
onMouseDown={handleMouseDown}
|
||||||
|
onMouseMove={handleMouseMove}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
<Flex
|
<Flex
|
||||||
flex="1"
|
flex="1"
|
||||||
boxSize={'200px'}
|
boxSize={'200px'}
|
||||||
|
|||||||
@@ -65,7 +65,8 @@ export const BlockNode = ({ block, blockIndex }: Props) => {
|
|||||||
const handleTitleSubmit = (title: string) =>
|
const handleTitleSubmit = (title: string) =>
|
||||||
updateBlock(blockIndex, { title })
|
updateBlock(blockIndex, { title })
|
||||||
|
|
||||||
const handleMouseDown = () => {
|
const handleMouseDown = (e: React.MouseEvent) => {
|
||||||
|
e.stopPropagation()
|
||||||
setIsMouseDown(true)
|
setIsMouseDown(true)
|
||||||
}
|
}
|
||||||
const handleMouseUp = () => {
|
const handleMouseUp = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user