2
0

🚸 Auto focus new blocks and fix text editor close callback

This commit is contained in:
Baptiste Arnaud
2024-03-18 16:34:56 +01:00
parent 3ac211d74a
commit a797fc074c
5 changed files with 30 additions and 15 deletions

View File

@ -16,7 +16,10 @@ import { duplicateItemDraft } from './items'
import { parseNewBlock } from '@/features/typebot/helpers/parseNewBlock' import { parseNewBlock } from '@/features/typebot/helpers/parseNewBlock'
export type BlocksActions = { export type BlocksActions = {
createBlock: (block: BlockV6 | BlockV6['type'], indices: BlockIndices) => void createBlock: (
block: BlockV6 | BlockV6['type'],
indices: BlockIndices
) => string | undefined
updateBlock: ( updateBlock: (
indices: BlockIndices, indices: BlockIndices,
updates: Partial<Omit<BlockV6, 'id' | 'type'>> updates: Partial<Omit<BlockV6, 'id' | 'type'>>
@ -35,12 +38,15 @@ export type WebhookCallBacks = {
} }
export const blocksAction = (setTypebot: SetTypebot): BlocksActions => ({ export const blocksAction = (setTypebot: SetTypebot): BlocksActions => ({
createBlock: (block: BlockV6 | BlockV6['type'], indices: BlockIndices) => createBlock: (block: BlockV6 | BlockV6['type'], indices: BlockIndices) => {
let blockId
setTypebot((typebot) => setTypebot((typebot) =>
produce(typebot, (typebot) => { produce(typebot, (typebot) => {
createBlockDraft(typebot, block, indices) blockId = createBlockDraft(typebot, block, indices)
}) })
), )
return blockId
},
updateBlock: ( updateBlock: (
{ groupIndex, blockIndex }: BlockIndices, { groupIndex, blockIndex }: BlockIndices,
updates: Partial<Omit<Block, 'id' | 'type'>> updates: Partial<Omit<Block, 'id' | 'type'>>
@ -97,19 +103,22 @@ export const createBlockDraft = (
edgeId: blocks[blockIndex - 1].outgoingEdgeId as string, edgeId: blocks[blockIndex - 1].outgoingEdgeId as string,
groupIndex, groupIndex,
}) })
typeof block === 'string' const blockId =
? createNewBlock(typebot, block, { groupIndex, blockIndex }) typeof block === 'string'
: moveBlockToGroup(typebot, block, { groupIndex, blockIndex }) ? createNewBlock(typebot, block, { groupIndex, blockIndex })
: moveBlockToGroup(typebot, block, { groupIndex, blockIndex })
removeEmptyGroups(typebot) removeEmptyGroups(typebot)
return blockId
} }
const createNewBlock = async ( const createNewBlock = (
typebot: Draft<Typebot>, typebot: Draft<Typebot>,
type: BlockV6['type'], type: BlockV6['type'],
{ groupIndex, blockIndex }: BlockIndices { groupIndex, blockIndex }: BlockIndices
) => { ) => {
const newBlock = parseNewBlock(type) const newBlock = parseNewBlock(type)
typebot.groups[groupIndex].blocks.splice(blockIndex ?? 0, 0, newBlock) typebot.groups[groupIndex].blocks.splice(blockIndex ?? 0, 0, newBlock)
return newBlock.id
} }
const moveBlockToGroup = ( const moveBlockToGroup = (

View File

@ -26,7 +26,7 @@ export type GroupsActions = {
block: BlockV6 | BlockV6['type'] block: BlockV6 | BlockV6['type']
indices: BlockIndices indices: BlockIndices
} }
) => void ) => string | void
updateGroup: ( updateGroup: (
groupIndex: number, groupIndex: number,
updates: Partial<Omit<GroupV6, 'id'>> updates: Partial<Omit<GroupV6, 'id'>>
@ -54,7 +54,8 @@ const groupsActions = (setTypebot: SetTypebot): GroupsActions => ({
groupLabel?: string groupLabel?: string
block: BlockV6 | BlockV6['type'] block: BlockV6 | BlockV6['type']
indices: BlockIndices indices: BlockIndices
}) => }) => {
let newBlockId
setTypebot((typebot) => setTypebot((typebot) =>
produce(typebot, (typebot) => { produce(typebot, (typebot) => {
const newGroup: GroupV6 = { const newGroup: GroupV6 = {
@ -64,9 +65,11 @@ const groupsActions = (setTypebot: SetTypebot): GroupsActions => ({
blocks: [], blocks: [],
} }
typebot.groups.push(newGroup) typebot.groups.push(newGroup)
createBlockDraft(typebot, block, indices) newBlockId = createBlockDraft(typebot, block, indices)
}) })
), )
return newBlockId
},
updateGroup: (groupIndex: number, updates: Partial<Omit<GroupV6, 'id'>>) => updateGroup: (groupIndex: number, updates: Partial<Omit<GroupV6, 'id'>>) =>
setTypebot((typebot) => setTypebot((typebot) =>
produce(typebot, (typebot) => { produce(typebot, (typebot) => {

View File

@ -148,7 +148,7 @@ export const Graph = ({
) )
const id = createId() const id = createId()
updateGroupCoordinates(id, coordinates) updateGroupCoordinates(id, coordinates)
createGroup({ const newBlockId = createGroup({
id, id,
...coordinates, ...coordinates,
block: draggedBlock ?? (draggedBlockType as BlockV6['type']), block: draggedBlock ?? (draggedBlockType as BlockV6['type']),
@ -156,6 +156,7 @@ export const Graph = ({
}) })
setDraggedBlock(undefined) setDraggedBlock(undefined)
setDraggedBlockType(undefined) setDraggedBlockType(undefined)
if (newBlockId) setOpenedBlockId(newBlockId)
} }
const handleCaptureMouseDown = (e: MouseEvent) => { const handleCaptureMouseDown = (e: MouseEvent) => {

View File

@ -147,6 +147,7 @@ export const BlockNode = ({
const handleCloseEditor = (content: TElement[]) => { const handleCloseEditor = (content: TElement[]) => {
const updatedBlock = { ...block, content: { richText: content } } const updatedBlock = { ...block, content: { richText: content } }
updateBlock(indices, updatedBlock) updateBlock(indices, updatedBlock)
setOpenedBlockId(undefined)
} }
const handleClick = (e: React.MouseEvent) => { const handleClick = (e: React.MouseEvent) => {

View File

@ -27,7 +27,7 @@ export const BlockNodesList = ({ blocks, groupIndex, groupRef }: Props) => {
setDraggedBlockType, setDraggedBlockType,
} = useBlockDnd() } = useBlockDnd()
const { typebot, createBlock, detachBlockFromGroup } = useTypebot() const { typebot, createBlock, detachBlockFromGroup } = useTypebot()
const { isReadOnly, graphPosition } = useGraph() const { isReadOnly, graphPosition, setOpenedBlockId } = useGraph()
const [expandedPlaceholderIndex, setExpandedPlaceholderIndex] = useState< const [expandedPlaceholderIndex, setExpandedPlaceholderIndex] = useState<
number | undefined number | undefined
>() >()
@ -72,7 +72,7 @@ export const BlockNodesList = ({ blocks, groupIndex, groupRef }: Props) => {
e.clientY, e.clientY,
placeholderRefs placeholderRefs
) )
createBlock( const blockId = createBlock(
(draggedBlock || draggedBlockType) as BlockV6 | BlockV6['type'], (draggedBlock || draggedBlockType) as BlockV6 | BlockV6['type'],
{ {
groupIndex, groupIndex,
@ -81,6 +81,7 @@ export const BlockNodesList = ({ blocks, groupIndex, groupRef }: Props) => {
) )
setDraggedBlock(undefined) setDraggedBlock(undefined)
setDraggedBlockType(undefined) setDraggedBlockType(undefined)
if (blockId) setOpenedBlockId(blockId)
} }
const handleBlockMouseDown = const handleBlockMouseDown =