From e8056d2a709df9c9c182382fc7a31795e4dc1c0f Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 9 Sep 2017 22:07:27 +0900 Subject: console state as action/reducer in redux --- src/actions/index.js | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/actions/index.js (limited to 'src/actions/index.js') diff --git a/src/actions/index.js b/src/actions/index.js new file mode 100644 index 0000000..38ced9d --- /dev/null +++ b/src/actions/index.js @@ -0,0 +1,6 @@ +export default { + CONSOLE_SHOW_COMMAND: 'vimvixen.console.show.command', + CONSOLE_SET_COMPLETIONS: 'vimvixen.console.set.completions', + CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error', + CONSOLE_HIDE: 'vimvixen.console.hide' +}; -- cgit v1.2.3 From 3c67cc0a002aded07e88ea25acc881ff080d1ae4 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 10 Sep 2017 09:20:54 +0900 Subject: completion as action/reducer --- src/actions/background.js | 11 +++++++++++ src/actions/index.js | 4 +++- src/background/index.js | 20 +++++--------------- src/console/console-frame.js | 4 ---- src/console/console.js | 12 ++++-------- src/content/index.js | 19 ------------------- src/reducers/background.js | 38 ++++++++++++++++++++++++++++++++++++++ test/actions/background.test.js | 14 ++++++++++++++ 8 files changed, 75 insertions(+), 47 deletions(-) create mode 100644 src/actions/background.js create mode 100644 src/reducers/background.js create mode 100644 test/actions/background.test.js (limited to 'src/actions/index.js') diff --git a/src/actions/background.js b/src/actions/background.js new file mode 100644 index 0000000..40b901b --- /dev/null +++ b/src/actions/background.js @@ -0,0 +1,11 @@ +import actions from '../actions'; + +export function requestCompletions(line) { + let command = line.split(' ', 1)[0]; + let keywords = line.replace(command + ' ', ''); + return { + type: actions.BACKGROUND_REQUEST_COMPLETIONS, + command, + keywords + }; +} diff --git a/src/actions/index.js b/src/actions/index.js index 38ced9d..8f22193 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -2,5 +2,7 @@ export default { CONSOLE_SHOW_COMMAND: 'vimvixen.console.show.command', CONSOLE_SET_COMPLETIONS: 'vimvixen.console.set.completions', CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error', - CONSOLE_HIDE: 'vimvixen.console.hide' + CONSOLE_HIDE: 'vimvixen.console.hide', + + BACKGROUND_REQUEST_COMPLETIONS: 'vimvixen.background.request.completions' }; diff --git a/src/background/index.js b/src/background/index.js index 61d4895..a80d1ea 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -2,6 +2,7 @@ import * as actions from '../shared/actions'; import * as tabs from './tabs'; import * as zooms from './zooms'; import KeyQueue from './key-queue'; +import backgroundReducers from '../reducers/background'; const queue = new KeyQueue(); @@ -81,22 +82,11 @@ browser.runtime.onMessage.addListener((request, sender) => { return keyPressHandle(request, sender); case 'event.cmd.enter': return cmdEnterHandle(request, sender); - case 'event.cmd.tabs.completion': - return tabs.getCompletions(request.text).then((tabs) => { - let items = tabs.map((tab) => { - return { - caption: tab.title, - content: tab.title, - url: tab.url, - icon: tab.favIconUrl - } - }); - return { - name: "Buffers", - items: items - }; - }); default: return browser.tabs.sendMessage(sender.tab.id, request); } }); + +browser.runtime.onMessage.addListener((action, sender) => { + return backgroundReducers(undefined, action, sender.tab.id); +}); diff --git a/src/console/console-frame.js b/src/console/console-frame.js index 8b4c17a..063026c 100644 --- a/src/console/console-frame.js +++ b/src/console/console-frame.js @@ -45,8 +45,4 @@ export default class ConsoleFrame { isErrorShown() { return this.element.style.display === 'block' && this.errorShown; } - - setCompletions(completions) { - return browser.runtime.sendMessage(consoleActions.setCompletions(completions)); - } } diff --git a/src/console/console.js b/src/console/console.js index 35d98e0..d79e154 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,4 +1,5 @@ import './console.scss'; +import * as backgroundActions from '../actions/background'; import Completion from './completion'; import consoleReducer from '../reducers/console'; @@ -21,13 +22,6 @@ const keydownMessage = (input) => { }; }; -const keyupMessage = (input) => { - return { - type: 'vimvixen.command.change', - value: input.value - }; -}; - const handleBlur = () => { return browser.runtime.sendMessage(blurMessage()); }; @@ -88,7 +82,9 @@ const handleKeyup = (e) => { return; } prevValue = e.target.value; - return browser.runtime.sendMessage(keyupMessage(e.target)); + return browser.runtime.sendMessage( + backgroundActions.requestCompletions(e.target.value) + ); }; window.addEventListener('load', () => { diff --git a/src/content/index.js b/src/content/index.js index deb3506..1a7fa55 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -68,23 +68,6 @@ window.addEventListener("keypress", (e) => { }); }); -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); - }); - } - return Promise.resolve(); -}; - browser.runtime.onMessage.addListener((action) => { switch (action.type) { case 'vimvixen.command.blur': @@ -100,8 +83,6 @@ browser.runtime.onMessage.addListener((action) => { console.error("Vim Vixen:", err); vvConsole.showError(err.message); }); - case 'vimvixen.command.change': - return doCompletion(action.value); default: return Promise.resolve(); } diff --git a/src/reducers/background.js b/src/reducers/background.js new file mode 100644 index 0000000..eccc8ca --- /dev/null +++ b/src/reducers/background.js @@ -0,0 +1,38 @@ +import * as tabs from '../background/tabs'; +import * as consoleActions from '../actions/console'; +import actions from '../actions'; + +const doCompletion = (command, keywords, sendto) => { + if (command === 'buffer') { + return tabs.getCompletions(keywords).then((tabs) => { + let items = tabs.map((tab) => { + return { + caption: tab.title, + content: tab.title, + url: tab.url, + icon: tab.favIconUrl + } + }); + let completions = { + name: "Buffers", + items: items + }; + return browser.tabs.sendMessage( + sendto, + consoleActions.setCompletions([completions])); + }); + } + return Promise.resolve(); +}; + + + +export default function reducer(state, action = {}, sendto) { + // TODO hide sendto object + switch (action.type) { + case actions.BACKGROUND_REQUEST_COMPLETIONS: + return doCompletion(action.command, action.keywords, sendto); + default: + return Promise.resolve(); + } +} diff --git a/test/actions/background.test.js b/test/actions/background.test.js new file mode 100644 index 0000000..a3203ee --- /dev/null +++ b/test/actions/background.test.js @@ -0,0 +1,14 @@ +import { expect } from "chai"; +import actions from '../../src/actions'; +import * as backgroundActions from '../../src/actions/background'; + +describe("background actions", () => { + describe("requestCompletions", () => { + it('create BACKGROUND_REQUEST_COMPLETIONS action', () => { + let action = backgroundActions.requestCompletions('buffer hoge fuga'); + expect(action.type).to.equal(actions.BACKGROUND_REQUEST_COMPLETIONS); + expect(action.command).to.equal('buffer'); + expect(action.keywords).to.equal('hoge fuga'); + }); + }); +}); -- cgit v1.2.3 From 2c40d239f0962f1442f357c0697e4c8aa2eafd31 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 10 Sep 2017 17:27:55 +0900 Subject: background command from reducer --- src/actions/index.js | 11 ++++++++++- src/background/index.js | 32 ++++---------------------------- src/background/key-queue.js | 19 ++++++++++--------- src/reducers/background.js | 29 ++++++++++++++++++++++------- src/shared/actions.js | 23 ----------------------- 5 files changed, 46 insertions(+), 68 deletions(-) (limited to 'src/actions/index.js') diff --git a/src/actions/index.js b/src/actions/index.js index 8f22193..bc44fec 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -4,5 +4,14 @@ export default { CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error', CONSOLE_HIDE: 'vimvixen.console.hide', - BACKGROUND_REQUEST_COMPLETIONS: 'vimvixen.background.request.completions' + BACKGROUND_REQUEST_COMPLETIONS: 'vimvixen.background.request.completions', + + TABS_CLOSE: 'tabs.close', + TABS_REOPEN: 'tabs.reopen', + TABS_PREV: 'tabs.prev', + TABS_NEXT: 'tabs.next', + TABS_RELOAD: 'tabs.reload', + ZOOM_IN: 'zoom.in', + ZOOM_OUT: 'zoom.out', + ZOOM_NEUTRAL: 'zoom.neutral', }; diff --git a/src/background/index.js b/src/background/index.js index a80d1ea..cb7a3ff 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,6 +1,5 @@ import * as actions from '../shared/actions'; import * as tabs from './tabs'; -import * as zooms from './zooms'; import KeyQueue from './key-queue'; import backgroundReducers from '../reducers/background'; @@ -15,36 +14,13 @@ const keyPressHandle = (request, sender) => { return Promise.resolve(); } - if (actions.isBackgroundAction(action.type)) { - return doBackgroundAction(sender, action); - } else if (actions.isContentAction(action.type)) { + if (actions.isContentAction(action.type)) { return browser.tabs.sendMessage(sender.tab.id, action); + } else { + return backgroundReducers(undefined, action, sender); } - return Promise.resolve(); }; -const doBackgroundAction = (sender, action) => { - switch(action.type) { - case actions.TABS_CLOSE: - return tabs.closeTab(sender.tab.id); - case actions.TABS_REOPEN: - return tabs.reopenTab(); - case actions.TABS_PREV: - return tabs.selectPrevTab(sender.tab.index, action.count); - case actions.TABS_NEXT: - return tabs.selectNextTab(sender.tab.index, action.count); - case actions.TABS_RELOAD: - return tabs.reload(sender.tab, actions.cache); - case actions.ZOOM_IN: - return zooms.zoomIn(); - case actions.ZOOM_OUT: - return zooms.zoomOut(); - case actions.ZOOM_NEUTRAL: - return zooms.neutral(); - } - return Promise.resolve(); -} - const normalizeUrl = (string) => { try { return new URL(string).href @@ -88,5 +64,5 @@ browser.runtime.onMessage.addListener((request, sender) => { }); browser.runtime.onMessage.addListener((action, sender) => { - return backgroundReducers(undefined, action, sender.tab.id); + return backgroundReducers(undefined, action, sender); }); diff --git a/src/background/key-queue.js b/src/background/key-queue.js index b2f5a34..25f1d24 100644 --- a/src/background/key-queue.js +++ b/src/background/key-queue.js @@ -1,3 +1,4 @@ +import newActions from '../actions'; import * as actions from '../shared/actions'; const DEFAULT_KEYMAP = { @@ -17,15 +18,15 @@ const DEFAULT_KEYMAP = { 'G': { type: actions.SCROLL_BOTTOM }, '0': { type: actions.SCROLL_LEFT }, '$': { type: actions.SCROLL_RIGHT }, - 'd': { type: actions.TABS_CLOSE }, - 'u': { type: actions.TABS_REOPEN }, - 'h': { type: actions.TABS_PREV, count: 1 }, - 'l': { type: actions.TABS_NEXT, count: 1 }, - 'r': { type: actions.TABS_RELOAD, cache: false }, - 'R': { type: actions.TABS_RELOAD, cache: true }, - 'zi': { type: actions.ZOOM_IN }, - 'zo': { type: actions.ZOOM_OUT }, - 'zz': { type: actions.ZOOM_NEUTRAL }, + 'd': { type: newActions.TABS_CLOSE }, + 'u': { type: newActions.TABS_REOPEN }, + 'h': { type: newActions.TABS_PREV, count: 1 }, + 'l': { type: newActions.TABS_NEXT, count: 1 }, + 'r': { type: newActions.TABS_RELOAD, cache: false }, + 'R': { type: newActions.TABS_RELOAD, cache: true }, + 'zi': { type: newActions.ZOOM_IN }, + 'zo': { type: newActions.ZOOM_OUT }, + 'zz': { type: newActions.ZOOM_NEUTRAL }, 'f': { type: actions.FOLLOW_START, newTab: false }, 'F': { type: actions.FOLLOW_START, newTab: true }, 'H': { type: actions.HISTORY_PREV }, diff --git a/src/reducers/background.js b/src/reducers/background.js index eccc8ca..d7d7860 100644 --- a/src/reducers/background.js +++ b/src/reducers/background.js @@ -1,8 +1,9 @@ import * as tabs from '../background/tabs'; +import * as zooms from '../background/zooms'; import * as consoleActions from '../actions/console'; import actions from '../actions'; -const doCompletion = (command, keywords, sendto) => { +const doCompletion = (command, keywords, sender) => { if (command === 'buffer') { return tabs.getCompletions(keywords).then((tabs) => { let items = tabs.map((tab) => { @@ -18,20 +19,34 @@ const doCompletion = (command, keywords, sendto) => { items: items }; return browser.tabs.sendMessage( - sendto, + sender, consoleActions.setCompletions([completions])); }); } return Promise.resolve(); }; - - -export default function reducer(state, action = {}, sendto) { - // TODO hide sendto object +export default function reducer(state, action = {}, sender) { + // TODO hide sender object switch (action.type) { case actions.BACKGROUND_REQUEST_COMPLETIONS: - return doCompletion(action.command, action.keywords, sendto); + return doCompletion(action.command, action.keywords, sender.tab.id); + case actions.TABS_CLOSE: + return tabs.closeTab(sender.tab.id); + case actions.TABS_REOPEN: + return tabs.reopenTab(); + case actions.TABS_PREV: + return tabs.selectPrevTab(sender.tab.index, action.count); + case actions.TABS_NEXT: + return tabs.selectNextTab(sender.tab.index, action.count); + case actions.TABS_RELOAD: + return tabs.reload(sender.tab, action.cache); + case actions.ZOOM_IN: + return zooms.zoomIn(); + case actions.ZOOM_OUT: + return zooms.zoomOut(); + case actions.ZOOM_NEUTRAL: + return zooms.neutral(); default: return Promise.resolve(); } diff --git a/src/shared/actions.js b/src/shared/actions.js index 7151dd1..5163cf3 100644 --- a/src/shared/actions.js +++ b/src/shared/actions.js @@ -1,11 +1,6 @@ export const CMD_OPEN = 'cmd.open'; export const CMD_TABS_OPEN = 'cmd.tabs.open'; export const CMD_BUFFER = 'cmd.buffer'; -export const TABS_CLOSE = 'tabs.close'; -export const TABS_REOPEN = 'tabs.reopen'; -export const TABS_PREV = 'tabs.prev'; -export const TABS_NEXT = 'tabs.next'; -export const TABS_RELOAD = 'tabs.reload'; export const SCROLL_LINES = 'scroll.lines'; export const SCROLL_PAGES = 'scroll.pages'; export const SCROLL_TOP = 'scroll.top'; @@ -15,20 +10,6 @@ export const SCROLL_RIGHT= 'scroll.right'; export const FOLLOW_START = 'follow.start'; export const HISTORY_PREV = 'history.prev'; export const HISTORY_NEXT = 'history.next'; -export const ZOOM_IN = 'zoom.in'; -export const ZOOM_OUT = 'zoom.out'; -export const ZOOM_NEUTRAL = 'zoom.neutral'; - -const BACKGROUND_ACTION_SET = new Set([ - TABS_CLOSE, - TABS_REOPEN, - TABS_PREV, - TABS_NEXT, - TABS_RELOAD, - ZOOM_IN, - ZOOM_OUT, - ZOOM_NEUTRAL -]); const CONTENT_ACTION_SET = new Set([ CMD_OPEN, @@ -45,10 +26,6 @@ const CONTENT_ACTION_SET = new Set([ HISTORY_NEXT ]); -export const isBackgroundAction = (action) => { - return BACKGROUND_ACTION_SET.has(action); -}; - export const isContentAction = (action) => { return CONTENT_ACTION_SET.has(action); }; -- cgit v1.2.3 From adc6a5175c0d8b83e45b9e8d99109c1605ad29ac Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 10 Sep 2017 17:51:25 +0900 Subject: content commands as action/reducer --- src/actions/index.js | 17 ++++++++++++++- src/background/index.js | 7 ++----- src/background/key-queue.js | 21 +++++++++---------- src/content/index.js | 50 ++++----------------------------------------- src/reducers/content.js | 48 +++++++++++++++++++++++++++++++++++++++++++ src/shared/actions.js | 31 ---------------------------- 6 files changed, 80 insertions(+), 94 deletions(-) create mode 100644 src/reducers/content.js delete mode 100644 src/shared/actions.js (limited to 'src/actions/index.js') diff --git a/src/actions/index.js b/src/actions/index.js index bc44fec..de3ab42 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -1,11 +1,12 @@ export default { + // console commands CONSOLE_SHOW_COMMAND: 'vimvixen.console.show.command', CONSOLE_SET_COMPLETIONS: 'vimvixen.console.set.completions', CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error', CONSOLE_HIDE: 'vimvixen.console.hide', + // Background commands BACKGROUND_REQUEST_COMPLETIONS: 'vimvixen.background.request.completions', - TABS_CLOSE: 'tabs.close', TABS_REOPEN: 'tabs.reopen', TABS_PREV: 'tabs.prev', @@ -14,4 +15,18 @@ export default { ZOOM_IN: 'zoom.in', ZOOM_OUT: 'zoom.out', ZOOM_NEUTRAL: 'zoom.neutral', + + // content commands + CMD_OPEN: 'cmd.open', + CMD_TABS_OPEN: 'cmd.tabs.open', + CMD_BUFFER: 'cmd.buffer', + SCROLL_LINES: 'scroll.lines', + SCROLL_PAGES: 'scroll.pages', + SCROLL_TOP: 'scroll.top', + SCROLL_BOTTOM: 'scroll.bottom', + SCROLL_LEFT: 'scroll.left', + SCROLL_RIGHT: 'scroll.right', + FOLLOW_START: 'follow.start', + HISTORY_PREV: 'history.prev', + HISTORY_NEXT: 'history.next', }; diff --git a/src/background/index.js b/src/background/index.js index cb7a3ff..3fa8553 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,4 +1,3 @@ -import * as actions from '../shared/actions'; import * as tabs from './tabs'; import KeyQueue from './key-queue'; import backgroundReducers from '../reducers/background'; @@ -14,11 +13,9 @@ const keyPressHandle = (request, sender) => { return Promise.resolve(); } - if (actions.isContentAction(action.type)) { + return backgroundReducers(undefined, action, sender).then(() => { return browser.tabs.sendMessage(sender.tab.id, action); - } else { - return backgroundReducers(undefined, action, sender); - } + }); }; const normalizeUrl = (string) => { diff --git a/src/background/key-queue.js b/src/background/key-queue.js index 25f1d24..924bf77 100644 --- a/src/background/key-queue.js +++ b/src/background/key-queue.js @@ -1,5 +1,4 @@ -import newActions from '../actions'; -import * as actions from '../shared/actions'; +import actions from '../actions'; const DEFAULT_KEYMAP = { ':': { type: actions.CMD_OPEN }, @@ -18,15 +17,15 @@ const DEFAULT_KEYMAP = { 'G': { type: actions.SCROLL_BOTTOM }, '0': { type: actions.SCROLL_LEFT }, '$': { type: actions.SCROLL_RIGHT }, - 'd': { type: newActions.TABS_CLOSE }, - 'u': { type: newActions.TABS_REOPEN }, - 'h': { type: newActions.TABS_PREV, count: 1 }, - 'l': { type: newActions.TABS_NEXT, count: 1 }, - 'r': { type: newActions.TABS_RELOAD, cache: false }, - 'R': { type: newActions.TABS_RELOAD, cache: true }, - 'zi': { type: newActions.ZOOM_IN }, - 'zo': { type: newActions.ZOOM_OUT }, - 'zz': { type: newActions.ZOOM_NEUTRAL }, + 'd': { type: actions.TABS_CLOSE }, + 'u': { type: actions.TABS_REOPEN }, + 'h': { type: actions.TABS_PREV, count: 1 }, + 'l': { type: actions.TABS_NEXT, count: 1 }, + 'r': { type: actions.TABS_RELOAD, cache: false }, + 'R': { type: actions.TABS_RELOAD, cache: true }, + 'zi': { type: actions.ZOOM_IN }, + 'zo': { type: actions.ZOOM_OUT }, + 'zz': { type: actions.ZOOM_NEUTRAL }, 'f': { type: actions.FOLLOW_START, newTab: false }, 'F': { type: actions.FOLLOW_START, newTab: true }, 'H': { type: actions.HISTORY_PREV }, diff --git a/src/content/index.js b/src/content/index.js index 4fba516..314dfea 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,54 +1,12 @@ import '../console/console-frame.scss'; -import * as scrolls from './scrolls'; -import * as histories from './histories'; -import * as actions from '../shared/actions'; import * as consoleFrames from '../console/frames'; -import actionTypes from '../actions'; -import Follow from './follow'; +import actions from '../actions'; +import contentReducer from '../reducers/content'; consoleFrames.initialize(window.document); browser.runtime.onMessage.addListener((action) => { - switch (action.type) { - case actions.CMD_OPEN: - return consoleFrames.showCommand(''); - case actions.CMD_TABS_OPEN: - if (action.alter) { - // alter url - return consoleFrames.showCommand('open ' + window.location.href); - } else { - return consoleFrames.showCommand('open '); - } - case actions.CMD_BUFFER: - return consoleFrames.showCommand('buffer '); - case actions.SCROLL_LINES: - scrolls.scrollLines(window, action.count); - break; - case actions.SCROLL_PAGES: - scrolls.scrollPages(window, action.count); - 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.newTab); - break; - case actions.HISTORY_PREV: - histories.prev(window); - break; - case actions.HISTORY_NEXT: - histories.next(window); - break; - } + contentReducer(undefined, action); return Promise.resolve(); }); @@ -72,7 +30,7 @@ window.addEventListener("keypress", (e) => { browser.runtime.onMessage.addListener((action) => { switch (action.type) { - case actionTypes.CONSOLE_HIDE: + case actions.CONSOLE_HIDE: window.focus(); return consoleFrames.blur(window.document); case 'vimvixen.command.enter': diff --git a/src/reducers/content.js b/src/reducers/content.js new file mode 100644 index 0000000..bcf1160 --- /dev/null +++ b/src/reducers/content.js @@ -0,0 +1,48 @@ +import * as consoleFrames from '../console/frames'; +import * as histories from '../content/histories'; +import * as scrolls from '../content/scrolls'; +import Follow from '../content/follow'; +import actions from '../actions'; + +export default function reducer(state, action = {}) { + switch (action.type) { + case actions.CMD_OPEN: + return consoleFrames.showCommand(''); + case actions.CMD_TABS_OPEN: + if (action.alter) { + // alter url + return consoleFrames.showCommand('open ' + window.location.href); + } else { + return consoleFrames.showCommand('open '); + } + case actions.CMD_BUFFER: + return consoleFrames.showCommand('buffer '); + case actions.SCROLL_LINES: + scrolls.scrollLines(window, action.count); + break; + case actions.SCROLL_PAGES: + scrolls.scrollPages(window, action.count); + 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.newTab); + break; + case actions.HISTORY_PREV: + histories.prev(window); + break; + case actions.HISTORY_NEXT: + histories.next(window); + break; + } +} diff --git a/src/shared/actions.js b/src/shared/actions.js deleted file mode 100644 index 5163cf3..0000000 --- a/src/shared/actions.js +++ /dev/null @@ -1,31 +0,0 @@ -export const CMD_OPEN = 'cmd.open'; -export const CMD_TABS_OPEN = 'cmd.tabs.open'; -export const CMD_BUFFER = 'cmd.buffer'; -export const SCROLL_LINES = 'scroll.lines'; -export const SCROLL_PAGES = 'scroll.pages'; -export const SCROLL_TOP = 'scroll.top'; -export const SCROLL_BOTTOM = 'scroll.bottom'; -export const SCROLL_LEFT= 'scroll.left'; -export const SCROLL_RIGHT= 'scroll.right'; -export const FOLLOW_START = 'follow.start'; -export const HISTORY_PREV = 'history.prev'; -export const HISTORY_NEXT = 'history.next'; - -const CONTENT_ACTION_SET = new Set([ - CMD_OPEN, - CMD_TABS_OPEN, - CMD_BUFFER, - SCROLL_LINES, - SCROLL_PAGES, - SCROLL_TOP, - SCROLL_BOTTOM, - SCROLL_LEFT, - SCROLL_RIGHT, - FOLLOW_START, - HISTORY_PREV, - HISTORY_NEXT -]); - -export const isContentAction = (action) => { - return CONTENT_ACTION_SET.has(action); -}; -- cgit v1.2.3 From 879b5afe66ee79424c3ffee3951ef1c0b8c86eaa Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 10 Sep 2017 21:28:00 +0900 Subject: key input sequence as action/reducer --- src/actions/index.js | 4 ++ src/actions/input.js | 15 +++++++ src/background/index.js | 56 ++++++++++++++++---------- src/background/key-queue.js | 82 --------------------------------------- src/background/keys.js | 57 +++++++++++++++++++++++++++ src/background/tabs.js | 4 +- src/content/index.js | 10 +---- src/reducers/input.js | 23 +++++++++++ test/background/key-queue.test.js | 50 ------------------------ test/background/keys.test.js | 31 +++++++++++++++ 10 files changed, 170 insertions(+), 162 deletions(-) create mode 100644 src/actions/input.js delete mode 100644 src/background/key-queue.js create mode 100644 src/background/keys.js create mode 100644 src/reducers/input.js delete mode 100644 test/background/key-queue.test.js create mode 100644 test/background/keys.test.js (limited to 'src/actions/index.js') diff --git a/src/actions/index.js b/src/actions/index.js index de3ab42..135dd4a 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -29,4 +29,8 @@ export default { FOLLOW_START: 'follow.start', HISTORY_PREV: 'history.prev', HISTORY_NEXT: 'history.next', + + // User input + INPUT_KEY_PRESS: 'input.key,press', + INPUT_CLEAR_KEYS: 'input.clear.keys', }; diff --git a/src/actions/input.js b/src/actions/input.js new file mode 100644 index 0000000..c72b9e0 --- /dev/null +++ b/src/actions/input.js @@ -0,0 +1,15 @@ +import actions from '../actions'; + +export function keyPress(code, ctrl) { + return { + type: actions.INPUT_KEY_PRESS, + code, + ctrl + }; +} + +export function clearKeys() { + return { + type: actions.INPUT_CLEAR_KEYS + } +} diff --git a/src/background/index.js b/src/background/index.js index 3fa8553..4d75b33 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,22 +1,10 @@ import * as tabs from './tabs'; -import KeyQueue from './key-queue'; +import * as keys from './keys'; +import * as inputActions from '../actions/input'; import backgroundReducers from '../reducers/background'; +import inputReducers from '../reducers/input'; -const queue = new KeyQueue(); - -const keyPressHandle = (request, sender) => { - let action = queue.push({ - code: request.code, - ctrl: request.ctrl - }); - if (!action) { - return Promise.resolve(); - } - - return backgroundReducers(undefined, action, sender).then(() => { - return browser.tabs.sendMessage(sender.tab.id, action); - }); -}; +let inputState = inputReducers(undefined, {}); const normalizeUrl = (string) => { try { @@ -51,8 +39,6 @@ const cmdEnterHandle = (request, sender) => { browser.runtime.onMessage.addListener((request, sender) => { switch (request.type) { - case 'event.keypress': - return keyPressHandle(request, sender); case 'event.cmd.enter': return cmdEnterHandle(request, sender); default: @@ -60,6 +46,36 @@ browser.runtime.onMessage.addListener((request, sender) => { } }); -browser.runtime.onMessage.addListener((action, sender) => { +const keyQueueChanged = (sender, prevState, state) => { + if (state.keys.length === 0) { + return Promise.resolve(); + } + + let prefix = keys.asKeymapChars(state.keys); + let matched = Object.keys(keys.defaultKeymap).filter((keys) => { + return keys.startsWith(prefix); + }); + if (matched.length == 0) { + return handleMessage(inputActions.clearKeys(), sender); + } else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) { + return Promise.resolve(); + } + let action = keys.defaultKeymap[matched]; + return handleMessage(inputActions.clearKeys(), sender).then(() => { + return backgroundReducers(undefined, action, sender).then(() => { + return browser.tabs.sendMessage(sender.tab.id, action); + }); + }); +}; + +const handleMessage = (action, sender) => { + let nextInputState = inputReducers(inputState, action); + if (JSON.stringify(nextInputState) !== JSON.stringify(inputState)) { + let prevState = inputState; + inputState = nextInputState; + return keyQueueChanged(sender, prevState, inputState); + } return backgroundReducers(undefined, action, sender); -}); +}; + +browser.runtime.onMessage.addListener(handleMessage); diff --git a/src/background/key-queue.js b/src/background/key-queue.js deleted file mode 100644 index 924bf77..0000000 --- a/src/background/key-queue.js +++ /dev/null @@ -1,82 +0,0 @@ -import actions from '../actions'; - -const DEFAULT_KEYMAP = { - ':': { type: actions.CMD_OPEN }, - 'o': { type: actions.CMD_TABS_OPEN, alter: false }, - 'O': { type: actions.CMD_TABS_OPEN, alter: true }, - 'b': { type: actions.CMD_BUFFER }, - 'k': { type: actions.SCROLL_LINES, count: -1 }, - 'j': { type: actions.SCROLL_LINES, count: 1 }, - '': { type: actions.SCROLL_LINES, count: -1 }, - '': { type: actions.SCROLL_LINES, count: 1 }, - '': { type: actions.SCROLL_PAGES, count: -0.5 }, - '': { type: actions.SCROLL_PAGES, count: 0.5 }, - '': { type: actions.SCROLL_PAGES, count: -1 }, - '': { type: actions.SCROLL_PAGES, count: 1 }, - 'gg': { type: actions.SCROLL_TOP }, - 'G': { type: actions.SCROLL_BOTTOM }, - '0': { type: actions.SCROLL_LEFT }, - '$': { type: actions.SCROLL_RIGHT }, - 'd': { type: actions.TABS_CLOSE }, - 'u': { type: actions.TABS_REOPEN }, - 'h': { type: actions.TABS_PREV, count: 1 }, - 'l': { type: actions.TABS_NEXT, count: 1 }, - 'r': { type: actions.TABS_RELOAD, cache: false }, - 'R': { type: actions.TABS_RELOAD, cache: true }, - 'zi': { type: actions.ZOOM_IN }, - 'zo': { type: actions.ZOOM_OUT }, - 'zz': { type: actions.ZOOM_NEUTRAL }, - 'f': { type: actions.FOLLOW_START, newTab: false }, - 'F': { type: actions.FOLLOW_START, newTab: true }, - 'H': { type: actions.HISTORY_PREV }, - 'L': { type: actions.HISTORY_NEXT }, -} - -export default class KeyQueue { - - constructor(keymap = DEFAULT_KEYMAP) { - this.data = []; - this.keymap = keymap; - } - - push(key) { - this.data.push(key); - - let current = this.asKeymapChars(); - let filtered = Object.keys(this.keymap).filter((keys) => { - return keys.startsWith(current); - }); - - if (filtered.length == 0) { - this.data = []; - return null; - } else if (filtered.length === 1 && current === filtered[0]) { - let action = this.keymap[filtered[0]]; - this.data = []; - return action; - } - return null; - } - - asKeymapChars() { - return this.data.map((k) => { - let c = String.fromCharCode(k.code); - if (k.ctrl) { - return ''; - } else { - return c - } - }).join(''); - } - - asCaretChars() { - return this.data.map((k) => { - let c = String.fromCharCode(k.code); - if (k.ctrl) { - return '^' + c.toUpperCase(); - } else { - return c; - } - }).join(''); - } -} diff --git a/src/background/keys.js b/src/background/keys.js new file mode 100644 index 0000000..0ce53fa --- /dev/null +++ b/src/background/keys.js @@ -0,0 +1,57 @@ +import actions from '../actions'; + +const defaultKeymap = { + ':': { type: actions.CMD_OPEN }, + 'o': { type: actions.CMD_TABS_OPEN, alter: false }, + 'O': { type: actions.CMD_TABS_OPEN, alter: true }, + 'b': { type: actions.CMD_BUFFER }, + 'k': { type: actions.SCROLL_LINES, count: -1 }, + 'j': { type: actions.SCROLL_LINES, count: 1 }, + '': { type: actions.SCROLL_LINES, count: -1 }, + '': { type: actions.SCROLL_LINES, count: 1 }, + '': { type: actions.SCROLL_PAGES, count: -0.5 }, + '': { type: actions.SCROLL_PAGES, count: 0.5 }, + '': { type: actions.SCROLL_PAGES, count: -1 }, + '': { type: actions.SCROLL_PAGES, count: 1 }, + 'gg': { type: actions.SCROLL_TOP }, + 'G': { type: actions.SCROLL_BOTTOM }, + '0': { type: actions.SCROLL_LEFT }, + '$': { type: actions.SCROLL_RIGHT }, + 'd': { type: actions.TABS_CLOSE }, + 'u': { type: actions.TABS_REOPEN }, + 'h': { type: actions.TABS_PREV, count: 1 }, + 'l': { type: actions.TABS_NEXT, count: 1 }, + 'r': { type: actions.TABS_RELOAD, cache: false }, + 'R': { type: actions.TABS_RELOAD, cache: true }, + 'zi': { type: actions.ZOOM_IN }, + 'zo': { type: actions.ZOOM_OUT }, + 'zz': { type: actions.ZOOM_NEUTRAL }, + 'f': { type: actions.FOLLOW_START, newTab: false }, + 'F': { type: actions.FOLLOW_START, newTab: true }, + 'H': { type: actions.HISTORY_PREV }, + 'L': { type: actions.HISTORY_NEXT }, +} + +const asKeymapChars = (keys) => { + return keys.map((k) => { + let c = String.fromCharCode(k.code); + if (k.ctrl) { + return ''; + } else { + return c + } + }).join(''); +} + +const asCaretChars = (keys) => { + return keys.map((k) => { + let c = String.fromCharCode(k.code); + if (k.ctrl) { + return '^' + c.toUpperCase(); + } else { + return c; + } + }).join(''); +} + +export { defaultKeymap, asKeymapChars, asCaretChars }; diff --git a/src/background/tabs.js b/src/background/tabs.js index 111bbd9..bd69b4b 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -59,7 +59,7 @@ const getCompletions = (keyword) => { }; const selectPrevTab = (current, count) => { - return browser.tabs.query({ currentWindow: true }, (tabs) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { if (tabs.length < 2) { return; } @@ -70,7 +70,7 @@ const selectPrevTab = (current, count) => { }; const selectNextTab = (current, count) => { - return browser.tabs.query({ currentWindow: true }, (tabs) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { if (tabs.length < 2) { return; } diff --git a/src/content/index.js b/src/content/index.js index 314dfea..12d079f 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,4 +1,5 @@ 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'; @@ -14,14 +15,7 @@ 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) + browser.runtime.sendMessage(inputActions.keyPress(e.which, e.ctrlKey)) .catch((err) => { console.error("Vim Vixen:", err); return consoleFrames.showError(err.message); diff --git a/src/reducers/input.js b/src/reducers/input.js new file mode 100644 index 0000000..25ff1a3 --- /dev/null +++ b/src/reducers/input.js @@ -0,0 +1,23 @@ +import actions from '../actions'; + +const defaultState = { + keys: [], +}; + +export default function reducer(state = defaultState, action = {}) { + switch (action.type) { + case actions.INPUT_KEY_PRESS: + return Object.assign({}, state, { + keys: state.keys.concat([{ + code: action.code, + ctrl: action.ctrl + }]) + }); + case actions.INPUT_CLEAR_KEYS: + return Object.assign({}, state, { + keys: [], + }); + default: + return state; + } +} diff --git a/test/background/key-queue.test.js b/test/background/key-queue.test.js deleted file mode 100644 index ac43228..0000000 --- a/test/background/key-queue.test.js +++ /dev/null @@ -1,50 +0,0 @@ -import { expect } from "chai"; -import KeyQueue from '../../src/background/key-queue'; - -describe("keyQueue class", () => { - const KEYMAP = { - 'gGG': [], - 'gg': [ 'scroll.top' ], - }; - - const g = 'g'.charCodeAt(0); - const G = 'G'.charCodeAt(0); - const x = 'x'.charCodeAt(0); - - describe("#push", () => { - it("returns matched action", () => { - let queue = new KeyQueue(KEYMAP); - queue.push({ code: g }); - let action = queue.push({ code: g }); - - expect(action).to.deep.equal([ 'scroll.top' ]); - }); - - it("returns null on no actions matched", () => { - let queue = new KeyQueue(KEYMAP); - queue.push({ code: g }); - let action = queue.push({ code: G }); - - expect(action).to.be.null; - expect(queue.asKeymapChars()).to.be.empty; - }); - }); - - describe('#asKeymapChars', () => { - let queue = new KeyQueue(KEYMAP); - queue.push({ code: g }); - queue.push({ code: x, ctrl: true }); - queue.push({ code: G }); - - expect(queue.asKeymapChars()).to.equal('gG'); - }); - - describe('#asCaretChars', () => { - let queue = new KeyQueue(KEYMAP); - queue.push({ code: g }); - queue.push({ code: x, ctrl: true }); - queue.push({ code: G }); - - expect(queue.asCaretChars()).to.equal('g^XG'); - }); -}); diff --git a/test/background/keys.test.js b/test/background/keys.test.js new file mode 100644 index 0000000..2cb9a3a --- /dev/null +++ b/test/background/keys.test.js @@ -0,0 +1,31 @@ +import { expect } from "chai"; +import * as keys from '../../src/background/keys'; + +describe("keys", () => { + const KEYMAP = { + 'gGG': [], + 'gg': { type: 'scroll.top' }, + }; + + const g = 'g'.charCodeAt(0); + const G = 'G'.charCodeAt(0); + const x = 'x'.charCodeAt(0); + + describe('#asKeymapChars', () => { + let keySequence = [ + { code: g }, + { code: x, ctrl: true }, + { code: G } + ]; + expect(keys.asKeymapChars(keySequence)).to.equal('gG'); + }); + + describe('#asCaretChars', () => { + let keySequence = [ + { code: g }, + { code: x, ctrl: true }, + { code: G } + ]; + expect(keys.asCaretChars(keySequence)).to.equal('g^XG'); + }); +}); -- cgit v1.2.3 From bf890a6d9d793b581a44ee267616ed589f429bef Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 11 Sep 2017 21:07:02 +0900 Subject: command as action/reducer --- src/actions/command.js | 35 +++++++++++++++++++++++++++++++++++ src/actions/index.js | 4 ++++ src/background/index.js | 48 ++++++------------------------------------------ src/console/console.js | 10 ++-------- src/reducers/command.js | 24 ++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 src/actions/command.js create mode 100644 src/reducers/command.js (limited to 'src/actions/index.js') diff --git a/src/actions/command.js b/src/actions/command.js new file mode 100644 index 0000000..982255c --- /dev/null +++ b/src/actions/command.js @@ -0,0 +1,35 @@ +import actions from '../actions'; + +const normalizeUrl = (string) => { + try { + return new URL(string).href + } catch (e) { + return 'http://' + string; + } +} + +export function exec(line) { + let name = line.split(' ')[0]; + let remaining = line.replace(name + ' ', ''); + + switch (name) { + case 'open': + // TODO use search engined and pass keywords to them + return { + type: actions.COMMAND_OPEN_URL, + url: normalizeUrl(remaining) + }; + case 'tabopen': + return { + type: actions.COMMAND_TABOPEN_URL, + url: remaining + }; + case 'b': + case 'buffer': + return { + type: actions.COMMAND_BUFFER, + keywords: remaining + }; + } + throw new Error(name + ' command is not defined'); +} diff --git a/src/actions/index.js b/src/actions/index.js index 135dd4a..63d5f6f 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -33,4 +33,8 @@ export default { // User input INPUT_KEY_PRESS: 'input.key,press', INPUT_CLEAR_KEYS: 'input.clear.keys', + + COMMAND_OPEN_URL: 'command.open.url', + COMMAND_TABOPEN_URL: 'command.tabopen.url', + COMMAND_BUFFER: 'command.buffer', }; diff --git a/src/background/index.js b/src/background/index.js index 4d75b33..e72cab0 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,51 +1,11 @@ -import * as tabs from './tabs'; import * as keys from './keys'; import * as inputActions from '../actions/input'; import backgroundReducers from '../reducers/background'; +import commandReducer from '../reducers/command'; import inputReducers from '../reducers/input'; let inputState = inputReducers(undefined, {}); -const normalizeUrl = (string) => { - try { - return new URL(string).href - } catch (e) { - return 'http://' + string; - } -} - -const cmdBuffer = (sender, arg) => { - if (isNaN(arg)) { - return tabs.selectByKeyword(sender.tab, arg); - } else { - let index = parseInt(arg, 10) - 1; - return tabs.selectAt(index); - } -} - -const cmdEnterHandle = (request, sender) => { - let words = request.text.split(' ').filter((s) => s.length > 0); - switch (words[0]) { - case 'open': - return browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) }); - case 'tabopen': - return browser.tabs.create({ url: normalizeUrl(words[1]) }); - case 'b': - case 'buffer': - return cmdBuffer(sender, words[1]); - } - throw new Error(words[0] + ' command is not defined'); -}; - -browser.runtime.onMessage.addListener((request, sender) => { - switch (request.type) { - case 'event.cmd.enter': - return cmdEnterHandle(request, sender); - default: - return browser.tabs.sendMessage(sender.tab.id, request); - } -}); - const keyQueueChanged = (sender, prevState, state) => { if (state.keys.length === 0) { return Promise.resolve(); @@ -75,7 +35,11 @@ const handleMessage = (action, sender) => { inputState = nextInputState; return keyQueueChanged(sender, prevState, inputState); } - return backgroundReducers(undefined, action, sender); + return backgroundReducers(undefined, action, sender).then(() => { + return commandReducer(undefined, action, sender).then(() => { + return browser.tabs.sendMessage(sender.tab.id, action); + }); + }); }; browser.runtime.onMessage.addListener(handleMessage); diff --git a/src/console/console.js b/src/console/console.js index f83f79c..25ab36d 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,6 +1,7 @@ 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'; @@ -10,13 +11,6 @@ var completion = null; var completionOrigin = ""; let state = consoleReducer(undefined, {}); -const keydownMessage = (input) => { - return { - type: 'vimvixen.command.enter', - value: input.value - }; -}; - const handleBlur = () => { return browser.runtime.sendMessage(consoleActions.hide()); }; @@ -58,7 +52,7 @@ const handleKeydown = (e) => { case KeyboardEvent.DOM_VK_ESCAPE: return input.blur(); case KeyboardEvent.DOM_VK_RETURN: - return browser.runtime.sendMessage(keydownMessage(e.target)); + return browser.runtime.sendMessage(commandActions.exec(e.target.value)); case KeyboardEvent.DOM_VK_TAB: if (e.shiftKey) { completePrev(); diff --git a/src/reducers/command.js b/src/reducers/command.js new file mode 100644 index 0000000..7e03593 --- /dev/null +++ b/src/reducers/command.js @@ -0,0 +1,24 @@ +import * as tabs from '../background/tabs'; +import actions from '../actions'; + +const cmdBuffer = (sender, arg) => { + if (isNaN(arg)) { + return tabs.selectByKeyword(sender.tab, arg); + } else { + let index = parseInt(arg, 10) - 1; + return tabs.selectAt(index); + } +} + +export default function reducer(state, action, sender) { + switch (action.type) { + case actions.COMMAND_OPEN_URL: + return browser.tabs.update(sender.tab.id, { url: action.url }); + case actions.COMMAND_TABOPEN_URL: + return browser.tabs.create({ url: action.url }); + case actions.COMMAND_BUFFER: + return cmdBuffer(sender, action.keywords); + default: + return Promise.resolve(); + } +} -- cgit v1.2.3