From ccdb0a2428bbdc56e8288442f844a4bb3b8f9a11 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 28 Jul 2018 10:51:52 +0900 Subject: [PATCH] Clean old codes --- src/background/actions/command.js | 147 --------------- src/background/actions/console.js | 41 ----- src/background/actions/find.js | 10 - src/background/actions/index.js | 11 -- src/background/actions/tab.js | 34 ---- src/background/components/background.js | 35 ---- src/background/components/indicator.js | 43 ----- src/background/components/operation.js | 124 ------------- src/background/components/tab.js | 16 -- .../docs.js => domains/command-docs.js} | 1 + src/background/index.js | 18 -- src/background/reducers/find.js | 15 -- src/background/reducers/index.js | 8 - src/background/reducers/setting.js | 22 --- src/background/reducers/tab.js | 19 -- src/background/shared/bookmarks.js | 9 - src/background/shared/commands/parsers.js | 59 ------ .../shared/completions/bookmarks.js | 14 -- .../shared/completions/histories.js | 82 --------- src/background/shared/completions/index.js | 173 ------------------ src/background/shared/completions/tabs.js | 8 - src/background/shared/indicators.js | 13 -- src/background/shared/tabs.js | 158 ---------------- src/background/shared/zooms.js | 32 ---- src/background/usecases/completions.js | 2 +- test/background/actions/find.test.js | 12 -- test/background/actions/tab.test.js | 13 -- test/background/reducers/find.test.js | 18 -- test/background/reducers/setting.test.js | 35 ---- test/background/reducers/tab.test.js | 22 --- .../shared/commands/parsers.test.js | 84 --------- test/background/usecases/parsers.test.js | 5 +- 32 files changed, 4 insertions(+), 1279 deletions(-) delete mode 100644 src/background/actions/command.js delete mode 100644 src/background/actions/console.js delete mode 100644 src/background/actions/find.js delete mode 100644 src/background/actions/index.js delete mode 100644 src/background/actions/tab.js delete mode 100644 src/background/components/background.js delete mode 100644 src/background/components/indicator.js delete mode 100644 src/background/components/operation.js delete mode 100644 src/background/components/tab.js rename src/background/{shared/commands/docs.js => domains/command-docs.js} (99%) delete mode 100644 src/background/reducers/find.js delete mode 100644 src/background/reducers/index.js delete mode 100644 src/background/reducers/setting.js delete mode 100644 src/background/reducers/tab.js delete mode 100644 src/background/shared/bookmarks.js delete mode 100644 src/background/shared/commands/parsers.js delete mode 100644 src/background/shared/completions/bookmarks.js delete mode 100644 src/background/shared/completions/histories.js delete mode 100644 src/background/shared/completions/index.js delete mode 100644 src/background/shared/completions/tabs.js delete mode 100644 src/background/shared/indicators.js delete mode 100644 src/background/shared/tabs.js delete mode 100644 src/background/shared/zooms.js delete mode 100644 test/background/actions/find.test.js delete mode 100644 test/background/actions/tab.test.js delete mode 100644 test/background/reducers/find.test.js delete mode 100644 test/background/reducers/setting.test.js delete mode 100644 test/background/reducers/tab.test.js delete mode 100644 test/background/shared/commands/parsers.test.js diff --git a/src/background/actions/command.js b/src/background/actions/command.js deleted file mode 100644 index feb16d8..0000000 --- a/src/background/actions/command.js +++ /dev/null @@ -1,147 +0,0 @@ -import actions from '../actions'; -import * as consoleActions from './console'; -import * as tabs from '../shared/tabs'; -import * as bookmarks from '../shared/bookmarks'; -import * as parsers from 'background/shared/commands/parsers'; -import * as properties from 'shared/settings/properties'; - -const openCommand = async(url) => { - let got = await browser.tabs.query({ - active: true, currentWindow: true - }); - if (got.length > 0) { - return browser.tabs.update(got[0].id, { url: url }); - } -}; - -const tabopenCommand = (url) => { - return browser.tabs.create({ url: url }); -}; - -const tabcloseCommand = async() => { - let got = await browser.tabs.query({ - active: true, currentWindow: true - }); - return browser.tabs.remove(got.map(tab => tab.id)); -}; - -const tabcloseAllCommand = () => { - return browser.tabs.query({ - currentWindow: true - }).then((tabList) => { - return browser.tabs.remove(tabList.map(tab => tab.id)); - }); -}; - -const winopenCommand = (url) => { - return browser.windows.create({ url }); -}; - -const bufferCommand = async(keywords) => { - if (keywords.length === 0) { - return; - } - let keywordsStr = keywords.join(' '); - let got = await browser.tabs.query({ - active: true, currentWindow: true - }); - if (got.length === 0) { - return; - } - if (isNaN(keywordsStr)) { - return tabs.selectByKeyword(got[0], keywordsStr); - } - let index = parseInt(keywordsStr, 10) - 1; - return tabs.selectAt(index); -}; - -const addbookmarkCommand = async(tab, args) => { - if (!args[0]) { - return { type: '' }; - } - let item = await bookmarks.create(args.join(' '), tab.url); - if (!item) { - return consoleActions.error(tab, 'Could not create a bookmark'); - } - return consoleActions.info(tab, 'Saved current page: ' + item.url); -}; - -const setCommand = (args) => { - if (!args[0]) { - return { type: '' }; - } - - let [name, value] = parsers.parseSetOption(args[0], properties.types); - return { - type: actions.SETTING_SET_PROPERTY, - name, - value - }; -}; - -// eslint-disable-next-line complexity, max-lines-per-function -const doExec = async(tab, line, settings) => { - let [name, args] = parsers.parseCommandLine(line); - - switch (name) { - case 'o': - case 'open': - await openCommand(parsers.normalizeUrl(args, settings.search)); - break; - case 't': - case 'tabopen': - await tabopenCommand(parsers.normalizeUrl(args, settings.search)); - break; - case 'w': - case 'winopen': - await winopenCommand(parsers.normalizeUrl(args, settings.search)); - break; - case 'b': - case 'buffer': - await bufferCommand(args); - break; - case 'bd': - case 'bdel': - case 'bdelete': - await tabs.closeTabByKeywords(args.join(' ')); - break; - case 'bd!': - case 'bdel!': - case 'bdelete!': - await tabs.closeTabByKeywordsForce(args.join(' ')); - break; - case 'bdeletes': - await tabs.closeTabsByKeywords(args.join(' ')); - break; - case 'bdeletes!': - await tabs.closeTabsByKeywordsForce(args.join(' ')); - break; - case 'addbookmark': - return addbookmarkCommand(tab, args); - case 'set': - return setCommand(args); - case 'q': - case 'quit': - await tabcloseCommand(); - break; - case 'qa': - case 'quitall': - await tabcloseAllCommand(); - break; - default: - return consoleActions.error(tab, name + ' command is not defined'); - } - return { type: '' }; -}; - -// eslint-disable-next-line complexity -const exec = async(tab, line, settings) => { - try { - let action = await doExec(tab, line, settings); - return action; - } catch (e) { - return consoleActions.error(tab, e.toString()); - } -}; - -export { exec }; diff --git a/src/background/actions/console.js b/src/background/actions/console.js deleted file mode 100644 index d385b2d..0000000 --- a/src/background/actions/console.js +++ /dev/null @@ -1,41 +0,0 @@ -import messages from 'shared/messages'; - -const error = async(tab, text) => { - await browser.tabs.sendMessage(tab.id, { - type: messages.CONSOLE_SHOW_ERROR, - text, - }); - return { type: '' }; -}; - -const info = async(tab, text) => { - await browser.tabs.sendMessage(tab.id, { - type: messages.CONSOLE_SHOW_INFO, - text, - }); - return { type: '' }; -}; - -const showCommand = async(tab, command) => { - await browser.tabs.sendMessage(tab.id, { - type: messages.CONSOLE_SHOW_COMMAND, - command, - }); - return { type: '' }; -}; - -const showFind = async(tab) => { - await browser.tabs.sendMessage(tab.id, { - type: messages.CONSOLE_SHOW_FIND - }); - return { type: '' }; -}; - -const hide = async(tab) => { - await browser.tabs.sendMessage(tab.id, { - type: messages.CONSOLE_HIDE, - }); - return { type: '' }; -}; - -export { error, info, showCommand, showFind, hide }; diff --git a/src/background/actions/find.js b/src/background/actions/find.js deleted file mode 100644 index 8da5572..0000000 --- a/src/background/actions/find.js +++ /dev/null @@ -1,10 +0,0 @@ -import actions from './index'; - -const setKeyword = (keyword) => { - return { - type: actions.FIND_SET_KEYWORD, - keyword, - }; -}; - -export { setKeyword }; diff --git a/src/background/actions/index.js b/src/background/actions/index.js deleted file mode 100644 index 3833389..0000000 --- a/src/background/actions/index.js +++ /dev/null @@ -1,11 +0,0 @@ -export default { - // Settings - SETTING_SET_SETTINGS: 'setting.set.settings', - SETTING_SET_PROPERTY: 'setting.set.property', - - // 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 deleted file mode 100644 index 0f32a90..0000000 --- a/src/background/actions/tab.js +++ /dev/null @@ -1,34 +0,0 @@ -import actions from './index'; - -const openNewTab = async( - url, openerTabId, background = false, adjacent = false -) => { - if (!adjacent) { - await browser.tabs.create({ url, active: !background }); - return { type: '' }; - } - let tabs = await browser.tabs.query({ - active: true, currentWindow: true - }); - await browser.tabs.create({ - url, - openerTabId, - active: !background, - index: tabs[0].index + 1 - }); - return { type: '' }; -}; - -const openToTab = async(url, tab) => { - await browser.tabs.update(tab.id, { url: url }); - return { type: '' }; -}; - -const selected = (tabId) => { - return { - type: actions.TAB_SELECTED, - tabId, - }; -}; - -export { openNewTab, openToTab, selected }; diff --git a/src/background/components/background.js b/src/background/components/background.js deleted file mode 100644 index 23131d2..0000000 --- a/src/background/components/background.js +++ /dev/null @@ -1,35 +0,0 @@ -import messages from 'shared/messages'; -import * as commandActions from 'background/actions/command'; - -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) { - let settings = this.store.getState().setting; - - switch (message.type) { - } - } - - async broadcastSettingsChanged() { - let tabs = await browser.tabs.query({}); - for (let tab of tabs) { - browser.tabs.sendMessage(tab.id, { - type: messages.SETTINGS_CHANGED, - }); - } - } -} diff --git a/src/background/components/indicator.js b/src/background/components/indicator.js deleted file mode 100644 index 1ded329..0000000 --- a/src/background/components/indicator.js +++ /dev/null @@ -1,43 +0,0 @@ -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(async(info) => { - await browser.tabs.query({ currentWindow: true }); - return this.onTabActivated(info); - }); - } - - async onTabActivated(info) { - let { enabled } = await browser.tabs.sendMessage(info.tabId, { - type: messages.ADDON_ENABLED_QUERY, - }); - return this.updateIndicator(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(); - } -} diff --git a/src/background/components/operation.js b/src/background/components/operation.js deleted file mode 100644 index 57b2497..0000000 --- a/src/background/components/operation.js +++ /dev/null @@ -1,124 +0,0 @@ -import messages from 'shared/messages'; -import operations from 'shared/operations'; -import * as tabs from '../shared//tabs'; -import * as zooms from '../shared/zooms'; -import * as consoleActions from '../actions/console'; - -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) { - } - } - - // eslint-disable-next-line complexity, max-lines-per-function - async exec(operation, tab) { - let tabState = this.store.getState().tab; - - switch (operation.type) { - case operations.TAB_CLOSE: - await tabs.closeTab(tab.id); - break; - case operations.TAB_CLOSE_FORCE: - await tabs.closeTabForce(tab.id); - break; - case operations.TAB_REOPEN: - await tabs.reopenTab(); - break; - case operations.TAB_PREV: - await tabs.selectPrevTab(tab.index, operation.count); - break; - case operations.TAB_NEXT: - await tabs.selectNextTab(tab.index, operation.count); - break; - case operations.TAB_FIRST: - await tabs.selectFirstTab(); - break; - case operations.TAB_LAST: - await tabs.selectLastTab(); - break; - case operations.TAB_PREV_SEL: - if (tabState.previousSelected > 0) { - await tabs.selectTab(tabState.previousSelected); - } - break; - case operations.TAB_RELOAD: - await tabs.reload(tab, operation.cache); - break; - case operations.TAB_PIN: - await tabs.updateTabPinned(tab, true); - break; - case operations.TAB_UNPIN: - await tabs.updateTabPinned(tab, false); - break; - case operations.TAB_TOGGLE_PINNED: - await tabs.toggleTabPinned(tab); - break; - case operations.TAB_DUPLICATE: - await tabs.duplicate(tab.id); - break; - case operations.ZOOM_IN: - await zooms.zoomIn(); - break; - case operations.ZOOM_OUT: - await zooms.zoomOut(); - break; - case operations.ZOOM_NEUTRAL: - await zooms.neutral(); - break; - case operations.COMMAND_SHOW: - return consoleActions.showCommand(tab, ''); - case operations.COMMAND_SHOW_OPEN: - if (operation.alter) { - // alter url - return consoleActions.showCommand(tab, 'open ' + tab.url); - } - return consoleActions.showCommand(tab, 'open '); - case operations.COMMAND_SHOW_TABOPEN: - if (operation.alter) { - // alter url - return consoleActions.showCommand(tab, 'tabopen ' + tab.url); - } - return consoleActions.showCommand(tab, 'tabopen '); - case operations.COMMAND_SHOW_WINOPEN: - if (operation.alter) { - // alter url - return consoleActions.showCommand(tab, 'winopen ' + tab.url); - } - return consoleActions.showCommand(tab, 'winopen '); - case operations.COMMAND_SHOW_BUFFER: - return consoleActions.showCommand(tab, 'buffer '); - case operations.COMMAND_SHOW_ADDBOOKMARK: - if (operation.alter) { - return consoleActions.showCommand(tab, 'addbookmark ' + tab.title); - } - return consoleActions.showCommand(tab, 'addbookmark '); - case operations.FIND_START: - return consoleActions.showFind(tab); - case operations.CANCEL: - return consoleActions.hide(tab); - case operations.PAGE_SOURCE: - await browser.tabs.create({ - url: 'view-source:' + tab.url, - index: tab.index + 1, - openerTabId: tab.id, - }); - break; - } - return { type: '' }; - } -} diff --git a/src/background/components/tab.js b/src/background/components/tab.js deleted file mode 100644 index 6af3fd7..0000000 --- a/src/background/components/tab.js +++ /dev/null @@ -1,16 +0,0 @@ -import * as tabActions from '../actions/tab'; - -export default class TabComponent { - constructor(store) { - this.store = store; - - browser.tabs.onActivated.addListener(async(info) => { - await browser.tabs.query({ currentWindow: true }); - return this.onTabActivated(info); - }); - } - - onTabActivated(info) { - return this.store.dispatch(tabActions.selected(info.tabId)); - } -} diff --git a/src/background/shared/commands/docs.js b/src/background/domains/command-docs.js similarity index 99% rename from src/background/shared/commands/docs.js rename to src/background/domains/command-docs.js index c73eb71..0b8ede7 100644 --- a/src/background/shared/commands/docs.js +++ b/src/background/domains/command-docs.js @@ -9,3 +9,4 @@ export default { quit: 'Close the current tab', quitall: 'Close all tabs', }; + diff --git a/src/background/index.js b/src/background/index.js index 70d514f..3492398 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,25 +1,7 @@ -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, applyMiddleware } from 'redux'; -import promise from 'redux-promise'; - import ContentMessageListener from './infrastructures/content-message-listener'; import SettingController from './controllers/setting'; import VersionRepository from './controllers/version'; -const store = createStore( - reducers, - applyMiddleware(promise), -); - -/* eslint-disable no-unused-vars */ -const backgroundComponent = new BackgroundComponent(store); -const operationComponent = new OperationComponent(store); -const tabComponent = new TabComponent(store); -/* eslint-enable no-unused-vars */ - new SettingController().reload(); new VersionRepository().notifyIfUpdated(); diff --git a/src/background/reducers/find.js b/src/background/reducers/find.js deleted file mode 100644 index bbc6b36..0000000 --- a/src/background/reducers/find.js +++ /dev/null @@ -1,15 +0,0 @@ -import actions from 'content/actions'; - -const defaultState = { - keyword: null, -}; - -export default function reducer(state = defaultState, action = {}) { - switch (action.type) { - case actions.FIND_SET_KEYWORD: - return { ...state, - keyword: action.keyword, }; - default: - return state; - } -} diff --git a/src/background/reducers/index.js b/src/background/reducers/index.js deleted file mode 100644 index 465f927..0000000 --- a/src/background/reducers/index.js +++ /dev/null @@ -1,8 +0,0 @@ -import { combineReducers } from 'redux'; -import setting from './setting'; -import find from './find'; -import tab from './tab'; - -export default combineReducers({ - setting, find, tab, -}); diff --git a/src/background/reducers/setting.js b/src/background/reducers/setting.js deleted file mode 100644 index 8dbc1b4..0000000 --- a/src/background/reducers/setting.js +++ /dev/null @@ -1,22 +0,0 @@ -import actions from 'background/actions'; - -const defaultState = { - value: {}, -}; - -export default function reducer(state = defaultState, action = {}) { - switch (action.type) { - case actions.SETTING_SET_SETTINGS: - return { - value: action.value, - }; - case actions.SETTING_SET_PROPERTY: - return { - value: { ...state.value, - properties: { ...state.value.properties, [action.name]: action.value }} - }; - default: - return state; - } -} - diff --git a/src/background/reducers/tab.js b/src/background/reducers/tab.js deleted file mode 100644 index e0cdf32..0000000 --- a/src/background/reducers/tab.js +++ /dev/null @@ -1,19 +0,0 @@ -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/bookmarks.js b/src/background/shared/bookmarks.js deleted file mode 100644 index 5e7927b..0000000 --- a/src/background/shared/bookmarks.js +++ /dev/null @@ -1,9 +0,0 @@ -const create = (title, url) => { - return browser.bookmarks.create({ - type: 'bookmark', - title, - url, - }); -}; - -export { create }; diff --git a/src/background/shared/commands/parsers.js b/src/background/shared/commands/parsers.js deleted file mode 100644 index fb37d2a..0000000 --- a/src/background/shared/commands/parsers.js +++ /dev/null @@ -1,59 +0,0 @@ -const normalizeUrl = (args, searchConfig) => { - let concat = args.join(' '); - try { - return new URL(concat).href; - } catch (e) { - if (concat.includes('.') && !concat.includes(' ')) { - return 'http://' + concat; - } - let query = concat; - let template = searchConfig.engines[ - searchConfig.default - ]; - for (let key in searchConfig.engines) { - if (args[0] === key) { - query = args.slice(1).join(' '); - template = searchConfig.engines[key]; - } - } - return template.replace('{}', encodeURIComponent(query)); - } -}; - -const mustNumber = (v) => { - let num = Number(v); - if (isNaN(num)) { - throw new Error('Not number: ' + v); - } - return num; -}; - -const parseSetOption = (word, types) => { - let [key, value] = word.split('='); - if (value === undefined) { - value = !key.startsWith('no'); - key = value ? key : key.slice(2); - } - let type = types[key]; - if (!type) { - throw new Error('Unknown property: ' + key); - } - if (type === 'boolean' && typeof value !== 'boolean' || - type !== 'boolean' && typeof value === 'boolean') { - throw new Error('Invalid argument: ' + word); - } - - switch (type) { - case 'string': return [key, value]; - case 'number': return [key, mustNumber(value)]; - case 'boolean': return [key, value]; - } -}; - -const parseCommandLine = (line) => { - let words = line.trim().split(/ +/); - let name = words.shift(); - return [name, words]; -}; - -export { normalizeUrl, parseCommandLine, parseSetOption }; diff --git a/src/background/shared/completions/bookmarks.js b/src/background/shared/completions/bookmarks.js deleted file mode 100644 index bd753af..0000000 --- a/src/background/shared/completions/bookmarks.js +++ /dev/null @@ -1,14 +0,0 @@ -const getCompletions = async(keywords) => { - let items = await browser.bookmarks.search({ query: keywords }); - return items.filter((item) => { - let url = undefined; - try { - url = new URL(item.url); - } catch (e) { - return false; - } - return item.type === 'bookmark' && url.protocol !== 'place:'; - }).slice(0, 10); -}; - -export { getCompletions }; diff --git a/src/background/shared/completions/histories.js b/src/background/shared/completions/histories.js deleted file mode 100644 index 2d35401..0000000 --- a/src/background/shared/completions/histories.js +++ /dev/null @@ -1,82 +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 = async(keyword) => { - let historyItems = await browser.history.search({ - text: keyword, - startTime: 0, - }); - 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 deleted file mode 100644 index 157604e..0000000 --- a/src/background/shared/completions/index.js +++ /dev/null @@ -1,173 +0,0 @@ -import commandDocs from 'background/shared/commands/docs'; -import * as tabs from './tabs'; -import * as histories from './histories'; -import * as bookmarks from './bookmarks'; -import * as properties from 'shared/settings/properties'; - -const completeCommands = (typing) => { - let keys = Object.keys(commandDocs); - return keys - .filter(name => name.startsWith(typing)) - .map(name => ({ - caption: name, - content: name, - url: commandDocs[name], - })); -}; - -const getSearchCompletions = (command, keywords, searchConfig) => { - let engineNames = Object.keys(searchConfig.engines); - let engineItems = engineNames.filter(name => name.startsWith(keywords)) - .map(name => ({ - caption: name, - content: command + ' ' + name - })); - return Promise.resolve(engineItems); -}; - -const getHistoryCompletions = async(command, keywords) => { - let items = await histories.getCompletions(keywords); - return items.map((page) => { - return { - caption: page.title, - content: command + ' ' + page.url, - url: page.url - }; - }); -}; - -const getBookmarksCompletions = async(command, keywords) => { - let items = await bookmarks.getCompletions(keywords); - return items.map(item => ({ - caption: item.title, - content: command + ' ' + item.url, - url: item.url, - })); -}; - -const getOpenCompletions = async(command, keywords, searchConfig) => { - let engineItems = await getSearchCompletions(command, keywords, searchConfig); - let historyItems = await getHistoryCompletions(command, keywords); - let bookmarkItems = await getBookmarksCompletions(command, keywords); - let completions = []; - if (engineItems.length > 0) { - completions.push({ - name: 'Search Engines', - items: engineItems - }); - } - if (historyItems.length > 0) { - completions.push({ - name: 'History', - items: historyItems - }); - } - if (bookmarkItems.length > 0) { - completions.push({ - name: 'Bookmarks', - items: bookmarkItems - }); - } - return completions; -}; - -const getBufferCompletions = async(command, keywords, excludePinned) => { - let items = await tabs.getCompletions(keywords, excludePinned); - items = items.map(tab => ({ - caption: tab.title, - content: command + ' ' + tab.title, - url: tab.url, - icon: tab.favIconUrl - })); - return [ - { - name: 'Buffers', - items: items - } - ]; -}; - -const getSetCompletions = (command, keywords) => { - let keys = Object.keys(properties.docs).filter( - name => name.startsWith(keywords) - ); - let items = keys.map((key) => { - if (properties.types[key] === 'boolean') { - return [ - { - caption: key, - content: command + ' ' + key, - url: 'Enable ' + properties.docs[key], - }, { - caption: 'no' + key, - content: command + ' no' + key, - url: 'Disable ' + properties.docs[key], - } - ]; - } - return [ - { - caption: key, - content: command + ' ' + key, - url: 'Set ' + properties.docs[key], - } - ]; - }); - items = items.reduce((acc, val) => acc.concat(val), []); - if (items.length === 0) { - return Promise.resolve([]); - } - return Promise.resolve([ - { - name: 'Properties', - items, - } - ]); -}; - -const complete = (line, settings) => { - let trimmed = line.trimStart(); - let words = trimmed.split(/ +/); - let name = words[0]; - if (words.length === 1) { - let items = completeCommands(name); - if (items.length === 0) { - return Promise.resolve([]); - } - return Promise.resolve([ - { - name: 'Console Command', - items: completeCommands(name), - } - ]); - } - let keywords = trimmed.slice(name.length).trimStart(); - - switch (words[0]) { - case 'o': - case 'open': - case 't': - case 'tabopen': - case 'w': - case 'winopen': - return getOpenCompletions(name, keywords, settings.search); - case 'b': - case 'buffer': - return getBufferCompletions(name, keywords, false); - case 'bd!': - case 'bdel!': - case 'bdelete!': - case 'bdeletes!': - return getBufferCompletions(name, keywords, false); - case 'bd': - case 'bdel': - case 'bdelete': - case 'bdeletes': - return getBufferCompletions(name, keywords, true); - case 'set': - return getSetCompletions(name, keywords); - } - return Promise.resolve([]); -}; - -export { complete }; diff --git a/src/background/shared/completions/tabs.js b/src/background/shared/completions/tabs.js deleted file mode 100644 index bdb2741..0000000 --- a/src/background/shared/completions/tabs.js +++ /dev/null @@ -1,8 +0,0 @@ -import * as tabs from '../tabs'; - -const getCompletions = (keyword, excludePinned) => { - return tabs.queryByKeyword(keyword, excludePinned); -}; - - -export { getCompletions }; diff --git a/src/background/shared/indicators.js b/src/background/shared/indicators.js deleted file mode 100644 index 74002c4..0000000 --- a/src/background/shared/indicators.js +++ /dev/null @@ -1,13 +0,0 @@ -const enable = () => { - return browser.browserAction.setIcon({ - path: 'resources/enabled_32x32.png', - }); -}; - -const disable = () => { - return browser.browserAction.setIcon({ - path: 'resources/disabled_32x32.png', - }); -}; - -export { enable, disable }; diff --git a/src/background/shared/tabs.js b/src/background/shared/tabs.js deleted file mode 100644 index 26e2e44..0000000 --- a/src/background/shared/tabs.js +++ /dev/null @@ -1,158 +0,0 @@ -import * as tabCompletions from './completions/tabs'; - -const closeTab = async(id) => { - let tab = await browser.tabs.get(id); - if (!tab.pinned) { - return browser.tabs.remove(id); - } -}; - -const closeTabForce = (id) => { - return browser.tabs.remove(id); -}; - -const queryByKeyword = async(keyword, excludePinned = false) => { - let tabs = await browser.tabs.query({ currentWindow: true }); - return tabs.filter((t) => { - return t.url.toLowerCase().includes(keyword.toLowerCase()) || - t.title && t.title.toLowerCase().includes(keyword.toLowerCase()); - }).filter((t) => { - return !(excludePinned && t.pinned); - }); -}; - -const closeTabByKeywords = async(keyword) => { - let tabs = await queryByKeyword(keyword, true); - if (tabs.length === 0) { - throw new Error('No matching buffer for ' + keyword); - } else if (tabs.length > 1) { - throw new Error('More than one match for ' + keyword); - } - return browser.tabs.remove(tabs[0].id); -}; - -const closeTabByKeywordsForce = async(keyword) => { - let tabs = await queryByKeyword(keyword, false); - if (tabs.length === 0) { - throw new Error('No matching buffer for ' + keyword); - } else if (tabs.length > 1) { - throw new Error('More than one match for ' + keyword); - } - return browser.tabs.remove(tabs[0].id); -}; - -const closeTabsByKeywords = async(keyword) => { - let tabs = await tabCompletions.getCompletions(keyword); - tabs = tabs.filter(tab => !tab.pinned); - return browser.tabs.remove(tabs.map(tab => tab.id)); -}; - -const closeTabsByKeywordsForce = async(keyword) => { - let tabs = await tabCompletions.getCompletions(keyword); - return browser.tabs.remove(tabs.map(tab => tab.id)); -}; - -const reopenTab = async() => { - let window = await browser.windows.getCurrent(); - let sessions = await browser.sessions.getRecentlyClosed(); - let session = sessions.find((s) => { - return s.tab && s.tab.windowId === window.id; - }); - if (!session) { - return; - } - if (session.tab) { - return browser.sessions.restore(session.tab.sessionId); - } - return browser.sessions.restore(session.window.sessionId); -}; - -const selectAt = async(index) => { - let tabs = await browser.tabs.query({ currentWindow: true }); - 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 = async(current, keyword) => { - let tabs = await queryByKeyword(keyword); - if (tabs.length === 0) { - throw new RangeError('No matching buffer for ' + keyword); - } - for (let tab of tabs) { - if (tab.index > current.index) { - return browser.tabs.update(tab.id, { active: true }); - } - } - return browser.tabs.update(tabs[0].id, { active: true }); -}; - -const selectPrevTab = async(current, count) => { - let tabs = await browser.tabs.query({ currentWindow: true }); - 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 = async(current, count) => { - let tabs = await browser.tabs.query({ currentWindow: true }); - 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 = async() => { - let tabs = await browser.tabs.query({ currentWindow: true }); - let id = tabs[0].id; - return browser.tabs.update(id, { active: true }); -}; - -const selectLastTab = async() => { - let tabs = await browser.tabs.query({ currentWindow: true }); - let id = tabs[tabs.length - 1].id; - return browser.tabs.update(id, { active: true }); -}; - -const selectTab = (id) => { - return browser.tabs.update(id, { active: true }); -}; - -const reload = (current, cache) => { - return browser.tabs.reload( - current.id, - { bypassCache: cache } - ); -}; - -const updateTabPinned = (current, pinned) => { - return browser.tabs.update(current.id, { pinned }); -}; - -const toggleTabPinned = (current) => { - return updateTabPinned(current, !current.pinned); -}; - -const duplicate = (id) => { - return browser.tabs.duplicate(id); -}; - -export { - closeTab, closeTabForce, - queryByKeyword, closeTabByKeywords, closeTabByKeywordsForce, - closeTabsByKeywords, closeTabsByKeywordsForce, - reopenTab, selectAt, selectByKeyword, - selectPrevTab, selectNextTab, selectFirstTab, - selectLastTab, selectTab, reload, updateTabPinned, - toggleTabPinned, duplicate -}; diff --git a/src/background/shared/zooms.js b/src/background/shared/zooms.js deleted file mode 100644 index 17b28fa..0000000 --- a/src/background/shared/zooms.js +++ /dev/null @@ -1,32 +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 = async(tabId = undefined) => { - let current = await browser.tabs.getZoom(tabId); - let factor = ZOOM_SETTINGS.find(f => f > current); - if (factor) { - return browser.tabs.setZoom(tabId, factor); - } -}; - -const zoomOut = async(tabId = undefined) => { - let current = await browser.tabs.getZoom(tabId); - let factor = [].concat(ZOOM_SETTINGS).reverse().find(f => f < current); - if (factor) { - return browser.tabs.setZoom(tabId, factor); - } -}; - -const neutral = (tabId = undefined) => { - return browser.tabs.setZoom(tabId, 1); -}; - -export { zoomIn, zoomOut, neutral }; diff --git a/src/background/usecases/completions.js b/src/background/usecases/completions.js index 626f9e2..92a54c1 100644 --- a/src/background/usecases/completions.js +++ b/src/background/usecases/completions.js @@ -1,8 +1,8 @@ import CompletionItem from '../domains/completion-item'; import CompletionGroup from '../domains/completion-group'; import Completions from '../domains/completions'; +import CommandDocs from '../domains/command-docs'; import CompletionRepository from '../repositories/completions'; -import CommandDocs from 'background/shared/commands/docs'; import * as filters from './filters'; import SettingRepository from '../repositories/setting'; import * as properties from '../../shared/settings/properties'; diff --git a/test/background/actions/find.test.js b/test/background/actions/find.test.js deleted file mode 100644 index 6b0b846..0000000 --- a/test/background/actions/find.test.js +++ /dev/null @@ -1,12 +0,0 @@ -import actions from 'background/actions'; -import * as findActions from 'background/actions/find'; - -describe("find actions", () => { - describe("setKeyword", () => { - it('create FIND_SET_KEYWORD action', () => { - let action = findActions.setKeyword('banana'); - expect(action.type).to.equal(actions.FIND_SET_KEYWORD); - expect(action.keyword).to.equal('banana'); - }); - }); -}); diff --git a/test/background/actions/tab.test.js b/test/background/actions/tab.test.js deleted file mode 100644 index ab57374..0000000 --- a/test/background/actions/tab.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import actions from 'background/actions'; -import * as tabActions from 'background/actions/tab'; - -describe("tab actions", () => { - describe("selected", () => { - it('create TAB_SELECTED action', () => { - let action = tabActions.selected(123); - expect(action.type).to.equal(actions.TAB_SELECTED); - expect(action.tabId).to.equal(123); - }); - }); -}); - diff --git a/test/background/reducers/find.test.js b/test/background/reducers/find.test.js deleted file mode 100644 index c366223..0000000 --- a/test/background/reducers/find.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import actions from 'background/actions'; -import findReducer from 'background/reducers/find'; - -describe("find reducer", () => { - it('return the initial state', () => { - let state = findReducer(undefined, {}); - expect(state).to.have.deep.property('keyword', null); - }); - - it('return next state for FIND_SET_KEYWORD', () => { - let action = { - type: actions.FIND_SET_KEYWORD, - keyword: 'cherry', - }; - let state = findReducer(undefined, action); - expect(state).to.have.deep.property('keyword', 'cherry') - }); -}); diff --git a/test/background/reducers/setting.test.js b/test/background/reducers/setting.test.js deleted file mode 100644 index 24d02ea..0000000 --- a/test/background/reducers/setting.test.js +++ /dev/null @@ -1,35 +0,0 @@ -import actions from 'background/actions'; -import settingReducer from 'background/reducers/setting'; - -describe("setting reducer", () => { - it('return the initial state', () => { - let state = settingReducer(undefined, {}); - expect(state).to.have.deep.property('value', {}); - }); - - it('return next state for SETTING_SET_SETTINGS', () => { - let action = { - type: actions.SETTING_SET_SETTINGS, - value: { key: 123 }, - }; - let state = settingReducer(undefined, action); - expect(state).to.have.deep.property('value', { key: 123 }); - }); - - it('return next state for SETTING_SET_PROPERTY', () => { - let state = { - value: { - properties: { smoothscroll: true } - } - } - let action = { - type: actions.SETTING_SET_PROPERTY, - name: 'encoding', - value: 'utf-8', - }; - state = settingReducer(state, action); - - expect(state.value.properties).to.have.property('smoothscroll', true); - expect(state.value.properties).to.have.property('encoding', 'utf-8'); - }); -}); diff --git a/test/background/reducers/tab.test.js b/test/background/reducers/tab.test.js deleted file mode 100644 index 09fa8a7..0000000 --- a/test/background/reducers/tab.test.js +++ /dev/null @@ -1,22 +0,0 @@ -import actions from 'background/actions'; -import tabReducer from 'background/reducers/tab'; - -describe("tab reducer", () => { - it('return the initial state', () => { - let state = tabReducer(undefined, {}); - expect(state.previousSelected).to.equal(-1); - expect(state.currentSelected).to.equal(-1); - }); - - it('return next state for TAB_SELECTED', () => { - let state = undefined; - - state = tabReducer(state, { type: actions.TAB_SELECTED, tabId: 123 }); - expect(state.previousSelected).to.equal(-1); - expect(state.currentSelected).to.equal(123); - - state = tabReducer(state, { type: actions.TAB_SELECTED, tabId: 456 }); - expect(state.previousSelected).to.equal(123); - expect(state.currentSelected).to.equal(456); - }); -}); diff --git a/test/background/shared/commands/parsers.test.js b/test/background/shared/commands/parsers.test.js deleted file mode 100644 index 4a0c9f0..0000000 --- a/test/background/shared/commands/parsers.test.js +++ /dev/null @@ -1,84 +0,0 @@ -import * as parsers from 'background/shared/commands/parsers'; - -describe("shared/commands/parsers", () => { - describe("#parsers.parseSetOption", () => { - it('parse set string', () => { - let [key, value] = parsers.parseSetOption('encoding=utf-8', { encoding: 'string' }); - expect(key).to.equal('encoding'); - expect(value).to.equal('utf-8'); - }); - - it('parse set empty string', () => { - let [key, value] = parsers.parseSetOption('encoding=', { encoding: 'string' }); - expect(key).to.equal('encoding'); - expect(value).to.equal(''); - }); - - it('parse set string', () => { - let [key, value] = parsers.parseSetOption('history=50', { history: 'number' }); - expect(key).to.equal('history'); - expect(value).to.equal(50); - }); - - it('parse set boolean', () => { - let [key, value] = parsers.parseSetOption('paste', { paste: 'boolean' }); - expect(key).to.equal('paste'); - expect(value).to.be.true; - - [key, value] = parsers.parseSetOption('nopaste', { paste: 'boolean' }); - expect(key).to.equal('paste'); - expect(value).to.be.false; - }); - - it('throws error on unknown property', () => { - expect(() => parsers.parseSetOption('charset=utf-8', {})).to.throw(Error, 'Unknown'); - expect(() => parsers.parseSetOption('smoothscroll', {})).to.throw(Error, 'Unknown'); - expect(() => parsers.parseSetOption('nosmoothscroll', {})).to.throw(Error, 'Unknown'); - }) - - it('throws error on invalid property', () => { - expect(() => parsers.parseSetOption('charset=utf-8', { charset: 'number' })).to.throw(Error, 'Not number'); - expect(() => parsers.parseSetOption('charset=utf-8', { charset: 'boolean' })).to.throw(Error, 'Invalid'); - expect(() => parsers.parseSetOption('charset=', { charset: 'boolean' })).to.throw(Error, 'Invalid'); - expect(() => parsers.parseSetOption('smoothscroll', { smoothscroll: 'string' })).to.throw(Error, 'Invalid'); - expect(() => parsers.parseSetOption('smoothscroll', { smoothscroll: 'number' })).to.throw(Error, 'Invalid'); - }) - }); - - describe('#normalizeUrl', () => { - const config = { - default: 'google', - engines: { - google: 'https://google.com/search?q={}', - yahoo: 'https://yahoo.com/search?q={}', - } - }; - - it('convertes search url', () => { - expect(parsers.normalizeUrl(['google', 'apple'], config)) - .to.equal('https://google.com/search?q=apple'); - expect(parsers.normalizeUrl(['yahoo', 'apple'], config)) - .to.equal('https://yahoo.com/search?q=apple'); - expect(parsers.normalizeUrl(['google', 'apple', 'banana'], config)) - .to.equal('https://google.com/search?q=apple%20banana'); - expect(parsers.normalizeUrl(['yahoo', 'C++CLI'], config)) - .to.equal('https://yahoo.com/search?q=C%2B%2BCLI'); - }); - - it('user default search engine', () => { - expect(parsers.normalizeUrl(['apple', 'banana'], config)) - .to.equal('https://google.com/search?q=apple%20banana'); - }); - }); - - describe('#parseCommandLine', () => { - it('parse command line as name and args', () => { - expect(parsers.parseCommandLine('open google apple')).to.deep.equal(['open', ['google', 'apple']]); - expect(parsers.parseCommandLine(' open google apple ')).to.deep.equal(['open', ['google', 'apple']]); - expect(parsers.parseCommandLine('')).to.deep.equal(['', []]); - expect(parsers.parseCommandLine(' ')).to.deep.equal(['', []]); - expect(parsers.parseCommandLine('exit')).to.deep.equal(['exit', []]); - expect(parsers.parseCommandLine(' exit ')).to.deep.equal(['exit', []]); - }); - }); -}); diff --git a/test/background/usecases/parsers.test.js b/test/background/usecases/parsers.test.js index fa0e757..a58c4a3 100644 --- a/test/background/usecases/parsers.test.js +++ b/test/background/usecases/parsers.test.js @@ -1,6 +1,6 @@ import * as parsers from 'background/usecases/parsers'; -describe("background/usecases/parsers", () => { +describe("shared/commands/parsers", () => { describe("#parsers.parseSetOption", () => { it('parse set string', () => { let [key, value] = parsers.parseSetOption('encoding=utf-8', { encoding: 'string' }); @@ -65,10 +65,9 @@ describe("background/usecases/parsers", () => { .to.equal('https://yahoo.com/search?q=C%2B%2BCLI'); }); - it('user default search engine', () => { + it('user default search engine', () => { expect(parsers.normalizeUrl('apple banana', config)) .to.equal('https://google.com/search?q=apple%20banana'); }); }); }); -