2
0

feat(editor): ️ Create new item when hitting enter

This commit is contained in:
Baptiste Arnaud
2022-04-04 10:53:03 +02:00
parent f6b518989c
commit 68671433bc
4 changed files with 4 additions and 31 deletions

View File

@@ -20,20 +20,13 @@ type Props = {
item: Item item: Item
indices: ItemIndices indices: ItemIndices
isReadOnly: boolean isReadOnly: boolean
isLastItem: boolean
onMouseDown?: ( onMouseDown?: (
stepNodePosition: { absolute: Coordinates; relative: Coordinates }, stepNodePosition: { absolute: Coordinates; relative: Coordinates },
item: ButtonItem item: ButtonItem
) => void ) => void
} }
export const ItemNode = ({ export const ItemNode = ({ item, indices, isReadOnly, onMouseDown }: Props) => {
item,
indices,
isReadOnly,
isLastItem,
onMouseDown,
}: Props) => {
const { typebot } = useTypebot() const { typebot } = useTypebot()
const { previewingEdge } = useGraph() const { previewingEdge } = useGraph()
const [isMouseOver, setIsMouseOver] = useState(false) const [isMouseOver, setIsMouseOver] = useState(false)
@@ -86,7 +79,6 @@ export const ItemNode = ({
item={item} item={item}
isMouseOver={isMouseOver} isMouseOver={isMouseOver}
indices={indices} indices={indices}
isLastItem={isLastItem}
/> />
{typebot && isConnectable && ( {typebot && isConnectable && (
<SourceEndpoint <SourceEndpoint

View File

@@ -7,15 +7,9 @@ type Props = {
item: Item item: Item
indices: ItemIndices indices: ItemIndices
isMouseOver: boolean isMouseOver: boolean
isLastItem: boolean
} }
export const ItemNodeContent = ({ export const ItemNodeContent = ({ item, indices, isMouseOver }: Props) => {
item,
indices,
isMouseOver,
isLastItem,
}: Props) => {
switch (item.type) { switch (item.type) {
case ItemType.BUTTON: case ItemType.BUTTON:
return ( return (
@@ -23,7 +17,6 @@ export const ItemNodeContent = ({
item={item} item={item}
isMouseOver={isMouseOver} isMouseOver={isMouseOver}
indices={indices} indices={indices}
isLastItem={isLastItem}
/> />
) )
case ItemType.CONDITION: case ItemType.CONDITION:

View File

@@ -15,16 +15,10 @@ import { isNotDefined } from 'utils'
type Props = { type Props = {
item: ButtonItem item: ButtonItem
indices: ItemIndices indices: ItemIndices
isLastItem: boolean
isMouseOver: boolean isMouseOver: boolean
} }
export const ButtonNodeContent = ({ export const ButtonNodeContent = ({ item, indices, isMouseOver }: Props) => {
item,
indices,
isMouseOver,
isLastItem,
}: Props) => {
const { deleteItem, updateItem, createItem } = useTypebot() const { deleteItem, updateItem, createItem } = useTypebot()
const [initialContent] = useState(item.content ?? '') const [initialContent] = useState(item.content ?? '')
const [itemValue, setItemValue] = useState(item.content ?? 'Click to edit') const [itemValue, setItemValue] = useState(item.content ?? 'Click to edit')
@@ -44,12 +38,7 @@ export const ButtonNodeContent = ({
const handleKeyPress = (e: React.KeyboardEvent<HTMLDivElement>) => { const handleKeyPress = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.key === 'Escape' && itemValue === 'Click to edit') deleteItem(indices) if (e.key === 'Escape' && itemValue === 'Click to edit') deleteItem(indices)
if ( if (e.key === 'Enter' && itemValue !== '' && initialContent === '')
e.key === 'Enter' &&
itemValue !== '' &&
isLastItem &&
initialContent === ''
)
handlePlusClick() handlePlusClick()
} }

View File

@@ -135,7 +135,6 @@ export const ItemNodesList = ({
indices={{ blockIndex, stepIndex, itemIndex: idx }} indices={{ blockIndex, stepIndex, itemIndex: idx }}
onMouseDown={handleStepMouseDown(idx)} onMouseDown={handleStepMouseDown(idx)}
isReadOnly={isReadOnly} isReadOnly={isReadOnly}
isLastItem={idx === step.items.length - 1}
/> />
<Flex <Flex
ref={handlePushElementRef(idx + 1)} ref={handlePushElementRef(idx + 1)}