From 85b4bd5b073af729ff325e90fa3c9078f90277ac Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 7 Jul 2018 14:33:14 +0900 Subject: Use pure redux on console --- src/console/actions/console.js | 30 +++++++++++++++++-- src/console/components/console.js | 61 +++++++++++++-------------------------- src/console/index.js | 8 +++-- 3 files changed, 53 insertions(+), 46 deletions(-) (limited to 'src/console') diff --git a/src/console/actions/console.js b/src/console/actions/console.js index f80045f..3713a76 100644 --- a/src/console/actions/console.js +++ b/src/console/actions/console.js @@ -1,3 +1,4 @@ +import messages from 'shared/messages'; import actions from 'console/actions'; const hide = () => { @@ -34,11 +35,30 @@ const showInfo = (text) => { }; const hideCommand = () => { + window.top.postMessage(JSON.stringify({ + type: messages.CONSOLE_UNFOCUS, + }), '*'); return { type: actions.CONSOLE_HIDE_COMMAND, }; }; +const enterCommand = async(text) => { + await browser.runtime.sendMessage({ + type: messages.CONSOLE_ENTER_COMMAND, + text, + }); + return hideCommand(text); +}; + +const enterFind = (text) => { + window.top.postMessage(JSON.stringify({ + type: messages.CONSOLE_ENTER_FIND, + text, + }), '*'); + return hideCommand(); +}; + const setConsoleText = (consoleText) => { return { type: actions.CONSOLE_SET_CONSOLE_TEXT, @@ -46,11 +66,15 @@ const setConsoleText = (consoleText) => { }; }; -const setCompletions = (completionSource, completions) => { +const getCompletions = async(text) => { + let completions = await browser.runtime.sendMessage({ + type: messages.CONSOLE_QUERY_COMPLETIONS, + text, + }); return { type: actions.CONSOLE_SET_COMPLETIONS, - completionSource, completions, + completionSource: text, }; }; @@ -68,5 +92,5 @@ const completionPrev = () => { export { hide, showCommand, showFind, showError, showInfo, hideCommand, setConsoleText, - setCompletions, completionNext, completionPrev + enterCommand, enterFind, getCompletions, completionNext, completionPrev }; diff --git a/src/console/components/console.js b/src/console/components/console.js index 417c9f6..bd3e344 100644 --- a/src/console/components/console.js +++ b/src/console/components/console.js @@ -1,4 +1,3 @@ -import messages from 'shared/messages'; import * as consoleActions from 'console/actions/console'; const inputShownMode = (state) => { @@ -26,15 +25,22 @@ export default class ConsoleComponent { onBlur() { let state = this.store.getState(); - if (state.mode === 'command') { - this.hideCommand(); + if (state.mode === 'command' || state.mode === 'find') { + return this.store.dispatch(consoleActions.hideCommand()); } } doEnter(e) { e.stopPropagation(); e.preventDefault(); - return this.onEntered(e.target.value); + + let state = this.store.getState(); + let value = e.target.value; + if (state.mode === 'command') { + return this.store.dispatch(consoleActions.enterCommand(value)); + } else if (state.mode === 'find') { + return this.store.dispatch(consoleActions.enterFind(value)); + } } selectNext(e) { @@ -51,11 +57,11 @@ export default class ConsoleComponent { onKeyDown(e) { if (e.keyCode === KeyboardEvent.DOM_VK_ESCAPE && e.ctrlKey) { - return this.hideCommand(); + this.store.dispatch(consoleActions.hideCommand()); } switch (e.keyCode) { case KeyboardEvent.DOM_VK_ESCAPE: - return this.hideCommand(); + return this.store.dispatch(consoleActions.hideCommand()); case KeyboardEvent.DOM_VK_RETURN: return this.doEnter(e); case KeyboardEvent.DOM_VK_TAB: @@ -69,7 +75,7 @@ export default class ConsoleComponent { break; case KeyboardEvent.DOM_VK_OPEN_BRACKET: if (e.ctrlKey) { - return this.hideCommand(); + return this.store.dispatch(consoleActions.hideCommand()); } break; case KeyboardEvent.DOM_VK_M: @@ -90,32 +96,10 @@ export default class ConsoleComponent { } } - onEntered(value) { - let state = this.store.getState(); - if (state.mode === 'command') { - browser.runtime.sendMessage({ - type: messages.CONSOLE_ENTER_COMMAND, - text: value, - }); - this.hideCommand(); - } else if (state.mode === 'find') { - this.hideCommand(); - window.top.postMessage(JSON.stringify({ - type: messages.CONSOLE_ENTER_FIND, - text: value, - }), '*'); - } - } - - async onInput(e) { - this.store.dispatch(consoleActions.setConsoleText(e.target.value)); - - let source = e.target.value; - let completions = await browser.runtime.sendMessage({ - type: messages.CONSOLE_QUERY_COMPLETIONS, - text: source, - }); - this.store.dispatch(consoleActions.setCompletions(source, completions)); + onInput(e) { + let text = e.target.value; + this.store.dispatch(consoleActions.setConsoleText(text)); + this.store.dispatch(consoleActions.getCompletions(text)); } onInputShown(state) { @@ -126,17 +110,12 @@ export default class ConsoleComponent { window.focus(); if (state.mode === 'command') { - this.onInput({ target: input }); + let text = state.consoleText; + input.value = text; + this.store.dispatch(consoleActions.getCompletions(text)); } } - hideCommand() { - this.store.dispatch(consoleActions.hideCommand()); - window.top.postMessage(JSON.stringify({ - type: messages.CONSOLE_UNFOCUS, - }), '*'); - } - update() { let state = this.store.getState(); diff --git a/src/console/index.js b/src/console/index.js index 156456c..8724a44 100644 --- a/src/console/index.js +++ b/src/console/index.js @@ -3,10 +3,14 @@ import messages from 'shared/messages'; import CompletionComponent from 'console/components/completion'; import ConsoleComponent from 'console/components/console'; import reducers from 'console/reducers'; -import { createStore } from 'shared/store'; +import { createStore, applyMiddleware } from 'redux'; +import promise from 'redux-promise'; import * as consoleActions from 'console/actions/console'; -const store = createStore(reducers); +const store = createStore( + reducers, + applyMiddleware(promise), +); window.addEventListener('load', () => { let wrapper = document.querySelector('#vimvixen-console-completion'); -- cgit v1.2.3 From 8db779388f260631286332aec6cb6a448a29d791 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 13 Jul 2018 21:59:23 +0900 Subject: Fix error on no completion items --- src/console/reducers/index.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/console') diff --git a/src/console/reducers/index.js b/src/console/reducers/index.js index 71b0776..043689c 100644 --- a/src/console/reducers/index.js +++ b/src/console/reducers/index.js @@ -11,6 +11,9 @@ const defaultState = { }; const nextSelection = (state) => { + if (state.completions.length === 0) { + return [-1, -1]; + }; if (state.groupSelection < 0) { return [0, 0]; } -- cgit v1.2.3 From ccc6a31ddeab78660305d9088e8260156b251779 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 13 Jul 2018 22:09:04 +0900 Subject: fix --- src/background/shared/completions/index.js | 2 +- src/console/reducers/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/console') diff --git a/src/background/shared/completions/index.js b/src/background/shared/completions/index.js index 4fd37fd..d0d00ef 100644 --- a/src/background/shared/completions/index.js +++ b/src/background/shared/completions/index.js @@ -130,7 +130,7 @@ const complete = (line, settings) => { let name = words[0]; if (words.length === 1) { let items = completeCommands(name); - if (items.length === 0) { + if (items.length === 0) { return Promise.resolve([]); } return Promise.resolve([ diff --git a/src/console/reducers/index.js b/src/console/reducers/index.js index 043689c..7dcad17 100644 --- a/src/console/reducers/index.js +++ b/src/console/reducers/index.js @@ -13,7 +13,7 @@ const defaultState = { const nextSelection = (state) => { if (state.completions.length === 0) { return [-1, -1]; - }; + } if (state.groupSelection < 0) { return [0, 0]; } -- cgit v1.2.3