Merge pull request #19 from ueokande/content-and-background-redux-completely

Refactor: full redux on content and background
jh-changes
Shin'ya Ueoka 7 years ago committed by GitHub
commit d995ab0030
  1. 22
      src/actions/completion.js
  2. 28
      src/actions/console.js
  3. 2
      src/actions/follow.js
  4. 10
      src/actions/index.js
  5. 2
      src/actions/input.js
  6. 28
      src/actions/operation.js
  7. 6
      src/actions/setting.js
  8. 36
      src/background/index.js
  9. 4
      src/components/background-input.js
  10. 36
      src/components/background.js
  11. 6
      src/components/completion.js
  12. 30
      src/components/console.js
  13. 2
      src/components/content-input.js
  14. 10
      src/components/follow.js
  15. 4
      src/components/setting.js
  16. 37
      src/content/index.js
  17. 6
      src/content/messages.js
  18. 47
      src/pages/console.js
  19. 12
      src/pages/settings.js
  20. 68
      src/reducers/completion.js
  21. 62
      src/reducers/console.js
  22. 2
      src/reducers/follow.js
  23. 9
      src/reducers/index.js
  24. 2
      src/reducers/input.js
  25. 2
      src/reducers/setting.js
  26. 12
      src/shared/commands.js
  27. 0
      src/shared/operations.js
  28. 2
      src/shared/validators/setting.js
  29. 27
      test/actions/completion.test.js
  30. 34
      test/actions/console.test.js
  31. 4
      test/actions/follow.test.js
  32. 4
      test/actions/input.test.js
  33. 2
      test/components/follow.test.js
  34. 2
      test/content/hint-key-producer.test.js
  35. 2
      test/content/hint.test.js
  36. 2
      test/content/navigates.test.js
  37. 90
      test/reducers/completion.test.js
  38. 91
      test/reducers/console.test.js
  39. 4
      test/reducers/follow.test.js
  40. 4
      test/reducers/input.test.js
  41. 4
      test/reducers/setting.test.js
  42. 2
      test/shared/validators/setting.test.js
  43. 2
      test/store/index.test.js
  44. 3
      webpack.config.js

@ -1,22 +0,0 @@
import actions from '../actions';
const setItems = (groups) => {
return {
type: actions.COMPLETION_SET_ITEMS,
groups,
};
};
const selectNext = () => {
return {
type: actions.COMPLETION_SELECT_NEXT
};
};
const selectPrev = () => {
return {
type: actions.COMPLETION_SELECT_PREV
};
};
export { setItems, selectNext, selectPrev };

