Select last tab on presenter

jh-changes
Shin'ya Ueoka 6 years ago
parent 09c5247dba
commit 0ca138cba4
  1. 25
      src/background/presenters/tab.js
  2. 18
      src/background/usecases/operation.js

@ -1,3 +1,8 @@
import MemoryStorage from '../infrastructures/memory-storage';
const CURRENT_SELECTED_KEY = 'tabs.current.selected';
const LAST_SELECTED_KEY = 'tabs.last.selected';
export default class TabPresenter {
open(url, tabId) {
return browser.tabs.update(tabId, { url });
@ -18,6 +23,15 @@ export default class TabPresenter {
return browser.tabs.query({ currentWindow: true });
}
async getLastSelectedId() {
let cache = new MemoryStorage();
let tabId = await cache.get(LAST_SELECTED_KEY);
if (tabId === null || typeof tabId === 'undefined') {
return;
}
return tabId;
}
async getByKeyword(keyword, excludePinned = false) {
let tabs = await browser.tabs.query({ currentWindow: true });
return tabs.filter((t) => {
@ -99,3 +113,14 @@ export default class TabPresenter {
browser.tabs.onActivated.addListener(listener);
}
}
let tabPresenter = new TabPresenter();
tabPresenter.onSelected((tab) => {
let cache = new MemoryStorage();
let lastId = cache.get(CURRENT_SELECTED_KEY);
if (lastId) {
cache.set(LAST_SELECTED_KEY, lastId);
}
cache.set(CURRENT_SELECTED_KEY, tab.tabId);
});

@ -1,10 +1,6 @@
import MemoryStorage from '../infrastructures/memory-storage';
import TabPresenter from '../presenters/tab';
import ConsolePresenter from '../presenters/console';
const CURRENT_SELECTED_KEY = 'tabs.current.selected';
const LAST_SELECTED_KEY = 'tabs.last.selected';
const ZOOM_SETTINGS = [
0.33, 0.50, 0.66, 0.75, 0.80, 0.90, 1.00,
1.10, 1.25, 1.50, 1.75, 2.00, 2.50, 3.00
@ -13,11 +9,7 @@ const ZOOM_SETTINGS = [
export default class OperationInteractor {
constructor() {
this.tabPresenter = new TabPresenter();
this.tabPresenter.onSelected(info => this.onTabSelected(info.tabId));
this.consolePresenter = new ConsolePresenter();
this.cache = new MemoryStorage();
}
async close(force) {
@ -69,7 +61,7 @@ export default class OperationInteractor {
}
async selectPrevSelected() {
let tabId = await this.cache.get(LAST_SELECTED_KEY);
let tabId = await this.tabPresenter.getLastSelectedId();
if (tabId === null || typeof tabId === 'undefined') {
return;
}
@ -180,13 +172,5 @@ export default class OperationInteractor {
let tab = await this.tabPresenter.getCurrent();
return this.consolePresenter.hide(tab.id);
}
onTabSelected(tabId) {
let lastId = this.cache.get(CURRENT_SELECTED_KEY);
if (lastId) {
this.cache.set(LAST_SELECTED_KEY, lastId);
}
this.cache.set(CURRENT_SELECTED_KEY, tabId);
}
}