add block shortcut

This commit is contained in:
Yi 2024-08-23 15:54:29 +08:00
parent 071b7d607b
commit 1bcfa747db
6 changed files with 28 additions and 21 deletions

View File

@ -5,7 +5,7 @@ import { useWorkflowStore } from '../store'
export const usePanelInteractions = () => { export const usePanelInteractions = () => {
const workflowStore = useWorkflowStore() const workflowStore = useWorkflowStore()
const handlePaneContextMenu = useCallback((e: MouseEvent) => { const handlePanelContextMenu = useCallback((e: MouseEvent) => {
e.preventDefault() e.preventDefault()
const container = document.querySelector('#workflow-container') const container = document.querySelector('#workflow-container')
const { x, y } = container!.getBoundingClientRect() const { x, y } = container!.getBoundingClientRect()
@ -17,7 +17,7 @@ export const usePanelInteractions = () => {
}) })
}, [workflowStore]) }, [workflowStore])
const handlePaneContextmenuCancel = useCallback(() => { const handlePanelContextmenuCancel = useCallback(() => {
workflowStore.setState({ workflowStore.setState({
panelMenu: undefined, panelMenu: undefined,
}) })
@ -30,8 +30,8 @@ export const usePanelInteractions = () => {
}, [workflowStore]) }, [workflowStore])
return { return {
handlePaneContextMenu, handlePanelContextMenu,
handlePaneContextmenuCancel, handlePanelContextmenuCancel,
handleNodeContextmenuCancel, handleNodeContextmenuCancel,
} }
} }

View File

