diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-02-24 22:45:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-24 22:45:47 +0900 |
commit | dfeb7e75498384af5e24255ee0fe7f8af37ac489 (patch) | |
tree | 12094b3a72d20d07c4cb040c37849c4680fd222b /src/background/usecases | |
parent | 83684a78e6e54b1e15bd4280553e28eb1d21df09 (diff) | |
parent | 80a4a347ec92f3e702075e448aba191ad3627cf6 (diff) |
Merge pull request #544 from ueokande/refactor-background
Refactor background
Diffstat (limited to 'src/background/usecases')
14 files changed, 293 insertions, 271 deletions
diff --git a/src/background/usecases/addon-enabled.js b/src/background/usecases/AddonEnabledUseCase.js index d83192f..bb2c347 100644 --- a/src/background/usecases/addon-enabled.js +++ b/src/background/usecases/AddonEnabledUseCase.js @@ -1,8 +1,8 @@ -import IndicatorPresenter from '../presenters/indicator'; -import TabPresenter from '../presenters/tab'; -import ContentMessageClient from '../infrastructures/content-message-client'; +import IndicatorPresenter from '../presenters/IndicatorPresenter'; +import TabPresenter from '../presenters/TabPresenter'; +import ContentMessageClient from '../infrastructures/ContentMessageClient'; -export default class AddonEnabledInteractor { +export default class AddonEnabledUseCase { constructor() { this.indicatorPresentor = new IndicatorPresenter(); diff --git a/src/background/usecases/command.js b/src/background/usecases/CommandUseCase.js index 0d21041..9ec46fe 100644 --- a/src/background/usecases/command.js +++ b/src/background/usecases/CommandUseCase.js @@ -1,11 +1,11 @@ import * as parsers from './parsers'; import * as urls from '../../shared/urls'; -import TabPresenter from '../presenters/tab'; -import WindowPresenter from '../presenters/window'; -import SettingRepository from '../repositories/setting'; -import BookmarkRepository from '../repositories/bookmark'; -import ConsolePresenter from '../presenters/console'; -import ContentMessageClient from '../infrastructures/content-message-client'; +import TabPresenter from '../presenters/TabPresenter'; +import WindowPresenter from '../presenters/WindowPresenter'; +import SettingRepository from '../repositories/SettingRepository'; +import BookmarkRepository from '../repositories/BookmarkRepository'; +import ConsoleClient from '../infrastructures/ConsoleClient'; +import ContentMessageClient from '../infrastructures/ContentMessageClient'; import * as properties from 'shared/settings/properties'; export default class CommandIndicator { @@ -14,7 +14,7 @@ export default class CommandIndicator { this.windowPresenter = new WindowPresenter(); this.settingRepository = new SettingRepository(); this.bookmarkRepository = new BookmarkRepository(); - this.consolePresenter = new ConsolePresenter(); + this.consoleClient = new ConsoleClient(); this.contentMessageClient = new ContentMessageClient(); } @@ -105,7 +105,7 @@ export default class CommandIndicator { let tab = await this.tabPresenter.getCurrent(); let item = await this.bookmarkRepository.create(title, tab.url); let message = 'Saved current page: ' + item.url; - return this.consolePresenter.showInfo(tab.id, message); + return this.consoleClient.showInfo(tab.id, message); } async set(keywords) { diff --git a/src/background/usecases/completions.js b/src/background/usecases/CompletionsUseCase.js index 21dc668..7dc30ac 100644 --- a/src/background/usecases/completions.js +++ b/src/background/usecases/CompletionsUseCase.js @@ -1,19 +1,19 @@ -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 CompletionItem from '../domains/CompletionItem'; +import CompletionGroup from '../domains/CompletionGroup'; +import Completions from '../domains/Completions'; +import CommandDocs from '../domains/CommandDocs'; +import CompletionsRepository from '../repositories/CompletionsRepository'; import * as filters from './filters'; -import SettingRepository from '../repositories/setting'; -import TabPresenter from '../presenters/tab'; +import SettingRepository from '../repositories/SettingRepository'; +import TabPresenter from '../presenters/TabPresenter'; import * as properties from '../../shared/settings/properties'; const COMPLETION_ITEM_LIMIT = 10; -export default class CompletionsInteractor { +export default class CompletionsUseCase { constructor() { this.tabPresenter = new TabPresenter(); - this.completionRepository = new CompletionRepository(); + this.completionsRepository = new CompletionsRepository(); this.settingRepository = new SettingRepository(); } @@ -86,7 +86,7 @@ export default class CompletionsInteractor { tabs = [tab]; } } else { - tabs = await this.completionRepository.queryTabs(keywords, false); + tabs = await this.completionsRepository.queryTabs(keywords, false); } const flag = (tab) => { if (tab.active) { @@ -153,7 +153,7 @@ export default class CompletionsInteractor { } async queryTabs(name, excludePinned, args) { - let tabs = await this.completionRepository.queryTabs(args, excludePinned); + let tabs = await this.completionsRepository.queryTabs(args, excludePinned); let items = tabs.map(tab => new CompletionItem({ caption: tab.title, content: name + ' ' + tab.title, @@ -177,7 +177,7 @@ export default class CompletionsInteractor { } async queryHistoryItems(name, keywords) { - let histories = await this.completionRepository.queryHistories(keywords); + let histories = await this.completionsRepository.queryHistories(keywords); histories = [histories] .map(filters.filterBlankTitle) .map(filters.filterHttp) @@ -194,7 +194,7 @@ export default class CompletionsInteractor { } async queryBookmarkItems(name, keywords) { - let bookmarks = await this.completionRepository.queryBookmarks(keywords); + let bookmarks = await this.completionsRepository.queryBookmarks(keywords); return bookmarks.slice(0, COMPLETION_ITEM_LIMIT) .map(page => new CompletionItem({ caption: page.title, diff --git a/src/background/usecases/ConsoleUseCase.js b/src/background/usecases/ConsoleUseCase.js new file mode 100644 index 0000000..e8e5d4a --- /dev/null +++ b/src/background/usecases/ConsoleUseCase.js @@ -0,0 +1,61 @@ +import TabPresenter from '../presenters/TabPresenter'; +import ConsoleClient from '../infrastructures/ConsoleClient'; + +export default class ConsoleUseCase { + constructor() { + this.tabPresenter = new TabPresenter(); + this.consoleClient = new ConsoleClient(); + } + + async showCommand() { + let tab = await this.tabPresenter.getCurrent(); + return this.consoleClient.showCommand(tab.id, ''); + } + + async showOpenCommand(alter) { + let tab = await this.tabPresenter.getCurrent(); + let command = 'open '; + if (alter) { + command += tab.url; + } + return this.consoleClient.showCommand(tab.id, command); + } + + async showTabopenCommand(alter) { + let tab = await this.tabPresenter.getCurrent(); + let command = 'tabopen '; + if (alter) { + command += tab.url; + } + return this.consoleClient.showCommand(tab.id, command); + } + + async showWinopenCommand(alter) { + let tab = await this.tabPresenter.getCurrent(); + let command = 'winopen '; + if (alter) { + command += tab.url; + } + return this.consoleClient.showCommand(tab.id, command); + } + + async showBufferCommand() { + let tab = await this.tabPresenter.getCurrent(); + let command = 'buffer '; + return this.consoleClient.showCommand(tab.id, command); + } + + async showAddbookmarkCommand(alter) { + let tab = await this.tabPresenter.getCurrent(); + let command = 'addbookmark '; + if (alter) { + command += tab.title; + } + return this.consoleClient.showCommand(tab.id, command); + } + + async hideConsole() { + let tab = await this.tabPresenter.getCurrent(); + return this.consoleClient.hide(tab.id); + } +} diff --git a/src/background/usecases/FindUseCase.js b/src/background/usecases/FindUseCase.js new file mode 100644 index 0000000..224e4a9 --- /dev/null +++ b/src/background/usecases/FindUseCase.js @@ -0,0 +1,24 @@ +import FindRepository from '../repositories/FindRepository'; +import TabPresenter from '../presenters/TabPresenter'; +import ConsoleClient from '../infrastructures/ConsoleClient'; + +export default class FindUseCase { + constructor() { + this.tabPresenter = new TabPresenter(); + this.findRepository = new FindRepository(); + this.consoleClient = new ConsoleClient(); + } + + getKeyword() { + return this.findRepository.getKeyword(); + } + + setKeyword(keyword) { + return this.findRepository.setKeyword(keyword); + } + + async findStart() { + let tab = await this.tabPresenter.getCurrent(); + return this.consoleClient.showFind(tab.id); + } +} diff --git a/src/background/usecases/link.js b/src/background/usecases/LinkUseCase.js index b8ed719..89412c5 100644 --- a/src/background/usecases/link.js +++ b/src/background/usecases/LinkUseCase.js @@ -1,7 +1,7 @@ -import SettingRepository from '../repositories/setting'; -import TabPresenter from '../presenters/tab'; +import SettingRepository from '../repositories/SettingRepository'; +import TabPresenter from '../presenters/TabPresenter'; -export default class LinkInteractor { +export default class LinkUseCase { constructor() { this.settingRepository = new SettingRepository(); this.tabPresenter = new TabPresenter(); diff --git a/src/background/usecases/mark.js b/src/background/usecases/MarkUseCase.js index 34b8a74..39c796b 100644 --- a/src/background/usecases/mark.js +++ b/src/background/usecases/MarkUseCase.js @@ -1,14 +1,14 @@ -import GlobalMark from '../domains/global-mark'; -import TabPresenter from '../presenters/tab'; -import MarkRepository from '../repositories/mark'; -import ConsolePresenter from '../presenters/console'; -import ContentMessageClient from '../infrastructures/content-message-client'; +import GlobalMark from '../domains/GlobalMark'; +import TabPresenter from '../presenters/TabPresenter'; +import MarkRepository from '../repositories/MarkRepository'; +import ConsoleClient from '../infrastructures/ConsoleClient'; +import ContentMessageClient from '../infrastructures/ContentMessageClient'; -export default class MarkInteractor { +export default class MarkUseCase { constructor() { this.tabPresenter = new TabPresenter(); this.markRepository = new MarkRepository(); - this.consolePresenter = new ConsolePresenter(); + this.consoleClient = new ConsoleClient(); this.contentMessageClient = new ContentMessageClient(); } @@ -23,7 +23,7 @@ export default class MarkInteractor { let mark = await this.markRepository.getMark(key); if (!mark) { - return this.consolePresenter.showError(current.id, 'Mark is not set'); + return this.consoleClient.showError(current.id, 'Mark is not set'); } return this.contentMessageClient.scrollTo( diff --git a/src/background/usecases/setting.js b/src/background/usecases/SettingUseCase.js index 656fc3f..9e17408 100644 --- a/src/background/usecases/setting.js +++ b/src/background/usecases/SettingUseCase.js @@ -1,17 +1,14 @@ -import Setting from '../domains/setting'; -import PersistentSettingRepository from '../repositories/persistent-setting'; -import SettingRepository from '../repositories/setting'; +import Setting from '../domains/Setting'; +// eslint-disable-next-line max-len +import PersistentSettingRepository from '../repositories/PersistentSettingRepository'; +import SettingRepository from '../repositories/SettingRepository'; -export default class SettingInteractor { +export default class SettingUseCase { constructor() { this.persistentSettingRepository = new PersistentSettingRepository(); this.settingRepository = new SettingRepository(); } - save(settings) { - this.persistentSettingRepository.save(settings); - } - get() { return this.settingRepository.get(); } diff --git a/src/background/usecases/TabSelectUseCase.js b/src/background/usecases/TabSelectUseCase.js new file mode 100644 index 0000000..16b3e14 --- /dev/null +++ b/src/background/usecases/TabSelectUseCase.js @@ -0,0 +1,51 @@ +import TabPresenter from '../presenters/TabPresenter'; + +export default class TabSelectUseCase { + constructor() { + this.tabPresenter = new TabPresenter(); + } + + async selectPrev(count) { + let tabs = await this.tabPresenter.getAll(); + if (tabs.length < 2) { + return; + } + let tab = tabs.find(t => t.active); + if (!tab) { + return; + } + let select = (tab.index - count + tabs.length) % tabs.length; + return this.tabPresenter.select(tabs[select].id); + } + + async selectNext(count) { + let tabs = await this.tabPresenter.getAll(); + if (tabs.length < 2) { + return; + } + let tab = tabs.find(t => t.active); + if (!tab) { + return; + } + let select = (tab.index + count) % tabs.length; + return this.tabPresenter.select(tabs[select].id); + } + + async selectFirst() { + let tabs = await this.tabPresenter.getAll(); + return this.tabPresenter.select(tabs[0].id); + } + + async selectLast() { + let tabs = await this.tabPresenter.getAll(); + return this.tabPresenter.select(tabs[tabs.length - 1].id); + } + + async selectPrevSelected() { + let tabId = await this.tabPresenter.getLastSelectedId(); + if (tabId === null || typeof tabId === 'undefined') { + return; + } + this.tabPresenter.select(tabId); + } +} diff --git a/src/background/usecases/TabUseCase.js b/src/background/usecases/TabUseCase.js new file mode 100644 index 0000000..d930842 --- /dev/null +++ b/src/background/usecases/TabUseCase.js @@ -0,0 +1,77 @@ +import TabPresenter from '../presenters/TabPresenter'; +import BrowserSettingRepository from '../repositories/BrowserSettingRepository'; + +export default class TabUseCase { + constructor() { + this.tabPresenter = new TabPresenter(); + this.browserSettingRepository = new BrowserSettingRepository(); + } + + async close(force) { + let tab = await this.tabPresenter.getCurrent(); + if (!force && tab.pinned) { + return; + } + return this.tabPresenter.remove([tab.id]); + } + + async closeRight() { + let tabs = await this.tabPresenter.getAll(); + tabs.sort((t1, t2) => t1.index - t2.index); + let index = tabs.findIndex(t => t.active); + if (index < 0) { + return; + } + for (let i = index + 1; i < tabs.length; ++i) { + let tab = tabs[i]; + if (!tab.pinned) { + this.tabPresenter.remove(tab.id); + } + } + } + + reopen() { + return this.tabPresenter.reopen(); + } + + async reload(cache) { + let tab = await this.tabPresenter.getCurrent(); + return this.tabPresenter.reload(tab.id, cache); + } + + async setPinned(pinned) { + let tab = await this.tabPresenter.getCurrent(); + return this.tabPresenter.setPinned(tab.id, pinned); + } + + async togglePinned() { + let tab = await this.tabPresenter.getCurrent(); + return this.tabPresenter.setPinned(tab.id, !tab.pinned); + } + + async duplicate() { + let tab = await this.tabPresenter.getCurrent(); + return this.tabPresenter.duplicate(tab.id); + } + + async openPageSource() { + let tab = await this.tabPresenter.getCurrent(); + let url = 'view-source:' + tab.url; + return this.tabPresenter.create(url); + } + + async openHome(newTab) { + let tab = await this.tabPresenter.getCurrent(); + let urls = await this.browserSettingRepository.getHomepageUrls(); + if (urls.length === 1 && urls[0] === 'about:home') { + // eslint-disable-next-line max-len + throw new Error('Cannot open Firefox Home (about:home) by WebExtensions, set your custom URLs'); + } + if (urls.length === 1 && !newTab) { + return this.tabPresenter.open(urls[0], tab.id); + } + for (let url of urls) { + this.tabPresenter.create(url); + } + } +} diff --git a/src/background/usecases/version.js b/src/background/usecases/VersionUseCase.js index a71f90d..704c808 100644 --- a/src/background/usecases/version.js +++ b/src/background/usecases/VersionUseCase.js @@ -1,9 +1,9 @@ import manifest from '../../../manifest.json'; -import VersionRepository from '../repositories/version'; -import TabPresenter from '../presenters/tab'; -import Notifier from '../infrastructures/notifier'; +import VersionRepository from '../repositories/VersionRepository'; +import TabPresenter from '../presenters/TabPresenter'; +import Notifier from '../infrastructures/Notifier'; -export default class VersionInteractor { +export default class VersionUseCase { constructor() { this.versionRepository = new VersionRepository(); this.tabPresenter = new TabPresenter(); diff --git a/src/background/usecases/ZoomUseCase.js b/src/background/usecases/ZoomUseCase.js new file mode 100644 index 0000000..692d6d9 --- /dev/null +++ b/src/background/usecases/ZoomUseCase.js @@ -0,0 +1,35 @@ +import TabPresenter from '../presenters/TabPresenter'; + +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 +]; + +export default class ZoomUseCase { + constructor() { + this.tabPresenter = new TabPresenter(); + } + + async zoomIn(tabId) { + let tab = await this.tabPresenter.getCurrent(); + let current = await this.tabPresenter.getZoom(tab.id); + let factor = ZOOM_SETTINGS.find(f => f > current); + if (factor) { + return this.tabPresenter.setZoom(tabId, factor); + } + } + + async zoomOut(tabId) { + let tab = await this.tabPresenter.getCurrent(); + let current = await this.tabPresenter.getZoom(tab.id); + let factor = [].concat(ZOOM_SETTINGS).reverse().find(f => f < current); + if (factor) { + return this.tabPresenter.setZoom(tabId, factor); + } + } + + zoomNutoral(tabId) { + return this.tabPresenter.setZoom(tabId, 1); + } + +} diff --git a/src/background/usecases/find.js b/src/background/usecases/find.js deleted file mode 100644 index eae480d..0000000 --- a/src/background/usecases/find.js +++ /dev/null @@ -1,15 +0,0 @@ -import FindRepository from '../repositories/find'; - -export default class FindInteractor { - constructor() { - this.findRepository = new FindRepository(); - } - - getKeyword() { - return this.findRepository.getKeyword(); - } - - setKeyword(keyword) { - return this.findRepository.setKeyword(keyword); - } -} diff --git a/src/background/usecases/operation.js b/src/background/usecases/operation.js deleted file mode 100644 index ed64032..0000000 --- a/src/background/usecases/operation.js +++ /dev/null @@ -1,208 +0,0 @@ -import TabPresenter from '../presenters/tab'; -import ConsolePresenter from '../presenters/console'; -import * as urls from '../../shared/urls'; - -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 -]; - -export default class OperationInteractor { - constructor() { - this.tabPresenter = new TabPresenter(); - this.consolePresenter = new ConsolePresenter(); - } - - async close(force) { - let tab = await this.tabPresenter.getCurrent(); - if (!force && tab.pinned) { - return; - } - return this.tabPresenter.remove([tab.id]); - } - - async closeRight() { - let tabs = await this.tabPresenter.getAll(); - tabs.sort((t1, t2) => t1.index - t2.index); - let index = tabs.findIndex(t => t.active); - if (index < 0) { - return; - } - for (let i = index + 1; i < tabs.length; ++i) { - let tab = tabs[i]; - if (!tab.pinned) { - this.tabPresenter.remove(tab.id); - } - } - } - - reopen() { - return this.tabPresenter.reopen(); - } - - async selectPrev(count) { - let tabs = await this.tabPresenter.getAll(); - if (tabs.length < 2) { - return; - } - let tab = tabs.find(t => t.active); - if (!tab) { - return; - } - let select = (tab.index - count + tabs.length) % tabs.length; - return this.tabPresenter.select(tabs[select].id); - } - - async selectNext(count) { - let tabs = await this.tabPresenter.getAll(); - if (tabs.length < 2) { - return; - } - let tab = tabs.find(t => t.active); - if (!tab) { - return; - } - let select = (tab.index + count) % tabs.length; - return this.tabPresenter.select(tabs[select].id); - } - - async selectFirst() { - let tabs = await this.tabPresenter.getAll(); - return this.tabPresenter.select(tabs[0].id); - } - - async selectLast() { - let tabs = await this.tabPresenter.getAll(); - return this.tabPresenter.select(tabs[tabs.length - 1].id); - } - - async selectPrevSelected() { - let tabId = await this.tabPresenter.getLastSelectedId(); - if (tabId === null || typeof tabId === 'undefined') { - return; - } - this.tabPresenter.select(tabId); - } - - async reload(cache) { - let tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.reload(tab.id, cache); - } - - async setPinned(pinned) { - let tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.setPinned(tab.id, pinned); - } - - async togglePinned() { - let tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.setPinned(tab.id, !tab.pinned); - } - - async duplicate() { - let tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.duplicate(tab.id); - } - - async openPageSource() { - let tab = await this.tabPresenter.getCurrent(); - let url = 'view-source:' + tab.url; - return this.tabPresenter.create(url); - } - - async zoomIn(tabId) { - let tab = await this.tabPresenter.getCurrent(); - let current = await this.tabPresenter.getZoom(tab.id); - let factor = ZOOM_SETTINGS.find(f => f > current); - if (factor) { - return this.tabPresenter.setZoom(tabId, factor); - } - } - - async zoomOut(tabId) { - let tab = await this.tabPresenter.getCurrent(); - let current = await this.tabPresenter.getZoom(tab.id); - let factor = [].concat(ZOOM_SETTINGS).reverse().find(f => f < current); - if (factor) { - return this.tabPresenter.setZoom(tabId, factor); - } - } - - zoomNutoral(tabId) { - return this.tabPresenter.setZoom(tabId, 1); - } - - async showCommand() { - let tab = await this.tabPresenter.getCurrent(); - return this.consolePresenter.showCommand(tab.id, ''); - } - - async showOpenCommand(alter) { - let tab = await this.tabPresenter.getCurrent(); - let command = 'open '; - if (alter) { - command += tab.url; - } - return this.consolePresenter.showCommand(tab.id, command); - } - - async showTabopenCommand(alter) { - let tab = await this.tabPresenter.getCurrent(); - let command = 'tabopen '; - if (alter) { - command += tab.url; - } - return this.consolePresenter.showCommand(tab.id, command); - } - - async showWinopenCommand(alter) { - let tab = await this.tabPresenter.getCurrent(); - let command = 'winopen '; - if (alter) { - command += tab.url; - } - return this.consolePresenter.showCommand(tab.id, command); - } - - async showBufferCommand() { - let tab = await this.tabPresenter.getCurrent(); - let command = 'buffer '; - return this.consolePresenter.showCommand(tab.id, command); - } - - async showAddbookmarkCommand(alter) { - let tab = await this.tabPresenter.getCurrent(); - let command = 'addbookmark '; - if (alter) { - command += tab.title; - } - return this.consolePresenter.showCommand(tab.id, command); - } - - async findStart() { - let tab = await this.tabPresenter.getCurrent(); - return this.consolePresenter.showFind(tab.id); - } - - async hideConsole() { - let tab = await this.tabPresenter.getCurrent(); - return this.consolePresenter.hide(tab.id); - } - - async openHome(newTab) { - let tab = await this.tabPresenter.getCurrent(); - let result = await browser.browserSettings.homepageOverride.get({}); - let us = urls.homepageUrls(result.value); - if (us.length === 1 && us[0] === 'about:home') { - // eslint-disable-next-line max-len - throw new Error('Cannot open Firefox Home (about:home) by WebExtensions, set your custom URLs'); - } - if (us.length === 1 && !newTab) { - return this.tabPresenter.open(us[0], tab.id); - } - for (let u of us) { - this.tabPresenter.create(u); - } - } -} - |