Skip to content

Image Pipeline Playground

feat(frontend/canvas): implement node context menu for actions

Mohammad Rajaeicf3bcfa2cb71
diff --git a/frontend/src/features/canvas/NodeMenu.tsx b/frontend/src/features/canvas/NodeMenu.tsx
new file mode 100644
index 0000000..be1a939
--- /dev/null
+++ b/frontend/src/features/canvas/NodeMenu.tsx
@@ -0,0 +1,276 @@
+import { useCallback, useState, type MouseEvent, type ReactNode } from 'react'
+import {
+ Box,
+ Divider,
+ IconButton,
+ ListItemIcon,
+ ListItemText,
+ Menu,
+ MenuItem,
+ Typography,
+} from '@mui/material'
+import { useGraphStore } from '../../store/graphStore'
+
+type MenuAnchor =
+ | { kind: 'element'; el: HTMLElement }
+ | { kind: 'position'; top: number; left: number }
+
+function MenuGlyph({ children, danger }: { children: ReactNode; danger?: boolean }) {
+ return (
+ <Box
+ component="span"
+ sx={{
+ width: 28,
+ height: 28,
+ borderRadius: 1,
+ display: 'grid',
+ placeItems: 'center',
+ bgcolor: danger ? 'rgba(192,57,43,0.18)' : 'rgba(255,255,255,0.06)',
+ color: danger ? '#ff8a80' : 'rgba(255,255,255,0.85)',
+ fontSize: 14,
+ fontWeight: 700,
+ lineHeight: 1,
+ }}
+ >
+ {children}
+ </Box>
+ )
+}
+
+function ShortcutHint({ keys }: { keys: string }) {
+ return (
+ <Typography
+ component="span"
+ sx={{
+ ml: 2,
+ color: 'rgba(255,255,255,0.38)',
+ fontSize: 11,
+ fontWeight: 600,
+ letterSpacing: '0.04em',
+ fontFamily: '"IBM Plex Sans", "Segoe UI", sans-serif',
+ }}
+ >
+ {keys}
+ </Typography>
+ )
+}
+
+export function useNodeMenu({
+ nodeId,
+ label,
+ category,
+}: {
+ nodeId: string
+ label: string
+ category: string
+}) {
+ const [anchor, setAnchor] = useState<MenuAnchor | null>(null)
+ const removeNode = useGraphStore((state) => state.removeNode)
+ const duplicateNode = useGraphStore((state) => state.duplicateNode)
+ const selectNode = useGraphStore((state) => state.selectNode)
+ const open = Boolean(anchor)
+ const isMac =
+ typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform)
+ const modKey = isMac ? '⌘' : 'Ctrl'
+
+ const close = useCallback(() => setAnchor(null), [])
+
+ const openFromButton = useCallback(
+ (event: MouseEvent<HTMLElement>) => {
+ event.stopPropagation()
+ event.preventDefault()
+ selectNode(nodeId)
+ setAnchor({ kind: 'element', el: event.currentTarget })
+ },
+ [nodeId, selectNode],
+ )
+
+ const openFromContext = useCallback(
+ (event: MouseEvent) => {
+ event.preventDefault()
+ event.stopPropagation()
+ selectNode(nodeId)
+ setAnchor({ kind: 'position', top: event.clientY, left: event.clientX })
+ },
+ [nodeId, selectNode],
+ )
+
+ const runDuplicate = useCallback(() => {
+ duplicateNode(nodeId)
+ close()
+ }, [close, duplicateNode, nodeId])
+
+ const runDelete = useCallback(() => {
+ removeNode(nodeId)
+ close()
+ }, [close, nodeId, removeNode])
+
+ return {
+ openFromContext,
+ menu: (
+ <>
+ <IconButton
+ size="small"
+ className="nodrag nopan"
+ aria-label="Node actions"
+ aria-haspopup="menu"
+ aria-expanded={open}
+ onClick={openFromButton}
+ onMouseDown={(event) => event.stopPropagation()}
+ sx={{
+ width: 28,
+ height: 28,
+ color: 'rgba(255,255,255,0.55)',
+ bgcolor: open ? 'rgba(255,255,255,0.1)' : 'transparent',
+ border: '1px solid',
+ borderColor: open ? 'rgba(255,255,255,0.16)' : 'transparent',
+ transition: 'background-color 120ms ease, border-color 120ms ease, color 120ms ease',
+ '&:hover': {
+ color: '#fff',
+ bgcolor: 'rgba(255,255,255,0.1)',
+ borderColor: 'rgba(255,255,255,0.14)',
+ },
+ }}
+ >
+ <Box
+ component="span"
+ sx={{
+ display: 'grid',
+ gap: '3px',
+ placeItems: 'center',
+ }}
+ >
+ {[0, 1, 2].map((dot) => (
+ <Box
+ key={dot}
+ sx={{
+ width: 3,
+ height: 3,
+ borderRadius: '50%',
+ bgcolor: 'currentColor',
+ }}
+ />
+ ))}
+ </Box>
+ </IconButton>
+
+ <Menu
+ className="nodrag nopan"
+ open={open}
+ onClose={close}
+ onClick={(event) => event.stopPropagation()}
+ anchorReference={anchor?.kind === 'position' ? 'anchorPosition' : 'anchorEl'}
+ anchorEl={anchor?.kind === 'element' ? anchor.el : undefined}
+ anchorPosition={
+ anchor?.kind === 'position'
+ ? { top: anchor.top, left: anchor.left }
+ : undefined
+ }
+ anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
+ transformOrigin={{ vertical: 'top', horizontal: 'right' }}
+ slotProps={{
+ paper: {
+ elevation: 0,
+ sx: {
+ mt: anchor?.kind === 'element' ? 0.75 : 0,
+ minWidth: 220,
+ bgcolor: '#161616',
+ color: '#f4f1ea',
+ border: '1px solid rgba(255,255,255,0.1)',
+ borderRadius: 1.5,
+ overflow: 'hidden',
+ boxShadow: '0 18px 40px rgba(0,0,0,0.55)',
+ backgroundImage: 'none',
+ },
+ },
+ list: {
+ dense: true,
+ sx: { py: 0.5 },
+ },
+ }}
+ >
+ <Box
+ sx={{
+ px: 1.5,
+ pt: 1.25,
+ pb: 1,
+ borderBottom: '1px solid rgba(255,255,255,0.08)',
+ }}
+ >
+ <Typography
+ sx={{
+ color: 'rgba(255,255,255,0.42)',
+ fontSize: 10,
+ fontWeight: 700,
+ letterSpacing: '0.08em',
+ textTransform: 'uppercase',
+ lineHeight: 1.2,
+ }}
+ >
+ {category}
+ </Typography>
+ <Typography
+ sx={{
+ mt: 0.25,
+ fontSize: 13,
+ fontWeight: 700,
+ lineHeight: 1.3,
+ whiteSpace: 'nowrap',
+ overflow: 'hidden',
+ textOverflow: 'ellipsis',
+ maxWidth: 190,
+ }}
+ >
+ {label}
+ </Typography>
+ </Box>
+
+ <MenuItem
+ className="nodrag nopan"
+ onClick={runDuplicate}
+ sx={{
+ mx: 0.5,
+ mt: 0.5,
+ borderRadius: 1,
+ py: 1,
+ '&:hover': { bgcolor: 'rgba(125,206,160,0.12)' },
+ }}
+ >
+ <ListItemIcon sx={{ minWidth: 40 }}>
+ <MenuGlyph>⎘</MenuGlyph>
+ </ListItemIcon>
+ <ListItemText
+ primary="Duplicate"
+ primaryTypographyProps={{ fontSize: 13, fontWeight: 600 }}
+ />
+ <ShortcutHint keys={`${modKey}+D`} />
+ </MenuItem>
+
+ <Divider sx={{ my: 0.75, borderColor: 'rgba(255,255,255,0.08)' }} />
+
+ <MenuItem
+ className="nodrag nopan"
+ onClick={runDelete}
+ sx={{
+ mx: 0.5,
+ mb: 0.5,
+ borderRadius: 1,
+ py: 1,
+ color: '#ff8a80',
+ '&:hover': { bgcolor: 'rgba(192,57,43,0.16)' },
+ }}
+ >
+ <ListItemIcon sx={{ minWidth: 40 }}>
+ <MenuGlyph danger>⌫</MenuGlyph>
+ </ListItemIcon>
+ <ListItemText
+ primary="Delete"
+ primaryTypographyProps={{ fontSize: 13, fontWeight: 600, color: '#ff8a80' }}
+ />
+ <ShortcutHint keys="Del" />
+ </MenuItem>
+ </Menu>
+ </>
+ ),
+ }
+}
diff --git a/frontend/src/features/canvas/PipelineCanvas.tsx b/frontend/src/features/canvas/PipelineCanvas.tsx
index 3259f60..7877396 100644
--- a/frontend/src/features/canvas/PipelineCanvas.tsx
+++ b/frontend/src/features/canvas/PipelineCanvas.tsx
@@ -25,6 +25,8 @@ export function PipelineCanvas() {
const onConnect = useGraphStore((s) => s.onConnect)
const selectNode = useGraphStore((s) => s.selectNode)
const addNodeFromType = useGraphStore((s) => s.addNodeFromType)
+ const duplicateNode = useGraphStore((s) => s.duplicateNode)
+ const selectedNodeId = useGraphStore((s) => s.selectedNodeId)
const nodeTypes: NodeTypes = useMemo(() => ({ pipeline: PipelineNodeView }), [])
const edgeTypes: EdgeTypes = useMemo(() => ({ editable: EditableEdge }), [])
@@ -59,12 +61,25 @@ export function PipelineCanvas() {
[],
)
+ const onKeyDown = useCallback(
+ (event: React.KeyboardEvent) => {
+ if (!(event.metaKey || event.ctrlKey) || event.key.toLowerCase() !== 'd') return
+ if (!selectedNodeId) return
+ event.preventDefault()
+ duplicateNode(selectedNodeId)
+ },
+ [duplicateNode, selectedNodeId],
+ )
+
return (
<Box
+ tabIndex={0}
+ onKeyDown={onKeyDown}
sx={{
width: '100%',
height: '100%',
bgcolor: '#0c0c0c',
+ outline: 'none',
// Connectors above node bodies; bend/remove controls stay on top.
'& .react-flow__nodes': { zIndex: 1 },
'& .react-flow__edges': { zIndex: 1000 },
diff --git a/frontend/src/features/canvas/PipelineNode.tsx b/frontend/src/features/canvas/PipelineNode.tsx
index d9cfa1e..8c0d7ac 100644
--- a/frontend/src/features/canvas/PipelineNode.tsx
+++ b/frontend/src/features/canvas/PipelineNode.tsx
@@ -11,16 +11,12 @@ import {
Dialog,
DialogContent,
DialogTitle,
- IconButton,
- ListItemIcon,
- ListItemText,
- Menu,
- MenuItem,
Stack,
Typography,
} from '@mui/material'
import type { GraphNodeData, PortSpec } from '../../types'
import { useGraphStore, type NodeImageState } from '../../store/graphStore'
+import { useNodeMenu } from './NodeMenu'
type PipelineFlowNode = Node<GraphNodeData, 'pipeline'>
@@ -207,70 +203,6 @@ function buildPreviewGrid({
return { columns, rows: [], sectionPorts }
}
-
-function NodeActions({ nodeId }: { nodeId: string }) {
- const [anchor, setAnchor] = useState<HTMLElement | null>(null)
- const removeNode = useGraphStore((state) => state.removeNode)
- const duplicateNode = useGraphStore((state) => state.duplicateNode)
- const selectNode = useGraphStore((state) => state.selectNode)
-
- return (
- <>
- <IconButton
- size="small"
- className="nodrag nopan"
- aria-label="Node actions"
- onClick={(event) => {
- event.stopPropagation()
- selectNode(nodeId)
- setAnchor(event.currentTarget)
- }}
- sx={{ width: 26, height: 26, color: 'rgba(255,255,255,0.75)' }}
- >
- <Box component="span" sx={{ fontSize: 16, lineHeight: 1, fontWeight: 700 }}>
- ⋮
- </Box>
- </IconButton>
- <Menu
- anchorEl={anchor}
- open={Boolean(anchor)}
- onClose={() => setAnchor(null)}
- onClick={(event) => event.stopPropagation()}
- >
- <MenuItem
- className="nodrag nopan"
- onClick={() => {
- duplicateNode(nodeId)
- setAnchor(null)
- }}
- >
- <ListItemIcon>
- <Box component="span" sx={{ fontSize: 14 }}>
- ⎘
- </Box>
- </ListItemIcon>
- <ListItemText>Duplicate</ListItemText>
- </MenuItem>
- <MenuItem
- className="nodrag nopan"
- onClick={() => {
- removeNode(nodeId)
- setAnchor(null)
- }}
- sx={{ color: 'error.main' }}
- >
- <ListItemIcon>
- <Box component="span" sx={{ fontSize: 14, color: 'error.main' }}>
- ⌫
- </Box>
- </ListItemIcon>
- <ListItemText>Delete</ListItemText>
- </MenuItem>
- </Menu>
- </>
- )
-}
-
export function PipelineNodeView({ id, data, selected }: NodeProps<PipelineFlowNode>) {
const [viewer, setViewer] = useState<ImageItem | null>(null)
const updateNodeInternals = useUpdateNodeInternals()
@@ -279,6 +211,11 @@ export function PipelineNodeView({ id, data, selected }: NodeProps<PipelineFlowN
const localPreviewUrls = useGraphStore(
(state) => state.nodes.find((node) => node.id === id)?.data.localPreviewUrls ?? [],
)
+ const { menu: nodeMenu, openFromContext } = useNodeMenu({
+ nodeId: id,
+ label: data.label,
+ category: data.category,
+ })
const ports = useMemo(() => {
if (data.ports?.length) return data.ports
@@ -333,6 +270,7 @@ export function PipelineNodeView({ id, data, selected }: NodeProps<PipelineFlowN
return (
<>
<Box
+ onContextMenu={openFromContext}
sx={{
position: 'relative',
width: nodeWidth,
@@ -388,7 +326,7 @@ export function PipelineNodeView({ id, data, selected }: NodeProps<PipelineFlowN
{data.label}
</Typography>
</Box>
- <NodeActions nodeId={id} />
+ {nodeMenu}
</Stack>
<Box