mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-21 19:56:02 +00:00
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
import React from 'react';
|
|
|
|
function reducer(state, action) {
|
|
switch (action.type) {
|
|
case 'set':
|
|
// console.log('SET', state.history, action.value);
|
|
return {
|
|
history: [...state.history.slice(0, state.current + 1), action.value],
|
|
current: state.current + 1,
|
|
value: action.value,
|
|
canUndo: true,
|
|
canRedo: false,
|
|
};
|
|
case 'undo':
|
|
if (state.current > 0)
|
|
return {
|
|
history: state.history,
|
|
current: state.current - 1,
|
|
value: state.history[state.current - 1],
|
|
canUndo: state.current > 1,
|
|
canRedo: true,
|
|
};
|
|
return state;
|
|
case 'redo':
|
|
if (state.current < state.history.length - 1)
|
|
return {
|
|
history: state.history,
|
|
current: state.current + 1,
|
|
value: state.history[state.current + 1],
|
|
canUndo: true,
|
|
canRedo: state.current < state.history.length - 2,
|
|
};
|
|
return state;
|
|
case 'reset':
|
|
return {
|
|
history: [action.value],
|
|
current: 0,
|
|
value: action.value,
|
|
};
|
|
}
|
|
}
|
|
|
|
export default function useUndoReducer(initialValue) {
|
|
return React.useReducer(reducer, {
|
|
history: [initialValue],
|
|
current: 0,
|
|
value: initialValue,
|
|
});
|
|
}
|