mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-21 18:46:00 +00:00
query designer - undo
This commit is contained in:
@@ -1,24 +1,31 @@
|
||||
import _ from 'lodash';
|
||||
import React from 'react';
|
||||
|
||||
function reducer(state, action) {
|
||||
const reducer = (options) => (state, action) => {
|
||||
const { mergeNearActions } = options || {};
|
||||
|
||||
const useMerge =
|
||||
action.useMerge || (mergeNearActions && state.lastActionTm && new Date().getTime() - state.lastActionTm < 100);
|
||||
|
||||
switch (action.type) {
|
||||
case 'set':
|
||||
return {
|
||||
history: [...state.history.slice(0, state.current + 1), action.value],
|
||||
current: state.current + 1,
|
||||
history: [...state.history.slice(0, useMerge ? state.current : state.current + 1), action.value],
|
||||
current: useMerge ? state.current : state.current + 1,
|
||||
value: action.value,
|
||||
canUndo: true,
|
||||
canRedo: false,
|
||||
lastActionTm: new Date().getTime(),
|
||||
};
|
||||
case 'compute': {
|
||||
const newValue = action.compute(state.history[state.current]);
|
||||
return {
|
||||
history: [...state.history.slice(0, state.current + 1), newValue],
|
||||
current: state.current + 1,
|
||||
history: [...state.history.slice(0, useMerge ? state.current : state.current + 1), newValue],
|
||||
current: useMerge ? state.current : state.current + 1,
|
||||
value: newValue,
|
||||
canUndo: true,
|
||||
canRedo: false,
|
||||
lastActionTm: new Date().getTime(),
|
||||
};
|
||||
}
|
||||
case 'undo':
|
||||
@@ -29,6 +36,7 @@ function reducer(state, action) {
|
||||
value: state.history[state.current - 1],
|
||||
canUndo: state.current > 1,
|
||||
canRedo: true,
|
||||
lastActionTm: null,
|
||||
};
|
||||
return state;
|
||||
case 'redo':
|
||||
@@ -39,6 +47,7 @@ function reducer(state, action) {
|
||||
value: state.history[state.current + 1],
|
||||
canUndo: true,
|
||||
canRedo: state.current < state.history.length - 2,
|
||||
lastActionTm: null,
|
||||
};
|
||||
return state;
|
||||
case 'reset':
|
||||
@@ -46,12 +55,13 @@ function reducer(state, action) {
|
||||
history: [action.value],
|
||||
current: 0,
|
||||
value: action.value,
|
||||
lastActionTm: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default function useUndoReducer(initialValue) {
|
||||
return React.useReducer(reducer, {
|
||||
export default function useUndoReducer(initialValue, options) {
|
||||
return React.useReducer(reducer(options), {
|
||||
history: [initialValue],
|
||||
current: 0,
|
||||
value: initialValue,
|
||||
|
||||
Reference in New Issue
Block a user