From c5c08783d2b8454336b6cff2134c8636e889e1c3 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 6 May 2018 11:12:52 +0900 Subject: OperationComponent --- src/background/actions/operation.js | 92 ------------------------------------- 1 file changed, 92 deletions(-) delete mode 100644 src/background/actions/operation.js (limited to 'src/background/actions') diff --git a/src/background/actions/operation.js b/src/background/actions/operation.js deleted file mode 100644 index 10c366f..0000000 --- a/src/background/actions/operation.js +++ /dev/null @@ -1,92 +0,0 @@ -import operations from 'shared/operations'; -import messages from 'shared/messages'; -import * as tabs from 'background/tabs'; -import * as zooms from 'background/zooms'; - -const sendConsoleShowCommand = (tab, command) => { - return browser.tabs.sendMessage(tab.id, { - type: messages.CONSOLE_SHOW_COMMAND, - command, - }); -}; - -// This switch statement is only gonna get longer as more -// features are added, so disable complexity check -/* eslint-disable complexity */ -const exec = (operation, 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: - return tabs.selectPrevSelTab(); - 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 sendConsoleShowCommand(tab, ''); - case operations.COMMAND_SHOW_OPEN: - if (operation.alter) { - // alter url - return sendConsoleShowCommand(tab, 'open ' + tab.url); - } - return sendConsoleShowCommand(tab, 'open '); - case operations.COMMAND_SHOW_TABOPEN: - if (operation.alter) { - // alter url - return sendConsoleShowCommand(tab, 'tabopen ' + tab.url); - } - return sendConsoleShowCommand(tab, 'tabopen '); - case operations.COMMAND_SHOW_WINOPEN: - if (operation.alter) { - // alter url - return sendConsoleShowCommand(tab, 'winopen ' + tab.url); - } - return sendConsoleShowCommand(tab, 'winopen '); - case operations.COMMAND_SHOW_BUFFER: - return 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(); - } -}; -/* eslint-enable complexity */ - -export { exec }; -- cgit v1.2.3 From 98bc2326eeeb5d915706dee9aadc2ac3e9af1789 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 6 May 2018 11:45:07 +0900 Subject: Refactor background directories --- src/background/actions/command.js | 2 +- src/background/components/background.js | 4 +- src/background/components/operation.js | 4 +- src/background/histories.js | 83 -------------- src/background/shared/completions/histories.js | 83 ++++++++++++++ src/background/shared/completions/index.js | 84 ++++++++++++++ src/background/shared/completions/tabs.js | 10 ++ src/background/shared/tabs.js | 136 +++++++++++++++++++++++ src/background/shared/zooms.js | 38 +++++++ src/background/tabs.js | 145 ------------------------- src/background/zooms.js | 38 ------- src/shared/commands/complete.js | 84 -------------- src/shared/commands/index.js | 3 - 13 files changed, 356 insertions(+), 358 deletions(-) delete mode 100644 src/background/histories.js create mode 100644 src/background/shared/completions/histories.js create mode 100644 src/background/shared/completions/index.js create mode 100644 src/background/shared/completions/tabs.js create mode 100644 src/background/shared/tabs.js create mode 100644 src/background/shared/zooms.js delete mode 100644 src/background/tabs.js delete mode 100644 src/background/zooms.js delete mode 100644 src/shared/commands/complete.js delete mode 100644 src/shared/commands/index.js (limited to 'src/background/actions') diff --git a/src/background/actions/command.js b/src/background/actions/command.js index 4c52bca..2f7305a 100644 --- a/src/background/actions/command.js +++ b/src/background/actions/command.js @@ -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'; diff --git a/src/background/components/background.js b/src/background/components/background.js index 81d815b..e13424b 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -3,7 +3,7 @@ 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) { @@ -44,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(); diff --git a/src/background/components/operation.js b/src/background/components/operation.js index e1094c5..b9581c9 100644 --- a/src/background/components/operation.js +++ b/src/background/components/operation.js @@ -1,7 +1,7 @@ import messages from 'shared/messages'; import operations from 'shared/operations'; -import * as tabs from 'background/tabs'; -import * as zooms from 'background/zooms'; +import * as tabs from '../shared//tabs'; +import * as zooms from '../shared/zooms'; export default class BackgroundComponent { constructor(store) { diff --git a/src/background/histories.js b/src/background/histories.js deleted file mode 100644 index a7d3d47..0000000 --- a/src/background/histories.js +++ /dev/null @@ -1,83 +0,0 @@ -const filterHttp = (items) => { - const httpsHosts = items - .filter(item => item[1].protocol === 'https:') - .map(item => item[1].host); - const httpsHostSet = new Set(httpsHosts); - return items.filter( - item => !(item[1].protocol === 'http:' && httpsHostSet.has(item[1].host)) - ); -}; - -const filterEmptyTitle = (items) => { - return items.filter(item => item[0].title && item[0].title !== ''); -}; - -const filterClosedPath = (items) => { - const allSimplePaths = items - .filter(item => item[1].hash === '' && item[1].search === '') - .map(item => item[1].origin + item[1].pathname); - const allSimplePathSet = new Set(allSimplePaths); - return items.filter( - item => !(item[1].hash === '' && item[1].search === '' && - (/\/$/).test(item[1].pathname) && - allSimplePathSet.has( - (item[1].origin + item[1].pathname).replace(/\/$/, '') - ) - ) - ); -}; - -const reduceByPathname = (items, min) => { - let hash = {}; - for (let item of items) { - let pathname = item[1].origin + item[1].pathname; - if (!hash[pathname]) { - hash[pathname] = item; - } else if (hash[pathname][1].href.length > item[1].href.length) { - hash[pathname] = item; - } - } - let filtered = Object.values(hash); - if (filtered.length < min) { - return items; - } - return filtered; -}; - -const reduceByOrigin = (items, min) => { - let hash = {}; - for (let item of items) { - let origin = item[1].origin; - if (!hash[origin]) { - hash[origin] = item; - } else if (hash[origin][1].href.length > item[1].href.length) { - hash[origin] = item; - } - } - let filtered = Object.values(hash); - if (filtered.length < min) { - return items; - } - return filtered; -}; - -const getCompletions = (keyword) => { - return browser.history.search({ - text: keyword, - startTime: 0, - }).then((historyItems) => { - return [historyItems.map(item => [item, new URL(item.url)])] - .map(filterEmptyTitle) - .map(filterHttp) - .map(filterClosedPath) - .map(items => reduceByPathname(items, 10)) - .map(items => reduceByOrigin(items, 10)) - .map(items => items - .sort((x, y) => x[0].visitCount < y[0].visitCount) - .slice(0, 10) - .map(item => item[0]) - )[0]; - }); -}; - -export { getCompletions }; diff --git a/src/background/shared/completions/histories.js b/src/background/shared/completions/histories.js new file mode 100644 index 0000000..a7d3d47 --- /dev/null +++ b/src/background/shared/completions/histories.js @@ -0,0 +1,83 @@ +const filterHttp = (items) => { + const httpsHosts = items + .filter(item => item[1].protocol === 'https:') + .map(item => item[1].host); + const httpsHostSet = new Set(httpsHosts); + return items.filter( + item => !(item[1].protocol === 'http:' && httpsHostSet.has(item[1].host)) + ); +}; + +const filterEmptyTitle = (items) => { + return items.filter(item => item[0].title && item[0].title !== ''); +}; + +const filterClosedPath = (items) => { + const allSimplePaths = items + .filter(item => item[1].hash === '' && item[1].search === '') + .map(item => item[1].origin + item[1].pathname); + const allSimplePathSet = new Set(allSimplePaths); + return items.filter( + item => !(item[1].hash === '' && item[1].search === '' && + (/\/$/).test(item[1].pathname) && + allSimplePathSet.has( + (item[1].origin + item[1].pathname).replace(/\/$/, '') + ) + ) + ); +}; + +const reduceByPathname = (items, min) => { + let hash = {}; + for (let item of items) { + let pathname = item[1].origin + item[1].pathname; + if (!hash[pathname]) { + hash[pathname] = item; + } else if (hash[pathname][1].href.length > item[1].href.length) { + hash[pathname] = item; + } + } + let filtered = Object.values(hash); + if (filtered.length < min) { + return items; + } + return filtered; +}; + +const reduceByOrigin = (items, min) => { + let hash = {}; + for (let item of items) { + let origin = item[1].origin; + if (!hash[origin]) { + hash[origin] = item; + } else if (hash[origin][1].href.length > item[1].href.length) { + hash[origin] = item; + } + } + let filtered = Object.values(hash); + if (filtered.length < min) { + return items; + } + return filtered; +}; + +const getCompletions = (keyword) => { + return browser.history.search({ + text: keyword, + startTime: 0, + }).then((historyItems) => { + return [historyItems.map(item => [item, new URL(item.url)])] + .map(filterEmptyTitle) + .map(filterHttp) + .map(filterClosedPath) + .map(items => reduceByPathname(items, 10)) + .map(items => reduceByOrigin(items, 10)) + .map(items => items + .sort((x, y) => x[0].visitCount < y[0].visitCount) + .slice(0, 10) + .map(item => item[0]) + )[0]; + }); +}; + +export { getCompletions }; diff --git a/src/background/shared/completions/index.js b/src/background/shared/completions/index.js new file mode 100644 index 0000000..73b7b27 --- /dev/null +++ b/src/background/shared/completions/index.js @@ -0,0 +1,84 @@ +import * as tabs from './tabs'; +import * as histories from './histories'; + +const getOpenCompletions = (command, keywords, searchConfig) => { + return histories.getCompletions(keywords).then((pages) => { + let historyItems = pages.map((page) => { + return { + caption: page.title, + content: command + ' ' + page.url, + url: page.url + }; + }); + let engineNames = Object.keys(searchConfig.engines); + let engineItems = engineNames.filter(name => name.startsWith(keywords)) + .map(name => ({ + caption: name, + content: command + ' ' + name + })); + + let completions = []; + if (engineItems.length > 0) { + completions.push({ + name: 'Search Engines', + items: engineItems + }); + } + if (historyItems.length > 0) { + completions.push({ + name: 'History', + items: historyItems + }); + } + return completions; + }); +}; + +const getCompletions = (line, settings) => { + let typedWords = line.trim().split(/ +/); + let typing = ''; + if (!line.endsWith(' ')) { + typing = typedWords.pop(); + } + + if (typedWords.length === 0) { + return Promise.resolve([]); + } + let name = typedWords.shift(); + let keywords = typedWords.concat(typing).join(' '); + + switch (name) { + case 'o': + case 'open': + case 't': + case 'tabopen': + case 'w': + case 'winopen': + return getOpenCompletions(name, keywords, settings.search); + case 'b': + case 'buffer': + return tabs.getCompletions(keywords).then((gotTabs) => { + let items = gotTabs.map((tab) => { + return { + caption: tab.title, + content: name + ' ' + tab.title, + url: tab.url, + icon: tab.favIconUrl + }; + }); + return [ + { + name: 'Buffers', + items: items + } + ]; + }); + } + return Promise.resolve([]); +}; + +const complete = (line, settings) => { + return getCompletions(line, settings); +}; + +export { complete }; diff --git a/src/background/shared/completions/tabs.js b/src/background/shared/completions/tabs.js new file mode 100644 index 0000000..5edddca --- /dev/null +++ b/src/background/shared/completions/tabs.js @@ -0,0 +1,10 @@ +const getCompletions = (keyword) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let matched = tabs.filter((t) => { + return t.url.includes(keyword) || t.title && t.title.includes(keyword); + }); + return matched; + }); +}; + +export { getCompletions }; diff --git a/src/background/shared/tabs.js b/src/background/shared/tabs.js new file mode 100644 index 0000000..277afb2 --- /dev/null +++ b/src/background/shared/tabs.js @@ -0,0 +1,136 @@ +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) => { + return browser.tabs.get(id).then((tab) => { + if (!tab.pinned) { + return browser.tabs.remove(id); + } + }); +}; + +const closeTabForce = (id) => { + return browser.tabs.remove(id); +}; + +const reopenTab = () => { + return browser.sessions.getRecentlyClosed({ + maxResults: 1 + }).then((sessions) => { + if (sessions.length === 0) { + return; + } + let session = sessions[0]; + if (session.tab) { + return browser.sessions.restore(session.tab.sessionId); + } + return browser.sessions.restore(session.window.sessionId); + }); +}; + +const selectAt = (index) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + if (tabs.length < 2) { + return; + } + if (index < 0 || tabs.length <= index) { + throw new RangeError(`tab ${index + 1} does not exist`); + } + let id = tabs[index].id; + return browser.tabs.update(id, { active: true }); + }); +}; + +const selectByKeyword = (current, keyword) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let matched = tabs.filter((t) => { + return t.url.includes(keyword) || t.title.includes(keyword); + }); + + if (matched.length === 0) { + throw new RangeError('No matching buffer for ' + keyword); + } + for (let tab of matched) { + if (tab.index > current.index) { + return browser.tabs.update(tab.id, { active: true }); + } + } + return browser.tabs.update(matched[0].id, { active: true }); + }); +}; + +const selectPrevTab = (current, count) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + if (tabs.length < 2) { + return; + } + let select = (current - count + tabs.length) % tabs.length; + let id = tabs[select].id; + return browser.tabs.update(id, { active: true }); + }); +}; + +const selectNextTab = (current, count) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + if (tabs.length < 2) { + return; + } + let select = (current + count) % tabs.length; + let id = tabs[select].id; + return browser.tabs.update(id, { active: true }); + }); +}; + +const selectFirstTab = () => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let id = tabs[0].id; + return browser.tabs.update(id, { active: true }); + }); +}; + +const selectLastTab = () => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let id = tabs[tabs.length - 1].id; + return browser.tabs.update(id, { active: true }); + }); +}; + +const selectPrevSelTab = () => { + return browser.tabs.update(prevSelTab, { active: true }); +}; + +const reload = (current, cache) => { + return browser.tabs.reload( + current.id, + { bypassCache: cache } + ); +}; + +const updateTabPinned = (current, pinned) => { + return browser.tabs.query({ currentWindow: true, active: true }) + .then(() => { + return browser.tabs.update(current.id, { pinned: pinned }); + }); +}; + +const toggleTabPinned = (current) => { + updateTabPinned(current, !current.pinned); +}; + +const duplicate = (id) => { + return browser.tabs.duplicate(id); +}; + +export { + closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword, + selectPrevTab, selectNextTab, selectFirstTab, + selectLastTab, selectPrevSelTab, reload, updateTabPinned, + toggleTabPinned, duplicate +}; diff --git a/src/background/shared/zooms.js b/src/background/shared/zooms.js new file mode 100644 index 0000000..e3e2aa6 --- /dev/null +++ b/src/background/shared/zooms.js @@ -0,0 +1,38 @@ +// For chromium +// const ZOOM_SETTINGS = [ +// 0.25, 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, 4.00, 5.00 +// ]; + +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 +]; + +const zoomIn = (tabId = undefined) => { + return browser.tabs.getZoom(tabId).then((factor) => { + for (let f of ZOOM_SETTINGS) { + if (f > factor) { + browser.tabs.setZoom(tabId, f); + break; + } + } + }); +}; + +const zoomOut = (tabId = undefined) => { + return browser.tabs.getZoom(tabId).then((factor) => { + for (let f of [].concat(ZOOM_SETTINGS).reverse()) { + if (f < factor) { + browser.tabs.setZoom(tabId, f); + break; + } + } + }); +}; + +const neutral = (tabId = undefined) => { + return browser.tabs.setZoom(tabId, 1); +}; + +export { zoomIn, zoomOut, neutral }; diff --git a/src/background/tabs.js b/src/background/tabs.js deleted file mode 100644 index e939870..0000000 --- a/src/background/tabs.js +++ /dev/null @@ -1,145 +0,0 @@ -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) => { - return browser.tabs.get(id).then((tab) => { - if (!tab.pinned) { - return browser.tabs.remove(id); - } - }); -}; - -const closeTabForce = (id) => { - return browser.tabs.remove(id); -}; - -const reopenTab = () => { - return browser.sessions.getRecentlyClosed({ - maxResults: 1 - }).then((sessions) => { - if (sessions.length === 0) { - return; - } - let session = sessions[0]; - if (session.tab) { - return browser.sessions.restore(session.tab.sessionId); - } - return browser.sessions.restore(session.window.sessionId); - }); -}; - -const selectAt = (index) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - if (tabs.length < 2) { - return; - } - if (index < 0 || tabs.length <= index) { - throw new RangeError(`tab ${index + 1} does not exist`); - } - let id = tabs[index].id; - return browser.tabs.update(id, { active: true }); - }); -}; - -const selectByKeyword = (current, keyword) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let matched = tabs.filter((t) => { - return t.url.includes(keyword) || t.title.includes(keyword); - }); - - if (matched.length === 0) { - throw new RangeError('No matching buffer for ' + keyword); - } - for (let tab of matched) { - if (tab.index > current.index) { - return browser.tabs.update(tab.id, { active: true }); - } - } - return browser.tabs.update(matched[0].id, { active: true }); - }); -}; - -const getCompletions = (keyword) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let matched = tabs.filter((t) => { - return t.url.includes(keyword) || t.title && t.title.includes(keyword); - }); - return matched; - }); -}; - -const selectPrevTab = (current, count) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - if (tabs.length < 2) { - return; - } - let select = (current - count + tabs.length) % tabs.length; - let id = tabs[select].id; - return browser.tabs.update(id, { active: true }); - }); -}; - -const selectNextTab = (current, count) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - if (tabs.length < 2) { - return; - } - let select = (current + count) % tabs.length; - let id = tabs[select].id; - return browser.tabs.update(id, { active: true }); - }); -}; - -const selectFirstTab = () => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let id = tabs[0].id; - return browser.tabs.update(id, { active: true }); - }); -}; - -const selectLastTab = () => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let id = tabs[tabs.length - 1].id; - return browser.tabs.update(id, { active: true }); - }); -}; - -const selectPrevSelTab = () => { - return browser.tabs.update(prevSelTab, { active: true }); -}; - -const reload = (current, cache) => { - return browser.tabs.reload( - current.id, - { bypassCache: cache } - ); -}; - -const updateTabPinned = (current, pinned) => { - return browser.tabs.query({ currentWindow: true, active: true }) - .then(() => { - return browser.tabs.update(current.id, { pinned: pinned }); - }); -}; - -const toggleTabPinned = (current) => { - updateTabPinned(current, !current.pinned); -}; - -const duplicate = (id) => { - return browser.tabs.duplicate(id); -}; - -export { - closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword, - getCompletions, selectPrevTab, selectNextTab, selectFirstTab, - selectLastTab, selectPrevSelTab, reload, updateTabPinned, - toggleTabPinned, duplicate -}; diff --git a/src/background/zooms.js b/src/background/zooms.js deleted file mode 100644 index e3e2aa6..0000000 --- a/src/background/zooms.js +++ /dev/null @@ -1,38 +0,0 @@ -// For chromium -// const ZOOM_SETTINGS = [ -// 0.25, 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, 4.00, 5.00 -// ]; - -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 -]; - -const zoomIn = (tabId = undefined) => { - return browser.tabs.getZoom(tabId).then((factor) => { - for (let f of ZOOM_SETTINGS) { - if (f > factor) { - browser.tabs.setZoom(tabId, f); - break; - } - } - }); -}; - -const zoomOut = (tabId = undefined) => { - return browser.tabs.getZoom(tabId).then((factor) => { - for (let f of [].concat(ZOOM_SETTINGS).reverse()) { - if (f < factor) { - browser.tabs.setZoom(tabId, f); - break; - } - } - }); -}; - -const neutral = (tabId = undefined) => { - return browser.tabs.setZoom(tabId, 1); -}; - -export { zoomIn, zoomOut, neutral }; diff --git a/src/shared/commands/complete.js b/src/shared/commands/complete.js deleted file mode 100644 index 0bdbab8..0000000 --- a/src/shared/commands/complete.js +++ /dev/null @@ -1,84 +0,0 @@ -import * as tabs from 'background/tabs'; -import * as histories from 'background/histories'; - -const getOpenCompletions = (command, keywords, searchConfig) => { - return histories.getCompletions(keywords).then((pages) => { - let historyItems = pages.map((page) => { - return { - caption: page.title, - content: command + ' ' + page.url, - url: page.url - }; - }); - let engineNames = Object.keys(searchConfig.engines); - let engineItems = engineNames.filter(name => name.startsWith(keywords)) - .map(name => ({ - caption: name, - content: command + ' ' + name - })); - - let completions = []; - if (engineItems.length > 0) { - completions.push({ - name: 'Search Engines', - items: engineItems - }); - } - if (historyItems.length > 0) { - completions.push({ - name: 'History', - items: historyItems - }); - } - return completions; - }); -}; - -const getCompletions = (line, settings) => { - let typedWords = line.trim().split(/ +/); - let typing = ''; - if (!line.endsWith(' ')) { - typing = typedWords.pop(); - } - - if (typedWords.length === 0) { - return Promise.resolve([]); - } - let name = typedWords.shift(); - let keywords = typedWords.concat(typing).join(' '); - - switch (name) { - case 'o': - case 'open': - case 't': - case 'tabopen': - case 'w': - case 'winopen': - return getOpenCompletions(name, keywords, settings.search); - case 'b': - case 'buffer': - return tabs.getCompletions(keywords).then((gotTabs) => { - let items = gotTabs.map((tab) => { - return { - caption: tab.title, - content: name + ' ' + tab.title, - url: tab.url, - icon: tab.favIconUrl - }; - }); - return [ - { - name: 'Buffers', - items: items - } - ]; - }); - } - return Promise.resolve([]); -}; - -const complete = (line, settings) => { - return getCompletions(line, settings); -}; - -export default complete; diff --git a/src/shared/commands/index.js b/src/shared/commands/index.js deleted file mode 100644 index 78cb4df..0000000 --- a/src/shared/commands/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import complete from './complete'; - -export { complete }; -- cgit v1.2.3 From 2c366ac3b1d7114869567845c955238e96056565 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 6 May 2018 16:11:40 +0900 Subject: Previous selected tab as redux --- src/background/actions/index.js | 3 +++ src/background/actions/tab.js | 11 ++++++++++- src/background/components/operation.js | 7 ++++++- src/background/components/tab.js | 17 +++++++++++++++++ src/background/index.js | 3 +++ src/background/reducers/index.js | 3 +++ src/background/reducers/tab.js | 19 +++++++++++++++++++ src/background/shared/tabs.js | 16 +++------------- 8 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 src/background/components/tab.js create mode 100644 src/background/reducers/tab.js (limited to 'src/background/actions') diff --git a/src/background/actions/index.js b/src/background/actions/index.js index 2bdaaf2..3833389 100644 --- a/src/background/actions/index.js +++ b/src/background/actions/index.js @@ -5,4 +5,7 @@ export default { // Find FIND_SET_KEYWORD: 'find.set.keyword', + + // Tab + TAB_SELECTED: 'tab.selected', }; diff --git a/src/background/actions/tab.js b/src/background/actions/tab.js index 3c642fd..0d439fd 100644 --- a/src/background/actions/tab.js +++ b/src/background/actions/tab.js @@ -1,3 +1,5 @@ +import actions from './index'; + const openNewTab = (url, openerTabId, background = false, adjacent = false) => { if (adjacent) { return browser.tabs.query({ @@ -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 }; diff --git a/src/background/components/operation.js b/src/background/components/operation.js index b9581c9..9a0b4e1 100644 --- a/src/background/components/operation.js +++ b/src/background/components/operation.js @@ -30,6 +30,8 @@ export default class BackgroundComponent { // 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); @@ -46,7 +48,10 @@ export default class BackgroundComponent { case operations.TAB_LAST: return tabs.selectLastTab(); case operations.TAB_PREV_SEL: - return tabs.selectPrevSelTab(); + if (tabState.previousSelected > 0) { + return tabs.selectTab(tabState.previousSelected); + } + break; case operations.TAB_RELOAD: return tabs.reload(tab, operation.cache); case operations.TAB_PIN: diff --git a/src/background/components/tab.js b/src/background/components/tab.js new file mode 100644 index 0000000..b273546 --- /dev/null +++ b/src/background/components/tab.js @@ -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)); + } +} diff --git a/src/background/index.js b/src/background/index.js index 4f6b23f..be042ce 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -2,6 +2,7 @@ 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 reducers from 'background/reducers'; import { createStore } from 'shared/store'; import * as versions from 'shared/versions'; @@ -19,6 +20,8 @@ const store = createStore(reducers, (e, sender) => { const backgroundComponent = new BackgroundComponent(store); // eslint-disable-next-line no-unused-vars const operationComponent = new OperationComponent(store); +// eslint-disable-next-line no-unused-vars +const tabComponent = new TabComponent(store); store.dispatch(settingActions.load()); diff --git a/src/background/reducers/index.js b/src/background/reducers/index.js index 63ff0f8..5729f0a 100644 --- a/src/background/reducers/index.js +++ b/src/background/reducers/index.js @@ -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), }); } diff --git a/src/background/reducers/tab.js b/src/background/reducers/tab.js new file mode 100644 index 0000000..e0cdf32 --- /dev/null +++ b/src/background/reducers/tab.js @@ -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; + } +} + diff --git a/src/background/shared/tabs.js b/src/background/shared/tabs.js index 277afb2..f1dcc73 100644 --- a/src/background/shared/tabs.js +++ b/src/background/shared/tabs.js @@ -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) => { return browser.tabs.get(id).then((tab) => { if (!tab.pinned) { @@ -102,8 +92,8 @@ const selectLastTab = () => { }); }; -const selectPrevSelTab = () => { - return browser.tabs.update(prevSelTab, { active: true }); +const selectTab = (id) => { + return browser.tabs.update(id, { active: true }); }; const reload = (current, cache) => { @@ -131,6 +121,6 @@ const duplicate = (id) => { export { closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword, selectPrevTab, selectNextTab, selectFirstTab, - selectLastTab, selectPrevSelTab, reload, updateTabPinned, + selectLastTab, selectTab, reload, updateTabPinned, toggleTabPinned, duplicate }; -- cgit v1.2.3