2
0

🚑 (editor) Fix block drag when dropping at same spot

This commit is contained in:
Baptiste Arnaud
2023-02-28 16:07:27 +01:00
parent f8f98adc1c
commit 5024c1b22b
3 changed files with 48 additions and 20 deletions

View File

@@ -5,7 +5,6 @@ import {
PopoverTrigger, PopoverTrigger,
useColorModeValue, useColorModeValue,
useDisclosure, useDisclosure,
useEventListener,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import React, { useEffect, useRef, useState } from 'react' import React, { useEffect, useRef, useState } from 'react'
import { import {
@@ -30,7 +29,6 @@ import { BlockSettings } from './SettingsPopoverContent/SettingsPopoverContent'
import { TargetEndpoint } from '../../Endpoints' import { TargetEndpoint } from '../../Endpoints'
import { MediaBubblePopoverContent } from './MediaBubblePopoverContent' import { MediaBubblePopoverContent } from './MediaBubblePopoverContent'
import { ContextMenu } from '@/components/ContextMenu' import { ContextMenu } from '@/components/ContextMenu'
import { setMultipleRefs } from '@/utils/helpers'
import { TextBubbleEditor } from '@/features/blocks/bubbles/textBubble' import { TextBubbleEditor } from '@/features/blocks/bubbles/textBubble'
import { import {
NodePosition, NodePosition,
@@ -40,6 +38,7 @@ import {
ParentModalProvider, ParentModalProvider,
} from '../../../providers' } from '../../../providers'
import { hasDefaultConnector } from '../../../utils' import { hasDefaultConnector } from '../../../utils'
import { setMultipleRefs } from '@/utils/helpers'
export const BlockNode = ({ export const BlockNode = ({
block, block,
@@ -86,6 +85,7 @@ export const BlockNode = ({
if (block.type === 'start' || !onMouseDown) return if (block.type === 'start' || !onMouseDown) return
onMouseDown(position, block) onMouseDown(position, block)
} }
useDragDistance({ useDragDistance({
ref: blockRef, ref: blockRef,
onDrag, onDrag,
@@ -160,10 +160,19 @@ export const BlockNode = ({
useEffect(() => { useEffect(() => {
setIsPopoverOpened(openedBlockId === block.id) setIsPopoverOpened(openedBlockId === block.id)
// eslint-disable-next-line react-hooks/exhaustive-deps }, [block.id, openedBlockId])
}, [openedBlockId])
useEventListener('pointerdown', (e) => e.stopPropagation(), blockRef.current) useEffect(() => {
if (!blockRef.current) return
const blockElement = blockRef.current
blockElement.addEventListener('pointerdown', (e) => e.stopPropagation())
return () => {
blockElement.removeEventListener('pointerdown', (e) =>
e.stopPropagation()
)
}
}, [])
const hasIcomingEdge = typebot?.edges.some((edge) => { const hasIcomingEdge = typebot?.edges.some((edge) => {
return edge.to.blockId === block.id return edge.to.blockId === block.id

View File

@@ -95,15 +95,18 @@ export const BlockNodesList = ({
const handleBlockMouseDown = const handleBlockMouseDown =
(blockIndex: number) => (blockIndex: number) =>
( (
{ absolute, relative }: { absolute: Coordinates; relative: Coordinates }, { relative, absolute }: { absolute: Coordinates; relative: Coordinates },
block: DraggableBlock block: DraggableBlock
) => { ) => {
if (isReadOnly) return if (isReadOnly) return
placeholderRefs.current.splice(blockIndex + 1, 1) placeholderRefs.current.splice(blockIndex + 1, 1)
detachBlockFromGroup({ groupIndex, blockIndex })
setPosition(absolute)
setMousePositionInElement(relative) setMousePositionInElement(relative)
setPosition({
x: absolute.x - relative.x,
y: absolute.y - relative.y,
})
setDraggedBlock(block) setDraggedBlock(block)
detachBlockFromGroup({ groupIndex, blockIndex })
} }
const handlePushElementRef = const handlePushElementRef =

View File

@@ -7,6 +7,7 @@ import {
SetStateAction, SetStateAction,
useCallback, useCallback,
useContext, useContext,
useEffect,
useRef, useRef,
useState, useState,
} from 'react' } from 'react'
@@ -114,19 +115,34 @@ export const useDragDistance = ({
} }
useEventListener('mousedown', handleMouseDown, ref.current) useEventListener('mousedown', handleMouseDown, ref.current)
const handleMouseMove = (e: MouseEvent) => { useEffect(() => {
if (!mouseDownPosition.current) return let triggered = false
const { clientX, clientY } = e const triggerDragCallbackIfMouseMovedEnough = (e: MouseEvent) => {
if ( if (!mouseDownPosition.current || triggered) return
Math.abs(mouseDownPosition.current.absolute.x - clientX) > const { clientX, clientY } = e
distanceTolerance || if (
Math.abs(mouseDownPosition.current.absolute.y - clientY) > Math.abs(mouseDownPosition.current.absolute.x - clientX) >
distanceTolerance distanceTolerance ||
) { Math.abs(mouseDownPosition.current.absolute.y - clientY) >
onDrag(mouseDownPosition.current) distanceTolerance
) {
triggered = true
onDrag(mouseDownPosition.current)
}
} }
}
useEventListener('mousemove', handleMouseMove) document.addEventListener(
'mousemove',
triggerDragCallbackIfMouseMovedEnough
)
return () => {
document.removeEventListener(
'mousemove',
triggerDragCallbackIfMouseMovedEnough
)
}
}, [distanceTolerance, onDrag])
} }
export const computeNearestPlaceholderIndex = ( export const computeNearestPlaceholderIndex = (