1
- import { useEffect , useRef } from 'react'
1
+ import { useEffect , useRef , useState } from 'react'
2
2
import ReactFlow , { Controls , Background , useNodesState , useEdgesState } from 'reactflow'
3
3
import 'reactflow/dist/style.css'
4
4
import '@/views/canvas/index.css'
5
5
6
6
import { useLocation , useNavigate } from 'react-router-dom'
7
+ import { useSelector } from 'react-redux'
7
8
8
9
// material-ui
9
10
import { Toolbar , Box , AppBar } from '@mui/material'
@@ -14,6 +15,9 @@ import MarketplaceCanvasNode from './MarketplaceCanvasNode'
14
15
import MarketplaceCanvasHeader from './MarketplaceCanvasHeader'
15
16
import StickyNote from '../canvas/StickyNote'
16
17
18
+ // icons
19
+ import { IconMagnetFilled , IconMagnetOff } from '@tabler/icons-react'
20
+
17
21
const nodeTypes = { customNode : MarketplaceCanvasNode , stickyNote : StickyNote }
18
22
const edgeTypes = { buttonedge : '' }
19
23
@@ -22,15 +26,16 @@ const edgeTypes = { buttonedge: '' }
22
26
const MarketplaceCanvas = ( ) => {
23
27
const theme = useTheme ( )
24
28
const navigate = useNavigate ( )
29
+ const customization = useSelector ( ( state ) => state . customization )
25
30
26
31
const { state } = useLocation ( )
27
- const flowData = state ?. flowData || '{}'
28
- const name = state ?. name || 'Untitled'
32
+ const { flowData, name } = state
29
33
30
34
// ==============================|| ReactFlow ||============================== //
31
35
32
36
const [ nodes , setNodes , onNodesChange ] = useNodesState ( )
33
37
const [ edges , setEdges , onEdgesChange ] = useEdgesState ( )
38
+ const [ isSnappingEnabled , setIsSnappingEnabled ] = useState ( false )
34
39
35
40
const reactFlowWrapper = useRef ( null )
36
41
@@ -87,15 +92,29 @@ const MarketplaceCanvas = () => {
87
92
edgeTypes = { edgeTypes }
88
93
fitView
89
94
minZoom = { 0.1 }
95
+ snapGrid = { [ 25 , 25 ] }
96
+ snapToGrid = { isSnappingEnabled }
90
97
>
91
98
< Controls
99
+ className = { customization . isDarkMode ? 'dark-mode-controls' : '' }
92
100
style = { {
93
101
display : 'flex' ,
94
102
flexDirection : 'row' ,
95
103
left : '50%' ,
96
104
transform : 'translate(-50%, -50%)'
97
105
} }
98
- />
106
+ >
107
+ < button
108
+ className = 'react-flow__controls-button react-flow__controls-interactive'
109
+ onClick = { ( ) => {
110
+ setIsSnappingEnabled ( ! isSnappingEnabled )
111
+ } }
112
+ title = 'toggle snapping'
113
+ aria-label = 'toggle snapping'
114
+ >
115
+ { isSnappingEnabled ? < IconMagnetFilled /> : < IconMagnetOff /> }
116
+ </ button >
117
+ </ Controls >
99
118
< Background color = '#aaa' gap = { 16 } />
100
119
</ ReactFlow >
101
120
</ div >
0 commit comments