From 83cb277ba2af2bc2f87ace1d97fa582a7043bcd5 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 14 Sep 2017 22:04:42 +0900 Subject: consome as store/reducers --- src/console/console.js | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'src/console') diff --git a/src/console/console.js b/src/console/console.js index 25ab36d..7e9bffa 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,18 +1,17 @@ import './console.scss'; -import * as backgroundActions from '../actions/background'; -import * as consoleActions from '../actions/console'; -import * as commandActions from '../actions/command'; import Completion from './completion'; -import consoleReducer from '../reducers/console'; +import messages from '../messages'; // TODO consider object-oriented var prevValue = ""; var completion = null; var completionOrigin = ""; -let state = consoleReducer(undefined, {}); +var prevState = {}; const handleBlur = () => { - return browser.runtime.sendMessage(consoleActions.hide()); + return browser.runtime.sendMessage({ + type: messages.CONSOLE_BLURRED, + }); }; const completeNext = () => { @@ -52,7 +51,10 @@ const handleKeydown = (e) => { case KeyboardEvent.DOM_VK_ESCAPE: return input.blur(); case KeyboardEvent.DOM_VK_RETURN: - return browser.runtime.sendMessage(commandActions.exec(e.target.value)); + return browser.runtime.sendMessage({ + type: messages.CONSOLE_ENTERED, + text: e.target.value + }); case KeyboardEvent.DOM_VK_TAB: if (e.shiftKey) { completePrev(); @@ -73,9 +75,10 @@ const handleKeyup = (e) => { return; } prevValue = e.target.value; - return browser.runtime.sendMessage( - backgroundActions.requestCompletions(e.target.value) - ); + return browser.runtime.sendMessage({ + type: messages.CONSOLE_CHANGEED, + text: e.target.value + }); }; window.addEventListener('load', () => { @@ -147,7 +150,7 @@ const updateCompletions = (completions) => { completionOrigin = input.value.split(' ')[0]; } -const update = (prevState, state) => { +const update = (state) => { let error = window.document.querySelector('#vimvixen-console-error'); let command = window.document.querySelector('#vimvixen-console-command'); let input = window.document.querySelector('#vimvixen-console-command-input'); @@ -156,25 +159,26 @@ const update = (prevState, state) => { error.textContent = state.errorText; command.style.display = state.commandShown ? 'block' : 'none'; - if (!prevState.commandShown && state.commandShown) { - // setup input on firstly shown + if (state.commandShown && !prevState.commandShown) { input.value = state.commandText; input.focus(); } - if (JSON.stringify(state.completions) !== JSON.stringify(prevState.completions)) { updateCompletions(state.completions); } + + prevState = state; } browser.runtime.onMessage.addListener((action) => { - let nextState = consoleReducer(state, action); - if (JSON.stringify(nextState) !== JSON.stringify(state)) { - update(state, nextState); - state = nextState; + if (action.type === 'state.changed') { + return update(action.state.console); } }); window.addEventListener('load', () => { - update({}, state); + let error = window.document.querySelector('#vimvixen-console-error'); + let command = window.document.querySelector('#vimvixen-console-command'); + error.style.display = 'none'; + command.style.display = 'none'; }); -- cgit v1.2.3 From c7a3dd16e666b5f844bc98f19e58a3fe9967b5a4 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 16 Sep 2017 20:26:13 +0900 Subject: message constants --- src/actions/operation.js | 3 ++- src/background/index.js | 2 +- src/console/console.js | 2 +- src/content/index.js | 5 +++-- src/messages/index.js | 3 +++ 5 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/console') diff --git a/src/actions/operation.js b/src/actions/operation.js index 2009818..e589b89 100644 --- a/src/actions/operation.js +++ b/src/actions/operation.js @@ -1,4 +1,5 @@ import operations from '../operations'; +import messages from '../messages'; import * as consoleActions from './console'; import * as tabs from '../background/tabs'; import * as zooms from '../background/zooms'; @@ -34,7 +35,7 @@ export function exec(operation, tab) { return consoleActions.showCommand('buffer '); default: return browser.tabs.sendMessage(tab.id, { - type: 'require.content.operation', + type: messages.CONTENT_OPERATION, operation }); } diff --git a/src/background/index.js b/src/background/index.js index d2cfeb4..f329a9c 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -32,7 +32,7 @@ backgroundStore.subscribe(() => { browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { if (tabs.length > 0) { return browser.tabs.sendMessage(tabs[0].id, { - type: 'state.changed', + type: messages.STATE_UPDATE, state: backgroundStore.getState() }); } diff --git a/src/console/console.js b/src/console/console.js index 7e9bffa..d1547b8 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -171,7 +171,7 @@ const update = (state) => { } browser.runtime.onMessage.addListener((action) => { - if (action.type === 'state.changed') { + if (action.type === messages.STATE_UPDATE) { return update(action.state.console); } }); diff --git a/src/content/index.js b/src/content/index.js index d81e83e..43d632e 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -5,6 +5,7 @@ import * as scrolls from '../content/scrolls'; import * as histories from '../content/histories'; import Follow from '../content/follow'; import operations from '../operations'; +import messages from '../messages'; consoleFrames.initialize(window.document); @@ -51,9 +52,9 @@ const update = (state) => { browser.runtime.onMessage.addListener((action) => { switch (action.type) { - case 'state.changed': + case messages.STATE_UPDATE: return update(action.state); - case 'require.content.operation': + case messages.CONTENT_OPERATION: execOperation(action.operation); return Promise.resolve(); default: diff --git a/src/messages/index.js b/src/messages/index.js index 078ecaf..ad036a8 100644 --- a/src/messages/index.js +++ b/src/messages/index.js @@ -1,4 +1,7 @@ export default { + STATE_UPDATE: 'state.update', + CONTENT_OPERATION: 'content.operation', + CONSOLE_BLURRED: 'console.blured', CONSOLE_ENTERED: 'console.entered', CONSOLE_CHANGEED: 'console.changed' -- cgit v1.2.3 From 27702ef40236ca055e63373b2ee81d399d124cca Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 16 Sep 2017 21:28:21 +0900 Subject: remove actions from content --- src/background/index.js | 13 +++---------- src/console/frames.js | 11 +---------- src/content/index.js | 11 +++++------ src/messages/index.js | 4 +++- 4 files changed, 12 insertions(+), 27 deletions(-) (limited to 'src/console') diff --git a/src/background/index.js b/src/background/index.js index f329a9c..cbd4721 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -56,18 +56,11 @@ const keyQueueChanged = (sendToTab, state) => { return browser.tabs.sendMessage(sendToTab.id, action); }; -const handleMessage = (action, sendToTab) => { - backgroundStore.dispatch(action); - - return browser.tabs.sendMessage(sendToTab.id, action); -}; - -browser.runtime.onMessage.addListener((action, sender) => { - handleMessage(action, sender.tab); -}); - browser.runtime.onMessage.addListener((message) => { switch (message.type) { + case messages.KEYDOWN: + backgroundStore.dispatch(inputActions.keyPress(message.code, message.ctrl)); + break; case messages.CONSOLE_BLURRED: backgroundStore.dispatch(consoleActions.hide()); break; diff --git a/src/console/frames.js b/src/console/frames.js index 0b6f3e2..ec1e38c 100644 --- a/src/console/frames.js +++ b/src/console/frames.js @@ -1,5 +1,4 @@ import './console-frame.scss'; -import * as consoleActions from '../actions/console'; const initialize = (doc) => { let iframe = doc.createElement('iframe'); @@ -11,17 +10,9 @@ const initialize = (doc) => { return iframe; } -const showCommand = (text) => { - return browser.runtime.sendMessage(consoleActions.showCommand(text)); -}; - -const showError = (text) => { - return browser.runtime.sendMessage(consoleActions.showError(text)); -} - const blur = (doc) => { let iframe = doc.getElementById('vimvixen-console-frame'); iframe.blur(); } -export { initialize, showCommand, showError, blur }; +export { initialize, blur }; diff --git a/src/content/index.js b/src/content/index.js index 43d632e..5d3735c 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,5 +1,4 @@ import '../console/console-frame.scss'; -import * as inputActions from '../actions/input'; import * as consoleFrames from '../console/frames'; import * as scrolls from '../content/scrolls'; import * as histories from '../content/histories'; @@ -13,11 +12,11 @@ window.addEventListener("keypress", (e) => { if (e.target instanceof HTMLInputElement) { return; } - browser.runtime.sendMessage(inputActions.keyPress(e.which, e.ctrlKey)) - .catch((err) => { - console.error("Vim Vixen:", err); - return consoleFrames.showError(err.message); - }); + browser.runtime.sendMessage({ + type: messages.KEYDOWN, + code: e.which, + ctrl: e.ctrl + }); }); const execOperation = (operation) => { diff --git a/src/messages/index.js b/src/messages/index.js index ad036a8..3bdecca 100644 --- a/src/messages/index.js +++ b/src/messages/index.js @@ -4,5 +4,7 @@ export default { CONSOLE_BLURRED: 'console.blured', CONSOLE_ENTERED: 'console.entered', - CONSOLE_CHANGEED: 'console.changed' + CONSOLE_CHANGEED: 'console.changed', + + KEYDOWN: 'keydown' }; -- cgit v1.2.3