2
0

feat(editor): ️ Improve previewing blocks

This commit is contained in:
Baptiste Arnaud
2022-04-02 12:03:21 +02:00
parent 05009814d4
commit 327da3104d
3 changed files with 46 additions and 35 deletions

View File

@ -38,8 +38,9 @@ export const BlockNode = ({ block, blockIndex }: Props) => {
const [isMouseDown, setIsMouseDown] = useState(false) const [isMouseDown, setIsMouseDown] = useState(false)
const [isConnecting, setIsConnecting] = useState(false) const [isConnecting, setIsConnecting] = useState(false)
const isPreviewing = const isPreviewing =
previewingEdge?.to.blockId === block.id || previewingEdge?.from.blockId === block.id ||
previewingEdge?.from.blockId === block.id (previewingEdge?.to.blockId === block.id &&
isNotDefined(previewingEdge.to.stepId))
const isStartBlock = const isStartBlock =
isDefined(block.steps[0]) && block.steps[0].type === 'start' isDefined(block.steps[0]) && block.steps[0].type === 'start'

View File

@ -1,6 +1,6 @@
import { Flex } from '@chakra-ui/react' import { Flex } from '@chakra-ui/react'
import { ContextMenu } from 'components/shared/ContextMenu' import { ContextMenu } from 'components/shared/ContextMenu'
import { Coordinates } from 'contexts/GraphContext' import { Coordinates, useGraph } from 'contexts/GraphContext'
import { NodePosition, useDragDistance } from 'contexts/GraphDndContext' import { NodePosition, useDragDistance } from 'contexts/GraphDndContext'
import { useTypebot } from 'contexts/TypebotContext' import { useTypebot } from 'contexts/TypebotContext'
import { import {
@ -35,8 +35,10 @@ export const ItemNode = ({
onMouseDown, onMouseDown,
}: Props) => { }: Props) => {
const { typebot } = useTypebot() const { typebot } = useTypebot()
const { previewingEdge } = useGraph()
const [isMouseOver, setIsMouseOver] = useState(false) const [isMouseOver, setIsMouseOver] = useState(false)
const itemRef = useRef<HTMLDivElement | null>(null) const itemRef = useRef<HTMLDivElement | null>(null)
const isPreviewing = previewingEdge?.from.itemId === item.id
const isConnectable = !( const isConnectable = !(
typebot?.blocks[indices.blockIndex].steps[ typebot?.blocks[indices.blockIndex].steps[
indices.stepIndex indices.stepIndex
@ -62,17 +64,20 @@ export const ItemNode = ({
{(ref, isOpened) => ( {(ref, isOpened) => (
<Flex <Flex
data-testid="item" data-testid="item"
ref={setMultipleRefs([ref, itemRef])}
align="center"
pos="relative" pos="relative"
ref={setMultipleRefs([ref, itemRef])}
>
<Flex
align="center"
onMouseEnter={handleMouseEnter} onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave} onMouseLeave={handleMouseLeave}
shadow="sm" shadow="sm"
_hover={isReadOnly ? {} : { shadow: 'md' }} _hover={isReadOnly ? {} : { shadow: 'md' }}
transition="box-shadow 200ms" transition="box-shadow 200ms, border-color 200ms"
borderWidth="1px"
rounded="md" rounded="md"
borderColor={isOpened ? 'blue.400' : 'gray.100'} borderWidth={isOpened || isPreviewing ? '2px' : '1px'}
borderColor={isOpened || isPreviewing ? 'blue.400' : 'gray.100'}
margin={isOpened || isPreviewing ? '-1px' : 0}
pointerEvents={isReadOnly ? 'none' : 'all'} pointerEvents={isReadOnly ? 'none' : 'all'}
bgColor="white" bgColor="white"
w="full" w="full"
@ -96,6 +101,7 @@ export const ItemNode = ({
/> />
)} )}
</Flex> </Flex>
</Flex>
)} )}
</ContextMenu> </ContextMenu>
) )

View File

@ -51,6 +51,7 @@ export const StepNode = ({
openedStepId, openedStepId,
setOpenedStepId, setOpenedStepId,
setFocusedBlockId, setFocusedBlockId,
previewingEdge,
} = useGraph() } = useGraph()
const { updateStep } = useTypebot() const { updateStep } = useTypebot()
const [isConnecting, setIsConnecting] = useState(false) const [isConnecting, setIsConnecting] = useState(false)
@ -62,6 +63,8 @@ export const StepNode = ({
) )
const stepRef = useRef<HTMLDivElement | null>(null) const stepRef = useRef<HTMLDivElement | null>(null)
const isPreviewing = isConnecting || previewingEdge?.to.stepId === step.id
const onDrag = (position: NodePosition) => { const onDrag = (position: NodePosition) => {
if (step.type === 'start' || !onMouseDown) return if (step.type === 'start' || !onMouseDown) return
onMouseDown(position, step) onMouseDown(position, step)
@ -170,8 +173,9 @@ export const StepNode = ({
flex="1" flex="1"
userSelect="none" userSelect="none"
p="3" p="3"
borderWidth="1px" borderWidth={isOpened || isPreviewing ? '2px' : '1px'}
borderColor={isConnecting || isOpened ? 'blue.400' : 'gray.200'} borderColor={isOpened || isPreviewing ? 'blue.400' : 'gray.200'}
margin={isOpened || isPreviewing ? '-1px' : 0}
rounded="lg" rounded="lg"
cursor={'pointer'} cursor={'pointer'}
bgColor="gray.50" bgColor="gray.50"