diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-11 21:45:48 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-11 21:45:48 +0900 |
commit | b2cddcd69b4ae06770d66808624fc43f3dcbcb0e (patch) | |
tree | 548eb65f678cfa1dca36773f01c635ec6c0e2066 /src/content | |
parent | 15d39a479aa7f2c4b804bac8c4352dd0a120bc75 (diff) | |
parent | 7bc569eac745b97137e1db8b9271493b3e5c8a20 (diff) |
Merge branch 'message-passing-refactoring'
Diffstat (limited to 'src/content')
-rw-r--r-- | src/content/index.js | 130 |
1 files changed, 21 insertions, 109 deletions
diff --git a/src/content/index.js b/src/content/index.js index fdc7e89..12d079f 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,129 +1,41 @@ -import * as scrolls from './scrolls'; -import * as histories from './histories'; -import * as actions from '../shared/actions'; -import * as messages from '../shared/messages'; -import ConsoleFrame from '../console/console-frame'; -import Follow from './follow'; +import '../console/console-frame.scss'; +import * as inputActions from '../actions/input'; +import * as consoleFrames from '../console/frames'; +import actions from '../actions'; +import contentReducer from '../reducers/content'; -let vvConsole = new ConsoleFrame(window); +consoleFrames.initialize(window.document); -const doAction = (action) => { - if (typeof action === 'undefined' || action === null) { - return; - } - - switch (action[0]) { - case actions.CMD_OPEN: - vvConsole.showCommand(''); - break; - case actions.CMD_TABS_OPEN: - if (action[1] || false) { - // alter url - vvConsole.showCommand('open ' + window.location.href); - } else { - vvConsole.showCommand('open '); - } - break; - case actions.CMD_BUFFER: - vvConsole.showCommand('buffer '); - break; - case actions.SCROLL_LINES: - scrolls.scrollLines(window, action[1]); - break; - case actions.SCROLL_PAGES: - scrolls.scrollPages(window, action[1]); - break; - case actions.SCROLL_TOP: - scrolls.scrollTop(window); - break; - case actions.SCROLL_BOTTOM: - scrolls.scrollBottom(window); - break; - case actions.SCROLL_LEFT: - scrolls.scrollLeft(window); - break; - case actions.SCROLL_RIGHT: - scrolls.scrollRight(window); - break; - case actions.FOLLOW_START: - new Follow(window.document, action[1] || false); - break; - case actions.HISTORY_PREV: - histories.prev(window); - break; - case actions.HISTORY_NEXT: - histories.next(window); - break; - } -} - -const handleResponse = (response) => { - if (!response) { - return; - } - - switch(response.type) { - case 'response.action': - doAction(response.action); - break; - } -}; +browser.runtime.onMessage.addListener((action) => { + contentReducer(undefined, action); + return Promise.resolve(); +}); window.addEventListener("keypress", (e) => { if (e.target instanceof HTMLInputElement) { return; } - - let request = { - type: 'event.keypress', - code: e.which, - ctrl: e.ctrlKey, - } - - browser.runtime.sendMessage(request) - .then(handleResponse) + browser.runtime.sendMessage(inputActions.keyPress(e.which, e.ctrlKey)) .catch((err) => { console.error("Vim Vixen:", err); - vvConsole.showError(err.message); + return consoleFrames.showError(err.message); }); }); -const doCompletion = (line) => { - if (line.startsWith('buffer ')) { - let keyword = line.replace('buffer ', ''); - - browser.runtime.sendMessage({ - type: 'event.cmd.tabs.completion', - text: keyword - }).then((completions) => { - vvConsole.setCompletions([completions]); - }).catch((err) => { - console.error("Vim Vixen:", err); - vvConsole.showError(err.message); - }); - } -}; - -messages.receive(window, (message) => { - switch (message.type) { - case 'vimvixen.command.blur': - if (!vvConsole.isErrorShown()) { - vvConsole.hide(); - } - break; +browser.runtime.onMessage.addListener((action) => { + switch (action.type) { + case actions.CONSOLE_HIDE: + window.focus(); + return consoleFrames.blur(window.document); case 'vimvixen.command.enter': - browser.runtime.sendMessage({ + return browser.runtime.sendMessage({ type: 'event.cmd.enter', - text: message.value + text: action.value }).catch((err) => { console.error("Vim Vixen:", err); - vvConsole.showError(err.message); + return consoleFrames.showError(err.message); }); - break; - case 'vimvixen.command.change': - doCompletion(message.value); - break; default: - return; + return Promise.resolve(); } }); |