diff --git a/src/actions/operation.js b/src/actions/operation.js index 97bcf45..295fd4f 100644 --- a/src/actions/operation.js +++ b/src/actions/operation.js @@ -1,9 +1,15 @@ import operations from 'shared/operations'; import messages from 'content/messages'; -import * as consoleActions from './console'; import * as tabs from 'background/tabs'; 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) => { switch (operation.type) { case operations.TAB_CLOSE: @@ -23,21 +29,21 @@ const exec = (operation, tab) => { case operations.ZOOM_NEUTRAL: return zooms.neutral(); case operations.COMMAND_SHOW: - return consoleActions.showCommand(''); + return sendConsoleShowCommand(tab, ''); case operations.COMMAND_SHOW_OPEN: if (operation.alter) { // 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: if (operation.alter) { // 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: - return consoleActions.showCommand('buffer '); + return sendConsoleShowCommand(tab, 'buffer '); default: return browser.tabs.sendMessage(tab.id, { type: messages.CONTENT_OPERATION, diff --git a/src/background/index.js b/src/background/index.js index 8dc55cb..05d3553 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -3,7 +3,6 @@ import * as settingsActions from 'actions/setting'; import BackgroundComponent from 'components/background'; import BackgroundInputComponent from 'components/background-input'; import reducers from 'reducers'; -import messages from 'content/messages'; import { createStore } from 'store'; const store = createStore(reducers, (e, sender) => { @@ -18,13 +17,5 @@ store.subscribe((sender) => { backgroundComponent.update(sender); backgroundInputComponent.update(sender); }); -store.subscribe((sender) => { - if (sender) { - return browser.tabs.sendMessage(sender.tab.id, { - type: messages.STATE_UPDATE, - state: store.getState() - }); - } -}); store.dispatch(settingsActions.load()); diff --git a/src/components/console.js b/src/components/console.js index 177cfe5..3a7f88b 100644 --- a/src/components/console.js +++ b/src/components/console.js @@ -70,8 +70,8 @@ export default class ConsoleComponent { }); } - // TODO use store/reducer to update state. - update(state) { + update() { + let state = this.store.getState().console; if (!this.prevState.commandShown && state.commandShown) { this.showCommand(state.commandText); } else if (!state.commandShown) { diff --git a/src/content/index.js b/src/content/index.js index 31b37cf..b29118d 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -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) => { switch (action.type) { - case messages.STATE_UPDATE: - return update(action.state); + case messages.CONSOLE_HIDE: + window.focus(); + consoleFrames.blur(window.document); + return Promise.resolve(); case messages.CONTENT_OPERATION: execOperation(action.operation); return Promise.resolve(); diff --git a/src/content/messages.js b/src/content/messages.js index 72a566b..0e66fa0 100644 --- a/src/content/messages.js +++ b/src/content/messages.js @@ -1,10 +1,12 @@ export default { - STATE_UPDATE: 'state.update', CONTENT_OPERATION: 'content.operation', CONSOLE_BLURRED: 'console.blured', CONSOLE_ENTERED: 'console.entered', CONSOLE_QUERY_COMPLETIONS: 'console.query.completions', + CONSOLE_SHOW_COMMAND: 'console.show.command', + CONSOLE_SHOW_ERROR: 'console.show.error', + CONSOLE_HIDE: 'console.hide', KEYDOWN: 'keydown', diff --git a/src/pages/console.js b/src/pages/console.js index 4d78826..4d3dd3f 100644 --- a/src/pages/console.js +++ b/src/pages/console.js @@ -4,7 +4,7 @@ import CompletionComponent from 'components/completion'; import ConsoleComponent from 'components/console'; import reducers from 'reducers'; import { createStore } from 'store'; -import * as completionActions from 'actions/completion'; +import * as consoleActions from 'actions/console'; const store = createStore(reducers); let completionComponent = null; @@ -20,6 +20,7 @@ window.addEventListener('load', () => { store.subscribe(() => { completionComponent.update(); + consoleComponent.update(); let state = store.getState().completion; @@ -36,8 +37,12 @@ store.subscribe(() => { }); browser.runtime.onMessage.addListener((action) => { - if (action.type === messages.STATE_UPDATE) { - let state = action.state.console; - consoleComponent.update(state); + switch (action.type) { + case messages.CONSOLE_SHOW_COMMAND: + return store.dispatch(consoleActions.showCommand(action.command)); + case messages.CONSOLE_SHOW_ERROR: + return store.dispatch(consoleActions.showError(action.command)); + case messages.CONSOLE_HIDE: + return store.dispatch(consoleActions.hide(action.command)); } });