2021-12-16 10:43:49 +01:00
|
|
|
import {
|
2022-06-11 07:27:38 +02:00
|
|
|
Flex,
|
|
|
|
HStack,
|
|
|
|
Popover,
|
|
|
|
PopoverTrigger,
|
|
|
|
useDisclosure,
|
2021-12-16 10:43:49 +01:00
|
|
|
} from '@chakra-ui/react'
|
2022-02-04 19:00:08 +01:00
|
|
|
import React, { useEffect, useRef, useState } from 'react'
|
2022-06-11 07:27:38 +02:00
|
|
|
import {
|
|
|
|
BubbleBlock,
|
|
|
|
BubbleBlockContent,
|
|
|
|
ConditionBlock,
|
|
|
|
DraggableBlock,
|
|
|
|
Block,
|
|
|
|
BlockWithOptions,
|
|
|
|
TextBubbleContent,
|
|
|
|
TextBubbleBlock,
|
|
|
|
} from 'models'
|
2021-12-16 10:43:49 +01:00
|
|
|
import { useGraph } from 'contexts/GraphContext'
|
2022-06-11 07:27:38 +02:00
|
|
|
import { BlockIcon } from 'components/editor/BlocksSideBar/BlockIcon'
|
|
|
|
import { isBubbleBlock, isTextBubbleBlock } from 'utils'
|
|
|
|
import { BlockNodeContent } from './BlockNodeContent/BlockNodeContent'
|
|
|
|
import { useTypebot } from 'contexts/TypebotContext'
|
2021-12-22 14:59:07 +01:00
|
|
|
import { ContextMenu } from 'components/shared/ContextMenu'
|
2022-06-11 07:27:38 +02:00
|
|
|
import { SettingsPopoverContent } from './SettingsPopoverContent'
|
2021-12-22 14:59:07 +01:00
|
|
|
import { BlockNodeContextMenu } from './BlockNodeContextMenu'
|
2022-06-11 07:27:38 +02:00
|
|
|
import { SourceEndpoint } from '../../Endpoints/SourceEndpoint'
|
|
|
|
import { hasDefaultConnector } from 'services/typebots'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { SettingsModal } from './SettingsPopoverContent/SettingsModal'
|
|
|
|
import { BlockSettings } from './SettingsPopoverContent/SettingsPopoverContent'
|
|
|
|
import { TextBubbleEditor } from './TextBubbleEditor'
|
|
|
|
import { TargetEndpoint } from '../../Endpoints'
|
|
|
|
import { MediaBubblePopoverContent } from './MediaBubblePopoverContent'
|
|
|
|
import { NodePosition, useDragDistance } from 'contexts/GraphDndContext'
|
2022-02-04 19:00:08 +01:00
|
|
|
import { setMultipleRefs } from 'services/utils'
|
2021-12-16 10:43:49 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const BlockNode = ({
|
|
|
|
block,
|
|
|
|
isConnectable,
|
|
|
|
indices,
|
|
|
|
onMouseDown,
|
|
|
|
}: {
|
2022-01-03 17:39:59 +01:00
|
|
|
block: Block
|
2022-06-11 07:27:38 +02:00
|
|
|
isConnectable: boolean
|
|
|
|
indices: { blockIndex: number; groupIndex: number }
|
|
|
|
onMouseDown?: (blockNodePosition: NodePosition, block: DraggableBlock) => void
|
|
|
|
}) => {
|
|
|
|
const { query } = useRouter()
|
2022-02-02 08:05:02 +01:00
|
|
|
const {
|
|
|
|
setConnectingIds,
|
2022-06-11 07:27:38 +02:00
|
|
|
connectingIds,
|
|
|
|
openedBlockId,
|
|
|
|
setOpenedBlockId,
|
|
|
|
setFocusedGroupId,
|
2022-02-04 19:00:08 +01:00
|
|
|
previewingEdge,
|
2022-02-02 08:05:02 +01:00
|
|
|
} = useGraph()
|
2022-06-11 07:27:38 +02:00
|
|
|
const { updateBlock } = useTypebot()
|
2021-12-16 10:43:49 +01:00
|
|
|
const [isConnecting, setIsConnecting] = useState(false)
|
2022-06-11 07:27:38 +02:00
|
|
|
const [isPopoverOpened, setIsPopoverOpened] = useState(
|
|
|
|
openedBlockId === block.id
|
|
|
|
)
|
|
|
|
const [isEditing, setIsEditing] = useState<boolean>(
|
|
|
|
isTextBubbleBlock(block) && block.content.plainText === ''
|
|
|
|
)
|
2022-02-04 19:00:08 +01:00
|
|
|
const blockRef = useRef<HTMLDivElement | null>(null)
|
2022-06-11 07:27:38 +02:00
|
|
|
|
|
|
|
const isPreviewing = isConnecting || previewingEdge?.to.blockId === block.id
|
|
|
|
|
|
|
|
const onDrag = (position: NodePosition) => {
|
|
|
|
if (block.type === 'start' || !onMouseDown) return
|
|
|
|
onMouseDown(position, block)
|
|
|
|
}
|
|
|
|
useDragDistance({
|
|
|
|
ref: blockRef,
|
|
|
|
onDrag,
|
|
|
|
isDisabled: !onMouseDown || block.type === 'start',
|
|
|
|
})
|
|
|
|
|
|
|
|
const {
|
|
|
|
isOpen: isModalOpen,
|
|
|
|
onOpen: onModalOpen,
|
|
|
|
onClose: onModalClose,
|
|
|
|
} = useDisclosure()
|
|
|
|
|
2022-02-02 08:05:02 +01:00
|
|
|
useEffect(() => {
|
2022-06-11 07:27:38 +02:00
|
|
|
if (query.blockId?.toString() === block.id) setOpenedBlockId(block.id)
|
2022-02-02 08:05:02 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-06-11 07:27:38 +02:00
|
|
|
}, [query])
|
2022-02-02 08:05:02 +01:00
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
useEffect(() => {
|
|
|
|
setIsConnecting(
|
2022-06-11 07:27:38 +02:00
|
|
|
connectingIds?.target?.groupId === block.groupId &&
|
|
|
|
connectingIds?.target?.blockId === block.id
|
2021-12-16 10:43:49 +01:00
|
|
|
)
|
2022-06-11 07:27:38 +02:00
|
|
|
}, [connectingIds, block.groupId, block.id])
|
2021-12-16 10:43:49 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
const handleModalClose = () => {
|
|
|
|
updateBlock(indices, { ...block })
|
|
|
|
onModalClose()
|
2021-12-16 10:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleMouseEnter = () => {
|
|
|
|
if (connectingIds)
|
2022-06-11 07:27:38 +02:00
|
|
|
setConnectingIds({
|
|
|
|
...connectingIds,
|
|
|
|
target: { groupId: block.groupId, blockId: block.id },
|
|
|
|
})
|
2021-12-16 10:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleMouseLeave = () => {
|
2022-06-11 07:27:38 +02:00
|
|
|
if (connectingIds?.target)
|
|
|
|
setConnectingIds({
|
|
|
|
...connectingIds,
|
|
|
|
target: { ...connectingIds.target, blockId: undefined },
|
|
|
|
})
|
2021-12-16 10:43:49 +01:00
|
|
|
}
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
const handleCloseEditor = (content: TextBubbleContent) => {
|
|
|
|
const updatedBlock = { ...block, content } as Block
|
|
|
|
updateBlock(indices, updatedBlock)
|
|
|
|
setIsEditing(false)
|
2022-02-19 18:09:09 +01:00
|
|
|
}
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
const handleClick = (e: React.MouseEvent) => {
|
|
|
|
setFocusedGroupId(block.groupId)
|
|
|
|
e.stopPropagation()
|
|
|
|
if (isTextBubbleBlock(block)) setIsEditing(true)
|
|
|
|
setOpenedBlockId(block.id)
|
2022-03-29 10:23:45 +02:00
|
|
|
}
|
2022-06-02 10:07:50 +02:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
const handleExpandClick = () => {
|
|
|
|
setOpenedBlockId(undefined)
|
|
|
|
onModalOpen()
|
2022-06-02 10:07:50 +02:00
|
|
|
}
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
const handleBlockUpdate = (updates: Partial<Block>) =>
|
|
|
|
updateBlock(indices, { ...block, ...updates })
|
|
|
|
|
|
|
|
const handleContentChange = (content: BubbleBlockContent) =>
|
|
|
|
updateBlock(indices, { ...block, content } as Block)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setIsPopoverOpened(openedBlockId === block.id)
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [openedBlockId])
|
|
|
|
|
|
|
|
return isEditing && isTextBubbleBlock(block) ? (
|
|
|
|
<TextBubbleEditor
|
2022-10-22 09:05:58 +02:00
|
|
|
id={block.id}
|
2022-06-11 07:27:38 +02:00
|
|
|
initialValue={block.content.richText}
|
|
|
|
onClose={handleCloseEditor}
|
|
|
|
/>
|
|
|
|
) : (
|
2021-12-22 14:59:07 +01:00
|
|
|
<ContextMenu<HTMLDivElement>
|
2022-06-11 07:27:38 +02:00
|
|
|
renderMenu={() => <BlockNodeContextMenu indices={indices} />}
|
2021-12-16 10:43:49 +01:00
|
|
|
>
|
2021-12-22 14:59:07 +01:00
|
|
|
{(ref, isOpened) => (
|
2022-06-11 07:27:38 +02:00
|
|
|
<Popover
|
|
|
|
placement="left"
|
|
|
|
isLazy
|
|
|
|
isOpen={isPopoverOpened}
|
|
|
|
closeOnBlur={false}
|
2022-02-22 10:16:35 +01:00
|
|
|
>
|
2022-06-11 07:27:38 +02:00
|
|
|
<PopoverTrigger>
|
|
|
|
<Flex
|
|
|
|
pos="relative"
|
|
|
|
ref={setMultipleRefs([ref, blockRef])}
|
|
|
|
onMouseEnter={handleMouseEnter}
|
|
|
|
onMouseLeave={handleMouseLeave}
|
|
|
|
onClick={handleClick}
|
|
|
|
data-testid={`block`}
|
|
|
|
w="full"
|
2022-02-19 18:09:09 +01:00
|
|
|
>
|
2022-06-11 07:27:38 +02:00
|
|
|
<HStack
|
|
|
|
flex="1"
|
|
|
|
userSelect="none"
|
|
|
|
p="3"
|
|
|
|
borderWidth={isOpened || isPreviewing ? '2px' : '1px'}
|
|
|
|
borderColor={isOpened || isPreviewing ? 'blue.400' : 'gray.200'}
|
|
|
|
margin={isOpened || isPreviewing ? '-1px' : 0}
|
|
|
|
rounded="lg"
|
|
|
|
cursor={'pointer'}
|
|
|
|
bgColor="gray.50"
|
|
|
|
align="flex-start"
|
|
|
|
w="full"
|
|
|
|
transition="border-color 0.2s"
|
|
|
|
>
|
|
|
|
<BlockIcon
|
|
|
|
type={block.type}
|
|
|
|
mt="1"
|
|
|
|
data-testid={`${block.id}-icon`}
|
|
|
|
/>
|
|
|
|
<BlockNodeContent block={block} indices={indices} />
|
|
|
|
<TargetEndpoint
|
|
|
|
pos="absolute"
|
|
|
|
left="-32px"
|
|
|
|
top="19px"
|
|
|
|
blockId={block.id}
|
|
|
|
/>
|
|
|
|
{isConnectable && hasDefaultConnector(block) && (
|
|
|
|
<SourceEndpoint
|
|
|
|
source={{
|
|
|
|
groupId: block.groupId,
|
|
|
|
blockId: block.id,
|
|
|
|
}}
|
|
|
|
pos="absolute"
|
|
|
|
right="-34px"
|
|
|
|
bottom="10px"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</HStack>
|
|
|
|
</Flex>
|
|
|
|
</PopoverTrigger>
|
|
|
|
{hasSettingsPopover(block) && (
|
|
|
|
<>
|
|
|
|
<SettingsPopoverContent
|
|
|
|
block={block}
|
|
|
|
onExpandClick={handleExpandClick}
|
|
|
|
onBlockChange={handleBlockUpdate}
|
2022-05-11 06:39:51 -07:00
|
|
|
/>
|
2022-06-11 07:27:38 +02:00
|
|
|
<SettingsModal isOpen={isModalOpen} onClose={handleModalClose}>
|
|
|
|
<BlockSettings
|
|
|
|
block={block}
|
|
|
|
onBlockChange={handleBlockUpdate}
|
|
|
|
/>
|
|
|
|
</SettingsModal>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{isMediaBubbleBlock(block) && (
|
|
|
|
<MediaBubblePopoverContent
|
|
|
|
block={block}
|
|
|
|
onContentChange={handleContentChange}
|
2022-06-02 10:07:50 +02:00
|
|
|
/>
|
2022-06-11 07:27:38 +02:00
|
|
|
)}
|
|
|
|
</Popover>
|
2021-12-22 14:59:07 +01:00
|
|
|
)}
|
|
|
|
</ContextMenu>
|
2021-12-16 10:43:49 +01:00
|
|
|
)
|
|
|
|
}
|
2022-06-11 07:27:38 +02:00
|
|
|
|
|
|
|
const hasSettingsPopover = (
|
|
|
|
block: Block
|
|
|
|
): block is BlockWithOptions | ConditionBlock => !isBubbleBlock(block)
|
|
|
|
|
|
|
|
const isMediaBubbleBlock = (
|
|
|
|
block: Block
|
|
|
|
): block is Exclude<BubbleBlock, TextBubbleBlock> =>
|
|
|
|
isBubbleBlock(block) && !isTextBubbleBlock(block)
|