Previous selected tab as redux
This commit is contained in:
parent
98bc2326ee
commit
2c366ac3b1
8 changed files with 64 additions and 15 deletions
|
@ -5,4 +5,7 @@ export default {
|
||||||
|
|
||||||
// Find
|
// Find
|
||||||
FIND_SET_KEYWORD: 'find.set.keyword',
|
FIND_SET_KEYWORD: 'find.set.keyword',
|
||||||
|
|
||||||
|
// Tab
|
||||||
|
TAB_SELECTED: 'tab.selected',
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import actions from './index';
|
||||||
|
|
||||||
const openNewTab = (url, openerTabId, background = false, adjacent = false) => {
|
const openNewTab = (url, openerTabId, background = false, adjacent = false) => {
|
||||||
if (adjacent) {
|
if (adjacent) {
|
||||||
return browser.tabs.query({
|
return browser.tabs.query({
|
||||||
|
@ -18,4 +20,11 @@ const openToTab = (url, tab) => {
|
||||||
return browser.tabs.update(tab.id, { url: url });
|
return browser.tabs.update(tab.id, { url: url });
|
||||||
};
|
};
|
||||||
|
|
||||||
export { openNewTab, openToTab };
|
const selected = (tabId) => {
|
||||||
|
return {
|
||||||
|
type: actions.TAB_SELECTED,
|
||||||
|
tabId,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export { openNewTab, openToTab, selected };
|
||||||
|
|
|
@ -30,6 +30,8 @@ export default class BackgroundComponent {
|
||||||
|
|
||||||
// eslint-disable-next-line complexity
|
// eslint-disable-next-line complexity
|
||||||
exec(operation, tab) {
|
exec(operation, 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);
|
return tabs.closeTab(tab.id);
|
||||||
|
@ -46,7 +48,10 @@ export default class BackgroundComponent {
|
||||||
case operations.TAB_LAST:
|
case operations.TAB_LAST:
|
||||||
return tabs.selectLastTab();
|
return tabs.selectLastTab();
|
||||||
case operations.TAB_PREV_SEL:
|
case operations.TAB_PREV_SEL:
|
||||||
return tabs.selectPrevSelTab();
|
if (tabState.previousSelected > 0) {
|
||||||
|
return tabs.selectTab(tabState.previousSelected);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case operations.TAB_RELOAD:
|
case operations.TAB_RELOAD:
|
||||||
return tabs.reload(tab, operation.cache);
|
return tabs.reload(tab, operation.cache);
|
||||||
case operations.TAB_PIN:
|
case operations.TAB_PIN:
|
||||||
|
|
17
src/background/components/tab.js
Normal file
17
src/background/components/tab.js
Normal file
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ import * as settingActions from 'background/actions/setting';
|
||||||
import messages from 'shared/messages';
|
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 reducers from 'background/reducers';
|
import reducers from 'background/reducers';
|
||||||
import { createStore } from 'shared/store';
|
import { createStore } from 'shared/store';
|
||||||
import * as versions from 'shared/versions';
|
import * as versions from 'shared/versions';
|
||||||
|
@ -19,6 +20,8 @@ const store = createStore(reducers, (e, sender) => {
|
||||||
const backgroundComponent = new BackgroundComponent(store);
|
const backgroundComponent = new BackgroundComponent(store);
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const operationComponent = new OperationComponent(store);
|
const operationComponent = new OperationComponent(store);
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
const tabComponent = new TabComponent(store);
|
||||||
|
|
||||||
store.dispatch(settingActions.load());
|
store.dispatch(settingActions.load());
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
import settingReducer from './setting';
|
import settingReducer from './setting';
|
||||||
import findReducer from './find';
|
import findReducer from './find';
|
||||||
|
import tabReducer from './tab';
|
||||||
|
|
||||||
// Make setting reducer instead of re-use
|
// Make setting reducer instead of re-use
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
setting: settingReducer(undefined, {}),
|
setting: settingReducer(undefined, {}),
|
||||||
find: findReducer(undefined, {}),
|
find: findReducer(undefined, {}),
|
||||||
|
tab: tabReducer(undefined, {}),
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function reducer(state = defaultState, action = {}) {
|
export default function reducer(state = defaultState, action = {}) {
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
setting: settingReducer(state.setting, action),
|
setting: settingReducer(state.setting, action),
|
||||||
find: findReducer(state.find, action),
|
find: findReducer(state.find, action),
|
||||||
|
tab: tabReducer(state.tab, action),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
19
src/background/reducers/tab.js
Normal file
19
src/background/reducers/tab.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import actions from 'background/actions';
|
||||||
|
|
||||||
|
const defaultState = {
|
||||||
|
previousSelected: -1,
|
||||||
|
currentSelected: -1,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function reducer(state = defaultState, action = {}) {
|
||||||
|
switch (action.type) {
|
||||||
|
case actions.TAB_SELECTED:
|
||||||
|
return {
|
||||||
|
previousSelected: state.currentSelected,
|
||||||
|
currentSelected: action.tabId,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,3 @@
|
||||||
let prevSelTab = 1;
|
|
||||||
let currSelTab = 1;
|
|
||||||
|
|
||||||
browser.tabs.onActivated.addListener((activeInfo) => {
|
|
||||||
return browser.tabs.query({ currentWindow: true }).then(() => {
|
|
||||||
prevSelTab = currSelTab;
|
|
||||||
currSelTab = activeInfo.tabId;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const closeTab = (id) => {
|
const closeTab = (id) => {
|
||||||
return browser.tabs.get(id).then((tab) => {
|
return browser.tabs.get(id).then((tab) => {
|
||||||
if (!tab.pinned) {
|
if (!tab.pinned) {
|
||||||
|
@ -102,8 +92,8 @@ const selectLastTab = () => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const selectPrevSelTab = () => {
|
const selectTab = (id) => {
|
||||||
return browser.tabs.update(prevSelTab, { active: true });
|
return browser.tabs.update(id, { active: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
const reload = (current, cache) => {
|
const reload = (current, cache) => {
|
||||||
|
@ -131,6 +121,6 @@ const duplicate = (id) => {
|
||||||
export {
|
export {
|
||||||
closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword,
|
closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword,
|
||||||
selectPrevTab, selectNextTab, selectFirstTab,
|
selectPrevTab, selectNextTab, selectFirstTab,
|
||||||
selectLastTab, selectPrevSelTab, reload, updateTabPinned,
|
selectLastTab, selectTab, reload, updateTabPinned,
|
||||||
toggleTabPinned, duplicate
|
toggleTabPinned, duplicate
|
||||||
};
|
};
|
||||||
|
|
Reference in a new issue