dispatch/client/js/state/input.js

70 lines
1.3 KiB
JavaScript
Raw Normal View History

import createReducer from 'utils/createReducer';
import * as actions from './actions';
2015-12-28 23:34:32 +00:00
const HISTORY_MAX_LENGTH = 128;
2018-04-25 03:36:27 +00:00
const initialState = {
history: [],
2015-12-28 23:34:32 +00:00
index: 0
2018-04-25 03:36:27 +00:00
};
2015-12-28 23:34:32 +00:00
export const getCurrentInputHistoryEntry = state => {
if (state.input.index === -1) {
return null;
}
2018-04-25 03:36:27 +00:00
return state.input.history[state.input.index];
};
2018-04-25 03:36:27 +00:00
export default createReducer(initialState, {
[actions.INPUT_HISTORY_ADD](state, { line }) {
if (line.trim() && line !== state.history[0]) {
if (state.history.length === HISTORY_MAX_LENGTH) {
2018-04-25 03:36:27 +00:00
state.history.pop();
2015-12-28 23:34:32 +00:00
}
2018-04-25 03:36:27 +00:00
state.history.unshift(line);
2015-12-28 23:34:32 +00:00
}
},
[actions.INPUT_HISTORY_RESET](state) {
2018-04-25 03:36:27 +00:00
state.index = -1;
2015-12-28 23:34:32 +00:00
},
[actions.INPUT_HISTORY_INCREMENT](state) {
2018-04-25 03:36:27 +00:00
if (state.index < state.history.length - 1) {
state.index++;
2015-12-28 23:34:32 +00:00
}
},
[actions.INPUT_HISTORY_DECREMENT](state) {
if (state.index >= 0) {
2018-04-25 03:36:27 +00:00
state.index--;
2015-12-28 23:34:32 +00:00
}
}
});
export function addInputHistory(line) {
return {
type: actions.INPUT_HISTORY_ADD,
line
};
}
export function resetInputHistory() {
return {
type: actions.INPUT_HISTORY_RESET
};
}
export function incrementInputHistory() {
return {
type: actions.INPUT_HISTORY_INCREMENT
};
}
export function decrementInputHistory() {
return {
type: actions.INPUT_HISTORY_DECREMENT
};
}