🚸 Auto focus new blocks and fix text editor close callback
This commit is contained in:
@@ -16,7 +16,10 @@ import { duplicateItemDraft } from './items'
|
||||
import { parseNewBlock } from '@/features/typebot/helpers/parseNewBlock'
|
||||
|
||||
export type BlocksActions = {
|
||||
createBlock: (block: BlockV6 | BlockV6['type'], indices: BlockIndices) => void
|
||||
createBlock: (
|
||||
block: BlockV6 | BlockV6['type'],
|
||||
indices: BlockIndices
|
||||
) => string | undefined
|
||||
updateBlock: (
|
||||
indices: BlockIndices,
|
||||
updates: Partial<Omit<BlockV6, 'id' | 'type'>>
|
||||
@@ -35,12 +38,15 @@ export type WebhookCallBacks = {
|
||||
}
|
||||
|
||||
export const blocksAction = (setTypebot: SetTypebot): BlocksActions => ({
|
||||
createBlock: (block: BlockV6 | BlockV6['type'], indices: BlockIndices) =>
|
||||
createBlock: (block: BlockV6 | BlockV6['type'], indices: BlockIndices) => {
|
||||
let blockId
|
||||
setTypebot((typebot) =>
|
||||
produce(typebot, (typebot) => {
|
||||
createBlockDraft(typebot, block, indices)
|
||||
blockId = createBlockDraft(typebot, block, indices)
|
||||
})
|
||||
),
|
||||
)
|
||||
return blockId
|
||||
},
|
||||
updateBlock: (
|
||||
{ groupIndex, blockIndex }: BlockIndices,
|
||||
updates: Partial<Omit<Block, 'id' | 'type'>>
|
||||
@@ -97,19 +103,22 @@ export const createBlockDraft = (
|
||||
edgeId: blocks[blockIndex - 1].outgoingEdgeId as string,
|
||||
groupIndex,
|
||||
})
|
||||
typeof block === 'string'
|
||||
? createNewBlock(typebot, block, { groupIndex, blockIndex })
|
||||
: moveBlockToGroup(typebot, block, { groupIndex, blockIndex })
|
||||
const blockId =
|
||||
typeof block === 'string'
|
||||
? createNewBlock(typebot, block, { groupIndex, blockIndex })
|
||||
: moveBlockToGroup(typebot, block, { groupIndex, blockIndex })
|
||||
removeEmptyGroups(typebot)
|
||||
return blockId
|
||||
}
|
||||
|
||||
const createNewBlock = async (
|
||||
const createNewBlock = (
|
||||
typebot: Draft<Typebot>,
|
||||
type: BlockV6['type'],
|
||||
{ groupIndex, blockIndex }: BlockIndices
|
||||
) => {
|
||||
const newBlock = parseNewBlock(type)
|
||||
typebot.groups[groupIndex].blocks.splice(blockIndex ?? 0, 0, newBlock)
|
||||
return newBlock.id
|
||||
}
|
||||
|
||||
const moveBlockToGroup = (
|
||||
|
||||
@@ -26,7 +26,7 @@ export type GroupsActions = {
|
||||
block: BlockV6 | BlockV6['type']
|
||||
indices: BlockIndices
|
||||
}
|
||||
) => void
|
||||
) => string | void
|
||||
updateGroup: (
|
||||
groupIndex: number,
|
||||
updates: Partial<Omit<GroupV6, 'id'>>
|
||||
@@ -54,7 +54,8 @@ const groupsActions = (setTypebot: SetTypebot): GroupsActions => ({
|
||||
groupLabel?: string
|
||||
block: BlockV6 | BlockV6['type']
|
||||
indices: BlockIndices
|
||||
}) =>
|
||||
}) => {
|
||||
let newBlockId
|
||||
setTypebot((typebot) =>
|
||||
produce(typebot, (typebot) => {
|
||||
const newGroup: GroupV6 = {
|
||||
@@ -64,9 +65,11 @@ const groupsActions = (setTypebot: SetTypebot): GroupsActions => ({
|
||||
blocks: [],
|
||||
}
|
||||
typebot.groups.push(newGroup)
|
||||
createBlockDraft(typebot, block, indices)
|
||||
newBlockId = createBlockDraft(typebot, block, indices)
|
||||
})
|
||||
),
|
||||
)
|
||||
return newBlockId
|
||||
},
|
||||
updateGroup: (groupIndex: number, updates: Partial<Omit<GroupV6, 'id'>>) =>
|
||||
setTypebot((typebot) =>
|
||||
produce(typebot, (typebot) => {
|
||||
|
||||
Reference in New Issue
Block a user