| 7 | import { createInitialState, createShape, PRESETS } from './types'; |
| 8 | |
| 9 | const useHistory = initialState => { |
| 10 | const [history, setHistory] = useState([initialState]); |
| 11 | const [historyIndex, setHistoryIndex] = useState(0); |
| 12 | |
| 13 | const current = history[historyIndex]; |
| 14 | |
| 15 | const push = useCallback( |
| 16 | newState => { |
| 17 | setHistory(prev => [...prev.slice(0, historyIndex + 1), newState]); |
| 18 | setHistoryIndex(prev => prev + 1); |
| 19 | }, |
| 20 | [historyIndex] |
| 21 | ); |
| 22 | |
| 23 | const undo = useCallback(() => { |
| 24 | if (historyIndex > 0) { |
| 25 | setHistoryIndex(prev => prev - 1); |
| 26 | } |
| 27 | }, [historyIndex]); |
| 28 | |
| 29 | const redo = useCallback(() => { |
| 30 | if (historyIndex < history.length - 1) { |
| 31 | setHistoryIndex(prev => prev + 1); |
| 32 | } |
| 33 | }, [historyIndex, history.length]); |
| 34 | |
| 35 | const canUndo = historyIndex > 0; |
| 36 | const canRedo = historyIndex < history.length - 1; |
| 37 | |
| 38 | return { current, push, undo, redo, canUndo, canRedo }; |
| 39 | }; |
| 40 | |
| 41 | export default function ShapeMagic({ toolSelector }) { |
| 42 | const isMobile = useBreakpointValue({ base: true, lg: false }); |