From 0ca138cba46408388ee46eab16f46cec25cac41a Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 29 Jul 2018 11:17:01 +0900 Subject: Select last tab on presenter --- src/background/presenters/tab.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/background/presenters') diff --git a/src/background/presenters/tab.js b/src/background/presenters/tab.js index 2a06a5a..6ad9736 100644 --- a/src/background/presenters/tab.js +++ b/src/background/presenters/tab.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); +}); -- cgit v1.2.3