@ -1,4 +1,4 @@
import actions from '../actions'; import actions from 'actions';
const showCommand = (text) => { const showCommand = (text) => {
return { return {
@ -7,6 +7,19 @@ const showCommand = (text) => {
}; };
}; };
const showError = (text) => {
return {
type: actions.CONSOLE_SHOW_ERROR,
text: text
};
};
const hide = () => {
return {
type: actions.CONSOLE_HIDE
};
};
const setCompletions = (completions) => { const setCompletions = (completions) => {
return { return {
type: actions.CONSOLE_SET_COMPLETIONS, type: actions.CONSOLE_SET_COMPLETIONS,
@ -14,17 +27,18 @@ const setCompletions = (completions) => {
}; };
}; };
const showError = (text) => { const completionNext = () => {
return { return {
type: actions.CONSOLE_SHOW_ERROR, type: actions.CONSOLE_COMPLETION_NEXT,
text: text
}; };
}; };
const hide = () => { const completionPrev = () => {
return { return {
type: actions.CONSOLE_HIDE type: actions.CONSOLE_COMPLETION_PREV,
}; };
}; };
export { showCommand, setCompletions, showError, hide }; export {
showCommand, showError, hide, setCompletions, completionNext, completionPrev
};

@ -1,4 +1,4 @@
import actions from '../actions'; import actions from 'actions';
const enable = (newTab) => { const enable = (newTab) => {
return { return {

@ -1,9 +1,11 @@
export default { export default {
// console commands // console commands
CONSOLE_SHOW_COMMAND: 'vimvixen.console.show.command', CONSOLE_SHOW_COMMAND: 'console.show.command',
CONSOLE_SET_COMPLETIONS: 'vimvixen.console.set.completions', CONSOLE_SET_COMPLETIONS: 'console.set.completions',
CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error', CONSOLE_SHOW_ERROR: 'console.show.error',
CONSOLE_HIDE: 'vimvixen.console.hide', CONSOLE_HIDE: 'console.hide',
CONSOLE_COMPLETION_NEXT: 'console.completion.next',
CONSOLE_COMPLETION_PREV: 'console.completion.prev',
// User input // User input
INPUT_KEY_PRESS: 'input.key,press', INPUT_KEY_PRESS: 'input.key,press',

@ -1,4 +1,4 @@
import actions from '../actions'; import actions from 'actions';
const asKeymapChars = (key, ctrl) => { const asKeymapChars = (key, ctrl) => {
if (ctrl) { if (ctrl) {

@ -1,8 +1,14 @@
import operations from '../operations'; import operations from 'shared/operations';
import messages from '../content/messages'; import messages from 'content/messages';
import * as consoleActions from './console'; import * as tabs from 'background/tabs';
import * as tabs from '../background/tabs'; import * as zooms from 'background/zooms';
import * as zooms from '../background/zooms';
const sendConsoleShowCommand = (tab, command) => {
return browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_SHOW_COMMAND,
command,
});
};
const exec = (operation, tab) => { const exec = (operation, tab) => {
switch (operation.type) { switch (operation.type) {
@ -23,21 +29,21 @@ const exec = (operation, tab) => {
case operations.ZOOM_NEUTRAL: case operations.ZOOM_NEUTRAL:
return zooms.neutral(); return zooms.neutral();
case operations.COMMAND_SHOW: case operations.COMMAND_SHOW:
return consoleActions.showCommand(''); return sendConsoleShowCommand(tab, '');
case operations.COMMAND_SHOW_OPEN: case operations.COMMAND_SHOW_OPEN:
if (operation.alter) { if (operation.alter) {
// alter url // alter url
return consoleActions.showCommand('open ' + tab.url); return sendConsoleShowCommand(tab, 'open ' + tab.url);
} }
return consoleActions.showCommand('open '); return sendConsoleShowCommand(tab, 'open ');
case operations.COMMAND_SHOW_TABOPEN: case operations.COMMAND_SHOW_TABOPEN:
if (operation.alter) { if (operation.alter) {
// alter url // alter url
return consoleActions.showCommand('tabopen ' + tab.url); return sendConsoleShowCommand(tab, 'tabopen ' + tab.url);
} }
return consoleActions.showCommand('tabopen '); return sendConsoleShowCommand(tab, 'tabopen ');
case operations.COMMAND_SHOW_BUFFER: case operations.COMMAND_SHOW_BUFFER:
return consoleActions.showCommand('buffer '); return sendConsoleShowCommand(tab, 'buffer ');
default: default:
return browser.tabs.sendMessage(tab.id, { return browser.tabs.sendMessage(tab.id, {
type: messages.CONTENT_OPERATION, type: messages.CONTENT_OPERATION,

@ -1,6 +1,6 @@
import actions from '../actions'; import actions from 'actions';
import messages from '../content/messages'; import messages from 'content/messages';
import DefaultSettings from '../shared/default-settings'; import DefaultSettings from 'shared/default-settings';
const load = () => { const load = () => {
return browser.storage.local.get('settings').then((value) => { return browser.storage.local.get('settings').then((value) => {

@ -1,30 +1,24 @@
import * as consoleActions from '../actions/console'; import * as settingsActions from 'actions/setting';
import * as settingsActions from '../actions/setting'; import messages from 'content/messages';
import BackgroundComponent from '../components/background'; import BackgroundComponent from 'components/background';
import BackgroundInputComponent from '../components/background-input'; import BackgroundInputComponent from 'components/background-input';
import reducers from '../reducers'; import reducers from 'reducers';
import messages from '../content/messages'; import { createStore } from 'store';
import * as store from '../store';
const backgroundStore = store.createStore(reducers, (e, sender) => { const store = createStore(reducers, (e, sender) => {
console.error('Vim-Vixen:', e); console.error('Vim-Vixen:', e);
if (sender) { if (sender) {
backgroundStore.dispatch(consoleActions.showError(e.message), sender); return browser.tabs.sendMessage(sender.tab.id, {
type: messages.CONSOLE_SHOW_ERROR,
text: e.message,
});
} }
}); });
const backgroundComponent = new BackgroundComponent(backgroundStore); const backgroundComponent = new BackgroundComponent(store);
const backgroundInputComponent = new BackgroundInputComponent(backgroundStore); const backgroundInputComponent = new BackgroundInputComponent(store);
backgroundStore.subscribe((sender) => { store.subscribe((sender) => {
backgroundComponent.update(sender); backgroundComponent.update(sender);
backgroundInputComponent.update(sender); backgroundInputComponent.update(sender);
}); });
backgroundStore.subscribe((sender) => {
if (sender) {
return browser.tabs.sendMessage(sender.tab.id, {
type: messages.STATE_UPDATE,
state: backgroundStore.getState()
});
}
});
backgroundStore.dispatch(settingsActions.load()); store.dispatch(settingsActions.load());

@ -1,5 +1,5 @@
import * as inputActions from '../actions/input'; import * as inputActions from 'actions/input';
import * as operationActions from '../actions/operation'; import * as operationActions from 'actions/operation';
export default class BackgroundInputComponent { export default class BackgroundInputComponent {
constructor(store) { constructor(store) {

@ -1,9 +1,8 @@
import messages from '../content/messages'; import messages from 'content/messages';
import * as commandActions from '../actions/command'; import * as inputActions from 'actions/input';
import * as consoleActions from '../actions/console'; import * as settingsActions from 'actions/setting';
import * as inputActions from '../actions/input'; import * as tabActions from 'actions/tab';
import * as settingsActions from '../actions/setting'; import * as commands from 'shared/commands';
import * as tabActions from '../actions/tab';
export default class BackgroundComponent { export default class BackgroundComponent {
constructor(store) { constructor(store) {
@ -12,9 +11,12 @@ export default class BackgroundComponent {
browser.runtime.onMessage.addListener((message, sender) => { browser.runtime.onMessage.addListener((message, sender) => {
try { try {
this.onMessage(message, sender); return this.onMessage(message, sender);
} catch (e) { } catch (e) {
this.store.dispatch(consoleActions.showError(e.message), sender); return browser.tabs.sendMessage(sender.tab.id, {
type: messages.CONSOLE_SHOW_ERROR,
text: e.message,
});
} }
}); });
} }
@ -44,14 +46,18 @@ export default class BackgroundComponent {
return this.store.dispatch( return this.store.dispatch(
tabActions.openToTab(message.url, sender.tab), sender); tabActions.openToTab(message.url, sender.tab), sender);
case messages.CONSOLE_BLURRED: case messages.CONSOLE_BLURRED:
return this.store.dispatch( return browser.tabs.sendMessage(sender.tab.id, {
consoleActions.hide(), sender); type: messages.CONSOLE_HIDE,
});
case messages.CONSOLE_ENTERED: case messages.CONSOLE_ENTERED:
return this.store.dispatch( return commands.exec(message.text, this.settings).catch((e) => {
commandActions.exec(message.text, this.settings), sender); return browser.tabs.sendMessage(sender.tab.id, {
case messages.CONSOLE_CHANGEED: type: messages.CONSOLE_SHOW_ERROR,
return this.store.dispatch( text: e.message,
commandActions.complete(message.text, this.settings), sender); });
});
case messages.CONSOLE_QUERY_COMPLETIONS:
return commands.complete(message.text, this.settings);
case messages.SETTINGS_RELOAD: case messages.SETTINGS_RELOAD:
this.store.dispatch(settingsActions.load()); this.store.dispatch(settingsActions.load());
} }

@ -6,15 +6,15 @@ export default class Completion {
} }
update() { update() {
let state = this.store.getState(); let state = this.store.getState().console;
if (JSON.stringify(this.prevState) === JSON.stringify(state)) { if (JSON.stringify(this.prevState) === JSON.stringify(state)) {
return; return;
} }
this.wrapper.innerHTML = ''; this.wrapper.innerHTML = '';
for (let i = 0; i < state.groups.length; ++i) { for (let i = 0; i < state.completions.length; ++i) {
let group = state.groups[i]; let group = state.completions[i];
let title = this.createCompletionTitle(group.name); let title = this.createCompletionTitle(group.name);
this.wrapper.append(title); this.wrapper.append(title);

@ -1,5 +1,5 @@
import messages from '../content/messages'; import messages from 'content/messages';
import * as completionActions from '../actions/completion'; import * as consoleActions from 'actions/console';
export default class ConsoleComponent { export default class ConsoleComponent {
constructor(wrapper, store) { constructor(wrapper, store) {
@ -36,12 +36,12 @@ export default class ConsoleComponent {
return browser.runtime.sendMessage({ return browser.runtime.sendMessage({
type: messages.CONSOLE_ENTERED, type: messages.CONSOLE_ENTERED,
text: e.target.value text: e.target.value
}); }).then(this.onBlur);
case KeyboardEvent.DOM_VK_TAB: case KeyboardEvent.DOM_VK_TAB:
if (e.shiftKey) { if (e.shiftKey) {
this.store.dispatch(completionActions.selectPrev()); this.store.dispatch(consoleActions.completionPrev());
} else { } else {
this.store.dispatch(completionActions.selectNext()); this.store.dispatch(consoleActions.completionNext());
} }
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
@ -63,13 +63,15 @@ export default class ConsoleComponent {
this.prevValue = e.target.value; this.prevValue = e.target.value;
return browser.runtime.sendMessage({ return browser.runtime.sendMessage({
type: messages.CONSOLE_CHANGEED, type: messages.CONSOLE_QUERY_COMPLETIONS,
text: e.target.value text: e.target.value
}).then((completions) => {
this.store.dispatch(consoleActions.setCompletions(completions));
}); });
} }
// TODO use store/reducer to update state. update() {
update(state) { let state = this.store.getState().console;
if (!this.prevState.commandShown && state.commandShown) { if (!this.prevState.commandShown && state.commandShown) {
this.showCommand(state.commandText); this.showCommand(state.commandText);
} else if (!state.commandShown) { } else if (!state.commandShown) {
@ -83,6 +85,18 @@ export default class ConsoleComponent {
this.hideError(); this.hideError();
} }
if (state.groupSelection >= 0 && state.itemSelection >= 0) {
let group = state.completions[state.groupSelection];
let item = group.items[state.itemSelection];
this.setCommandValue(item.content);
} else if (state.completions.length > 0 &&
JSON.stringify(this.prevState.completions) ===
JSON.stringify(state.completions)) {
// Reset input only completion groups not changed (unselected an item in
// completion) in order to avoid to override previous input
this.setCommandCompletionOrigin();
}
this.prevState = state; this.prevState = state;
} }

@ -1,4 +1,4 @@
import messages from '../content/messages'; import messages from 'content/messages';
export default class ContentInputComponent { export default class ContentInputComponent {
constructor(target) { constructor(target) {

@ -1,7 +1,7 @@
import * as followActions from '../actions/follow'; import * as followActions from 'actions/follow';
import messages from '../content/messages'; import messages from 'content/messages';
import Hint from '../content/hint'; import Hint from 'content/hint';
import HintKeyProducer from '../content/hint-key-producer'; import HintKeyProducer from 'content/hint-key-producer';
const DEFAULT_HINT_CHARSET = 'abcdefghijklmnopqrstuvwxyz'; const DEFAULT_HINT_CHARSET = 'abcdefghijklmnopqrstuvwxyz';
@ -44,7 +44,7 @@ export default class FollowComponent {
update() { update() {
let prevState = this.state; let prevState = this.state;
this.state = this.store.getState(); this.state = this.store.getState().follow;
if (!prevState.enabled && this.state.enabled) { if (!prevState.enabled && this.state.enabled) {
this.create(); this.create();
} else if (prevState.enabled && !this.state.enabled) { } else if (prevState.enabled && !this.state.enabled) {

@ -1,5 +1,5 @@
import * as settingActions from '../actions/setting'; import * as settingActions from 'actions/setting';
import { validate } from '../shared/validators/setting'; import { validate } from 'shared/validators/setting';
export default class SettingComponent { export default class SettingComponent {
constructor(wrapper, store) { constructor(wrapper, store) {

@ -1,18 +1,18 @@
import './console-frame.scss'; import './console-frame.scss';
import * as consoleFrames from './console-frames'; import * as consoleFrames from './console-frames';
import * as scrolls from '../content/scrolls'; import * as scrolls from 'content/scrolls';
import * as navigates from '../content/navigates'; import * as navigates from 'content/navigates';
import * as followActions from '../actions/follow'; import * as followActions from 'actions/follow';
import * as store from '../store'; import { createStore } from 'store';
import ContentInputComponent from '../components/content-input'; import ContentInputComponent from 'components/content-input';
import FollowComponent from '../components/follow'; import FollowComponent from 'components/follow';
import followReducer from '../reducers/follow'; import reducers from 'reducers';
import operations from '../operations'; import operations from 'shared/operations';
import messages from './messages'; import messages from './messages';
const followStore = store.createStore(followReducer); const store = createStore(reducers);
const followComponent = new FollowComponent(window.document.body, followStore); const followComponent = new FollowComponent(window.document.body, store);
followStore.subscribe(() => { store.subscribe(() => {
try { try {
followComponent.update(); followComponent.update();
} catch (e) { } catch (e) {
@ -39,7 +39,7 @@ const execOperation = (operation) => {
case operations.SCROLL_END: case operations.SCROLL_END:
return scrolls.scrollRight(window); return scrolls.scrollRight(window);
case operations.FOLLOW_START: case operations.FOLLOW_START:
return followStore.dispatch(followActions.enable(false)); return store.dispatch(followActions.enable(false));
case operations.NAVIGATE_HISTORY_PREV: case operations.NAVIGATE_HISTORY_PREV:
return navigates.historyPrev(window); return navigates.historyPrev(window);
case operations.NAVIGATE_HISTORY_NEXT: case operations.NAVIGATE_HISTORY_NEXT:
@ -55,17 +55,12 @@ const execOperation = (operation) => {
} }
}; };
const update = (state) => {
if (!state.console.commandShown) {
window.focus();
consoleFrames.blur(window.document);
}
};
browser.runtime.onMessage.addListener((action) => { browser.runtime.onMessage.addListener((action) => {
switch (action.type) { switch (action.type) {
case messages.STATE_UPDATE: case messages.CONSOLE_HIDE:
return update(action.state); window.focus();
consoleFrames.blur(window.document);
return Promise.resolve();
case messages.CONTENT_OPERATION: case messages.CONTENT_OPERATION:
execOperation(action.operation); execOperation(action.operation);
return Promise.resolve(); return Promise.resolve();

@ -1,10 +1,12 @@
export default { export default {
STATE_UPDATE: 'state.update',
CONTENT_OPERATION: 'content.operation', CONTENT_OPERATION: 'content.operation',
CONSOLE_BLURRED: 'console.blured', CONSOLE_BLURRED: 'console.blured',
CONSOLE_ENTERED: 'console.entered', CONSOLE_ENTERED: 'console.entered',
CONSOLE_CHANGEED: 'console.changed', CONSOLE_QUERY_COMPLETIONS: 'console.query.completions',
CONSOLE_SHOW_COMMAND: 'console.show.command',
CONSOLE_SHOW_ERROR: 'console.show.error',
CONSOLE_HIDE: 'console.hide',
KEYDOWN: 'keydown', KEYDOWN: 'keydown',

@ -1,45 +1,34 @@
import './console.scss'; import './console.scss';
import messages from '../content/messages'; import messages from 'content/messages';
import CompletionComponent from '../components/completion'; import CompletionComponent from 'components/completion';
import ConsoleComponent from '../components/console'; import ConsoleComponent from 'components/console';
import completionReducer from '../reducers/completion'; import reducers from 'reducers';
import * as store from '../store'; import { createStore } from 'store';
import * as completionActions from '../actions/completion'; import * as consoleActions from 'actions/console';
const completionStore = store.createStore(completionReducer); const store = createStore(reducers);
let completionComponent = null; let completionComponent = null;
let consoleComponent = null; let consoleComponent = null;
let prevState = {};
window.addEventListener('load', () => { window.addEventListener('load', () => {
let wrapper = document.querySelector('#vimvixen-console-completion'); let wrapper = document.querySelector('#vimvixen-console-completion');
completionComponent = new CompletionComponent(wrapper, completionStore); completionComponent = new CompletionComponent(wrapper, store);
// TODO use root root store instead of completionStore consoleComponent = new ConsoleComponent(document.body, store);
consoleComponent = new ConsoleComponent(document.body, completionStore);
}); });
completionStore.subscribe(() => { store.subscribe(() => {
completionComponent.update(); completionComponent.update();
consoleComponent.update();
let state = completionStore.getState();
if (state.groupSelection >= 0) {
let item = state.groups[state.groupSelection].items[state.itemSelection];
consoleComponent.setCommandValue(item.content);
} else if (state.groups.length > 0 &&
JSON.stringify(prevState.groups) === JSON.stringify(state.groups)) {
// Reset input only completion groups not changed (unselected an item in
// completion) in order to avoid to override previous input
consoleComponent.setCommandCompletionOrigin();
}
prevState = state;
}); });
browser.runtime.onMessage.addListener((action) => { browser.runtime.onMessage.addListener((action) => {
if (action.type === messages.STATE_UPDATE) { switch (action.type) {
let state = action.state.console; case messages.CONSOLE_SHOW_COMMAND:
consoleComponent.update(state); return store.dispatch(consoleActions.showCommand(action.command));
completionStore.dispatch(completionActions.setItems(state.completions)); case messages.CONSOLE_SHOW_ERROR:
return store.dispatch(consoleActions.showError(action.text));
case messages.CONSOLE_HIDE:
return store.dispatch(consoleActions.hide(action.command));
} }
}); });

@ -1,15 +1,15 @@
import './settings.scss'; import './settings.scss';
import SettingComponent from '../components/setting'; import SettingComponent from 'components/setting';
import settingReducer from '../reducers/setting'; import settingReducer from 'reducers/setting';
import * as store from '../store'; import { createStore } from 'store';
const settingStore = store.createStore(settingReducer); const store = createStore(settingReducer);
let settingComponent = null; let settingComponent = null;
settingStore.subscribe(() => { store.subscribe(() => {
settingComponent.update(); settingComponent.update();
}); });
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
settingComponent = new SettingComponent(document.body, settingStore); settingComponent = new SettingComponent(document.body, store);
}); });

@ -1,68 +0,0 @@
import actions from '../actions';
const defaultState = {
groupSelection: -1,
itemSelection: -1,
groups: [],
};
const nextSelection = (state) => {
if (state.groupSelection < 0) {
return [0, 0];
}
let group = state.groups[state.groupSelection];
if (state.groupSelection + 1 >= state.groups.length &&
state.itemSelection + 1 >= group.items.length) {
return [-1, -1];
}
if (state.itemSelection + 1 >= group.items.length) {
return [state.groupSelection + 1, 0];
}
return [state.groupSelection, state.itemSelection + 1];
};
const prevSelection = (state) => {
if (state.groupSelection < 0) {
return [
state.groups.length - 1,
state.groups[state.groups.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.groups[state.groupSelection - 1].items.length - 1
];
}
return [state.groupSelection, state.itemSelection - 1];
};
export default function reducer(state = defaultState, action = {}) {
switch (action.type) {
case actions.COMPLETION_SET_ITEMS:
return Object.assign({}, state, {
groups: action.groups,
groupSelection: -1,
itemSelection: -1,
});
case actions.COMPLETION_SELECT_NEXT: {
let next = nextSelection(state);
return Object.assign({}, state, {
groupSelection: next[0],
itemSelection: next[1],
});
}
case actions.COMPLETION_SELECT_PREV: {
let next = prevSelection(state);
return Object.assign({}, state, {
groupSelection: next[0],
itemSelection: next[1],
});
}
default:
return defaultState;
}
}

@ -1,4 +1,4 @@
import actions from '../actions'; import actions from 'actions';
const defaultState = { const defaultState = {
errorShown: false, errorShown: false,
@ -6,6 +6,42 @@ const defaultState = {
commandShown: false, commandShown: false,
commandText: '', commandText: '',
completions: [], completions: [],
groupSelection: -1,
itemSelection: -1,
};
const nextSelection = (state) => {
if (state.groupSelection < 0) {
return [0, 0];
}
let group = state.completions[state.groupSelection];
if (state.groupSelection + 1 >= state.completions.length &&
state.itemSelection + 1 >= group.items.length) {
return [-1, -1];
}
if (state.itemSelection + 1 >= group.items.length) {
return [state.groupSelection + 1, 0];
}
return [state.groupSelection, state.itemSelection + 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
];
}
return [state.groupSelection, state.itemSelection - 1];
}; };
export default function reducer(state = defaultState, action = {}) { export default function reducer(state = defaultState, action = {}) {
@ -17,10 +53,6 @@ export default function reducer(state = defaultState, action = {}) {
errorShown: false, errorShown: false,
completions: [] completions: []
}); });
case actions.CONSOLE_SET_COMPLETIONS:
return Object.assign({}, state, {
completions: action.completions
});
case actions.CONSOLE_SHOW_ERROR: case actions.CONSOLE_SHOW_ERROR:
return Object.assign({}, state, { return Object.assign({}, state, {
errorText: action.text, errorText: action.text,
@ -36,6 +68,26 @@ export default function reducer(state = defaultState, action = {}) {
errorShown: false, errorShown: false,
commandShown: false commandShown: false
}); });
case actions.CONSOLE_SET_COMPLETIONS:
return Object.assign({}, state, {
completions: action.completions,
groupSelection: -1,
itemSelection: -1,
});
case actions.CONSOLE_COMPLETION_NEXT: {
let next = nextSelection(state);
return Object.assign({}, state, {
groupSelection: next[0],
itemSelection: next[1],
});
}
case actions.CONSOLE_COMPLETION_PREV: {
let next = prevSelection(state);
return Object.assign({}, state, {
groupSelection: next[0],
itemSelection: next[1],
});
}
default: default:
return state; return state;
} }

@ -1,4 +1,4 @@
import actions from '../actions'; import actions from 'actions';
const defaultState = { const defaultState = {
enabled: false, enabled: false,

@ -1,11 +1,13 @@
import inputReducer from '../reducers/input'; import inputReducer from 'reducers/input';
import consoleReducer from '../reducers/console'; import consoleReducer from 'reducers/console';
import settingReducer from '../reducers/setting'; import settingReducer from 'reducers/setting';
import followReducer from 'reducers/follow';
const defaultState = { const defaultState = {
input: inputReducer(undefined, {}), input: inputReducer(undefined, {}),
console: consoleReducer(undefined, {}), console: consoleReducer(undefined, {}),
setting: settingReducer(undefined, {}), setting: settingReducer(undefined, {}),
follow: followReducer(undefined, {}),
}; };
export default function reducer(state = defaultState, action = {}) { export default function reducer(state = defaultState, action = {}) {
@ -13,5 +15,6 @@ export default function reducer(state = defaultState, action = {}) {
input: inputReducer(state.input, action), input: inputReducer(state.input, action),
console: consoleReducer(state.console, action), console: consoleReducer(state.console, action),
setting: settingReducer(state.setting, action), setting: settingReducer(state.setting, action),
follow: followReducer(state.follow, action),
}); });
} }

@ -1,4 +1,4 @@
import actions from '../actions'; import actions from 'actions';
const defaultState = { const defaultState = {
keys: '', keys: '',

@ -1,4 +1,4 @@
import actions from '../actions'; import actions from 'actions';
const defaultState = { const defaultState = {
settings: {} settings: {}

@ -1,6 +1,5 @@
import * as tabs from '../background/tabs'; import * as tabs from 'background/tabs';
import * as histories from '../background/histories'; import * as histories from 'background/histories';
import * as consoleActions from './console';
const normalizeUrl = (string, searchConfig) => { const normalizeUrl = (string, searchConfig) => {
try { try {
@ -132,16 +131,13 @@ const getCompletions = (command, keywords, settings) => {
const exec = (line, settings) => { const exec = (line, settings) => {
let name = line.split(' ')[0]; let name = line.split(' ')[0];
let remaining = line.replace(name + ' ', ''); let remaining = line.replace(name + ' ', '');
return doCommand(name, remaining, settings).then(() => { return doCommand(name, remaining, settings);
return consoleActions.hide();
});
}; };
const complete = (line, settings) => { const complete = (line, settings) => {
let command = line.split(' ', 1)[0]; let command = line.split(' ', 1)[0];
let keywords = line.replace(command + ' ', ''); let keywords = line.replace(command + ' ', '');
return getCompletions(command, keywords, settings) return getCompletions(command, keywords, settings);
.then(consoleActions.setCompletions);
}; };
export { exec, complete }; export { exec, complete };

@ -1,4 +1,4 @@
import operations from '../../operations'; import operations from 'shared/operations';
const VALID_TOP_KEYS = ['keymaps', 'search']; const VALID_TOP_KEYS = ['keymaps', 'search'];
const VALID_OPERATION_VALUES = Object.keys(operations).map((key) => { const VALID_OPERATION_VALUES = Object.keys(operations).map((key) => {

@ -1,27 +0,0 @@
import { expect } from "chai";
import actions from '../../src/actions';
import * as completionActions from '../../src/actions/completion';
describe("completion actions", () => {
describe('setItems', () => {
it('create COMPLETION_SET_ITEMS action', () => {
let action = completionActions.setItems([1, 2, 3]);
expect(action.type).to.equal(actions.COMPLETION_SET_ITEMS);
expect(action.groups).to.deep.equal([1, 2, 3]);
});
});
describe('selectNext', () => {
it('create COMPLETION_SELECT_NEXT action', () => {
let action = completionActions.selectNext();
expect(action.type).to.equal(actions.COMPLETION_SELECT_NEXT);
});
});
describe('selectPrev', () => {
it('create COMPLETION_SELECT_PREV action', () => {
let action = completionActions.selectPrev();
expect(action.type).to.equal(actions.COMPLETION_SELECT_PREV);
});
});
});

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import actions from '../../src/actions'; import actions from 'actions';
import * as consoleActions from '../../src/actions/console'; import * as consoleActions from 'actions/console';
describe("console actions", () => { describe("console actions", () => {
describe("showCommand", () => { describe("showCommand", () => {
@ -11,14 +11,6 @@ describe("console actions", () => {
}); });
}); });
describe("setCompletions", () => {
it('create CONSOLE_SET_COMPLETIONS action', () => {
let action = consoleActions.setCompletions([1,2,3]);
expect(action.type).to.equal(actions.CONSOLE_SET_COMPLETIONS);
expect(action.completions).to.deep.equal([1, 2, 3]);
});
});
describe("showError", () => { describe("showError", () => {
it('create CONSOLE_SHOW_ERROR action', () => { it('create CONSOLE_SHOW_ERROR action', () => {
let action = consoleActions.showError('an error'); let action = consoleActions.showError('an error');
@ -33,5 +25,27 @@ describe("console actions", () => {
expect(action.type).to.equal(actions.CONSOLE_HIDE); expect(action.type).to.equal(actions.CONSOLE_HIDE);
}); });
}); });
describe("setCompletions", () => {
it('create CONSOLE_SET_COMPLETIONS action', () => {
let action = consoleActions.setCompletions([1,2,3]);
expect(action.type).to.equal(actions.CONSOLE_SET_COMPLETIONS);
expect(action.completions).to.deep.equal([1, 2, 3]);
});
});
describe("completionPrev", () => {
it('create CONSOLE_COMPLETION_PREV action', () => {
let action = consoleActions.completionPrev();
expect(action.type).to.equal(actions.CONSOLE_COMPLETION_PREV);
});
});
describe("completionNext", () => {
it('create CONSOLE_COMPLETION_NEXT action', () => {
let action = consoleActions.completionNext();
expect(action.type).to.equal(actions.CONSOLE_COMPLETION_NEXT);
});
});
}); });

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import actions from '../../src/actions'; import actions from 'actions';
import * as followActions from '../../src/actions/follow'; import * as followActions from 'actions/follow';
describe('follow actions', () => { describe('follow actions', () => {
describe('enable', () => { describe('enable', () => {

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import actions from '../../src/actions'; import actions from 'actions';
import * as inputActions from '../../src/actions/input'; import * as inputActions from 'actions/input';
describe("input actions", () => { describe("input actions", () => {
describe("keyPress", () => { describe("keyPress", () => {

@ -1,5 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import FollowComponent from '../../src/components/follow'; import FollowComponent from 'components/follow';
describe('FollowComponent', () => { describe('FollowComponent', () => {
describe('#codeChars', () => { describe('#codeChars', () => {

@ -1,5 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import HintKeyProducer from '../../src/content/hint-key-producer'; import HintKeyProducer from 'content/hint-key-producer';
describe('HintKeyProducer class', () => { describe('HintKeyProducer class', () => {
describe('#constructor', () => { describe('#constructor', () => {

@ -1,5 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import Hint from '../../src/content/hint'; import Hint from 'content/hint';
describe('Hint class', () => { describe('Hint class', () => {
beforeEach(() => { beforeEach(() => {

@ -1,5 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import * as navigates from '../../src/content/navigates'; import * as navigates from 'content/navigates';
describe('navigates module', () => { describe('navigates module', () => {
describe('#linkPrev', () => { describe('#linkPrev', () => {

@ -1,90 +0,0 @@
import { expect } from "chai";
import actions from '../../src/actions';
import completionReducer from '../../src/reducers/completion';
describe("completion reducer", () => {
it ('return the initial state', () => {
let state = completionReducer(undefined, {});
expect(state).to.have.property('groupSelection', -1);
expect(state).to.have.property('itemSelection', -1);
expect(state).to.have.deep.property('groups', []);
});
it ('return next state for COMPLETION_SET_ITEMS', () => {
let state = {
groupSelection: 0,
itemSelection: 0,
groups: [],
}
let action = {
type: actions.COMPLETION_SET_ITEMS,
groups: [{
name: 'Apple',
items: [1, 2, 3]
}, {
name: 'Banana',
items: [4, 5, 6]
}]
}
state = completionReducer(state, action);
expect(state).to.have.property('groups', action.groups);
expect(state).to.have.property('groupSelection', -1);
expect(state).to.have.property('itemSelection', -1);
});
it ('return next state for COMPLETION_SELECT_NEXT', () => {
let action = { type: actions.COMPLETION_SELECT_NEXT };
let state = {
groupSelection: -1,
itemSelection: -1,
groups: [{
name: 'Apple',
items: [1, 2]
}, {
name: 'Banana',
items: [3]
}]
};
state = completionReducer(state, action);
expect(state).to.have.property('groupSelection', 0);
expect(state).to.have.property('itemSelection', 0);
state = completionReducer(state, action);
expect(state).to.have.property('groupSelection', 0);
expect(state).to.have.property('itemSelection', 1);
state = completionReducer(state, action);
state = completionReducer(state, action);
expect(state).to.have.property('groupSelection', -1);
expect(state).to.have.property('itemSelection', -1);
});
it ('return next state for COMPLETION_SELECT_PREV', () => {
let action = { type: actions.COMPLETION_SELECT_PREV };
let state = {
groupSelection: -1,
itemSelection: -1,
groups: [{
name: 'Apple',
items: [1, 2]
}, {
name: 'Banana',
items: [3]
}]
};
state = completionReducer(state, action);
expect(state).to.have.property('groupSelection', 1);
expect(state).to.have.property('itemSelection', 0);
state = completionReducer(state, action);
expect(state).to.have.property('groupSelection', 0);
expect(state).to.have.property('itemSelection', 1);
state = completionReducer(state, action);
state = completionReducer(state, action);
expect(state).to.have.property('groupSelection', -1);
expect(state).to.have.property('itemSelection', -1);
});
});

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import actions from '../../src/actions'; import actions from 'actions';
import consoleReducer from '../../src/reducers/console'; import consoleReducer from 'reducers/console';
describe("console reducer", () => { describe("console reducer", () => {
it('return the initial state', () => { it('return the initial state', () => {
@ -10,6 +10,8 @@ describe("console reducer", () => {
expect(state).to.have.property('commandShown', false); expect(state).to.have.property('commandShown', false);
expect(state).to.have.property('commandText', ''); expect(state).to.have.property('commandText', '');
expect(state).to.have.deep.property('completions', []); expect(state).to.have.deep.property('completions', []);
expect(state).to.have.property('groupSelection', -1);
expect(state).to.have.property('itemSelection', -1);
}); });
it('return next state for CONSOLE_SHOW_COMMAND', () => { it('return next state for CONSOLE_SHOW_COMMAND', () => {
@ -20,12 +22,6 @@ describe("console reducer", () => {
expect(state).to.have.property('errorShown', false); expect(state).to.have.property('errorShown', false);
}); });
it('return next state for CONSOLE_SET_COMPLETIONS', () => {
let action = { type: actions.CONSOLE_SET_COMPLETIONS, completions: [1, 2, 3] };
let state = consoleReducer({}, action);
expect(state).to.have.deep.property('completions', [1, 2, 3]);
});
it('return next state for CONSOLE_SHOW_ERROR', () => { it('return next state for CONSOLE_SHOW_ERROR', () => {
let action = { type: actions.CONSOLE_SHOW_ERROR, text: 'an error' }; let action = { type: actions.CONSOLE_SHOW_ERROR, text: 'an error' };
let state = consoleReducer({}, action); let state = consoleReducer({}, action);
@ -40,4 +36,83 @@ describe("console reducer", () => {
expect(state).to.have.property('errorShown', false); expect(state).to.have.property('errorShown', false);
expect(state).to.have.property('commandShown', false); expect(state).to.have.property('commandShown', false);
}); });
it ('return next state for CONSOLE_SET_COMPLETIONS', () => {
let state = {
groupSelection: 0,
itemSelection: 0,
completions: [],
}
let action = {
type: actions.CONSOLE_SET_COMPLETIONS,
completions: [{
name: 'Apple',
items: [1, 2, 3]
}, {
name: 'Banana',
items: [4, 5, 6]
}]
}
state = consoleReducer(state, action);
expect(state).to.have.property('completions', action.completions);
expect(state).to.have.property('groupSelection', -1);
expect(state).to.have.property('itemSelection', -1);
});
it ('return next state for CONSOLE_COMPLETION_NEXT', () => {
let action = { type: actions.CONSOLE_COMPLETION_NEXT };
let state = {
groupSelection: -1,
itemSelection: -1,
completions: [{
name: 'Apple',
items: [1, 2]
}, {
name: 'Banana',
items: [3]
}]
};
state = consoleReducer(state, action);
expect(state).to.have.property('groupSelection', 0);
expect(state).to.have.property('itemSelection', 0);
state = consoleReducer(state, action);
expect(state).to.have.property('groupSelection', 0);
expect(state).to.have.property('itemSelection', 1);
state = consoleReducer(state, action);
state = consoleReducer(state, action);
expect(state).to.have.property('groupSelection', -1);
expect(state).to.have.property('itemSelection', -1);
});
it ('return next state for CONSOLE_COMPLETION_PREV', () => {
let action = { type: actions.CONSOLE_COMPLETION_PREV };
let state = {
groupSelection: -1,
itemSelection: -1,
completions: [{
name: 'Apple',
items: [1, 2]
}, {
name: 'Banana',
items: [3]
}]
};
state = consoleReducer(state, action);
expect(state).to.have.property('groupSelection', 1);
expect(state).to.have.property('itemSelection', 0);
state = consoleReducer(state, action);
expect(state).to.have.property('groupSelection', 0);
expect(state).to.have.property('itemSelection', 1);
state = consoleReducer(state, action);
state = consoleReducer(state, action);
expect(state).to.have.property('groupSelection', -1);
expect(state).to.have.property('itemSelection', -1);
});
}); });

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import actions from '../../src/actions'; import actions from 'actions';
import followReducer from '../../src/reducers/follow'; import followReducer from 'reducers/follow';
describe('follow reducer', () => { describe('follow reducer', () => {
it ('returns the initial state', () => { it ('returns the initial state', () => {

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import actions from '../../src/actions'; import actions from 'actions';
import inputReducer from '../../src/reducers/input'; import inputReducer from 'reducers/input';
describe("input reducer", () => { describe("input reducer", () => {
it('return the initial state', () => { it('return the initial state', () => {

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import actions from '../../src/actions'; import actions from 'actions';
import settingReducer from '../../src/reducers/setting'; import settingReducer from 'reducers/setting';
describe("setting reducer", () => { describe("setting reducer", () => {
it('return the initial state', () => { it('return the initial state', () => {

@ -1,5 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import { validate } from '../../../src/shared/validators/setting'; import { validate } from 'shared/validators/setting';
describe("setting validator", () => { describe("setting validator", () => {
describe("unknown top keys", () => { describe("unknown top keys", () => {

@ -1,5 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import { createStore } from '../../src/store'; import { createStore } from 'store';
describe("Store class", () => { describe("Store class", () => {
const reducer = (state, action) => { const reducer = (state, action) => {

@ -39,7 +39,8 @@ module.exports = {
}, },
resolve: { resolve: {
extensions: [ '.js' ] extensions: [ '.js' ],
modules: [path.join(__dirname, 'src'), 'node_modules']
}, },
plugins: [ plugins: [