save dialog

This commit is contained in:
Jan Prochazka
2021-03-06 18:01:39 +01:00
parent b64b6be68a
commit 423644e9d9
11 changed files with 573 additions and 29 deletions

View File

@@ -0,0 +1,11 @@
import { writable } from 'svelte/store';
export default function createReducer(reducer, initialState): any {
const state = writable(initialState);
function dispatch(action) {
state.update(x => reducer(x, action));
}
return [state, dispatch];
}

View File

@@ -0,0 +1,69 @@
import _ from 'lodash';
import createReducer from './createReducer';
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, 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, 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':
if (state.current > 0)
return {
history: state.history,
current: state.current - 1,
value: state.history[state.current - 1],
canUndo: state.current > 1,
canRedo: true,
lastActionTm: null,
};
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,
lastActionTm: null,
};
return state;
case 'reset':
return {
history: [action.value],
current: 0,
value: action.value,
lastActionTm: null,
};
}
};
export default function createUndoReducer(initialValue, options = null) {
return createReducer(reducer(options), {
history: [initialValue],
current: 0,
value: initialValue,
});
}