From 6e5286ef10b26d0a09f3882d6c3bfdbd92223d0f Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 1 Sep 2017 08:51:14 +0900 Subject: Rename command-line to console --- src/content/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/content/index.js') diff --git a/src/content/index.js b/src/content/index.js index 9bd4e15..591aa98 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,7 +1,7 @@ import * as scrolls from './scrolls'; import * as histories from './histories'; import * as actions from '../shared/actions'; -import CommandLineFrame from '../command-line/command-line-frame'; +import ConsoleFrame from '../console/console-frame'; import Follow from './follow'; let cmd = null; @@ -13,14 +13,14 @@ const invokeEvent = (action) => { switch (action[0]) { case actions.CMD_OPEN: - cmd = new CommandLineFrame(window); + cmd = new ConsoleFrame(window); break; case actions.CMD_TABS_OPEN: if (action[1] || false) { // alter url - cmd = new CommandLineFrame(window, 'open ' + window.location.href); + cmd = new ConsoleFrame(window, 'open ' + window.location.href); } else { - cmd = new CommandLineFrame(window, 'open '); + cmd = new ConsoleFrame(window, 'open '); } break; case actions.SCROLL_LINES: -- cgit v1.2.3 From adcaf2aa48d0453f231268f5db168376c08e4e6b Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 1 Sep 2017 11:40:24 +0900 Subject: message passing by helper functions --- src/console/console.js | 27 ++++++++++++++------------- src/content/index.js | 11 ++--------- src/shared/messages.js | 19 +++++++++++++++++++ test/shared/messages.test.js | 25 +++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 src/shared/messages.js create mode 100644 test/shared/messages.test.js (limited to 'src/content/index.js') diff --git a/src/console/console.js b/src/console/console.js index f8a91a6..67b9eab 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,41 +1,42 @@ import './console.scss'; +import * as messages from '../shared/messages'; const parent = window.parent; // TODO consider object-oriented var prevValue = ""; -const blurData = () => { - return JSON.stringify({ +const blurMessage = () => { + return { type: 'vimvixen.commandline.blur' - }); + }; }; -const keydownData = (input) => { - return JSON.stringify({ +const keydownMessage = (input) => { + return { type: 'vimvixen.commandline.enter', value: input.value - }); + }; }; -const keyupData = (input) => { - return JSON.stringify({ +const keyupMessage = (input) => { + return { type: 'vimvixen.commandline.change', value: input.value - }); + }; }; const handleBlur = () => { - parent.postMessage(blurData(), '*'); + messages.send(parent, blurMessage()); }; const handleKeydown = (e) => { switch(e.keyCode) { case KeyboardEvent.DOM_VK_ESCAPE: - parent.postMessage(blurData(), '*'); + messages.send(parent, blurMessage()); break; case KeyboardEvent.DOM_VK_RETURN: - parent.postMessage(keydownData(e.target), '*'); + messages.send(parent, keydownMessage(e.target)); break; } }; @@ -44,7 +45,7 @@ const handleKeyup = (e) => { if (e.target.value === prevValue) { return; } - parent.postMessage(keyupData(e.target), '*'); + messages.send(parent, keyupMessage(e.target)); prevValue = e.target.value; }; diff --git a/src/content/index.js b/src/content/index.js index 591aa98..ad3f740 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,6 +1,7 @@ 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'; @@ -71,15 +72,7 @@ window.addEventListener("keypress", (e) => { }); }); -window.addEventListener('message', (e) => { - let message; - try { - message = JSON.parse(e.data); - } catch (e) { - // ignore message posted by author of web page - return; - } - +messages.receive(window, (message) => { switch (message.type) { case 'vimvixen.commandline.blur': if (cmd) { diff --git a/src/shared/messages.js b/src/shared/messages.js new file mode 100644 index 0000000..517fc4c --- /dev/null +++ b/src/shared/messages.js @@ -0,0 +1,19 @@ +const receive = (win, callback) => { + win.addEventListener('message', (e) => { + let message; + try { + message = JSON.parse(e.data); + } catch (e) { + // ignore message posted by author of web page + return; + } + + callback(message); + }) +} + +const send = (win, message) => { + win.postMessage(JSON.stringify(message), '*'); +} + +export { receive, send }; diff --git a/test/shared/messages.test.js b/test/shared/messages.test.js new file mode 100644 index 0000000..0ebaf1a --- /dev/null +++ b/test/shared/messages.test.js @@ -0,0 +1,25 @@ +import { expect } from "chai"; +import * as messages from '../../src/shared/messages'; + +describe('messages', () => { + describe('#receive', () => { + it('received a message', (done) => { + messages.receive(window, (message) => { + expect(message).to.deep.equal({ type: 'vimvixen.test' }); + done(); + }); + window.postMessage(JSON.stringify({ type: 'vimvixen.test' }), '*'); + }); + }); + + describe('#send', () => { + it('sends a message', (done) => { + window.addEventListener('message', (e) => { + let json = JSON.parse(e.data); + expect(json).to.deep.equal({ type: 'vimvixen.test' }); + done(); + }); + messages.send(window, { type: 'vimvixen.test' }); + }); + }); +}); -- cgit v1.2.3 From d5007a747cdc39b2978847cd7c012515e5169ff7 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 1 Sep 2017 12:50:29 +0900 Subject: error title --- src/console/console-frame.js | 36 ++++++++++++++++++++++++++++++------ src/console/console.html | 2 +- src/console/console.js | 23 +++++++++++++++-------- src/content/index.js | 13 +++++-------- 4 files changed, 51 insertions(+), 23 deletions(-) (limited to 'src/content/index.js') diff --git a/src/console/console-frame.js b/src/console/console-frame.js index cf72f25..f8aeeeb 100644 --- a/src/console/console-frame.js +++ b/src/console/console-frame.js @@ -1,19 +1,43 @@ import './console-frame.scss'; +import * as messages from '../shared/messages'; export default class ConsoleFrame { constructor(win, initial = '') { - let url = browser.runtime.getURL('build/console.html') + - '#' + encodeURIComponent(initial); - let element = window.document.createElement('iframe'); - element.src = url; + element.src = browser.runtime.getURL('build/console.html'); element.className = 'vimvixen-console-frame'; win.document.body.append(element); this.element = element; + + this.hide(); + } + + showCommand(text) { + this.showFrame(); + + let message = { + type: 'vimvixen.console.show.command', + text: text + }; + messages.send(this.element.contentWindow, message); + } + + showError(text) { + this.showFrame(); + + let message = { + type: 'vimvixen.console.show.error', + text: text + }; + messages.send(this.element.contentWindow, message); + } + + showFrame() { + this.element.style.display = 'block'; } - remove() { - this.element.remove(); + hide() { + this.element.style.display = 'none'; } } diff --git a/src/console/console.html b/src/console/console.html index feee5d0..228660b 100644 --- a/src/console/console.html +++ b/src/console/console.html @@ -6,7 +6,7 @@ -

diff --git a/src/console/console.js b/src/console/console.js index 67b9eab..2db9d29 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -50,16 +50,23 @@ const handleKeyup = (e) => { }; window.addEventListener('load', () => { - let hash = window.location.hash; - let initial = ''; - if (hash.length > 0) { - initial = decodeURIComponent(hash.substring(1)); - } - let input = window.document.querySelector('#vimvixen-console-command-input'); input.addEventListener('blur', handleBlur); input.addEventListener('keydown', handleKeydown); input.addEventListener('keyup', handleKeyup); - input.value = initial; - input.focus(); +}); + +messages.receive(window, (message) => { + switch (message.type) { + case 'vimvixen.console.show.command': + if (message.text) { + let input = window.document.querySelector('#vimvixen-console-command-input'); + input.value = message.text; + input.focus(); + } + break; + case 'vimvixen.console.show.error': + window.document.querySelector('#vimvixen-console-error').textContent = message.text; + break; + } }); diff --git a/src/content/index.js b/src/content/index.js index ad3f740..66b1121 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -5,7 +5,7 @@ import * as messages from '../shared/messages'; import ConsoleFrame from '../console/console-frame'; import Follow from './follow'; -let cmd = null; +let vvConsole = new ConsoleFrame(window); const invokeEvent = (action) => { if (typeof action === 'undefined' || action === null) { @@ -14,14 +14,14 @@ const invokeEvent = (action) => { switch (action[0]) { case actions.CMD_OPEN: - cmd = new ConsoleFrame(window); + vvConsole.showCommand(''); break; case actions.CMD_TABS_OPEN: if (action[1] || false) { // alter url - cmd = new ConsoleFrame(window, 'open ' + window.location.href); + vvConsole.showCommand('open ' + window.location.href); } else { - cmd = new ConsoleFrame(window, 'open '); + vvConsole.showCommand('open '); } break; case actions.SCROLL_LINES: @@ -75,10 +75,7 @@ window.addEventListener("keypress", (e) => { messages.receive(window, (message) => { switch (message.type) { case 'vimvixen.commandline.blur': - if (cmd) { - cmd.remove(); - cmd = null; - } + vvConsole.hide(); break; case 'vimvixen.commandline.enter': browser.runtime.sendMessage({ -- cgit v1.2.3