content commands as action/reducer

jh-changes
Shin'ya Ueoka 7 years ago
parent 2c40d239f0
commit adc6a5175c
  1. 17
      src/actions/index.js
  2. 7
      src/background/index.js
  3. 21
      src/background/key-queue.js
  4. 50
      src/content/index.js
  5. 48
      src/reducers/content.js
  6. 31
      src/shared/actions.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',
};

@ -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) => {

@ -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 },

@ -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':

@ -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;
}
}

@ -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);
};