From 359fdb528821844f3c2fda718b82ce229a64b29f Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 10 Sep 2017 15:49:51 +0900 Subject: [PATCH] blur as action/reducer --- src/console/console-frame.js | 48 ------------------------------------ src/console/console.js | 17 +++++++------ src/console/frames.js | 27 ++++++++++++++++++++ src/content/index.js | 26 +++++++++---------- 4 files changed, 49 insertions(+), 69 deletions(-) delete mode 100644 src/console/console-frame.js create mode 100644 src/console/frames.js diff --git a/src/console/console-frame.js b/src/console/console-frame.js deleted file mode 100644 index 063026c..0000000 --- a/src/console/console-frame.js +++ /dev/null @@ -1,48 +0,0 @@ -import './console-frame.scss'; -import * as consoleActions from '../actions/console'; - -export default class ConsoleFrame { - constructor(win) { - let element = window.document.createElement('iframe'); - element.src = browser.runtime.getURL('build/console.html'); - element.className = 'vimvixen-console-frame'; - win.document.body.append(element); - - this.element = element; - - this.errorShown = true; - - this.hide(); - } - - showCommand(text) { - this.showFrame(); - this.errorShown = false; - return browser.runtime.sendMessage(consoleActions.showCommand(text)); - } - - showError(text) { - this.showFrame(); - - this.errorShown = true; - this.element.blur(); - - return browser.runtime.sendMessage(consoleActions.showError(text)); - } - - showFrame() { - this.element.style.display = 'block'; - } - - hide() { - this.element.style.display = 'none'; - this.element.blur(); - this.errorShown = false; - - return browser.runtime.sendMessage(consoleActions.hide()); - } - - isErrorShown() { - return this.element.style.display === 'block' && this.errorShown; - } -} diff --git a/src/console/console.js b/src/console/console.js index d79e154..f83f79c 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,5 +1,6 @@ import './console.scss'; import * as backgroundActions from '../actions/background'; +import * as consoleActions from '../actions/console'; import Completion from './completion'; import consoleReducer from '../reducers/console'; @@ -9,12 +10,6 @@ var completion = null; var completionOrigin = ""; let state = consoleReducer(undefined, {}); -const blurMessage = () => { - return { - type: 'vimvixen.command.blur' - }; -}; - const keydownMessage = (input) => { return { type: 'vimvixen.command.enter', @@ -23,7 +18,7 @@ const keydownMessage = (input) => { }; const handleBlur = () => { - return browser.runtime.sendMessage(blurMessage()); + return browser.runtime.sendMessage(consoleActions.hide()); }; const completeNext = () => { @@ -57,9 +52,11 @@ const completePrev = () => { } const handleKeydown = (e) => { + let input = window.document.querySelector('#vimvixen-console-command-input'); + switch(e.keyCode) { case KeyboardEvent.DOM_VK_ESCAPE: - return browser.runtime.sendMessage(blurMessage()); + return input.blur(); case KeyboardEvent.DOM_VK_RETURN: return browser.runtime.sendMessage(keydownMessage(e.target)); case KeyboardEvent.DOM_VK_TAB: @@ -183,3 +180,7 @@ browser.runtime.onMessage.addListener((action) => { state = nextState; } }); + +window.addEventListener('load', () => { + update({}, state); +}); diff --git a/src/console/frames.js b/src/console/frames.js new file mode 100644 index 0000000..0b6f3e2 --- /dev/null +++ b/src/console/frames.js @@ -0,0 +1,27 @@ +import './console-frame.scss'; +import * as consoleActions from '../actions/console'; + +const initialize = (doc) => { + let iframe = doc.createElement('iframe'); + iframe.src = browser.runtime.getURL('build/console.html'); + iframe.id = 'vimvixen-console-frame'; + iframe.className = 'vimvixen-console-frame'; + doc.body.append(iframe); + + 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 }; diff --git a/src/content/index.js b/src/content/index.js index 1a7fa55..4fba516 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,24 +1,26 @@ +import '../console/console-frame.scss'; import * as scrolls from './scrolls'; import * as histories from './histories'; import * as actions from '../shared/actions'; -import ConsoleFrame from '../console/console-frame'; +import * as consoleFrames from '../console/frames'; +import actionTypes from '../actions'; import Follow from './follow'; -let vvConsole = new ConsoleFrame(window); +consoleFrames.initialize(window.document); browser.runtime.onMessage.addListener((action) => { switch (action.type) { case actions.CMD_OPEN: - return vvConsole.showCommand(''); + return consoleFrames.showCommand(''); case actions.CMD_TABS_OPEN: if (action.alter) { // alter url - return vvConsole.showCommand('open ' + window.location.href); + return consoleFrames.showCommand('open ' + window.location.href); } else { - return vvConsole.showCommand('open '); + return consoleFrames.showCommand('open '); } case actions.CMD_BUFFER: - return vvConsole.showCommand('buffer '); + return consoleFrames.showCommand('buffer '); case actions.SCROLL_LINES: scrolls.scrollLines(window, action.count); break; @@ -64,24 +66,22 @@ window.addEventListener("keypress", (e) => { browser.runtime.sendMessage(request) .catch((err) => { console.error("Vim Vixen:", err); - vvConsole.showError(err.message); + return consoleFrames.showError(err.message); }); }); browser.runtime.onMessage.addListener((action) => { switch (action.type) { - case 'vimvixen.command.blur': - if (!vvConsole.isErrorShown()) { - vvConsole.hide(); - } - return Promise.resolve(); + case actionTypes.CONSOLE_HIDE: + window.focus(); + return consoleFrames.blur(window.document); case 'vimvixen.command.enter': return browser.runtime.sendMessage({ type: 'event.cmd.enter', text: action.value }).catch((err) => { console.error("Vim Vixen:", err); - vvConsole.showError(err.message); + return consoleFrames.showError(err.message); }); default: return Promise.resolve();