Skip to content

Commit

Permalink
🚸 Auto focus new blocks and fix text editor close callback
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 18, 2024
1 parent 3ac211d commit a797fc0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'>>
Expand All @@ -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'>>
Expand Down Expand Up @@ -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 = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type GroupsActions = {
block: BlockV6 | BlockV6['type']
indices: BlockIndices
}
) => void
) => string | void
updateGroup: (
groupIndex: number,
updates: Partial<Omit<GroupV6, 'id'>>
Expand Down Expand Up @@ -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 = {
Expand All @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion apps/builder/src/features/graph/components/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,15 @@ export const Graph = ({
)
const id = createId()
updateGroupCoordinates(id, coordinates)
createGroup({
const newBlockId = createGroup({
id,
...coordinates,
block: draggedBlock ?? (draggedBlockType as BlockV6['type']),
indices: { groupIndex: typebot.groups.length, blockIndex: 0 },
})
setDraggedBlock(undefined)
setDraggedBlockType(undefined)
if (newBlockId) setOpenedBlockId(newBlockId)
}

const handleCaptureMouseDown = (e: MouseEvent) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const BlockNode = ({
const handleCloseEditor = (content: TElement[]) => {
const updatedBlock = { ...block, content: { richText: content } }
updateBlock(indices, updatedBlock)
setOpenedBlockId(undefined)
}

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

const handleBlockMouseDown =
Expand Down

0 comments on commit a797fc0

Please sign in to comment.