@ -91,8 +91,10 @@ export const useShortcuts = (): void => {
}, { exactMatch: true, useCapture: true }) }, { exactMatch: true, useCapture: true })
useKeyPress(`${getKeyboardKeyCodeBySystem('shift')}.a`, (e) => { useKeyPress(`${getKeyboardKeyCodeBySystem('shift')}.a`, (e) => {
if (shouldHandleShortcut(e)) if (shouldHandleShortcut(e)) {
e.preventDefault() e.preventDefault()
workflowStore.setState({ showAddBlock: true })
}
}, { exactMatch: true, useCapture: true }) }, { exactMatch: true, useCapture: true })
useKeyPress(`${getKeyboardKeyCodeBySystem('alt')}.r`, (e) => { useKeyPress(`${getKeyboardKeyCodeBySystem('alt')}.r`, (e) => {

View File

@ -13,14 +13,14 @@ import { usePanelInteractions } from './hooks'
const NodeContextmenu = () => { const NodeContextmenu = () => {
const ref = useRef(null) const ref = useRef(null)
const nodes = useNodes() const nodes = useNodes()
const { handleNodeContextmenuCancel, handlePaneContextmenuCancel } = usePanelInteractions() const { handleNodeContextmenuCancel, handlePanelContextmenuCancel } = usePanelInteractions()
const nodeMenu = useStore(s => s.nodeMenu) const nodeMenu = useStore(s => s.nodeMenu)
const currentNode = nodes.find(node => node.id === nodeMenu?.nodeId) as Node const currentNode = nodes.find(node => node.id === nodeMenu?.nodeId) as Node
useEffect(() => { useEffect(() => {
if (nodeMenu) if (nodeMenu)
handlePaneContextmenuCancel() handlePanelContextmenuCancel()
}, [nodeMenu, handlePaneContextmenuCancel]) }, [nodeMenu, handlePanelContextmenuCancel])
useClickAway(() => { useClickAway(() => {
handleNodeContextmenuCancel() handleNodeContextmenuCancel()

View File

@ -1,7 +1,6 @@
import { import {
memo, memo,
useCallback, useCallback,
useState,
} from 'react' } from 'react'
import { RiAddCircleFill } from '@remixicon/react' import { RiAddCircleFill } from '@remixicon/react'
import { useStoreApi } from 'reactflow' import { useStoreApi } from 'reactflow'
@ -16,7 +15,7 @@ import {
usePanelInteractions, usePanelInteractions,
} from '../hooks' } from '../hooks'
import { NODES_INITIAL_DATA } from '../constants' import { NODES_INITIAL_DATA } from '../constants'
import { useWorkflowStore } from '../store' import { useStore, useWorkflowStore } from '../store'
import TipPopup from './tip-popup' import TipPopup from './tip-popup'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import BlockSelector from '@/app/components/workflow/block-selector' import BlockSelector from '@/app/components/workflow/block-selector'
@ -39,15 +38,17 @@ const AddBlock = ({
const store = useStoreApi() const store = useStoreApi()
const workflowStore = useWorkflowStore() const workflowStore = useWorkflowStore()
const { nodesReadOnly } = useNodesReadOnly() const { nodesReadOnly } = useNodesReadOnly()
const { handlePaneContextmenuCancel } = usePanelInteractions() const { handlePanelContextmenuCancel } = usePanelInteractions()
const [open, setOpen] = useState(false)
const { availableNextBlocks } = useAvailableBlocks(BlockEnum.Start, false) const { availableNextBlocks } = useAvailableBlocks(BlockEnum.Start, false)
const showAddBlock = useStore(state => state.showAddBlock)
const setShowAddBlock = useStore(state => state.setShowAddBlock)
const handleOpenChange = useCallback((open: boolean) => { const handleOpenChange = useCallback((open: boolean) => {
setOpen(open) setShowAddBlock(open)
if (!open) if (!open)
handlePaneContextmenuCancel() handlePanelContextmenuCancel()
}, [handlePaneContextmenuCancel]) }, [setShowAddBlock, handlePanelContextmenuCancel])
const handleSelect = useCallback<OnSelectBlock>((type, toolDefaultValue) => { const handleSelect = useCallback<OnSelectBlock>((type, toolDefaultValue) => {
const { const {
@ -90,7 +91,7 @@ const AddBlock = ({
return ( return (
<BlockSelector <BlockSelector
open={open} open={showAddBlock}
onOpenChange={handleOpenChange} onOpenChange={handleOpenChange}
disabled={nodesReadOnly} disabled={nodesReadOnly}
onSelect={handleSelect} onSelect={handleSelect}

View File

@ -24,7 +24,7 @@ const PanelContextmenu = () => {
const clipboardElements = useStore(s => s.clipboardElements) const clipboardElements = useStore(s => s.clipboardElements)
const setShowImportDSLModal = useStore(s => s.setShowImportDSLModal) const setShowImportDSLModal = useStore(s => s.setShowImportDSLModal)
const { handleNodesPaste } = useNodesInteractions() const { handleNodesPaste } = useNodesInteractions()
const { handlePaneContextmenuCancel, handleNodeContextmenuCancel } = usePanelInteractions() const { handlePanelContextmenuCancel, handleNodeContextmenuCancel } = usePanelInteractions()
const { handleStartWorkflowRun } = useWorkflowStartRun() const { handleStartWorkflowRun } = useWorkflowStartRun()
const { handleAddNote } = useOperator() const { handleAddNote } = useOperator()
const { exportCheck } = useDSL() const { exportCheck } = useDSL()
@ -35,7 +35,7 @@ const PanelContextmenu = () => {
}, [panelMenu, handleNodeContextmenuCancel]) }, [panelMenu, handleNodeContextmenuCancel])
useClickAway(() => { useClickAway(() => {
handlePaneContextmenuCancel() handlePanelContextmenuCancel()
}, ref) }, ref)
const renderTrigger = () => { const renderTrigger = () => {
@ -74,7 +74,7 @@ const PanelContextmenu = () => {
onClick={(e) => { onClick={(e) => {
e.stopPropagation() e.stopPropagation()
handleAddNote() handleAddNote()
handlePaneContextmenuCancel() handlePanelContextmenuCancel()
}} }}
> >
{t('workflow.nodes.note.addNote')} {t('workflow.nodes.note.addNote')}
@ -83,7 +83,7 @@ const PanelContextmenu = () => {
className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50' className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
onClick={() => { onClick={() => {
handleStartWorkflowRun() handleStartWorkflowRun()
handlePaneContextmenuCancel() handlePanelContextmenuCancel()
}} }}
> >
{t('workflow.common.run')} {t('workflow.common.run')}
@ -100,7 +100,7 @@ const PanelContextmenu = () => {
onClick={() => { onClick={() => {
if (clipboardElements.length) { if (clipboardElements.length) {
handleNodesPaste() handleNodesPaste()
handlePaneContextmenuCancel() handlePanelContextmenuCancel()
} }
}} }}
> >

View File

@ -162,6 +162,8 @@ type Shape = {
setControlPromptEditorRerenderKey: (controlPromptEditorRerenderKey: number) => void setControlPromptEditorRerenderKey: (controlPromptEditorRerenderKey: number) => void
showImportDSLModal: boolean showImportDSLModal: boolean
setShowImportDSLModal: (showImportDSLModal: boolean) => void setShowImportDSLModal: (showImportDSLModal: boolean) => void
showAddBlock: boolean
setShowAddBlock: (showAddBlock: boolean) => void
} }
export const createWorkflowStore = () => { export const createWorkflowStore = () => {
@ -262,6 +264,8 @@ export const createWorkflowStore = () => {
setControlPromptEditorRerenderKey: controlPromptEditorRerenderKey => set(() => ({ controlPromptEditorRerenderKey })), setControlPromptEditorRerenderKey: controlPromptEditorRerenderKey => set(() => ({ controlPromptEditorRerenderKey })),
showImportDSLModal: false, showImportDSLModal: false,
setShowImportDSLModal: showImportDSLModal => set(() => ({ showImportDSLModal })), setShowImportDSLModal: showImportDSLModal => set(() => ({ showImportDSLModal })),
showAddBlock: false,
setShowAddBlock: showAddBlock => set(() => ({ showAddBlock })),
})) }))
} }