Use official redux on background
This commit is contained in:
parent
efa1cb3967
commit
d781dfc786
5 changed files with 144 additions and 87 deletions
|
@ -1,5 +1,5 @@
|
|||
import messages from 'shared/messages';
|
||||
import actions from '../actions';
|
||||
import * as consoleActions from './console';
|
||||
import * as tabs from '../shared/tabs';
|
||||
import * as bookmarks from '../shared/bookmarks';
|
||||
import * as parsers from 'shared/commands/parsers';
|
||||
|
@ -39,7 +39,7 @@ const winopenCommand = (url) => {
|
|||
|
||||
const bufferCommand = async(keywords) => {
|
||||
if (keywords.length === 0) {
|
||||
return Promise.resolve([]);
|
||||
return;
|
||||
}
|
||||
let keywordsStr = keywords.join(' ');
|
||||
let got = await browser.tabs.query({
|
||||
|
@ -57,24 +57,18 @@ const bufferCommand = async(keywords) => {
|
|||
|
||||
const addbookmarkCommand = async(tab, args) => {
|
||||
if (!args[0]) {
|
||||
return;
|
||||
return { type: '' };
|
||||
}
|
||||
let item = await bookmarks.create(args.join(' '), tab.url);
|
||||
if (!item) {
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_SHOW_ERROR,
|
||||
text: 'Could not create a bookmark',
|
||||
});
|
||||
return consoleActions.error(tab, 'Could not create a bookmark');
|
||||
}
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_SHOW_INFO,
|
||||
text: 'Saved current page: ' + item.url,
|
||||
});
|
||||
return consoleActions.info(tab, 'Saved current page: ' + item.url);
|
||||
};
|
||||
|
||||
const setCommand = (args) => {
|
||||
if (!args[0]) {
|
||||
return Promise.resolve();
|
||||
return { type: '' };
|
||||
}
|
||||
|
||||
let [name, value] = parsers.parseSetOption(args[0], properties.types);
|
||||
|
@ -85,49 +79,69 @@ const setCommand = (args) => {
|
|||
};
|
||||
};
|
||||
|
||||
// eslint-disable-next-line complexity
|
||||
const exec = (tab, line, settings) => {
|
||||
// eslint-disable-next-line complexity, max-lines-per-function
|
||||
const doExec = async(tab, line, settings) => {
|
||||
let [name, args] = parsers.parseCommandLine(line);
|
||||
|
||||
switch (name) {
|
||||
case 'o':
|
||||
case 'open':
|
||||
return openCommand(parsers.normalizeUrl(args, settings.search));
|
||||
await openCommand(parsers.normalizeUrl(args, settings.search));
|
||||
break;
|
||||
case 't':
|
||||
case 'tabopen':
|
||||
return tabopenCommand(parsers.normalizeUrl(args, settings.search));
|
||||
await tabopenCommand(parsers.normalizeUrl(args, settings.search));
|
||||
break;
|
||||
case 'w':
|
||||
case 'winopen':
|
||||
return winopenCommand(parsers.normalizeUrl(args, settings.search));
|
||||
await winopenCommand(parsers.normalizeUrl(args, settings.search));
|
||||
break;
|
||||
case 'b':
|
||||
case 'buffer':
|
||||
return bufferCommand(args);
|
||||
await bufferCommand(args);
|
||||
break;
|
||||
case 'bd':
|
||||
case 'bdel':
|
||||
case 'bdelete':
|
||||
return tabs.closeTabByKeywords(args.join(' '));
|
||||
await tabs.closeTabByKeywords(args.join(' '));
|
||||
break;
|
||||
case 'bd!':
|
||||
case 'bdel!':
|
||||
case 'bdelete!':
|
||||
return tabs.closeTabByKeywordsForce(args.join(' '));
|
||||
await tabs.closeTabByKeywordsForce(args.join(' '));
|
||||
break;
|
||||
case 'bdeletes':
|
||||
return tabs.closeTabsByKeywords(args.join(' '));
|
||||
await tabs.closeTabsByKeywords(args.join(' '));
|
||||
break;
|
||||
case 'bdeletes!':
|
||||
return tabs.closeTabsByKeywordsForce(args.join(' '));
|
||||
await tabs.closeTabsByKeywordsForce(args.join(' '));
|
||||
break;
|
||||
case 'addbookmark':
|
||||
return addbookmarkCommand(tab, args);
|
||||
case 'set':
|
||||
return setCommand(args);
|
||||
case 'q':
|
||||
case 'quit':
|
||||
return tabcloseCommand();
|
||||
await tabcloseCommand();
|
||||
break;
|
||||
case 'qa':
|
||||
case 'quitall':
|
||||
return tabcloseAllCommand();
|
||||
case '':
|
||||
return Promise.resolve();
|
||||
await tabcloseAllCommand();
|
||||
break;
|
||||
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 };
|
||||
|
|
41
src/background/actions/console.js
Normal file
41
src/background/actions/console.js
Normal file
|
@ -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
|
||||
) => {
|
||||
if (!adjacent) {
|
||||
return browser.tabs.create({ url, active: !background });
|
||||
await browser.tabs.create({ url, active: !background });
|
||||
return { type: '' };
|
||||
}
|
||||
let tabs = await browser.tabs.query({
|
||||
active: true, currentWindow: true
|
||||
});
|
||||
return browser.tabs.create({
|
||||
await browser.tabs.create({
|
||||
url,
|
||||
openerTabId,
|
||||
active: !background,
|
||||
index: tabs[0].index + 1
|
||||
});
|
||||
return { type: '' };
|
||||
};
|
||||
|
||||
const openToTab = (url, tab) => {
|
||||
return browser.tabs.update(tab.id, { url: url });
|
||||
const openToTab = async(url, tab) => {
|
||||
await browser.tabs.update(tab.id, { url: url });
|
||||
return { type: '' };
|
||||
};
|
||||
|
||||
const selected = (tabId) => {
|
||||
|
|
|
@ -2,6 +2,7 @@ import messages from 'shared/messages';
|
|||
import operations from 'shared/operations';
|
||||
import * as tabs from '../shared//tabs';
|
||||
import * as zooms from '../shared/zooms';
|
||||
import * as consoleActions from '../actions/console';
|
||||
|
||||
export default class BackgroundComponent {
|
||||
constructor(store) {
|
||||
|
@ -23,101 +24,104 @@ export default class BackgroundComponent {
|
|||
switch (message.type) {
|
||||
case messages.BACKGROUND_OPERATION:
|
||||
return this.store.dispatch(
|
||||
this.exec(message.operation, sender.tab),
|
||||
sender);
|
||||
this.exec(message.operation, sender.tab));
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line complexity, max-lines-per-function
|
||||
exec(operation, tab) {
|
||||
async exec(operation, tab) {
|
||||
let tabState = this.store.getState().tab;
|
||||
|
||||
switch (operation.type) {
|
||||
case operations.TAB_CLOSE:
|
||||
return tabs.closeTab(tab.id);
|
||||
await tabs.closeTab(tab.id);
|
||||
break;
|
||||
case operations.TAB_CLOSE_FORCE:
|
||||
return tabs.closeTabForce(tab.id);
|
||||
await tabs.closeTabForce(tab.id);
|
||||
break;
|
||||
case operations.TAB_REOPEN:
|
||||
return tabs.reopenTab();
|
||||
await tabs.reopenTab();
|
||||
break;
|
||||
case operations.TAB_PREV:
|
||||
return tabs.selectPrevTab(tab.index, operation.count);
|
||||
await tabs.selectPrevTab(tab.index, operation.count);
|
||||
break;
|
||||
case operations.TAB_NEXT:
|
||||
return tabs.selectNextTab(tab.index, operation.count);
|
||||
await tabs.selectNextTab(tab.index, operation.count);
|
||||
break;
|
||||
case operations.TAB_FIRST:
|
||||
return tabs.selectFirstTab();
|
||||
await tabs.selectFirstTab();
|
||||
break;
|
||||
case operations.TAB_LAST:
|
||||
return tabs.selectLastTab();
|
||||
await tabs.selectLastTab();
|
||||
break;
|
||||
case operations.TAB_PREV_SEL:
|
||||
if (tabState.previousSelected > 0) {
|
||||
return tabs.selectTab(tabState.previousSelected);
|
||||
await tabs.selectTab(tabState.previousSelected);
|
||||
}
|
||||
break;
|
||||
case operations.TAB_RELOAD:
|
||||
return tabs.reload(tab, operation.cache);
|
||||
await tabs.reload(tab, operation.cache);
|
||||
break;
|
||||
case operations.TAB_PIN:
|
||||
return tabs.updateTabPinned(tab, true);
|
||||
await tabs.updateTabPinned(tab, true);
|
||||
break;
|
||||
case operations.TAB_UNPIN:
|
||||
return tabs.updateTabPinned(tab, false);
|
||||
await tabs.updateTabPinned(tab, false);
|
||||
break;
|
||||
case operations.TAB_TOGGLE_PINNED:
|
||||
return tabs.toggleTabPinned(tab);
|
||||
await tabs.toggleTabPinned(tab);
|
||||
break;
|
||||
case operations.TAB_DUPLICATE:
|
||||
return tabs.duplicate(tab.id);
|
||||
await tabs.duplicate(tab.id);
|
||||
break;
|
||||
case operations.ZOOM_IN:
|
||||
return zooms.zoomIn();
|
||||
await zooms.zoomIn();
|
||||
break;
|
||||
case operations.ZOOM_OUT:
|
||||
return zooms.zoomOut();
|
||||
await zooms.zoomOut();
|
||||
break;
|
||||
case operations.ZOOM_NEUTRAL:
|
||||
return zooms.neutral();
|
||||
await zooms.neutral();
|
||||
break;
|
||||
case operations.COMMAND_SHOW:
|
||||
return this.sendConsoleShowCommand(tab, '');
|
||||
return consoleActions.showCommand(tab, '');
|
||||
case operations.COMMAND_SHOW_OPEN:
|
||||
if (operation.alter) {
|
||||
// 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:
|
||||
if (operation.alter) {
|
||||
// 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:
|
||||
if (operation.alter) {
|
||||
// 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:
|
||||
return this.sendConsoleShowCommand(tab, 'buffer ');
|
||||
return consoleActions.showCommand(tab, 'buffer ');
|
||||
case operations.COMMAND_SHOW_ADDBOOKMARK:
|
||||
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:
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_SHOW_FIND
|
||||
});
|
||||
return consoleActions.showFind(tab);
|
||||
case operations.CANCEL:
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_HIDE,
|
||||
});
|
||||
return consoleActions.hide(tab);
|
||||
case operations.PAGE_SOURCE:
|
||||
return browser.tabs.create({
|
||||
await browser.tabs.create({
|
||||
url: 'view-source:' + tab.url,
|
||||
index: tab.index + 1,
|
||||
openerTabId: tab.id,
|
||||
});
|
||||
default:
|
||||
return Promise.resolve();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sendConsoleShowCommand(tab, command) {
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_SHOW_COMMAND,
|
||||
command,
|
||||
});
|
||||
return { type: '' };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,17 @@
|
|||
import * as settingActions from 'background/actions/setting';
|
||||
import messages from 'shared/messages';
|
||||
import BackgroundComponent from 'background/components/background';
|
||||
import OperationComponent from 'background/components/operation';
|
||||
import TabComponent from 'background/components/tab';
|
||||
import IndicatorComponent from 'background/components/indicator';
|
||||
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';
|
||||
|
||||
const store = createStore(reducers, (e, sender) => {
|
||||
console.error('Vim-Vixen:', e);
|
||||
if (sender) {
|
||||
return browser.tabs.sendMessage(sender.tab.id, {
|
||||
type: messages.CONSOLE_SHOW_ERROR,
|
||||
text: e.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
const store = createStore(
|
||||
reducers,
|
||||
applyMiddleware(promise),
|
||||
);
|
||||
|
||||
const checkAndNotifyUpdated = async() => {
|
||||
let updated = await versions.checkUpdated();
|
||||
|
|
Reference in a new issue