Skip to content

Commit

Permalink
feat(editor): 🚸 Auto move board when dragging an edge
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Apr 11, 2022
1 parent 4a2c662 commit b6ba40e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
68 changes: 61 additions & 7 deletions apps/builder/components/shared/Graph/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const Graph = ({
setOpenedStepId,
updateBlockCoordinates,
setPreviewingEdge,
connectingIds,
} = useGraph()
const [graphPosition, setGraphPosition] = useState(graphPositionDefaultValue)
const [debouncedGraphPosition] = useDebounce(graphPosition, 200)
Expand All @@ -59,6 +60,11 @@ export const Graph = ({
)
const { user } = useUser()

const [autoMoveDirection, setAutoMoveDirection] = useState<
'top' | 'right' | 'bottom' | 'left' | undefined
>()
useAutoMoveBoard(autoMoveDirection, setGraphPosition)

useEffect(() => {
editorContainerRef.current = document.getElementById(
'editor-container'
Expand Down Expand Up @@ -122,13 +128,6 @@ export const Graph = ({
setPreviewingEdge(undefined)
}

useEventListener('wheel', handleMouseWheel, graphContainerRef.current)
useEventListener('mousedown', handleCaptureMouseDown, undefined, {
capture: true,
})
useEventListener('mouseup', handleMouseUp, graphContainerRef.current)
useEventListener('click', handleClick, editorContainerRef.current)

const onDrag = (_: DraggableEvent, draggableData: DraggableData) => {
const { deltaX, deltaY } = draggableData
setGraphPosition({
Expand Down Expand Up @@ -159,6 +158,25 @@ export const Graph = ({
})
}

const handleMouseMove = (e: MouseEvent) => {
if (!connectingIds)
return autoMoveDirection ? setAutoMoveDirection(undefined) : undefined
if (e.clientX <= 50) return setAutoMoveDirection('left')
if (e.clientY <= 50 + headerHeight) return setAutoMoveDirection('top')
if (e.clientX >= window.innerWidth - 50)
return setAutoMoveDirection('right')
if (e.clientY >= window.innerHeight - 50)
return setAutoMoveDirection('bottom')
setAutoMoveDirection(undefined)
}

useEventListener('wheel', handleMouseWheel, graphContainerRef.current)
useEventListener('mousedown', handleCaptureMouseDown, undefined, {
capture: true,
})
useEventListener('mouseup', handleMouseUp, graphContainerRef.current)
useEventListener('click', handleClick, editorContainerRef.current)
useEventListener('mousemove', handleMouseMove)
return (
<DraggableCore onDrag={onDrag} enableUserSelectHack={false}>
<Flex ref={graphContainerRef} position="relative" {...props}>
Expand Down Expand Up @@ -204,3 +222,39 @@ const projectMouse = (
graphPosition.scale,
}
}

const useAutoMoveBoard = (
autoMoveDirection: 'top' | 'right' | 'bottom' | 'left' | undefined,
setGraphPosition: React.Dispatch<
React.SetStateAction<{
x: number
y: number
scale: number
}>
>
) =>
useEffect(() => {
if (!autoMoveDirection) return
const interval = setInterval(() => {
setGraphPosition((prev) => ({
...prev,
x:
autoMoveDirection === 'right'
? prev.x - 5
: autoMoveDirection === 'left'
? prev.x + 5
: prev.x,
y:
autoMoveDirection === 'bottom'
? prev.y - 5
: autoMoveDirection === 'top'
? prev.y + 5
: prev.y,
}))
}, 5)

return () => {
clearInterval(interval)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [autoMoveDirection])
6 changes: 3 additions & 3 deletions apps/builder/playwright/tests/integrations/webhook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test.describe('Webhook step', () => {
await page.goto(`/typebots/${typebotId}/edit`)
await page.click('text=Configure...')
await page.fill(
'input[placeholder="Your Webhook URL..."]',
'input[placeholder="Paste webhook URL..."]',
`${process.env.PLAYWRIGHT_BUILDER_TEST_BASE_URL}/api/mock/webhook-easy-config`
)
await page.click('text=Test the request')
Expand All @@ -41,7 +41,7 @@ test.describe('Webhook step', () => {
await page.goto(`/typebots/${typebotId}/edit`)
await page.click('text=Configure...')
await page.fill(
'input[placeholder="Your Webhook URL..."]',
'input[placeholder="Paste webhook URL..."]',
`${process.env.PLAYWRIGHT_BUILDER_TEST_BASE_URL}/api/mock/webhook-easy-config`
)
await page.click('text=Advanced configuration')
Expand All @@ -66,7 +66,7 @@ test.describe('Webhook step', () => {
await page.goto(`/typebots/${typebotId}/edit`)
await page.click('text=Configure...')
await page.fill(
'input[placeholder="Your Webhook URL..."]',
'input[placeholder="Paste webhook URL..."]',
`${process.env.PLAYWRIGHT_BUILDER_TEST_BASE_URL}/api/mock/webhook`
)
await page.click('text=Advanced configuration')
Expand Down

4 comments on commit b6ba40e

@vercel
Copy link

@vercel vercel bot commented on b6ba40e Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on b6ba40e Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on b6ba40e Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on b6ba40e Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

Please sign in to comment.