Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Addon enabled indicator #386

Merged
merged 7 commits into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@
],
"options_ui": {
"page": "build/settings.html"
},
"browser_action": {
"default_icon": {
"32": "resources/enabled_32x32.png"
},
"default_title": "Vim Vixen"
}
}
Binary file added resources/disabled_32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/enabled_32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/background/actions/command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import actions from '../actions';
import * as tabs from 'background/tabs';
import * as tabs from '../shared/tabs';
import * as parsers from 'shared/commands/parsers';
import * as properties from 'shared/settings/properties';

Expand Down
3 changes: 3 additions & 0 deletions src/background/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export default {

// Find
FIND_SET_KEYWORD: 'find.set.keyword',

// Tab
TAB_SELECTED: 'tab.selected',
};
92 changes: 0 additions & 92 deletions src/background/actions/operation.js

This file was deleted.

11 changes: 10 additions & 1 deletion src/background/actions/tab.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import actions from './index';

const openNewTab = (url, openerTabId, background = false, adjacent = false) => {
if (adjacent) {
return browser.tabs.query({
Expand All @@ -18,4 +20,11 @@ const openToTab = (url, tab) => {
return browser.tabs.update(tab.id, { url: url });
};

export { openNewTab, openToTab };
const selected = (tabId) => {
return {
type: actions.TAB_SELECTED,
tabId,
};
};

export { openNewTab, openToTab, selected };
9 changes: 2 additions & 7 deletions src/background/components/background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import messages from 'shared/messages';
import * as operationActions from 'background/actions/operation';
import * as commandActions from 'background/actions/command';
import * as settingActions from 'background/actions/setting';
import * as findActions from 'background/actions/find';
import * as tabActions from 'background/actions/tab';
import * as commands from 'shared/commands';
import * as completions from '../shared/completions';

export default class BackgroundComponent {
constructor(store) {
Expand All @@ -27,10 +26,6 @@ export default class BackgroundComponent {
let find = this.store.getState().find;

switch (message.type) {
case messages.BACKGROUND_OPERATION:
return this.store.dispatch(
operationActions.exec(message.operation, sender.tab),
sender);
case messages.OPEN_URL:
if (message.newTab) {
let action = tabActions.openNewTab(
Expand All @@ -49,7 +44,7 @@ export default class BackgroundComponent {
case messages.SETTINGS_QUERY:
return Promise.resolve(this.store.getState().setting.value);
case messages.CONSOLE_QUERY_COMPLETIONS:
return commands.complete(message.text, settings.value);
return completions.complete(message.text, settings.value);
case messages.SETTINGS_RELOAD:
this.store.dispatch(settingActions.load());
return this.broadcastSettingsChanged();
Expand Down
45 changes: 45 additions & 0 deletions src/background/components/indicator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as indicators from '../shared/indicators';
import messages from 'shared/messages';

export default class IndicatorComponent {
constructor(store) {
this.store = store;

messages.onMessage(this.onMessage.bind(this));

browser.browserAction.onClicked.addListener(this.onClicked);
browser.tabs.onActivated.addListener((info) => {
return browser.tabs.query({ currentWindow: true }).then(() => {
return this.onTabActivated(info);
});
});
}

onTabActivated(info) {
return browser.tabs.sendMessage(info.tabId, {
type: messages.ADDON_ENABLED_QUERY,
}).then((resp) => {
return this.updateIndicator(resp.enabled);
});
}

onClicked(tab) {
browser.tabs.sendMessage(tab.id, {
type: messages.ADDON_TOGGLE_ENABLED,
});
}

onMessage(message) {
switch (message.type) {
case messages.ADDON_ENABLED_RESPONSE:
return this.updateIndicator(message.enabled);
}
}

updateIndicator(enabled) {
if (enabled) {
return indicators.enable();
}
return indicators.disable();
}
}
118 changes: 118 additions & 0 deletions src/background/components/operation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import messages from 'shared/messages';
import operations from 'shared/operations';
import * as tabs from '../shared//tabs';
import * as zooms from '../shared/zooms';

export default class BackgroundComponent {
constructor(store) {
this.store = store;

browser.runtime.onMessage.addListener((message, sender) => {
try {
return this.onMessage(message, sender);
} catch (e) {
return browser.tabs.sendMessage(sender.tab.id, {
type: messages.CONSOLE_SHOW_ERROR,
text: e.message,
});
}
});
}

onMessage(message, sender) {
switch (message.type) {
case messages.BACKGROUND_OPERATION:
return this.store.dispatch(
this.exec(message.operation, sender.tab),
sender);
}
}

// eslint-disable-next-line complexity
exec(operation, tab) {
let tabState = this.store.getState().tab;

switch (operation.type) {
case operations.TAB_CLOSE:
return tabs.closeTab(tab.id);
case operations.TAB_CLOSE_FORCE:
return tabs.closeTabForce(tab.id);
case operations.TAB_REOPEN:
return tabs.reopenTab();
case operations.TAB_PREV:
return tabs.selectPrevTab(tab.index, operation.count);
case operations.TAB_NEXT:
return tabs.selectNextTab(tab.index, operation.count);
case operations.TAB_FIRST:
return tabs.selectFirstTab();
case operations.TAB_LAST:
return tabs.selectLastTab();
case operations.TAB_PREV_SEL:
if (tabState.previousSelected > 0) {
return tabs.selectTab(tabState.previousSelected);
}
break;
case operations.TAB_RELOAD:
return tabs.reload(tab, operation.cache);
case operations.TAB_PIN:
return tabs.updateTabPinned(tab, true);
case operations.TAB_UNPIN:
return tabs.updateTabPinned(tab, false);
case operations.TAB_TOGGLE_PINNED:
return tabs.toggleTabPinned(tab);
case operations.TAB_DUPLICATE:
return tabs.duplicate(tab.id);
case operations.ZOOM_IN:
return zooms.zoomIn();
case operations.ZOOM_OUT:
return zooms.zoomOut();
case operations.ZOOM_NEUTRAL:
return zooms.neutral();
case operations.COMMAND_SHOW:
return this.sendConsoleShowCommand(tab, '');
case operations.COMMAND_SHOW_OPEN:
if (operation.alter) {
// alter url
return this.sendConsoleShowCommand(tab, 'open ' + tab.url);
}
return this.sendConsoleShowCommand(tab, 'open ');
case operations.COMMAND_SHOW_TABOPEN:
if (operation.alter) {
// alter url
return this.sendConsoleShowCommand(tab, 'tabopen ' + tab.url);
}
return this.sendConsoleShowCommand(tab, 'tabopen ');
case operations.COMMAND_SHOW_WINOPEN:
if (operation.alter) {
// alter url
return this.sendConsoleShowCommand(tab, 'winopen ' + tab.url);
}
return this.sendConsoleShowCommand(tab, 'winopen ');
case operations.COMMAND_SHOW_BUFFER:
return this.sendConsoleShowCommand(tab, 'buffer ');
case operations.FIND_START:
return browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_SHOW_FIND
});
case operations.CANCEL:
return browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_HIDE,
});
case operations.PAGE_SOURCE:
return browser.tabs.create({
url: 'view-source:' + tab.url,
index: tab.index + 1,
openerTabId: tab.id,
});
default:
return Promise.resolve();
}
}

sendConsoleShowCommand(tab, command) {
return browser.tabs.sendMessage(tab.id, {
type: messages.CONSOLE_SHOW_COMMAND,
command,
});
}
}
17 changes: 17 additions & 0 deletions src/background/components/tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as tabActions from '../actions/tab';

export default class TabComponent {
constructor(store) {
this.store = store;

browser.tabs.onActivated.addListener((info) => {
return browser.tabs.query({ currentWindow: true }).then(() => {
return this.onTabActivated(info);
});
});
}

onTabActivated(info) {
return this.store.dispatch(tabActions.selected(info.tabId));
}
}
10 changes: 9 additions & 1 deletion src/background/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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 * as versions from 'shared/versions';
Expand All @@ -14,8 +17,13 @@ const store = createStore(reducers, (e, sender) => {
});
}
});
// eslint-disable-next-line no-unused-vars

/* eslint-disable no-unused-vars */
const backgroundComponent = new BackgroundComponent(store);
const operationComponent = new OperationComponent(store);
const tabComponent = new TabComponent(store);
const indicatorComponent = new IndicatorComponent(store);
/* eslint-enable no-unused-vars */

store.dispatch(settingActions.load());

Expand Down
3 changes: 3 additions & 0 deletions src/background/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import settingReducer from './setting';
import findReducer from './find';
import tabReducer from './tab';

// Make setting reducer instead of re-use
const defaultState = {
setting: settingReducer(undefined, {}),
find: findReducer(undefined, {}),
tab: tabReducer(undefined, {}),
};

export default function reducer(state = defaultState, action = {}) {
return Object.assign({}, state, {
setting: settingReducer(state.setting, action),
find: findReducer(state.find, action),
tab: tabReducer(state.tab, action),
});
}
Loading