Use official redux on background

jh-changes
Shin'ya Ueoka 6 years ago
parent efa1cb3967
commit d781dfc786
  1. 68
      src/background/actions/command.js
  2. 41
      src/background/actions/console.js
  3. 11
      src/background/actions/tab.js
  4. 94
      src/background/components/operation.js
  5. 17
      src/background/index.js

@ -1,5 +1,5 @@
import messages from 'shared/messages';
import actions from '../actions'; import actions from '../actions';
import * as consoleActions from './console';
import * as tabs from '../shared/tabs'; import * as tabs from '../shared/tabs';
import * as bookmarks from '../shared/bookmarks'; import * as bookmarks from '../shared/bookmarks';
import * as parsers from 'shared/commands/parsers'; import * as parsers from 'shared/commands/parsers';
@ -39,7 +39,7 @@ const winopenCommand = (url) => {
const bufferCommand = async(keywords) => { const bufferCommand = async(keywords) => {
if (keywords.length === 0) { if (keywords.length === 0) {
return Promise.resolve([]); return;
} }
let keywordsStr = keywords.join(' '); let keywordsStr = keywords.join(' ');
let got = await browser.tabs.query({ let got = await browser.tabs.query({
@ -57,24 +57,18 @@ const bufferCommand = async(keywords) => {
const addbookmarkCommand = async(tab, args) => { const addbookmarkCommand = async(tab, args) => {
if (!args[0]) { if (!args[0]) {
return; return { type: '' };
} }
let item = await bookmarks.create(args.join(' '), tab.url); let item = await bookmarks.create(args.join(' '), tab.url);
if (!item) { if (!item) {
return browser.tabs.sendMessage(tab.id, { return consoleActions.error(tab, 'Could not create a bookmark');
type: messages.CONSOLE_SHOW_ERROR,
text: 'Could not create a bookmark',
});
} }
return browser.tabs.sendMessage(tab.id, { return consoleActions.info(tab, 'Saved current page: ' + item.url);
type: messages.CONSOLE_SHOW_INFO,
text: 'Saved current page: ' + item.url,
});
}; };
const setCommand = (args) => { const setCommand = (args) => {
if (!args[0]) { if (!args[0]) {
return Promise.resolve(); return { type: '' };
} }
let [name, value] = parsers.parseSetOption(args[0], properties.types); let [name, value] = parsers.parseSetOption(args[0], properties.types);
@ -85,49 +79,69 @@ const setCommand = (args) => {
}; };
}; };
// eslint-disable-next-line complexity // eslint-disable-next-line complexity, max-lines-per-function
const exec = (tab, line, settings) => { const doExec = async(tab, line, settings) => {
let [name, args] = parsers.parseCommandLine(line); let [name, args] = parsers.parseCommandLine(line);
switch (name) { switch (name) {
case 'o': case 'o':
case 'open': case 'open':
return openCommand(parsers.normalizeUrl(args, settings.search)); await openCommand(parsers.normalizeUrl(args, settings.search));
break;
case 't': case 't':
case 'tabopen': case 'tabopen':
return tabopenCommand(parsers.normalizeUrl(args, settings.search)); await tabopenCommand(parsers.normalizeUrl(args, settings.search));
break;
case 'w': case 'w':
case 'winopen': case 'winopen':
return winopenCommand(parsers.normalizeUrl(args, settings.search)); await winopenCommand(parsers.normalizeUrl(args, settings.search));
break;
case 'b': case 'b':
case 'buffer': case 'buffer':
return bufferCommand(args); await bufferCommand(args);
break;
case 'bd': case 'bd':
case 'bdel': case 'bdel':
case 'bdelete': case 'bdelete':
return tabs.closeTabByKeywords(args.join(' ')); await tabs.closeTabByKeywords(args.join(' '));
break;
case 'bd!': case 'bd!':
case 'bdel!': case 'bdel!':
case 'bdelete!': case 'bdelete!':
return tabs.closeTabByKeywordsForce(args.join(' ')); await tabs.closeTabByKeywordsForce(args.join(' '));
break;
case 'bdeletes': case 'bdeletes':
return tabs.closeTabsByKeywords(args.join(' ')); await tabs.closeTabsByKeywords(args.join(' '));
break;
case 'bdeletes!': case 'bdeletes!':
return tabs.closeTabsByKeywordsForce(args.join(' ')); await tabs.closeTabsByKeywordsForce(args.join(' '));
break;
case 'addbookmark': case 'addbookmark':
return addbookmarkCommand(tab, args); return addbookmarkCommand(tab, args);
case 'set': case 'set':
return setCommand(args); return setCommand(args);
case 'q': case 'q':
case 'quit': case 'quit':
return tabcloseCommand(); await tabcloseCommand();
break;
case 'qa': case 'qa':
case 'quitall': case 'quitall':
return tabcloseAllCommand(); await tabcloseAllCommand();
case '': break;
return Promise.resolve(); default:
return consoleActions.error(tab, name + ' command is not defined');
}
return { type: '' };
};
// eslint-disable-next-line complexity
const exec = async(tab, line, settings) => {
try {
let action = await doExec(tab, line, settings);
return action;
} catch (e) {
return consoleActions.error(tab, e.toString());
} }
throw new Error(name + ' command is not defined');
}; };
export { exec }; export { exec };

@ -0,0 +1,41 @@
import messages from 'shared/messages';
const error = async(tab, text) => {
await browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_SHOW_ERROR,
text,
});
return { type: '' };
};
const info = async(tab, text) => {
await browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_SHOW_INFO,
text,
});
return { type: '' };
};
const showCommand = async(tab, command) => {
await browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_SHOW_COMMAND,
command,
});
return { type: '' };
};
const showFind = async(tab) => {
await browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_SHOW_FIND
});
return { type: '' };
};
const hide = async(tab) => {
await browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_HIDE,
});
return { type: '' };
};
export { error, info, showCommand, showFind, hide };

