aboutsummaryrefslogtreecommitdiff
path: root/src/console/reducers
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-02-25 22:04:12 +0900
committerGitHub <noreply@github.com>2019-02-25 22:04:12 +0900
commit124e221999e914d63326a91602517d1e5e1bea73 (patch)
tree99184620375b5608c31b58f14d74cb450f7f4224 /src/console/reducers
parentcebd64a69f705482957a5e6a3f23054ca7985aa4 (diff)
parent9868da1ac479ee75a4abe8142a9b406a18b5ac52 (diff)
Merge pull request #538 from ueokande/scroll-console
Scroll console
Diffstat (limited to 'src/console/reducers')
-rw-r--r--src/console/reducers/index.js70
1 files changed, 28 insertions, 42 deletions
diff --git a/src/console/reducers/index.js b/src/console/reducers/index.js
index 7dcad17..614a72f 100644
--- a/src/console/reducers/index.js
+++ b/src/console/reducers/index.js
@@ -6,52 +6,43 @@ const defaultState = {
consoleText: '',
completionSource: '',
completions: [],
- groupSelection: -1,
- itemSelection: -1,
+ select: -1,
+ viewIndex: 0,
};
const nextSelection = (state) => {
if (state.completions.length === 0) {
- return [-1, -1];
+ return -1;
}
- if (state.groupSelection < 0) {
- return [0, 0];
+ if (state.select < 0) {
+ return 0;
}
- let group = state.completions[state.groupSelection];
- if (state.groupSelection + 1 >= state.completions.length &&
- state.itemSelection + 1 >= group.items.length) {
- return [-1, -1];
+ let length = state.completions
+ .map(g => g.items.length)
+ .reduce((x, y) => x + y);
+ if (state.select + 1 < length) {
+ return state.select + 1;
}
- if (state.itemSelection + 1 >= group.items.length) {
- return [state.groupSelection + 1, 0];
- }
- return [state.groupSelection, state.itemSelection + 1];
+ return -1;
};
const prevSelection = (state) => {
- if (state.groupSelection < 0) {
- return [
- state.completions.length - 1,
- state.completions[state.completions.length - 1].items.length - 1
- ];
- }
- if (state.groupSelection === 0 && state.itemSelection === 0) {
- return [-1, -1];
- } else if (state.itemSelection === 0) {
- return [
- state.groupSelection - 1,
- state.completions[state.groupSelection - 1].items.length - 1
- ];
+ let length = state.completions
+ .map(g => g.items.length)
+ .reduce((x, y) => x + y);
+ if (state.select < 0) {
+ return length - 1;
}
- return [state.groupSelection, state.itemSelection - 1];
+ return state.select - 1;
};
-const nextConsoleText = (completions, group, item, defaults) => {
- if (group < 0 || item < 0) {
+const nextConsoleText = (completions, select, defaults) => {
+ if (select < 0) {
return defaults;
}
- return completions[group].items[item].content;
+ let items = completions.map(g => g.items).reduce((g1, g2) => g1.concat(g2));
+ return items[select].content;
};
// eslint-disable-next-line max-lines-per-function
@@ -90,25 +81,20 @@ export default function reducer(state = defaultState, action = {}) {
return { ...state,
completions: action.completions,
completionSource: action.completionSource,
- groupSelection: -1,
- itemSelection: -1, };
+ select: -1 };
case actions.CONSOLE_COMPLETION_NEXT: {
- let next = nextSelection(state);
+ let select = nextSelection(state);
return { ...state,
- groupSelection: next[0],
- itemSelection: next[1],
+ select: select,
consoleText: nextConsoleText(
- state.completions, next[0], next[1],
- state.completionSource), };
+ state.completions, select, state.completionSource) };
}
case actions.CONSOLE_COMPLETION_PREV: {
- let next = prevSelection(state);
+ let select = prevSelection(state);
return { ...state,
- groupSelection: next[0],
- itemSelection: next[1],
+ select: select,
consoleText: nextConsoleText(
- state.completions, next[0], next[1],
- state.completionSource), };
+ state.completions, select, state.completionSource) };
}
default:
return state;