import type { MouseEvent } from 'react' import { memo, useCallback, } from 'react' import { useTranslation } from 'react-i18next' import cn from 'classnames' import { RiCursorLine, RiFunctionAddLine, RiHand, RiStickyNoteAddLine, } from '@remixicon/react' import { useKeyPress } from 'ahooks' import { useNodesReadOnly, useSelectionInteractions, useWorkflow, } from '../hooks' import { isEventTargetInputArea } from '../utils' import { useStore } from '../store' import AddBlock from './add-block' import TipPopup from './tip-popup' import { useOperator } from './hooks' const Control = () => { const { t } = useTranslation() const controlMode = useStore(s => s.controlMode) const setControlMode = useStore(s => s.setControlMode) const { handleLayout } = useWorkflow() const { handleAddNote } = useOperator() const { nodesReadOnly, getNodesReadOnly, } = useNodesReadOnly() const { handleSelectionCancel } = useSelectionInteractions() const handleModePointer = useCallback(() => { if (getNodesReadOnly()) return setControlMode('pointer') }, [getNodesReadOnly, setControlMode]) const handleModeHand = useCallback(() => { if (getNodesReadOnly()) return setControlMode('hand') handleSelectionCancel() }, [getNodesReadOnly, setControlMode, handleSelectionCancel]) useKeyPress('h', (e) => { if (getNodesReadOnly()) return if (isEventTargetInputArea(e.target as HTMLElement)) return e.preventDefault() handleModeHand() }, { exactMatch: true, useCapture: true, }) useKeyPress('v', (e) => { if (isEventTargetInputArea(e.target as HTMLElement)) return e.preventDefault() handleModePointer() }, { exactMatch: true, useCapture: true, }) const goLayout = () => { if (getNodesReadOnly()) return handleLayout() } const addNote = (e: MouseEvent) => { if (getNodesReadOnly()) return e.stopPropagation() handleAddNote() } return (
) } export default memo(Control)