🐛 (editor) Fix undo / redo not working properly on button nodes
This commit is contained in:
@ -9,7 +9,7 @@ import {
|
|||||||
import { PlusIcon } from '@/components/icons'
|
import { PlusIcon } from '@/components/icons'
|
||||||
import { useTypebot } from '@/features/editor'
|
import { useTypebot } from '@/features/editor'
|
||||||
import { ButtonItem, ItemIndices, ItemType } from 'models'
|
import { ButtonItem, ItemIndices, ItemType } from 'models'
|
||||||
import React, { useEffect, useRef, useState } from 'react'
|
import React, { useRef, useState } from 'react'
|
||||||
import { isNotDefined } from 'utils'
|
import { isNotDefined } from 'utils'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
@ -20,16 +20,9 @@ type Props = {
|
|||||||
|
|
||||||
export const ButtonsItemNode = ({ item, indices, isMouseOver }: Props) => {
|
export const ButtonsItemNode = ({ item, indices, isMouseOver }: Props) => {
|
||||||
const { deleteItem, updateItem, createItem } = useTypebot()
|
const { deleteItem, updateItem, createItem } = useTypebot()
|
||||||
const [initialContent] = useState(item.content ?? '')
|
|
||||||
const [itemValue, setItemValue] = useState(item.content ?? 'Click to edit')
|
const [itemValue, setItemValue] = useState(item.content ?? 'Click to edit')
|
||||||
const editableRef = useRef<HTMLDivElement | null>(null)
|
const editableRef = useRef<HTMLDivElement | null>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (itemValue !== item.content)
|
|
||||||
setItemValue(item.content ?? 'Click to edit')
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [item])
|
|
||||||
|
|
||||||
const handleInputSubmit = () => {
|
const handleInputSubmit = () => {
|
||||||
if (itemValue === '') deleteItem(indices)
|
if (itemValue === '') deleteItem(indices)
|
||||||
else
|
else
|
||||||
@ -38,7 +31,7 @@ export const ButtonsItemNode = ({ item, indices, isMouseOver }: Props) => {
|
|||||||
|
|
||||||
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 (e.key === 'Enter' && itemValue !== '' && initialContent === '')
|
if (e.key === 'Enter' && itemValue !== '' && itemValue !== 'Click to edit')
|
||||||
handlePlusClick()
|
handlePlusClick()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ export const ItemNodeContent = ({ item, indices, isMouseOver }: Props) => {
|
|||||||
case ItemType.BUTTON:
|
case ItemType.BUTTON:
|
||||||
return (
|
return (
|
||||||
<ButtonsItemNode
|
<ButtonsItemNode
|
||||||
|
key={item.content}
|
||||||
item={item}
|
item={item}
|
||||||
isMouseOver={isMouseOver}
|
isMouseOver={isMouseOver}
|
||||||
indices={indices}
|
indices={indices}
|
||||||
|
Reference in New Issue
Block a user