refactoring useUndoRedo component
This commit is contained in:
26
src/components/undoRedo/useUndoRedo.tsx
Normal file
26
src/components/undoRedo/useUndoRedo.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
export type UndoRedoAction = {
|
||||
undo: () => void;
|
||||
do: () => void;
|
||||
};
|
||||
|
||||
export type UndoRedoContextType = {
|
||||
addAction: (action: UndoRedoAction) => void;
|
||||
undo: () => void;
|
||||
redo: () => void;
|
||||
canUndo: boolean;
|
||||
canRedo: boolean;
|
||||
stackCount: { undo: number; redo: number };
|
||||
setShortcutKeys?: (undoKey?: string, redoKey?: string) => void;
|
||||
};
|
||||
|
||||
export const UndoRedoContext = createContext<UndoRedoContextType | null>(null);
|
||||
|
||||
export const useUndoRedo = () => {
|
||||
const ctx = useContext(UndoRedoContext);
|
||||
if (!ctx) {
|
||||
throw new Error("useUndoRedo must be used inside an UndoRedoProvider");
|
||||
}
|
||||
return ctx;
|
||||
};
|
||||
Reference in New Issue
Block a user