Skip to content

Commit

Permalink
feat(builder): Enable ctrl+z to undo changes in editor#217
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorgelig committed Jan 16, 2023
1 parent 65c35fa commit 14f4ee3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { headerHeight } from '../../constants'
import { EditableEmojiOrImageIcon } from '@/components/EditableEmojiOrImageIcon'
import { PublishButton } from '@/features/publish'
import { CollaborationMenuButton } from '@/features/collaboration'
import useUndoShortcut from '@/hooks/useUndoShortcut';

export const TypebotHeader = () => {
const router = useRouter()
Expand All @@ -52,6 +53,8 @@ export const TypebotHeader = () => {
setRightPanel(RightPanel.PREVIEW)
}

useUndoShortcut(canUndo, undo);

const handleHelpClick = () => {
isCloudProdInstance
? getBubbleActions().open()
Expand Down
38 changes: 38 additions & 0 deletions apps/builder/src/hooks/useUndoShortcut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useEffect } from 'react'

const useUndoShortcut = (canUndo: boolean, undo: () => void) => {
const isUndoShortcut = (event: KeyboardEvent) => {
const key = event.key.toLowerCase()
const cmdHeld = event.metaKey
const ctrlHeld = event.ctrlKey
const Z = 'z'

return (cmdHeld && key === Z) || (ctrlHeld && key === Z)
}

useEffect(() => {
document.addEventListener(
'keydown',
(event: KeyboardEvent) => {
if (isUndoShortcut(event) && canUndo) {
undo()
}
},
false
)

return () => {
document.removeEventListener(
'keydown',
(event: KeyboardEvent) => {
if (isUndoShortcut(event) && canUndo) {
undo()
}
},
false
)
}
}, [canUndo, undo])
}

export default useUndoShortcut;

0 comments on commit 14f4ee3

Please sign in to comment.