27 lines
704 B
TypeScript
27 lines
704 B
TypeScript
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;
|
|
};
|