@ -4,21 +4,24 @@ const openNewTab = async(
url, openerTabId, background = false, adjacent = false url, openerTabId, background = false, adjacent = false
) => { ) => {
if (!adjacent) { if (!adjacent) {
return browser.tabs.create({ url, active: !background }); await browser.tabs.create({ url, active: !background });
return { type: '' };
} }
let tabs = await browser.tabs.query({ let tabs = await browser.tabs.query({
active: true, currentWindow: true active: true, currentWindow: true
}); });
return browser.tabs.create({ await browser.tabs.create({
url, url,
openerTabId, openerTabId,
active: !background, active: !background,
index: tabs[0].index + 1 index: tabs[0].index + 1
}); });
return { type: '' };
}; };
const openToTab = (url, tab) => { const openToTab = async(url, tab) => {
return browser.tabs.update(tab.id, { url: url }); await browser.tabs.update(tab.id, { url: url });
return { type: '' };
}; };
const selected = (tabId) => { const selected = (tabId) => {

@ -2,6 +2,7 @@ import messages from 'shared/messages';
import operations from 'shared/operations'; import operations from 'shared/operations';
import * as tabs from '../shared//tabs'; import * as tabs from '../shared//tabs';
import * as zooms from '../shared/zooms'; import * as zooms from '../shared/zooms';
import * as consoleActions from '../actions/console';
export default class BackgroundComponent { export default class BackgroundComponent {
constructor(store) { constructor(store) {
@ -23,101 +24,104 @@ export default class BackgroundComponent {
switch (message.type) { switch (message.type) {
case messages.BACKGROUND_OPERATION: case messages.BACKGROUND_OPERATION:
return this.store.dispatch( return this.store.dispatch(
this.exec(message.operation, sender.tab), this.exec(message.operation, sender.tab));
sender);
} }
} }
// eslint-disable-next-line complexity, max-lines-per-function // eslint-disable-next-line complexity, max-lines-per-function
exec(operation, tab) { async exec(operation, tab) {
let tabState = this.store.getState().tab; let tabState = this.store.getState().tab;
switch (operation.type) { switch (operation.type) {
case operations.TAB_CLOSE: case operations.TAB_CLOSE:
return tabs.closeTab(tab.id); await tabs.closeTab(tab.id);
break;
case operations.TAB_CLOSE_FORCE: case operations.TAB_CLOSE_FORCE:
return tabs.closeTabForce(tab.id); await tabs.closeTabForce(tab.id);
break;
case operations.TAB_REOPEN: case operations.TAB_REOPEN:
return tabs.reopenTab(); await tabs.reopenTab();
break;
case operations.TAB_PREV: case operations.TAB_PREV:
return tabs.selectPrevTab(tab.index, operation.count); await tabs.selectPrevTab(tab.index, operation.count);
break;
case operations.TAB_NEXT: case operations.TAB_NEXT:
return tabs.selectNextTab(tab.index, operation.count); await tabs.selectNextTab(tab.index, operation.count);
break;
case operations.TAB_FIRST: case operations.TAB_FIRST:
return tabs.selectFirstTab(); await tabs.selectFirstTab();
break;
case operations.TAB_LAST: case operations.TAB_LAST:
return tabs.selectLastTab(); await tabs.selectLastTab();
break;
case operations.TAB_PREV_SEL: case operations.TAB_PREV_SEL:
if (tabState.previousSelected > 0) { if (tabState.previousSelected > 0) {
return tabs.selectTab(tabState.previousSelected); await tabs.selectTab(tabState.previousSelected);
} }
break; break;
case operations.TAB_RELOAD: case operations.TAB_RELOAD:
return tabs.reload(tab, operation.cache); await tabs.reload(tab, operation.cache);
break;
case operations.TAB_PIN: case operations.TAB_PIN:
return tabs.updateTabPinned(tab, true); await tabs.updateTabPinned(tab, true);
break;
case operations.TAB_UNPIN: case operations.TAB_UNPIN:
return tabs.updateTabPinned(tab, false); await tabs.updateTabPinned(tab, false);
break;
case operations.TAB_TOGGLE_PINNED: case operations.TAB_TOGGLE_PINNED:
return tabs.toggleTabPinned(tab); await tabs.toggleTabPinned(tab);
break;
case operations.TAB_DUPLICATE: case operations.TAB_DUPLICATE:
return tabs.duplicate(tab.id); await tabs.duplicate(tab.id);
break;
case operations.ZOOM_IN: case operations.ZOOM_IN:
return zooms.zoomIn(); await zooms.zoomIn();
break;
case operations.ZOOM_OUT: case operations.ZOOM_OUT:
return zooms.zoomOut(); await zooms.zoomOut();
break;
case operations.ZOOM_NEUTRAL: case operations.ZOOM_NEUTRAL:
return zooms.neutral(); await zooms.neutral();
break;
case operations.COMMAND_SHOW: case operations.COMMAND_SHOW:
return this.sendConsoleShowCommand(tab, ''); return consoleActions.showCommand(tab, '');
case operations.COMMAND_SHOW_OPEN: case operations.COMMAND_SHOW_OPEN:
if (operation.alter) { if (operation.alter) {
// alter url // alter url
return this.sendConsoleShowCommand(tab, 'open ' + tab.url); return consoleActions.showCommand(tab, 'open ' + tab.url);
} }
return this.sendConsoleShowCommand(tab, 'open '); return consoleActions.showCommand(tab, 'open ');
case operations.COMMAND_SHOW_TABOPEN: case operations.COMMAND_SHOW_TABOPEN:
if (operation.alter) { if (operation.alter) {
// alter url // alter url
return this.sendConsoleShowCommand(tab, 'tabopen ' + tab.url); return consoleActions.showCommand(tab, 'tabopen ' + tab.url);
} }
return this.sendConsoleShowCommand(tab, 'tabopen '); return consoleActions.showCommand(tab, 'tabopen ');
case operations.COMMAND_SHOW_WINOPEN: case operations.COMMAND_SHOW_WINOPEN:
if (operation.alter) { if (operation.alter) {
// alter url // alter url
return this.sendConsoleShowCommand(tab, 'winopen ' + tab.url); return consoleActions.showCommand(tab, 'win ' + tab.url);
} }
return this.sendConsoleShowCommand(tab, 'winopen '); return consoleActions.showCommand(tab, 'winopen ');
case operations.COMMAND_SHOW_BUFFER: case operations.COMMAND_SHOW_BUFFER:
return this.sendConsoleShowCommand(tab, 'buffer '); return consoleActions.showCommand(tab, 'buffer ');
case operations.COMMAND_SHOW_ADDBOOKMARK: case operations.COMMAND_SHOW_ADDBOOKMARK:
if (operation.alter) { if (operation.alter) {
return this.sendConsoleShowCommand(tab, 'addbookmark ' + tab.title); return consoleActions.showCommand(tab, 'addbookmark ' + tab.title);
} }
return this.sendConsoleShowCommand(tab, 'addbookmark '); return consoleActions.showCommand(tab, 'addbookmark ');
case operations.FIND_START: case operations.FIND_START:
return browser.tabs.sendMessage(tab.id, { return consoleActions.showFind(tab);
type: messages.CONSOLE_SHOW_FIND
});
case operations.CANCEL: case operations.CANCEL:
return browser.tabs.sendMessage(tab.id, { return consoleActions.hide(tab);
type: messages.CONSOLE_HIDE,
});
case operations.PAGE_SOURCE: case operations.PAGE_SOURCE:
return browser.tabs.create({ await browser.tabs.create({
url: 'view-source:' + tab.url, url: 'view-source:' + tab.url,
index: tab.index + 1, index: tab.index + 1,
openerTabId: tab.id, openerTabId: tab.id,
}); });
default: break;
return Promise.resolve();
}
} }
return { type: '' };
sendConsoleShowCommand(tab, command) {
return browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_SHOW_COMMAND,
command,
});
} }
} }

@ -1,22 +1,17 @@
import * as settingActions from 'background/actions/setting'; import * as settingActions from 'background/actions/setting';
import messages from 'shared/messages';
import BackgroundComponent from 'background/components/background'; import BackgroundComponent from 'background/components/background';
import OperationComponent from 'background/components/operation'; import OperationComponent from 'background/components/operation';
import TabComponent from 'background/components/tab'; import TabComponent from 'background/components/tab';
import IndicatorComponent from 'background/components/indicator'; import IndicatorComponent from 'background/components/indicator';
import reducers from 'background/reducers'; import reducers from 'background/reducers';
import { createStore } from 'shared/store'; import { createStore, applyMiddleware } from 'redux';
import promise from 'redux-promise';
import * as versions from 'shared/versions'; import * as versions from 'shared/versions';
const store = createStore(reducers, (e, sender) => { const store = createStore(
console.error('Vim-Vixen:', e); reducers,
if (sender) { applyMiddleware(promise),
return browser.tabs.sendMessage(sender.tab.id, { );
type: messages.CONSOLE_SHOW_ERROR,
text: e.message,
});
}
});
const checkAndNotifyUpdated = async() => { const checkAndNotifyUpdated = async() => {
let updated = await versions.checkUpdated(); let updated = await versions.checkUpdated();