From a26d8a8a1bed48a77e062914c120a23ace7bb8cf Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 24 Feb 2019 20:54:35 +0900 Subject: Capitalize background scripts --- .../controllers/AddonEnabledController.js | 11 ++ src/background/controllers/CommandController.js | 99 ++++++++++ src/background/controllers/FindController.js | 15 ++ src/background/controllers/LinkController.js | 15 ++ src/background/controllers/MarkController.js | 15 ++ src/background/controllers/OperationController.js | 69 +++++++ src/background/controllers/SettingController.js | 18 ++ src/background/controllers/VersionController.js | 11 ++ src/background/controllers/addon-enabled.js | 11 -- src/background/controllers/command.js | 99 ---------- src/background/controllers/find.js | 15 -- src/background/controllers/link.js | 15 -- src/background/controllers/mark.js | 15 -- src/background/controllers/operation.js | 69 ------- src/background/controllers/setting.js | 18 -- src/background/controllers/version.js | 11 -- src/background/domains/CommandDocs.js | 12 ++ src/background/domains/CompletionGroup.js | 14 ++ src/background/domains/CompletionItem.js | 24 +++ src/background/domains/Completions.js | 27 +++ src/background/domains/GlobalMark.js | 24 +++ src/background/domains/Setting.js | 51 +++++ src/background/domains/command-docs.js | 12 -- src/background/domains/completion-group.js | 14 -- src/background/domains/completion-item.js | 24 --- src/background/domains/completions.js | 27 --- src/background/domains/global-mark.js | 24 --- src/background/domains/setting.js | 51 ----- src/background/index.js | 6 +- src/background/infrastructures/ConsoleClient.js | 37 ++++ .../infrastructures/ContentMessageClient.js | 33 ++++ .../infrastructures/ContentMessageListener.js | 141 ++++++++++++++ src/background/infrastructures/MemoryStorage.js | 19 ++ src/background/infrastructures/Notifier.js | 23 +++ .../infrastructures/content-message-client.js | 33 ---- .../infrastructures/content-message-listener.js | 141 -------------- src/background/infrastructures/memory-storage.js | 19 -- src/background/infrastructures/notifier.js | 23 --- src/background/presenters/IndicatorPresenter.js | 12 ++ src/background/presenters/TabPresenter.js | 102 ++++++++++ src/background/presenters/WindowPresenter.js | 5 + src/background/presenters/console.js | 36 ---- src/background/presenters/indicator.js | 12 -- src/background/presenters/tab.js | 102 ---------- src/background/presenters/window.js | 5 - src/background/repositories/BookmarkRepository.js | 13 ++ .../repositories/CompletionsRepository.js | 31 +++ src/background/repositories/FindRepository.js | 19 ++ src/background/repositories/MarkRepository.js | 33 ++++ .../repositories/PersistentSettingRepository.js | 16 ++ src/background/repositories/SettingRepository.js | 23 +++ src/background/repositories/VersionRepository.js | 10 + src/background/repositories/bookmark.js | 13 -- src/background/repositories/completions.js | 31 --- src/background/repositories/find.js | 19 -- src/background/repositories/mark.js | 33 ---- src/background/repositories/persistent-setting.js | 16 -- src/background/repositories/setting.js | 23 --- src/background/repositories/version.js | 10 - src/background/usecases/AddonEnabledUseCase.js | 29 +++ src/background/usecases/CommandUseCase.js | 125 +++++++++++++ src/background/usecases/CompletionsUseCase.js | 205 ++++++++++++++++++++ src/background/usecases/FindUseCase.js | 15 ++ src/background/usecases/LinkUseCase.js | 19 ++ src/background/usecases/MarkUseCase.js | 39 ++++ src/background/usecases/OperationUseCase.js | 208 +++++++++++++++++++++ src/background/usecases/SettingUseCase.js | 32 ++++ src/background/usecases/VersionUseCase.js | 41 ++++ src/background/usecases/addon-enabled.js | 29 --- src/background/usecases/command.js | 125 ------------- src/background/usecases/completions.js | 205 -------------------- src/background/usecases/find.js | 15 -- src/background/usecases/link.js | 19 -- src/background/usecases/mark.js | 39 ---- src/background/usecases/operation.js | 208 --------------------- src/background/usecases/setting.js | 31 --- src/background/usecases/version.js | 41 ---- 77 files changed, 1638 insertions(+), 1636 deletions(-) create mode 100644 src/background/controllers/AddonEnabledController.js create mode 100644 src/background/controllers/CommandController.js create mode 100644 src/background/controllers/FindController.js create mode 100644 src/background/controllers/LinkController.js create mode 100644 src/background/controllers/MarkController.js create mode 100644 src/background/controllers/OperationController.js create mode 100644 src/background/controllers/SettingController.js create mode 100644 src/background/controllers/VersionController.js delete mode 100644 src/background/controllers/addon-enabled.js delete mode 100644 src/background/controllers/command.js delete mode 100644 src/background/controllers/find.js delete mode 100644 src/background/controllers/link.js delete mode 100644 src/background/controllers/mark.js delete mode 100644 src/background/controllers/operation.js delete mode 100644 src/background/controllers/setting.js delete mode 100644 src/background/controllers/version.js create mode 100644 src/background/domains/CommandDocs.js create mode 100644 src/background/domains/CompletionGroup.js create mode 100644 src/background/domains/CompletionItem.js create mode 100644 src/background/domains/Completions.js create mode 100644 src/background/domains/GlobalMark.js create mode 100644 src/background/domains/Setting.js delete mode 100644 src/background/domains/command-docs.js delete mode 100644 src/background/domains/completion-group.js delete mode 100644 src/background/domains/completion-item.js delete mode 100644 src/background/domains/completions.js delete mode 100644 src/background/domains/global-mark.js delete mode 100644 src/background/domains/setting.js create mode 100644 src/background/infrastructures/ConsoleClient.js create mode 100644 src/background/infrastructures/ContentMessageClient.js create mode 100644 src/background/infrastructures/ContentMessageListener.js create mode 100644 src/background/infrastructures/MemoryStorage.js create mode 100644 src/background/infrastructures/Notifier.js delete mode 100644 src/background/infrastructures/content-message-client.js delete mode 100644 src/background/infrastructures/content-message-listener.js delete mode 100644 src/background/infrastructures/memory-storage.js delete mode 100644 src/background/infrastructures/notifier.js create mode 100644 src/background/presenters/IndicatorPresenter.js create mode 100644 src/background/presenters/TabPresenter.js create mode 100644 src/background/presenters/WindowPresenter.js delete mode 100644 src/background/presenters/console.js delete mode 100644 src/background/presenters/indicator.js delete mode 100644 src/background/presenters/tab.js delete mode 100644 src/background/presenters/window.js create mode 100644 src/background/repositories/BookmarkRepository.js create mode 100644 src/background/repositories/CompletionsRepository.js create mode 100644 src/background/repositories/FindRepository.js create mode 100644 src/background/repositories/MarkRepository.js create mode 100644 src/background/repositories/PersistentSettingRepository.js create mode 100644 src/background/repositories/SettingRepository.js create mode 100644 src/background/repositories/VersionRepository.js delete mode 100644 src/background/repositories/bookmark.js delete mode 100644 src/background/repositories/completions.js delete mode 100644 src/background/repositories/find.js delete mode 100644 src/background/repositories/mark.js delete mode 100644 src/background/repositories/persistent-setting.js delete mode 100644 src/background/repositories/setting.js delete mode 100644 src/background/repositories/version.js create mode 100644 src/background/usecases/AddonEnabledUseCase.js create mode 100644 src/background/usecases/CommandUseCase.js create mode 100644 src/background/usecases/CompletionsUseCase.js create mode 100644 src/background/usecases/FindUseCase.js create mode 100644 src/background/usecases/LinkUseCase.js create mode 100644 src/background/usecases/MarkUseCase.js create mode 100644 src/background/usecases/OperationUseCase.js create mode 100644 src/background/usecases/SettingUseCase.js create mode 100644 src/background/usecases/VersionUseCase.js delete mode 100644 src/background/usecases/addon-enabled.js delete mode 100644 src/background/usecases/command.js delete mode 100644 src/background/usecases/completions.js delete mode 100644 src/background/usecases/find.js delete mode 100644 src/background/usecases/link.js delete mode 100644 src/background/usecases/mark.js delete mode 100644 src/background/usecases/operation.js delete mode 100644 src/background/usecases/setting.js delete mode 100644 src/background/usecases/version.js (limited to 'src') diff --git a/src/background/controllers/AddonEnabledController.js b/src/background/controllers/AddonEnabledController.js new file mode 100644 index 0000000..9a3a521 --- /dev/null +++ b/src/background/controllers/AddonEnabledController.js @@ -0,0 +1,11 @@ +import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase'; + +export default class AddonEnabledController { + constructor() { + this.addonEnabledUseCase = new AddonEnabledUseCase(); + } + + indicate(enabled) { + return this.addonEnabledUseCase.indicate(enabled); + } +} diff --git a/src/background/controllers/CommandController.js b/src/background/controllers/CommandController.js new file mode 100644 index 0000000..b113709 --- /dev/null +++ b/src/background/controllers/CommandController.js @@ -0,0 +1,99 @@ +import CompletionsUseCase from '../usecases/CompletionsUseCase'; +import CommandUseCase from '../usecases/CommandUseCase'; +import Completions from '../domains/Completions'; + +const trimStart = (str) => { + // NOTE String.trimStart is available on Firefox 61 + return str.replace(/^\s+/, ''); +}; + +export default class CommandController { + constructor() { + this.completionsUseCase = new CompletionsUseCase(); + this.commandIndicator = new CommandUseCase(); + } + + getCompletions(line) { + let trimmed = trimStart(line); + let words = trimmed.split(/ +/); + let name = words[0]; + if (words.length === 1) { + return this.completionsUseCase.queryConsoleCommand(name); + } + let keywords = trimStart(trimmed.slice(name.length)); + switch (words[0]) { + case 'o': + case 'open': + case 't': + case 'tabopen': + case 'w': + case 'winopen': + return this.completionsUseCase.queryOpen(name, keywords); + case 'b': + case 'buffer': + return this.completionsUseCase.queryBuffer(name, keywords); + case 'bd': + case 'bdel': + case 'bdelete': + case 'bdeletes': + return this.completionsUseCase.queryBdelete(name, keywords); + case 'bd!': + case 'bdel!': + case 'bdelete!': + case 'bdeletes!': + return this.completionsUseCase.queryBdeleteForce(name, keywords); + case 'set': + return this.completionsUseCase.querySet(name, keywords); + } + return Promise.resolve(Completions.empty()); + } + + // eslint-disable-next-line complexity + exec(line) { + let trimmed = trimStart(line); + let words = trimmed.split(/ +/); + let name = words[0]; + if (words[0].length === 0) { + return Promise.resolve(); + } + + let keywords = trimStart(trimmed.slice(name.length)); + switch (words[0]) { + case 'o': + case 'open': + return this.commandIndicator.open(keywords); + case 't': + case 'tabopen': + return this.commandIndicator.tabopen(keywords); + case 'w': + case 'winopen': + return this.commandIndicator.winopen(keywords); + case 'b': + case 'buffer': + return this.commandIndicator.buffer(keywords); + case 'bd': + case 'bdel': + case 'bdelete': + return this.commandIndicator.bdelete(false, keywords); + case 'bd!': + case 'bdel!': + case 'bdelete!': + return this.commandIndicator.bdelete(true, keywords); + case 'bdeletes': + return this.commandIndicator.bdeletes(false, keywords); + case 'bdeletes!': + return this.commandIndicator.bdeletes(true, keywords); + case 'addbookmark': + return this.commandIndicator.addbookmark(keywords); + case 'q': + case 'quit': + return this.commandIndicator.quit(); + case 'qa': + case 'quitall': + return this.commandIndicator.quitAll(); + case 'set': + return this.commandIndicator.set(keywords); + } + throw new Error(words[0] + ' command is not defined'); + } +} diff --git a/src/background/controllers/FindController.js b/src/background/controllers/FindController.js new file mode 100644 index 0000000..caeff98 --- /dev/null +++ b/src/background/controllers/FindController.js @@ -0,0 +1,15 @@ +import FindUseCase from '../usecases/FindUseCase'; + +export default class FindController { + constructor() { + this.findUseCase = new FindUseCase(); + } + + getKeyword() { + return this.findUseCase.getKeyword(); + } + + setKeyword(keyword) { + return this.findUseCase.setKeyword(keyword); + } +} diff --git a/src/background/controllers/LinkController.js b/src/background/controllers/LinkController.js new file mode 100644 index 0000000..7e395b1 --- /dev/null +++ b/src/background/controllers/LinkController.js @@ -0,0 +1,15 @@ +import LinkUseCase from '../usecases/LinkUseCase'; + +export default class LinkController { + constructor() { + this.linkUseCase = new LinkUseCase(); + } + + openToTab(url, tabId) { + this.linkUseCase.openToTab(url, tabId); + } + + openNewTab(url, openerId, background) { + this.linkUseCase.openNewTab(url, openerId, background); + } +} diff --git a/src/background/controllers/MarkController.js b/src/background/controllers/MarkController.js new file mode 100644 index 0000000..0478369 --- /dev/null +++ b/src/background/controllers/MarkController.js @@ -0,0 +1,15 @@ +import MarkUseCase from '../usecases/MarkUseCase'; + +export default class MarkController { + constructor() { + this.markUseCase = new MarkUseCase(); + } + + setGlobal(key, x, y) { + this.markUseCase.setGlobal(key, x, y); + } + + jumpGlobal(key) { + this.markUseCase.jumpGlobal(key); + } +} diff --git a/src/background/controllers/OperationController.js b/src/background/controllers/OperationController.js new file mode 100644 index 0000000..609599e --- /dev/null +++ b/src/background/controllers/OperationController.js @@ -0,0 +1,69 @@ +import operations from '../../shared/operations'; +import OperationUseCase from '../usecases/OperationUseCase'; + +export default class OperationController { + constructor() { + this.operationUseCase = new OperationUseCase(); + } + + // eslint-disable-next-line complexity, max-lines-per-function + exec(operation) { + switch (operation.type) { + case operations.TAB_CLOSE: + return this.operationUseCase.close(false); + case operations.TAB_CLOSE_RIGHT: + return this.operationUseCase.closeRight(); + case operations.TAB_CLOSE_FORCE: + return this.operationUseCase.close(true); + case operations.TAB_REOPEN: + return this.operationUseCase.reopen(); + case operations.TAB_PREV: + return this.operationUseCase.selectPrev(1); + case operations.TAB_NEXT: + return this.operationUseCase.selectNext(1); + case operations.TAB_FIRST: + return this.operationUseCase.selectFirst(); + case operations.TAB_LAST: + return this.operationUseCase.selectLast(); + case operations.TAB_PREV_SEL: + return this.operationUseCase.selectPrevSelected(); + case operations.TAB_RELOAD: + return this.operationUseCase.reload(operation.cache); + case operations.TAB_PIN: + return this.operationUseCase.setPinned(true); + case operations.TAB_UNPIN: + return this.operationUseCase.setPinned(false); + case operations.TAB_TOGGLE_PINNED: + return this.operationUseCase.togglePinned(); + case operations.TAB_DUPLICATE: + return this.operationUseCase.duplicate(); + case operations.PAGE_SOURCE: + return this.operationUseCase.openPageSource(); + case operations.PAGE_HOME: + return this.operationUseCase.openHome(operation.newTab); + case operations.ZOOM_IN: + return this.operationUseCase.zoomIn(); + case operations.ZOOM_OUT: + return this.operationUseCase.zoomOut(); + case operations.ZOOM_NEUTRAL: + return this.operationUseCase.zoomNutoral(); + case operations.COMMAND_SHOW: + return this.operationUseCase.showCommand(); + case operations.COMMAND_SHOW_OPEN: + return this.operationUseCase.showOpenCommand(operation.alter); + case operations.COMMAND_SHOW_TABOPEN: + return this.operationUseCase.showTabopenCommand(operation.alter); + case operations.COMMAND_SHOW_WINOPEN: + return this.operationUseCase.showWinopenCommand(operation.alter); + case operations.COMMAND_SHOW_BUFFER: + return this.operationUseCase.showBufferCommand(); + case operations.COMMAND_SHOW_ADDBOOKMARK: + return this.operationUseCase.showAddbookmarkCommand(operation.alter); + case operations.FIND_START: + return this.operationUseCase.findStart(); + case operations.CANCEL: + return this.operationUseCase.hideConsole(); + } + } +} + diff --git a/src/background/controllers/SettingController.js b/src/background/controllers/SettingController.js new file mode 100644 index 0000000..e895d72 --- /dev/null +++ b/src/background/controllers/SettingController.js @@ -0,0 +1,18 @@ +import SettingUseCase from '../usecases/SettingUseCase'; +import ContentMessageClient from '../infrastructures/ContentMessageClient'; + +export default class SettingController { + constructor() { + this.settingUseCase = new SettingUseCase(); + this.contentMessageClient = new ContentMessageClient(); + } + + getSetting() { + return this.settingUseCase.get(); + } + + async reload() { + await this.settingUseCase.reload(); + this.contentMessageClient.broadcastSettingsChanged(); + } +} diff --git a/src/background/controllers/VersionController.js b/src/background/controllers/VersionController.js new file mode 100644 index 0000000..1bcac4c --- /dev/null +++ b/src/background/controllers/VersionController.js @@ -0,0 +1,11 @@ +import VersionUseCase from '../usecases/VersionUseCase'; + +export default class VersionController { + constructor() { + this.versionUseCase = new VersionUseCase(); + } + + notifyIfUpdated() { + this.versionUseCase.notifyIfUpdated(); + } +} diff --git a/src/background/controllers/addon-enabled.js b/src/background/controllers/addon-enabled.js deleted file mode 100644 index 6a9b776..0000000 --- a/src/background/controllers/addon-enabled.js +++ /dev/null @@ -1,11 +0,0 @@ -import AddonEnabledInteractor from '../usecases/addon-enabled'; - -export default class AddonEnabledController { - constructor() { - this.addonEnabledInteractor = new AddonEnabledInteractor(); - } - - indicate(enabled) { - return this.addonEnabledInteractor.indicate(enabled); - } -} diff --git a/src/background/controllers/command.js b/src/background/controllers/command.js deleted file mode 100644 index 9ab1054..0000000 --- a/src/background/controllers/command.js +++ /dev/null @@ -1,99 +0,0 @@ -import CompletionsInteractor from '../usecases/completions'; -import CommandInteractor from '../usecases/command'; -import Completions from '../domains/completions'; - -const trimStart = (str) => { - // NOTE String.trimStart is available on Firefox 61 - return str.replace(/^\s+/, ''); -}; - -export default class CommandController { - constructor() { - this.completionsInteractor = new CompletionsInteractor(); - this.commandIndicator = new CommandInteractor(); - } - - getCompletions(line) { - let trimmed = trimStart(line); - let words = trimmed.split(/ +/); - let name = words[0]; - if (words.length === 1) { - return this.completionsInteractor.queryConsoleCommand(name); - } - let keywords = trimStart(trimmed.slice(name.length)); - switch (words[0]) { - case 'o': - case 'open': - case 't': - case 'tabopen': - case 'w': - case 'winopen': - return this.completionsInteractor.queryOpen(name, keywords); - case 'b': - case 'buffer': - return this.completionsInteractor.queryBuffer(name, keywords); - case 'bd': - case 'bdel': - case 'bdelete': - case 'bdeletes': - return this.completionsInteractor.queryBdelete(name, keywords); - case 'bd!': - case 'bdel!': - case 'bdelete!': - case 'bdeletes!': - return this.completionsInteractor.queryBdeleteForce(name, keywords); - case 'set': - return this.completionsInteractor.querySet(name, keywords); - } - return Promise.resolve(Completions.empty()); - } - - // eslint-disable-next-line complexity - exec(line) { - let trimmed = trimStart(line); - let words = trimmed.split(/ +/); - let name = words[0]; - if (words[0].length === 0) { - return Promise.resolve(); - } - - let keywords = trimStart(trimmed.slice(name.length)); - switch (words[0]) { - case 'o': - case 'open': - return this.commandIndicator.open(keywords); - case 't': - case 'tabopen': - return this.commandIndicator.tabopen(keywords); - case 'w': - case 'winopen': - return this.commandIndicator.winopen(keywords); - case 'b': - case 'buffer': - return this.commandIndicator.buffer(keywords); - case 'bd': - case 'bdel': - case 'bdelete': - return this.commandIndicator.bdelete(false, keywords); - case 'bd!': - case 'bdel!': - case 'bdelete!': - return this.commandIndicator.bdelete(true, keywords); - case 'bdeletes': - return this.commandIndicator.bdeletes(false, keywords); - case 'bdeletes!': - return this.commandIndicator.bdeletes(true, keywords); - case 'addbookmark': - return this.commandIndicator.addbookmark(keywords); - case 'q': - case 'quit': - return this.commandIndicator.quit(); - case 'qa': - case 'quitall': - return this.commandIndicator.quitAll(); - case 'set': - return this.commandIndicator.set(keywords); - } - throw new Error(words[0] + ' command is not defined'); - } -} diff --git a/src/background/controllers/find.js b/src/background/controllers/find.js deleted file mode 100644 index 7096014..0000000 --- a/src/background/controllers/find.js +++ /dev/null @@ -1,15 +0,0 @@ -import FindInteractor from '../usecases/find'; - -export default class FindController { - constructor() { - this.findInteractor = new FindInteractor(); - } - - getKeyword() { - return this.findInteractor.getKeyword(); - } - - setKeyword(keyword) { - return this.findInteractor.setKeyword(keyword); - } -} diff --git a/src/background/controllers/link.js b/src/background/controllers/link.js deleted file mode 100644 index 7ebbb34..0000000 --- a/src/background/controllers/link.js +++ /dev/null @@ -1,15 +0,0 @@ -import LinkInteractor from '../usecases/link'; - -export default class LinkController { - constructor() { - this.linkInteractor = new LinkInteractor(); - } - - openToTab(url, tabId) { - this.linkInteractor.openToTab(url, tabId); - } - - openNewTab(url, openerId, background) { - this.linkInteractor.openNewTab(url, openerId, background); - } -} diff --git a/src/background/controllers/mark.js b/src/background/controllers/mark.js deleted file mode 100644 index 8d0cefd..0000000 --- a/src/background/controllers/mark.js +++ /dev/null @@ -1,15 +0,0 @@ -import MarkInteractor from '../usecases/mark'; - -export default class MarkController { - constructor() { - this.markInteractor = new MarkInteractor(); - } - - setGlobal(key, x, y) { - this.markInteractor.setGlobal(key, x, y); - } - - jumpGlobal(key) { - this.markInteractor.jumpGlobal(key); - } -} diff --git a/src/background/controllers/operation.js b/src/background/controllers/operation.js deleted file mode 100644 index 646f50e..0000000 --- a/src/background/controllers/operation.js +++ /dev/null @@ -1,69 +0,0 @@ -import operations from '../../shared/operations'; -import OperationInteractor from '../usecases/operation'; - -export default class OperationController { - constructor() { - this.operationInteractor = new OperationInteractor(); - } - - // eslint-disable-next-line complexity, max-lines-per-function - exec(operation) { - switch (operation.type) { - case operations.TAB_CLOSE: - return this.operationInteractor.close(false); - case operations.TAB_CLOSE_RIGHT: - return this.operationInteractor.closeRight(); - case operations.TAB_CLOSE_FORCE: - return this.operationInteractor.close(true); - case operations.TAB_REOPEN: - return this.operationInteractor.reopen(); - case operations.TAB_PREV: - return this.operationInteractor.selectPrev(1); - case operations.TAB_NEXT: - return this.operationInteractor.selectNext(1); - case operations.TAB_FIRST: - return this.operationInteractor.selectFirst(); - case operations.TAB_LAST: - return this.operationInteractor.selectLast(); - case operations.TAB_PREV_SEL: - return this.operationInteractor.selectPrevSelected(); - case operations.TAB_RELOAD: - return this.operationInteractor.reload(operation.cache); - case operations.TAB_PIN: - return this.operationInteractor.setPinned(true); - case operations.TAB_UNPIN: - return this.operationInteractor.setPinned(false); - case operations.TAB_TOGGLE_PINNED: - return this.operationInteractor.togglePinned(); - case operations.TAB_DUPLICATE: - return this.operationInteractor.duplicate(); - case operations.PAGE_SOURCE: - return this.operationInteractor.openPageSource(); - case operations.PAGE_HOME: - return this.operationInteractor.openHome(operation.newTab); - case operations.ZOOM_IN: - return this.operationInteractor.zoomIn(); - case operations.ZOOM_OUT: - return this.operationInteractor.zoomOut(); - case operations.ZOOM_NEUTRAL: - return this.operationInteractor.zoomNutoral(); - case operations.COMMAND_SHOW: - return this.operationInteractor.showCommand(); - case operations.COMMAND_SHOW_OPEN: - return this.operationInteractor.showOpenCommand(operation.alter); - case operations.COMMAND_SHOW_TABOPEN: - return this.operationInteractor.showTabopenCommand(operation.alter); - case operations.COMMAND_SHOW_WINOPEN: - return this.operationInteractor.showWinopenCommand(operation.alter); - case operations.COMMAND_SHOW_BUFFER: - return this.operationInteractor.showBufferCommand(); - case operations.COMMAND_SHOW_ADDBOOKMARK: - return this.operationInteractor.showAddbookmarkCommand(operation.alter); - case operations.FIND_START: - return this.operationInteractor.findStart(); - case operations.CANCEL: - return this.operationInteractor.hideConsole(); - } - } -} - diff --git a/src/background/controllers/setting.js b/src/background/controllers/setting.js deleted file mode 100644 index 9e6019e..0000000 --- a/src/background/controllers/setting.js +++ /dev/null @@ -1,18 +0,0 @@ -import SettingInteractor from '../usecases/setting'; -import ContentMessageClient from '../infrastructures/content-message-client'; - -export default class SettingController { - constructor() { - this.settingInteractor = new SettingInteractor(); - this.contentMessageClient = new ContentMessageClient(); - } - - getSetting() { - return this.settingInteractor.get(); - } - - async reload() { - await this.settingInteractor.reload(); - this.contentMessageClient.broadcastSettingsChanged(); - } -} diff --git a/src/background/controllers/version.js b/src/background/controllers/version.js deleted file mode 100644 index 04d99fe..0000000 --- a/src/background/controllers/version.js +++ /dev/null @@ -1,11 +0,0 @@ -import VersionInteractor from '../usecases/version'; - -export default class VersionController { - constructor() { - this.versionInteractor = new VersionInteractor(); - } - - notifyIfUpdated() { - this.versionInteractor.notifyIfUpdated(); - } -} diff --git a/src/background/domains/CommandDocs.js b/src/background/domains/CommandDocs.js new file mode 100644 index 0000000..734c68e --- /dev/null +++ b/src/background/domains/CommandDocs.js @@ -0,0 +1,12 @@ +export default { + set: 'Set a value of the property', + open: 'Open a URL or search by keywords in current tab', + tabopen: 'Open a URL or search by keywords in new tab', + winopen: 'Open a URL or search by keywords in new window', + buffer: 'Select tabs by matched keywords', + bdelete: 'Close a certain tab matched by keywords', + bdeletes: 'Close all tabs matched by keywords', + quit: 'Close the current tab', + quitall: 'Close all tabs', +}; + diff --git a/src/background/domains/CompletionGroup.js b/src/background/domains/CompletionGroup.js new file mode 100644 index 0000000..1749d72 --- /dev/null +++ b/src/background/domains/CompletionGroup.js @@ -0,0 +1,14 @@ +export default class CompletionGroup { + constructor(name, items) { + this.name0 = name; + this.items0 = items; + } + + get name() { + return this.name0; + } + + get items() { + return this.items0; + } +} diff --git a/src/background/domains/CompletionItem.js b/src/background/domains/CompletionItem.js new file mode 100644 index 0000000..c7ad8a1 --- /dev/null +++ b/src/background/domains/CompletionItem.js @@ -0,0 +1,24 @@ +export default class CompletionItem { + constructor({ caption, content, url, icon }) { + this.caption0 = caption; + this.content0 = content; + this.url0 = url; + this.icon0 = icon; + } + + get caption() { + return this.caption0; + } + + get content() { + return this.content0; + } + + get url() { + return this.url0; + } + + get icon() { + return this.icon0; + } +} diff --git a/src/background/domains/Completions.js b/src/background/domains/Completions.js new file mode 100644 index 0000000..4e4219f --- /dev/null +++ b/src/background/domains/Completions.js @@ -0,0 +1,27 @@ +export default class Completions { + constructor(groups) { + this.g = groups; + } + + get groups() { + return this.g; + } + + serialize() { + return this.groups.map(group => ({ + name: group.name, + items: group.items.map(item => ({ + caption: item.caption, + content: item.content, + url: item.url, + icon: item.icon, + })), + })); + } + + static EMPTY_COMPLETIONS = new Completions([]); + + static empty() { + return Completions.EMPTY_COMPLETIONS; + } +} diff --git a/src/background/domains/GlobalMark.js b/src/background/domains/GlobalMark.js new file mode 100644 index 0000000..f0586f1 --- /dev/null +++ b/src/background/domains/GlobalMark.js @@ -0,0 +1,24 @@ +export default class GlobalMark { + constructor(tabId, url, x, y) { + this.tabId0 = tabId; + this.url0 = url; + this.x0 = x; + this.y0 = y; + } + + get tabId() { + return this.tabId0; + } + + get url() { + return this.url0; + } + + get x() { + return this.x0; + } + + get y() { + return this.y0; + } +} diff --git a/src/background/domains/Setting.js b/src/background/domains/Setting.js new file mode 100644 index 0000000..106ec0f --- /dev/null +++ b/src/background/domains/Setting.js @@ -0,0 +1,51 @@ +import DefaultSettings from '../../shared/settings/default'; +import * as settingsValues from '../../shared/settings/values'; + +export default class Setting { + constructor({ source, json, form }) { + this.obj = { + source, json, form + }; + } + + get source() { + return this.obj.source; + } + + get json() { + return this.obj.json; + } + + get form() { + return this.obj.form; + } + + value() { + let value = JSON.parse(DefaultSettings.json); + if (this.obj.source === 'json') { + value = settingsValues.valueFromJson(this.obj.json); + } else if (this.obj.source === 'form') { + value = settingsValues.valueFromForm(this.obj.form); + } + if (!value.properties) { + value.properties = {}; + } + return { ...settingsValues.valueFromJson(DefaultSettings.json), ...value }; + } + + serialize() { + return this.obj; + } + + static deserialize(obj) { + return new Setting({ source: obj.source, json: obj.json, form: obj.form }); + } + + static defaultSettings() { + return new Setting({ + source: DefaultSettings.source, + json: DefaultSettings.json, + form: {}, + }); + } +} diff --git a/src/background/domains/command-docs.js b/src/background/domains/command-docs.js deleted file mode 100644 index 734c68e..0000000 --- a/src/background/domains/command-docs.js +++ /dev/null @@ -1,12 +0,0 @@ -export default { - set: 'Set a value of the property', - open: 'Open a URL or search by keywords in current tab', - tabopen: 'Open a URL or search by keywords in new tab', - winopen: 'Open a URL or search by keywords in new window', - buffer: 'Select tabs by matched keywords', - bdelete: 'Close a certain tab matched by keywords', - bdeletes: 'Close all tabs matched by keywords', - quit: 'Close the current tab', - quitall: 'Close all tabs', -}; - diff --git a/src/background/domains/completion-group.js b/src/background/domains/completion-group.js deleted file mode 100644 index 1749d72..0000000 --- a/src/background/domains/completion-group.js +++ /dev/null @@ -1,14 +0,0 @@ -export default class CompletionGroup { - constructor(name, items) { - this.name0 = name; - this.items0 = items; - } - - get name() { - return this.name0; - } - - get items() { - return this.items0; - } -} diff --git a/src/background/domains/completion-item.js b/src/background/domains/completion-item.js deleted file mode 100644 index c7ad8a1..0000000 --- a/src/background/domains/completion-item.js +++ /dev/null @@ -1,24 +0,0 @@ -export default class CompletionItem { - constructor({ caption, content, url, icon }) { - this.caption0 = caption; - this.content0 = content; - this.url0 = url; - this.icon0 = icon; - } - - get caption() { - return this.caption0; - } - - get content() { - return this.content0; - } - - get url() { - return this.url0; - } - - get icon() { - return this.icon0; - } -} diff --git a/src/background/domains/completions.js b/src/background/domains/completions.js deleted file mode 100644 index 4e4219f..0000000 --- a/src/background/domains/completions.js +++ /dev/null @@ -1,27 +0,0 @@ -export default class Completions { - constructor(groups) { - this.g = groups; - } - - get groups() { - return this.g; - } - - serialize() { - return this.groups.map(group => ({ - name: group.name, - items: group.items.map(item => ({ - caption: item.caption, - content: item.content, - url: item.url, - icon: item.icon, - })), - })); - } - - static EMPTY_COMPLETIONS = new Completions([]); - - static empty() { - return Completions.EMPTY_COMPLETIONS; - } -} diff --git a/src/background/domains/global-mark.js b/src/background/domains/global-mark.js deleted file mode 100644 index f0586f1..0000000 --- a/src/background/domains/global-mark.js +++ /dev/null @@ -1,24 +0,0 @@ -export default class GlobalMark { - constructor(tabId, url, x, y) { - this.tabId0 = tabId; - this.url0 = url; - this.x0 = x; - this.y0 = y; - } - - get tabId() { - return this.tabId0; - } - - get url() { - return this.url0; - } - - get x() { - return this.x0; - } - - get y() { - return this.y0; - } -} diff --git a/src/background/domains/setting.js b/src/background/domains/setting.js deleted file mode 100644 index 106ec0f..0000000 --- a/src/background/domains/setting.js +++ /dev/null @@ -1,51 +0,0 @@ -import DefaultSettings from '../../shared/settings/default'; -import * as settingsValues from '../../shared/settings/values'; - -export default class Setting { - constructor({ source, json, form }) { - this.obj = { - source, json, form - }; - } - - get source() { - return this.obj.source; - } - - get json() { - return this.obj.json; - } - - get form() { - return this.obj.form; - } - - value() { - let value = JSON.parse(DefaultSettings.json); - if (this.obj.source === 'json') { - value = settingsValues.valueFromJson(this.obj.json); - } else if (this.obj.source === 'form') { - value = settingsValues.valueFromForm(this.obj.form); - } - if (!value.properties) { - value.properties = {}; - } - return { ...settingsValues.valueFromJson(DefaultSettings.json), ...value }; - } - - serialize() { - return this.obj; - } - - static deserialize(obj) { - return new Setting({ source: obj.source, json: obj.json, form: obj.form }); - } - - static defaultSettings() { - return new Setting({ - source: DefaultSettings.source, - json: DefaultSettings.json, - form: {}, - }); - } -} diff --git a/src/background/index.js b/src/background/index.js index 30007a9..b829f99 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,6 +1,6 @@ -import ContentMessageListener from './infrastructures/content-message-listener'; -import SettingController from './controllers/setting'; -import VersionController from './controllers/version'; +import ContentMessageListener from './infrastructures/ContentMessageListener'; +import SettingController from './controllers/SettingController'; +import VersionController from './controllers/VersionController'; new SettingController().reload(); new VersionController().notifyIfUpdated(); diff --git a/src/background/infrastructures/ConsoleClient.js b/src/background/infrastructures/ConsoleClient.js new file mode 100644 index 0000000..f691515 --- /dev/null +++ b/src/background/infrastructures/ConsoleClient.js @@ -0,0 +1,37 @@ +import messages from '../../shared/messages'; + +export default class ConsoleClient { + showCommand(tabId, command) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_SHOW_COMMAND, + command, + }); + } + + showFind(tabId) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_SHOW_FIND + }); + } + + showInfo(tabId, message) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_SHOW_INFO, + text: message, + }); + } + + showError(tabId, message) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_SHOW_ERROR, + text: message, + }); + } + + hide(tabId) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_HIDE, + }); + } +} + diff --git a/src/background/infrastructures/ContentMessageClient.js b/src/background/infrastructures/ContentMessageClient.js new file mode 100644 index 0000000..7e7e602 --- /dev/null +++ b/src/background/infrastructures/ContentMessageClient.js @@ -0,0 +1,33 @@ +import messages from '../../shared/messages'; + +export default class ContentMessageClient { + async broadcastSettingsChanged() { + let tabs = await browser.tabs.query({}); + for (let tab of tabs) { + browser.tabs.sendMessage(tab.id, { + type: messages.SETTINGS_CHANGED, + }); + } + } + + async getAddonEnabled(tabId) { + let { enabled } = await browser.tabs.sendMessage(tabId, { + type: messages.ADDON_ENABLED_QUERY, + }); + return enabled; + } + + toggleAddonEnabled(tabId) { + return browser.tabs.sendMessage(tabId, { + type: messages.ADDON_TOGGLE_ENABLED, + }); + } + + scrollTo(tabId, x, y) { + return browser.tabs.sendMessage(tabId, { + type: messages.TAB_SCROLL_TO, + x, + y, + }); + } +} diff --git a/src/background/infrastructures/ContentMessageListener.js b/src/background/infrastructures/ContentMessageListener.js new file mode 100644 index 0000000..1179a8c --- /dev/null +++ b/src/background/infrastructures/ContentMessageListener.js @@ -0,0 +1,141 @@ +import messages from '../../shared/messages'; +import CommandController from '../controllers/CommandController'; +import SettingController from '../controllers/SettingController'; +import FindController from '../controllers/FindController'; +import AddonEnabledController from '../controllers/AddonEnabledController'; +import LinkController from '../controllers/LinkController'; +import OperationController from '../controllers/OperationController'; +import MarkController from '../controllers/MarkController'; + +export default class ContentMessageListener { + constructor() { + this.settingController = new SettingController(); + this.commandController = new CommandController(); + this.findController = new FindController(); + this.addonEnabledController = new AddonEnabledController(); + this.linkController = new LinkController(); + this.backgroundOperationController = new OperationController(); + this.markController = new MarkController(); + + this.consolePorts = {}; + } + + run() { + browser.runtime.onMessage.addListener((message, sender) => { + try { + let ret = this.onMessage(message, sender); + if (!(ret instanceof Promise)) { + return {}; + } + return ret.catch((e) => { + return browser.tabs.sendMessage(sender.tab.id, { + type: messages.CONSOLE_SHOW_ERROR, + text: e.message, + }); + }); + } catch (e) { + return browser.tabs.sendMessage(sender.tab.id, { + type: messages.CONSOLE_SHOW_ERROR, + text: e.message, + }); + } + }); + browser.runtime.onConnect.addListener(this.onConnected.bind(this)); + } + + onMessage(message, sender) { + switch (message.type) { + case messages.CONSOLE_QUERY_COMPLETIONS: + return this.onConsoleQueryCompletions(message.text); + case messages.CONSOLE_ENTER_COMMAND: + return this.onConsoleEnterCommand(message.text); + case messages.SETTINGS_QUERY: + return this.onSettingsQuery(); + case messages.SETTINGS_RELOAD: + return this.onSettingsReload(); + case messages.FIND_GET_KEYWORD: + return this.onFindGetKeyword(); + case messages.FIND_SET_KEYWORD: + return this.onFindSetKeyword(message.keyword); + case messages.ADDON_ENABLED_RESPONSE: + return this.onAddonEnabledResponse(message.enabled); + case messages.OPEN_URL: + return this.onOpenUrl( + message.newTab, message.url, sender.tab.id, message.background); + case messages.BACKGROUND_OPERATION: + return this.onBackgroundOperation(message.operation); + case messages.MARK_SET_GLOBAL: + return this.onMarkSetGlobal(message.key, message.x, message.y); + case messages.MARK_JUMP_GLOBAL: + return this.onMarkJumpGlobal(message.key); + case messages.CONSOLE_FRAME_MESSAGE: + return this.onConsoleFrameMessage(sender.tab.id, message.message); + } + } + + async onConsoleQueryCompletions(line) { + let completions = await this.commandController.getCompletions(line); + return Promise.resolve(completions.serialize()); + } + + onConsoleEnterCommand(text) { + return this.commandController.exec(text); + } + + + onSettingsQuery() { + return this.settingController.getSetting(); + } + + onSettingsReload() { + return this.settingController.reload(); + } + + onFindGetKeyword() { + return this.findController.getKeyword(); + } + + onFindSetKeyword(keyword) { + return this.findController.setKeyword(keyword); + } + + onAddonEnabledResponse(enabled) { + return this.addonEnabledController.indicate(enabled); + } + + onOpenUrl(newTab, url, openerId, background) { + if (newTab) { + return this.linkController.openNewTab(url, openerId, background); + } + return this.linkController.openToTab(url, openerId); + } + + onBackgroundOperation(operation) { + return this.backgroundOperationController.exec(operation); + } + + onMarkSetGlobal(key, x, y) { + return this.markController.setGlobal(key, x, y); + } + + onMarkJumpGlobal(key) { + return this.markController.jumpGlobal(key); + } + + onConsoleFrameMessage(tabId, message) { + let port = this.consolePorts[tabId]; + if (!port) { + return; + } + port.postMessage(message); + } + + onConnected(port) { + if (port.name !== 'vimvixen-console') { + return; + } + + let id = port.sender.tab.id; + this.consolePorts[id] = port; + } +} diff --git a/src/background/infrastructures/MemoryStorage.js b/src/background/infrastructures/MemoryStorage.js new file mode 100644 index 0000000..3a7e4f2 --- /dev/null +++ b/src/background/infrastructures/MemoryStorage.js @@ -0,0 +1,19 @@ +const db = {}; + +export default class MemoryStorage { + set(name, value) { + let data = JSON.stringify(value); + if (typeof data === 'undefined') { + throw new Error('value is not serializable'); + } + db[name] = data; + } + + get(name) { + let data = db[name]; + if (!data) { + return undefined; + } + return JSON.parse(data); + } +} diff --git a/src/background/infrastructures/Notifier.js b/src/background/infrastructures/Notifier.js new file mode 100644 index 0000000..1eccc47 --- /dev/null +++ b/src/background/infrastructures/Notifier.js @@ -0,0 +1,23 @@ +const NOTIFICATION_ID = 'vimvixen-update'; + +export default class Notifier { + notify(title, message, onclick) { + const listener = (id) => { + if (id !== NOTIFICATION_ID) { + return; + } + + onclick(); + + browser.notifications.onClicked.removeListener(listener); + }; + browser.notifications.onClicked.addListener(listener); + + return browser.notifications.create(NOTIFICATION_ID, { + 'type': 'basic', + 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), + title, + message, + }); + } +} diff --git a/src/background/infrastructures/content-message-client.js b/src/background/infrastructures/content-message-client.js deleted file mode 100644 index 7e7e602..0000000 --- a/src/background/infrastructures/content-message-client.js +++ /dev/null @@ -1,33 +0,0 @@ -import messages from '../../shared/messages'; - -export default class ContentMessageClient { - async broadcastSettingsChanged() { - let tabs = await browser.tabs.query({}); - for (let tab of tabs) { - browser.tabs.sendMessage(tab.id, { - type: messages.SETTINGS_CHANGED, - }); - } - } - - async getAddonEnabled(tabId) { - let { enabled } = await browser.tabs.sendMessage(tabId, { - type: messages.ADDON_ENABLED_QUERY, - }); - return enabled; - } - - toggleAddonEnabled(tabId) { - return browser.tabs.sendMessage(tabId, { - type: messages.ADDON_TOGGLE_ENABLED, - }); - } - - scrollTo(tabId, x, y) { - return browser.tabs.sendMessage(tabId, { - type: messages.TAB_SCROLL_TO, - x, - y, - }); - } -} diff --git a/src/background/infrastructures/content-message-listener.js b/src/background/infrastructures/content-message-listener.js deleted file mode 100644 index aae07c0..0000000 --- a/src/background/infrastructures/content-message-listener.js +++ /dev/null @@ -1,141 +0,0 @@ -import messages from '../../shared/messages'; -import CommandController from '../controllers/command'; -import SettingController from '../controllers/setting'; -import FindController from '../controllers/find'; -import AddonEnabledController from '../controllers/addon-enabled'; -import LinkController from '../controllers/link'; -import OperationController from '../controllers/operation'; -import MarkController from '../controllers/mark'; - -export default class ContentMessageListener { - constructor() { - this.settingController = new SettingController(); - this.commandController = new CommandController(); - this.findController = new FindController(); - this.addonEnabledController = new AddonEnabledController(); - this.linkController = new LinkController(); - this.backgroundOperationController = new OperationController(); - this.markController = new MarkController(); - - this.consolePorts = {}; - } - - run() { - browser.runtime.onMessage.addListener((message, sender) => { - try { - let ret = this.onMessage(message, sender); - if (!(ret instanceof Promise)) { - return {}; - } - return ret.catch((e) => { - return browser.tabs.sendMessage(sender.tab.id, { - type: messages.CONSOLE_SHOW_ERROR, - text: e.message, - }); - }); - } catch (e) { - return browser.tabs.sendMessage(sender.tab.id, { - type: messages.CONSOLE_SHOW_ERROR, - text: e.message, - }); - } - }); - browser.runtime.onConnect.addListener(this.onConnected.bind(this)); - } - - onMessage(message, sender) { - switch (message.type) { - case messages.CONSOLE_QUERY_COMPLETIONS: - return this.onConsoleQueryCompletions(message.text); - case messages.CONSOLE_ENTER_COMMAND: - return this.onConsoleEnterCommand(message.text); - case messages.SETTINGS_QUERY: - return this.onSettingsQuery(); - case messages.SETTINGS_RELOAD: - return this.onSettingsReload(); - case messages.FIND_GET_KEYWORD: - return this.onFindGetKeyword(); - case messages.FIND_SET_KEYWORD: - return this.onFindSetKeyword(message.keyword); - case messages.ADDON_ENABLED_RESPONSE: - return this.onAddonEnabledResponse(message.enabled); - case messages.OPEN_URL: - return this.onOpenUrl( - message.newTab, message.url, sender.tab.id, message.background); - case messages.BACKGROUND_OPERATION: - return this.onBackgroundOperation(message.operation); - case messages.MARK_SET_GLOBAL: - return this.onMarkSetGlobal(message.key, message.x, message.y); - case messages.MARK_JUMP_GLOBAL: - return this.onMarkJumpGlobal(message.key); - case messages.CONSOLE_FRAME_MESSAGE: - return this.onConsoleFrameMessage(sender.tab.id, message.message); - } - } - - async onConsoleQueryCompletions(line) { - let completions = await this.commandController.getCompletions(line); - return Promise.resolve(completions.serialize()); - } - - onConsoleEnterCommand(text) { - return this.commandController.exec(text); - } - - - onSettingsQuery() { - return this.settingController.getSetting(); - } - - onSettingsReload() { - return this.settingController.reload(); - } - - onFindGetKeyword() { - return this.findController.getKeyword(); - } - - onFindSetKeyword(keyword) { - return this.findController.setKeyword(keyword); - } - - onAddonEnabledResponse(enabled) { - return this.addonEnabledController.indicate(enabled); - } - - onOpenUrl(newTab, url, openerId, background) { - if (newTab) { - return this.linkController.openNewTab(url, openerId, background); - } - return this.linkController.openToTab(url, openerId); - } - - onBackgroundOperation(operation) { - return this.backgroundOperationController.exec(operation); - } - - onMarkSetGlobal(key, x, y) { - return this.markController.setGlobal(key, x, y); - } - - onMarkJumpGlobal(key) { - return this.markController.jumpGlobal(key); - } - - onConsoleFrameMessage(tabId, message) { - let port = this.consolePorts[tabId]; - if (!port) { - return; - } - port.postMessage(message); - } - - onConnected(port) { - if (port.name !== 'vimvixen-console') { - return; - } - - let id = port.sender.tab.id; - this.consolePorts[id] = port; - } -} diff --git a/src/background/infrastructures/memory-storage.js b/src/background/infrastructures/memory-storage.js deleted file mode 100644 index 3a7e4f2..0000000 --- a/src/background/infrastructures/memory-storage.js +++ /dev/null @@ -1,19 +0,0 @@ -const db = {}; - -export default class MemoryStorage { - set(name, value) { - let data = JSON.stringify(value); - if (typeof data === 'undefined') { - throw new Error('value is not serializable'); - } - db[name] = data; - } - - get(name) { - let data = db[name]; - if (!data) { - return undefined; - } - return JSON.parse(data); - } -} diff --git a/src/background/infrastructures/notifier.js b/src/background/infrastructures/notifier.js deleted file mode 100644 index 1eccc47..0000000 --- a/src/background/infrastructures/notifier.js +++ /dev/null @@ -1,23 +0,0 @@ -const NOTIFICATION_ID = 'vimvixen-update'; - -export default class Notifier { - notify(title, message, onclick) { - const listener = (id) => { - if (id !== NOTIFICATION_ID) { - return; - } - - onclick(); - - browser.notifications.onClicked.removeListener(listener); - }; - browser.notifications.onClicked.addListener(listener); - - return browser.notifications.create(NOTIFICATION_ID, { - 'type': 'basic', - 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), - title, - message, - }); - } -} diff --git a/src/background/presenters/IndicatorPresenter.js b/src/background/presenters/IndicatorPresenter.js new file mode 100644 index 0000000..5737519 --- /dev/null +++ b/src/background/presenters/IndicatorPresenter.js @@ -0,0 +1,12 @@ +export default class IndicatorPresenter { + indicate(enabled) { + let path = enabled + ? 'resources/enabled_32x32.png' + : 'resources/disabled_32x32.png'; + return browser.browserAction.setIcon({ path }); + } + + onClick(listener) { + browser.browserAction.onClicked.addListener(listener); + } +} diff --git a/src/background/presenters/TabPresenter.js b/src/background/presenters/TabPresenter.js new file mode 100644 index 0000000..744be39 --- /dev/null +++ b/src/background/presenters/TabPresenter.js @@ -0,0 +1,102 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; + +const CURRENT_SELECTED_KEY = 'tabs.current.selected'; +const LAST_SELECTED_KEY = 'tabs.last.selected'; + +export default class TabPresenter { + open(url, tabId) { + return browser.tabs.update(tabId, { url }); + } + + create(url, opts) { + return browser.tabs.create({ url, ...opts }); + } + + async getCurrent() { + let tabs = await browser.tabs.query({ + active: true, currentWindow: true + }); + return tabs[0]; + } + + getAll() { + return browser.tabs.query({ currentWindow: true }); + } + + async getLastSelectedId() { + let cache = new MemoryStorage(); + let tabId = await cache.get(LAST_SELECTED_KEY); + if (tabId === null || typeof tabId === 'undefined') { + return; + } + return tabId; + } + + async getByKeyword(keyword, excludePinned = false) { + let tabs = await browser.tabs.query({ currentWindow: true }); + return tabs.filter((t) => { + return t.url.toLowerCase().includes(keyword.toLowerCase()) || + t.title && t.title.toLowerCase().includes(keyword.toLowerCase()); + }).filter((t) => { + return !(excludePinned && t.pinned); + }); + } + + select(tabId) { + return browser.tabs.update(tabId, { active: true }); + } + + remove(ids) { + return browser.tabs.remove(ids); + } + + async reopen() { + 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); + } + + reload(tabId, cache) { + return browser.tabs.reload(tabId, { bypassCache: cache }); + } + + setPinned(tabId, pinned) { + return browser.tabs.update(tabId, { pinned }); + } + + duplicate(id) { + return browser.tabs.duplicate(id); + } + + getZoom(tabId) { + return browser.tabs.getZoom(tabId); + } + + setZoom(tabId, factor) { + return browser.tabs.setZoom(tabId, factor); + } + + onSelected(listener) { + browser.tabs.onActivated.addListener(listener); + } +} + +let tabPresenter = new TabPresenter(); +tabPresenter.onSelected((tab) => { + let cache = new MemoryStorage(); + + let lastId = cache.get(CURRENT_SELECTED_KEY); + if (lastId) { + cache.set(LAST_SELECTED_KEY, lastId); + } + cache.set(CURRENT_SELECTED_KEY, tab.tabId); +}); diff --git a/src/background/presenters/WindowPresenter.js b/src/background/presenters/WindowPresenter.js new file mode 100644 index 0000000..a82c4a2 --- /dev/null +++ b/src/background/presenters/WindowPresenter.js @@ -0,0 +1,5 @@ +export default class WindowPresenter { + create(url) { + return browser.windows.create({ url }); + } +} diff --git a/src/background/presenters/console.js b/src/background/presenters/console.js deleted file mode 100644 index 8259238..0000000 --- a/src/background/presenters/console.js +++ /dev/null @@ -1,36 +0,0 @@ -import messages from '../../shared/messages'; - -export default class ConsolePresenter { - showCommand(tabId, command) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_SHOW_COMMAND, - command, - }); - } - - showFind(tabId) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_SHOW_FIND - }); - } - - showInfo(tabId, message) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_SHOW_INFO, - text: message, - }); - } - - showError(tabId, message) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_SHOW_ERROR, - text: message, - }); - } - - hide(tabId) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_HIDE, - }); - } -} diff --git a/src/background/presenters/indicator.js b/src/background/presenters/indicator.js deleted file mode 100644 index 5737519..0000000 --- a/src/background/presenters/indicator.js +++ /dev/null @@ -1,12 +0,0 @@ -export default class IndicatorPresenter { - indicate(enabled) { - let path = enabled - ? 'resources/enabled_32x32.png' - : 'resources/disabled_32x32.png'; - return browser.browserAction.setIcon({ path }); - } - - onClick(listener) { - browser.browserAction.onClicked.addListener(listener); - } -} diff --git a/src/background/presenters/tab.js b/src/background/presenters/tab.js deleted file mode 100644 index 69cae1c..0000000 --- a/src/background/presenters/tab.js +++ /dev/null @@ -1,102 +0,0 @@ -import MemoryStorage from '../infrastructures/memory-storage'; - -const CURRENT_SELECTED_KEY = 'tabs.current.selected'; -const LAST_SELECTED_KEY = 'tabs.last.selected'; - -export default class TabPresenter { - open(url, tabId) { - return browser.tabs.update(tabId, { url }); - } - - create(url, opts) { - return browser.tabs.create({ url, ...opts }); - } - - async getCurrent() { - let tabs = await browser.tabs.query({ - active: true, currentWindow: true - }); - return tabs[0]; - } - - getAll() { - return browser.tabs.query({ currentWindow: true }); - } - - async getLastSelectedId() { - let cache = new MemoryStorage(); - let tabId = await cache.get(LAST_SELECTED_KEY); - if (tabId === null || typeof tabId === 'undefined') { - return; - } - return tabId; - } - - async getByKeyword(keyword, excludePinned = false) { - let tabs = await browser.tabs.query({ currentWindow: true }); - return tabs.filter((t) => { - return t.url.toLowerCase().includes(keyword.toLowerCase()) || - t.title && t.title.toLowerCase().includes(keyword.toLowerCase()); - }).filter((t) => { - return !(excludePinned && t.pinned); - }); - } - - select(tabId) { - return browser.tabs.update(tabId, { active: true }); - } - - remove(ids) { - return browser.tabs.remove(ids); - } - - async reopen() { - 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); - } - - reload(tabId, cache) { - return browser.tabs.reload(tabId, { bypassCache: cache }); - } - - setPinned(tabId, pinned) { - return browser.tabs.update(tabId, { pinned }); - } - - duplicate(id) { - return browser.tabs.duplicate(id); - } - - getZoom(tabId) { - return browser.tabs.getZoom(tabId); - } - - setZoom(tabId, factor) { - return browser.tabs.setZoom(tabId, factor); - } - - onSelected(listener) { - browser.tabs.onActivated.addListener(listener); - } -} - -let tabPresenter = new TabPresenter(); -tabPresenter.onSelected((tab) => { - let cache = new MemoryStorage(); - - let lastId = cache.get(CURRENT_SELECTED_KEY); - if (lastId) { - cache.set(LAST_SELECTED_KEY, lastId); - } - cache.set(CURRENT_SELECTED_KEY, tab.tabId); -}); diff --git a/src/background/presenters/window.js b/src/background/presenters/window.js deleted file mode 100644 index a82c4a2..0000000 --- a/src/background/presenters/window.js +++ /dev/null @@ -1,5 +0,0 @@ -export default class WindowPresenter { - create(url) { - return browser.windows.create({ url }); - } -} diff --git a/src/background/repositories/BookmarkRepository.js b/src/background/repositories/BookmarkRepository.js new file mode 100644 index 0000000..99f7ec4 --- /dev/null +++ b/src/background/repositories/BookmarkRepository.js @@ -0,0 +1,13 @@ +export default class BookmarkRepository { + async create(title, url) { + let item = await browser.bookmarks.create({ + type: 'bookmark', + title, + url, + }); + if (!item) { + throw new Error('Could not create a bookmark'); + } + return item; + } +} diff --git a/src/background/repositories/CompletionsRepository.js b/src/background/repositories/CompletionsRepository.js new file mode 100644 index 0000000..1318d36 --- /dev/null +++ b/src/background/repositories/CompletionsRepository.js @@ -0,0 +1,31 @@ +export default class CompletionsRepository { + async queryBookmarks(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:'; + }); + } + + queryHistories(keywords) { + return browser.history.search({ + text: keywords, + startTime: 0, + }); + } + + async queryTabs(keywords, excludePinned) { + let tabs = await browser.tabs.query({ currentWindow: true }); + return tabs.filter((t) => { + return t.url.toLowerCase().includes(keywords.toLowerCase()) || + t.title && t.title.toLowerCase().includes(keywords.toLowerCase()); + }).filter((t) => { + return !(excludePinned && t.pinned); + }); + } +} diff --git a/src/background/repositories/FindRepository.js b/src/background/repositories/FindRepository.js new file mode 100644 index 0000000..74ec914 --- /dev/null +++ b/src/background/repositories/FindRepository.js @@ -0,0 +1,19 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; + +const FIND_KEYWORD_KEY = 'find-keyword'; + +export default class FindRepository { + constructor() { + this.cache = new MemoryStorage(); + } + + getKeyword() { + return Promise.resolve(this.cache.get(FIND_KEYWORD_KEY)); + } + + setKeyword(keyword) { + this.cache.set(FIND_KEYWORD_KEY, keyword); + return Promise.resolve(); + } +} + diff --git a/src/background/repositories/MarkRepository.js b/src/background/repositories/MarkRepository.js new file mode 100644 index 0000000..282c712 --- /dev/null +++ b/src/background/repositories/MarkRepository.js @@ -0,0 +1,33 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; +import GlobalMark from '../domains/GlobalMark'; + +const MARK_KEY = 'mark'; + +export default class MarkRepository { + constructor() { + this.cache = new MemoryStorage(); + } + + getMark(key) { + let marks = this.getOrEmptyMarks(); + let data = marks[key]; + if (!data) { + return Promise.resolve(undefined); + } + let mark = new GlobalMark(data.tabId, data.url, data.x, data.y); + return Promise.resolve(mark); + } + + setMark(key, mark) { + let marks = this.getOrEmptyMarks(); + marks[key] = { tabId: mark.tabId, url: mark.url, x: mark.x, y: mark.y }; + this.cache.set(MARK_KEY, marks); + + return Promise.resolve(); + } + + getOrEmptyMarks() { + return this.cache.get(MARK_KEY) || {}; + } +} + diff --git a/src/background/repositories/PersistentSettingRepository.js b/src/background/repositories/PersistentSettingRepository.js new file mode 100644 index 0000000..639bcd1 --- /dev/null +++ b/src/background/repositories/PersistentSettingRepository.js @@ -0,0 +1,16 @@ +import Setting from '../domains/Setting'; + +export default class SettingRepository { + save(settings) { + return browser.storage.local.set({ settings: settings.serialize() }); + } + + async load() { + let { settings } = await browser.storage.local.get('settings'); + if (!settings) { + return null; + } + return Setting.deserialize(settings); + } +} + diff --git a/src/background/repositories/SettingRepository.js b/src/background/repositories/SettingRepository.js new file mode 100644 index 0000000..c4667a9 --- /dev/null +++ b/src/background/repositories/SettingRepository.js @@ -0,0 +1,23 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; + +const CACHED_SETTING_KEY = 'setting'; + +export default class SettingRepository { + constructor() { + this.cache = new MemoryStorage(); + } + + get() { + return Promise.resolve(this.cache.get(CACHED_SETTING_KEY)); + } + + update(value) { + return this.cache.set(CACHED_SETTING_KEY, value); + } + + async setProperty(name, value) { + let current = await this.get(); + current.properties[name] = value; + return this.update(current); + } +} diff --git a/src/background/repositories/VersionRepository.js b/src/background/repositories/VersionRepository.js new file mode 100644 index 0000000..4c71d05 --- /dev/null +++ b/src/background/repositories/VersionRepository.js @@ -0,0 +1,10 @@ +export default class VersionRepository { + async get() { + let { version } = await browser.storage.local.get('version'); + return version; + } + + update(version) { + return browser.storage.local.set({ version }); + } +} diff --git a/src/background/repositories/bookmark.js b/src/background/repositories/bookmark.js deleted file mode 100644 index 99f7ec4..0000000 --- a/src/background/repositories/bookmark.js +++ /dev/null @@ -1,13 +0,0 @@ -export default class BookmarkRepository { - async create(title, url) { - let item = await browser.bookmarks.create({ - type: 'bookmark', - title, - url, - }); - if (!item) { - throw new Error('Could not create a bookmark'); - } - return item; - } -} diff --git a/src/background/repositories/completions.js b/src/background/repositories/completions.js deleted file mode 100644 index 1318d36..0000000 --- a/src/background/repositories/completions.js +++ /dev/null @@ -1,31 +0,0 @@ -export default class CompletionsRepository { - async queryBookmarks(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:'; - }); - } - - queryHistories(keywords) { - return browser.history.search({ - text: keywords, - startTime: 0, - }); - } - - async queryTabs(keywords, excludePinned) { - let tabs = await browser.tabs.query({ currentWindow: true }); - return tabs.filter((t) => { - return t.url.toLowerCase().includes(keywords.toLowerCase()) || - t.title && t.title.toLowerCase().includes(keywords.toLowerCase()); - }).filter((t) => { - return !(excludePinned && t.pinned); - }); - } -} diff --git a/src/background/repositories/find.js b/src/background/repositories/find.js deleted file mode 100644 index 6656c58..0000000 --- a/src/background/repositories/find.js +++ /dev/null @@ -1,19 +0,0 @@ -import MemoryStorage from '../infrastructures/memory-storage'; - -const FIND_KEYWORD_KEY = 'find-keyword'; - -export default class FindRepository { - constructor() { - this.cache = new MemoryStorage(); - } - - getKeyword() { - return Promise.resolve(this.cache.get(FIND_KEYWORD_KEY)); - } - - setKeyword(keyword) { - this.cache.set(FIND_KEYWORD_KEY, keyword); - return Promise.resolve(); - } -} - diff --git a/src/background/repositories/mark.js b/src/background/repositories/mark.js deleted file mode 100644 index 339a660..0000000 --- a/src/background/repositories/mark.js +++ /dev/null @@ -1,33 +0,0 @@ -import MemoryStorage from '../infrastructures/memory-storage'; -import GlobalMark from 'background/domains/global-mark'; - -const MARK_KEY = 'mark'; - -export default class MarkRepository { - constructor() { - this.cache = new MemoryStorage(); - } - - getMark(key) { - let marks = this.getOrEmptyMarks(); - let data = marks[key]; - if (!data) { - return Promise.resolve(undefined); - } - let mark = new GlobalMark(data.tabId, data.url, data.x, data.y); - return Promise.resolve(mark); - } - - setMark(key, mark) { - let marks = this.getOrEmptyMarks(); - marks[key] = { tabId: mark.tabId, url: mark.url, x: mark.x, y: mark.y }; - this.cache.set(MARK_KEY, marks); - - return Promise.resolve(); - } - - getOrEmptyMarks() { - return this.cache.get(MARK_KEY) || {}; - } -} - diff --git a/src/background/repositories/persistent-setting.js b/src/background/repositories/persistent-setting.js deleted file mode 100644 index 247ea6f..0000000 --- a/src/background/repositories/persistent-setting.js +++ /dev/null @@ -1,16 +0,0 @@ -import Setting from '../domains/setting'; - -export default class SettingRepository { - save(settings) { - return browser.storage.local.set({ settings: settings.serialize() }); - } - - async load() { - let { settings } = await browser.storage.local.get('settings'); - if (!settings) { - return null; - } - return Setting.deserialize(settings); - } -} - diff --git a/src/background/repositories/setting.js b/src/background/repositories/setting.js deleted file mode 100644 index 6d48525..0000000 --- a/src/background/repositories/setting.js +++ /dev/null @@ -1,23 +0,0 @@ -import MemoryStorage from '../infrastructures/memory-storage'; - -const CACHED_SETTING_KEY = 'setting'; - -export default class SettingRepository { - constructor() { - this.cache = new MemoryStorage(); - } - - get() { - return Promise.resolve(this.cache.get(CACHED_SETTING_KEY)); - } - - update(value) { - return this.cache.set(CACHED_SETTING_KEY, value); - } - - async setProperty(name, value) { - let current = await this.get(); - current.properties[name] = value; - return this.update(current); - } -} diff --git a/src/background/repositories/version.js b/src/background/repositories/version.js deleted file mode 100644 index 4c71d05..0000000 --- a/src/background/repositories/version.js +++ /dev/null @@ -1,10 +0,0 @@ -export default class VersionRepository { - async get() { - let { version } = await browser.storage.local.get('version'); - return version; - } - - update(version) { - return browser.storage.local.set({ version }); - } -} diff --git a/src/background/usecases/AddonEnabledUseCase.js b/src/background/usecases/AddonEnabledUseCase.js new file mode 100644 index 0000000..bb2c347 --- /dev/null +++ b/src/background/usecases/AddonEnabledUseCase.js @@ -0,0 +1,29 @@ +import IndicatorPresenter from '../presenters/IndicatorPresenter'; +import TabPresenter from '../presenters/TabPresenter'; +import ContentMessageClient from '../infrastructures/ContentMessageClient'; + +export default class AddonEnabledUseCase { + constructor() { + this.indicatorPresentor = new IndicatorPresenter(); + + this.indicatorPresentor.onClick(tab => this.onIndicatorClick(tab.id)); + + this.tabPresenter = new TabPresenter(); + this.tabPresenter.onSelected(info => this.onTabSelected(info.tabId)); + + this.contentMessageClient = new ContentMessageClient(); + } + + indicate(enabled) { + return this.indicatorPresentor.indicate(enabled); + } + + onIndicatorClick(tabId) { + return this.contentMessageClient.toggleAddonEnabled(tabId); + } + + async onTabSelected(tabId) { + let enabled = await this.contentMessageClient.getAddonEnabled(tabId); + return this.indicatorPresentor.indicate(enabled); + } +} diff --git a/src/background/usecases/CommandUseCase.js b/src/background/usecases/CommandUseCase.js new file mode 100644 index 0000000..9ec46fe --- /dev/null +++ b/src/background/usecases/CommandUseCase.js @@ -0,0 +1,125 @@ +import * as parsers from './parsers'; +import * as urls from '../../shared/urls'; +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 { + constructor() { + this.tabPresenter = new TabPresenter(); + this.windowPresenter = new WindowPresenter(); + this.settingRepository = new SettingRepository(); + this.bookmarkRepository = new BookmarkRepository(); + this.consoleClient = new ConsoleClient(); + + this.contentMessageClient = new ContentMessageClient(); + } + + async open(keywords) { + let url = await this.urlOrSearch(keywords); + return this.tabPresenter.open(url); + } + + async tabopen(keywords) { + let url = await this.urlOrSearch(keywords); + return this.tabPresenter.create(url); + } + + async winopen(keywords) { + let url = await this.urlOrSearch(keywords); + return this.windowPresenter.create(url); + } + + // eslint-disable-next-line max-statements + async buffer(keywords) { + if (keywords.length === 0) { + return; + } + + if (!isNaN(keywords)) { + let tabs = await this.tabPresenter.getAll(); + let index = parseInt(keywords, 10) - 1; + if (index < 0 || tabs.length <= index) { + throw new RangeError(`tab ${index + 1} does not exist`); + } + return this.tabPresenter.select(tabs[index].id); + } else if (keywords.trim() === '%') { + // Select current window + return; + } else if (keywords.trim() === '#') { + // Select last selected window + let lastId = await this.tabPresenter.getLastSelectedId(); + if (typeof lastId === 'undefined' || lastId === null) { + throw new Error('No last selected tab'); + } + return this.tabPresenter.select(lastId); + } + + let current = await this.tabPresenter.getCurrent(); + let tabs = await this.tabPresenter.getByKeyword(keywords); + if (tabs.length === 0) { + throw new RangeError('No matching buffer for ' + keywords); + } + for (let tab of tabs) { + if (tab.index > current.index) { + return this.tabPresenter.select(tab.id); + } + } + return this.tabPresenter.select(tabs[0].id); + } + + async bdelete(force, keywords) { + let excludePinned = !force; + let tabs = await this.tabPresenter.getByKeyword(keywords, excludePinned); + if (tabs.length === 0) { + throw new Error('No matching buffer for ' + keywords); + } else if (tabs.length > 1) { + throw new Error('More than one match for ' + keywords); + } + return this.tabPresenter.remove([tabs[0].id]); + } + + async bdeletes(force, keywords) { + let excludePinned = !force; + let tabs = await this.tabPresenter.getByKeyword(keywords, excludePinned); + let ids = tabs.map(tab => tab.id); + return this.tabPresenter.remove(ids); + } + + async quit() { + let tab = await this.tabPresenter.getCurrent(); + return this.tabPresenter.remove([tab.id]); + } + + async quitAll() { + let tabs = await this.tabPresenter.getAll(); + let ids = tabs.map(tab => tab.id); + this.tabPresenter.remove(ids); + } + + async addbookmark(title) { + let tab = await this.tabPresenter.getCurrent(); + let item = await this.bookmarkRepository.create(title, tab.url); + let message = 'Saved current page: ' + item.url; + return this.consoleClient.showInfo(tab.id, message); + } + + async set(keywords) { + if (keywords.length === 0) { + return; + } + let [name, value] = parsers.parseSetOption(keywords, properties.types); + await this.settingRepository.setProperty(name, value); + + return this.contentMessageClient.broadcastSettingsChanged(); + } + + async urlOrSearch(keywords) { + let settings = await this.settingRepository.get(); + return urls.searchUrl(keywords, settings.search); + } +} diff --git a/src/background/usecases/CompletionsUseCase.js b/src/background/usecases/CompletionsUseCase.js new file mode 100644 index 0000000..7dc30ac --- /dev/null +++ b/src/background/usecases/CompletionsUseCase.js @@ -0,0 +1,205 @@ +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/SettingRepository'; +import TabPresenter from '../presenters/TabPresenter'; +import * as properties from '../../shared/settings/properties'; + +const COMPLETION_ITEM_LIMIT = 10; + +export default class CompletionsUseCase { + constructor() { + this.tabPresenter = new TabPresenter(); + this.completionsRepository = new CompletionsRepository(); + this.settingRepository = new SettingRepository(); + } + + queryConsoleCommand(prefix) { + let keys = Object.keys(CommandDocs); + let items = keys + .filter(name => name.startsWith(prefix)) + .map(name => ({ + caption: name, + content: name, + url: CommandDocs[name], + })); + + if (items.length === 0) { + return Promise.resolve(Completions.empty()); + } + return Promise.resolve( + new Completions([new CompletionGroup('Console Command', items)]) + ); + } + + async queryOpen(name, keywords) { + let settings = await this.settingRepository.get(); + let groups = []; + + let complete = settings.properties.complete || properties.defaults.complete; + for (let c of complete) { + if (c === 's') { + // eslint-disable-next-line no-await-in-loop + let engines = await this.querySearchEngineItems(name, keywords); + if (engines.length > 0) { + groups.push(new CompletionGroup('Search Engines', engines)); + } + } else if (c === 'h') { + // eslint-disable-next-line no-await-in-loop + let histories = await this.queryHistoryItems(name, keywords); + if (histories.length > 0) { + groups.push(new CompletionGroup('History', histories)); + } + } else if (c === 'b') { + // eslint-disable-next-line no-await-in-loop + let bookmarks = await this.queryBookmarkItems(name, keywords); + if (bookmarks.length > 0) { + groups.push(new CompletionGroup('Bookmarks', bookmarks)); + } + } + } + return new Completions(groups); + } + + // eslint-disable-next-line max-statements + async queryBuffer(name, keywords) { + let lastId = await this.tabPresenter.getLastSelectedId(); + let trimmed = keywords.trim(); + let tabs = []; + if (trimmed.length > 0 && !isNaN(trimmed)) { + let all = await this.tabPresenter.getAll(); + let index = parseInt(trimmed, 10) - 1; + if (index >= 0 && index < all.length) { + tabs = [all[index]]; + } + } else if (trimmed === '%') { + let all = await this.tabPresenter.getAll(); + let tab = all.find(t => t.active); + tabs = [tab]; + } else if (trimmed === '#') { + if (typeof lastId !== 'undefined' && lastId !== null) { + let all = await this.tabPresenter.getAll(); + let tab = all.find(t => t.id === lastId); + tabs = [tab]; + } + } else { + tabs = await this.completionsRepository.queryTabs(keywords, false); + } + const flag = (tab) => { + if (tab.active) { + return '%'; + } else if (tab.id === lastId) { + return '#'; + } + return ' '; + }; + let items = tabs.map(tab => new CompletionItem({ + caption: tab.index + 1 + ': ' + flag(tab) + ' ' + tab.title, + content: name + ' ' + tab.title, + url: tab.url, + icon: tab.favIconUrl + })); + if (items.length === 0) { + return Promise.resolve(Completions.empty()); + } + return new Completions([new CompletionGroup('Buffers', items)]); + } + + queryBdelete(name, keywords) { + return this.queryTabs(name, true, keywords); + } + + queryBdeleteForce(name, keywords) { + return this.queryTabs(name, false, keywords); + } + + querySet(name, keywords) { + let items = Object.keys(properties.docs).map((key) => { + if (properties.types[key] === 'boolean') { + return [ + new CompletionItem({ + caption: key, + content: name + ' ' + key, + url: 'Enable ' + properties.docs[key], + }), + new CompletionItem({ + caption: 'no' + key, + content: name + ' no' + key, + url: 'Disable ' + properties.docs[key], + }), + ]; + } + return [ + new CompletionItem({ + caption: key, + content: name + ' ' + key, + url: 'Set ' + properties.docs[key], + }) + ]; + }); + items = items.reduce((acc, val) => acc.concat(val), []); + items = items.filter((item) => { + return item.caption.startsWith(keywords); + }); + if (items.length === 0) { + return Promise.resolve(Completions.empty()); + } + return Promise.resolve( + new Completions([new CompletionGroup('Properties', items)]) + ); + } + + async queryTabs(name, excludePinned, args) { + let tabs = await this.completionsRepository.queryTabs(args, excludePinned); + let items = tabs.map(tab => new CompletionItem({ + caption: tab.title, + content: name + ' ' + tab.title, + url: tab.url, + icon: tab.favIconUrl + })); + if (items.length === 0) { + return Promise.resolve(Completions.empty()); + } + return new Completions([new CompletionGroup('Buffers', items)]); + } + + async querySearchEngineItems(name, keywords) { + let settings = await this.settingRepository.get(); + let engines = Object.keys(settings.search.engines) + .filter(key => key.startsWith(keywords)); + return engines.map(key => new CompletionItem({ + caption: key, + content: name + ' ' + key, + })); + } + + async queryHistoryItems(name, keywords) { + let histories = await this.completionsRepository.queryHistories(keywords); + histories = [histories] + .map(filters.filterBlankTitle) + .map(filters.filterHttp) + .map(filters.filterByTailingSlash) + .map(pages => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT)) + .map(pages => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0] + .sort((x, y) => x.visitCount < y.visitCount) + .slice(0, COMPLETION_ITEM_LIMIT); + return histories.map(page => new CompletionItem({ + caption: page.title, + content: name + ' ' + page.url, + url: page.url + })); + } + + async queryBookmarkItems(name, keywords) { + let bookmarks = await this.completionsRepository.queryBookmarks(keywords); + return bookmarks.slice(0, COMPLETION_ITEM_LIMIT) + .map(page => new CompletionItem({ + caption: page.title, + content: name + ' ' + page.url, + url: page.url + })); + } +} diff --git a/src/background/usecases/FindUseCase.js b/src/background/usecases/FindUseCase.js new file mode 100644 index 0000000..d6b0323 --- /dev/null +++ b/src/background/usecases/FindUseCase.js @@ -0,0 +1,15 @@ +import FindRepository from '../repositories/FindRepository'; + +export default class FindUseCase { + constructor() { + this.findRepository = new FindRepository(); + } + + getKeyword() { + return this.findRepository.getKeyword(); + } + + setKeyword(keyword) { + return this.findRepository.setKeyword(keyword); + } +} diff --git a/src/background/usecases/LinkUseCase.js b/src/background/usecases/LinkUseCase.js new file mode 100644 index 0000000..89412c5 --- /dev/null +++ b/src/background/usecases/LinkUseCase.js @@ -0,0 +1,19 @@ +import SettingRepository from '../repositories/SettingRepository'; +import TabPresenter from '../presenters/TabPresenter'; + +export default class LinkUseCase { + constructor() { + this.settingRepository = new SettingRepository(); + this.tabPresenter = new TabPresenter(); + } + + openToTab(url, tabId) { + return this.tabPresenter.open(url, tabId); + } + + openNewTab(url, openerId, background) { + return this.tabPresenter.create(url, { + openerTabId: openerId, active: !background + }); + } +} diff --git a/src/background/usecases/MarkUseCase.js b/src/background/usecases/MarkUseCase.js new file mode 100644 index 0000000..39c796b --- /dev/null +++ b/src/background/usecases/MarkUseCase.js @@ -0,0 +1,39 @@ +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 MarkUseCase { + constructor() { + this.tabPresenter = new TabPresenter(); + this.markRepository = new MarkRepository(); + this.consoleClient = new ConsoleClient(); + this.contentMessageClient = new ContentMessageClient(); + } + + async setGlobal(key, x, y) { + let tab = await this.tabPresenter.getCurrent(); + let mark = new GlobalMark(tab.id, tab.url, x, y); + return this.markRepository.setMark(key, mark); + } + + async jumpGlobal(key) { + let current = await this.tabPresenter.getCurrent(); + + let mark = await this.markRepository.getMark(key); + if (!mark) { + return this.consoleClient.showError(current.id, 'Mark is not set'); + } + + return this.contentMessageClient.scrollTo( + mark.tabId, mark.x, mark.y + ).then(() => { + return this.tabPresenter.select(mark.tabId); + }).catch(async() => { + let tab = await this.tabPresenter.create(mark.url); + let mark2 = new GlobalMark(tab.id, mark.url, mark.x, mark.y); + return this.markRepository.setMark(key, mark2); + }); + } +} diff --git a/src/background/usecases/OperationUseCase.js b/src/background/usecases/OperationUseCase.js new file mode 100644 index 0000000..1d75d1a --- /dev/null +++ b/src/background/usecases/OperationUseCase.js @@ -0,0 +1,208 @@ +import TabPresenter from '../presenters/TabPresenter'; +import ConsoleClient from '../infrastructures/ConsoleClient'; +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 OperationUseCase { + constructor() { + this.tabPresenter = new TabPresenter(); + this.consoleClient = new ConsoleClient(); + } + + 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.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 findStart() { + let tab = await this.tabPresenter.getCurrent(); + return this.consoleClient.showFind(tab.id); + } + + async hideConsole() { + let tab = await this.tabPresenter.getCurrent(); + return this.consoleClient.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); + } + } +} + diff --git a/src/background/usecases/SettingUseCase.js b/src/background/usecases/SettingUseCase.js new file mode 100644 index 0000000..fe3ca5d --- /dev/null +++ b/src/background/usecases/SettingUseCase.js @@ -0,0 +1,32 @@ +import Setting from '../domains/Setting'; +// eslint-disable-next-line max-len +import PersistentSettingRepository from '../repositories/PersistentSettingRepository'; +import SettingRepository from '../repositories/SettingRepository'; + +export default class SettingUseCase { + constructor() { + this.persistentSettingRepository = new PersistentSettingRepository(); + this.settingRepository = new SettingRepository(); + } + + save(settings) { + this.persistentSettingRepository.save(settings); + } + + get() { + return this.settingRepository.get(); + } + + async reload() { + let settings = await this.persistentSettingRepository.load(); + if (!settings) { + settings = Setting.defaultSettings(); + } + + let value = settings.value(); + + this.settingRepository.update(value); + + return value; + } +} diff --git a/src/background/usecases/VersionUseCase.js b/src/background/usecases/VersionUseCase.js new file mode 100644 index 0000000..704c808 --- /dev/null +++ b/src/background/usecases/VersionUseCase.js @@ -0,0 +1,41 @@ +import manifest from '../../../manifest.json'; +import VersionRepository from '../repositories/VersionRepository'; +import TabPresenter from '../presenters/TabPresenter'; +import Notifier from '../infrastructures/Notifier'; + +export default class VersionUseCase { + constructor() { + this.versionRepository = new VersionRepository(); + this.tabPresenter = new TabPresenter(); + this.notifier = new Notifier(); + } + + async notifyIfUpdated() { + if (!await this.checkUpdated()) { + return; + } + + let title = 'Vim Vixen ' + manifest.version + ' has been installed'; + let message = 'Click here to see release notes'; + this.notifier.notify(title, message, () => { + let url = this.releaseNoteUrl(manifest.version); + this.tabPresenter.create(url); + }); + this.versionRepository.update(manifest.version); + } + + async checkUpdated() { + let prev = await this.versionRepository.get(); + if (!prev) { + return true; + } + return manifest.version !== prev; + } + + releaseNoteUrl(version) { + if (version) { + return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; + } + return 'https://github.com/ueokande/vim-vixen/releases/'; + } +} diff --git a/src/background/usecases/addon-enabled.js b/src/background/usecases/addon-enabled.js deleted file mode 100644 index d83192f..0000000 --- a/src/background/usecases/addon-enabled.js +++ /dev/null @@ -1,29 +0,0 @@ -import IndicatorPresenter from '../presenters/indicator'; -import TabPresenter from '../presenters/tab'; -import ContentMessageClient from '../infrastructures/content-message-client'; - -export default class AddonEnabledInteractor { - constructor() { - this.indicatorPresentor = new IndicatorPresenter(); - - this.indicatorPresentor.onClick(tab => this.onIndicatorClick(tab.id)); - - this.tabPresenter = new TabPresenter(); - this.tabPresenter.onSelected(info => this.onTabSelected(info.tabId)); - - this.contentMessageClient = new ContentMessageClient(); - } - - indicate(enabled) { - return this.indicatorPresentor.indicate(enabled); - } - - onIndicatorClick(tabId) { - return this.contentMessageClient.toggleAddonEnabled(tabId); - } - - async onTabSelected(tabId) { - let enabled = await this.contentMessageClient.getAddonEnabled(tabId); - return this.indicatorPresentor.indicate(enabled); - } -} diff --git a/src/background/usecases/command.js b/src/background/usecases/command.js deleted file mode 100644 index 0d21041..0000000 --- a/src/background/usecases/command.js +++ /dev/null @@ -1,125 +0,0 @@ -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 * as properties from 'shared/settings/properties'; - -export default class CommandIndicator { - constructor() { - this.tabPresenter = new TabPresenter(); - this.windowPresenter = new WindowPresenter(); - this.settingRepository = new SettingRepository(); - this.bookmarkRepository = new BookmarkRepository(); - this.consolePresenter = new ConsolePresenter(); - - this.contentMessageClient = new ContentMessageClient(); - } - - async open(keywords) { - let url = await this.urlOrSearch(keywords); - return this.tabPresenter.open(url); - } - - async tabopen(keywords) { - let url = await this.urlOrSearch(keywords); - return this.tabPresenter.create(url); - } - - async winopen(keywords) { - let url = await this.urlOrSearch(keywords); - return this.windowPresenter.create(url); - } - - // eslint-disable-next-line max-statements - async buffer(keywords) { - if (keywords.length === 0) { - return; - } - - if (!isNaN(keywords)) { - let tabs = await this.tabPresenter.getAll(); - let index = parseInt(keywords, 10) - 1; - if (index < 0 || tabs.length <= index) { - throw new RangeError(`tab ${index + 1} does not exist`); - } - return this.tabPresenter.select(tabs[index].id); - } else if (keywords.trim() === '%') { - // Select current window - return; - } else if (keywords.trim() === '#') { - // Select last selected window - let lastId = await this.tabPresenter.getLastSelectedId(); - if (typeof lastId === 'undefined' || lastId === null) { - throw new Error('No last selected tab'); - } - return this.tabPresenter.select(lastId); - } - - let current = await this.tabPresenter.getCurrent(); - let tabs = await this.tabPresenter.getByKeyword(keywords); - if (tabs.length === 0) { - throw new RangeError('No matching buffer for ' + keywords); - } - for (let tab of tabs) { - if (tab.index > current.index) { - return this.tabPresenter.select(tab.id); - } - } - return this.tabPresenter.select(tabs[0].id); - } - - async bdelete(force, keywords) { - let excludePinned = !force; - let tabs = await this.tabPresenter.getByKeyword(keywords, excludePinned); - if (tabs.length === 0) { - throw new Error('No matching buffer for ' + keywords); - } else if (tabs.length > 1) { - throw new Error('More than one match for ' + keywords); - } - return this.tabPresenter.remove([tabs[0].id]); - } - - async bdeletes(force, keywords) { - let excludePinned = !force; - let tabs = await this.tabPresenter.getByKeyword(keywords, excludePinned); - let ids = tabs.map(tab => tab.id); - return this.tabPresenter.remove(ids); - } - - async quit() { - let tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.remove([tab.id]); - } - - async quitAll() { - let tabs = await this.tabPresenter.getAll(); - let ids = tabs.map(tab => tab.id); - this.tabPresenter.remove(ids); - } - - async addbookmark(title) { - 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); - } - - async set(keywords) { - if (keywords.length === 0) { - return; - } - let [name, value] = parsers.parseSetOption(keywords, properties.types); - await this.settingRepository.setProperty(name, value); - - return this.contentMessageClient.broadcastSettingsChanged(); - } - - async urlOrSearch(keywords) { - let settings = await this.settingRepository.get(); - return urls.searchUrl(keywords, settings.search); - } -} diff --git a/src/background/usecases/completions.js b/src/background/usecases/completions.js deleted file mode 100644 index 21dc668..0000000 --- a/src/background/usecases/completions.js +++ /dev/null @@ -1,205 +0,0 @@ -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 * as filters from './filters'; -import SettingRepository from '../repositories/setting'; -import TabPresenter from '../presenters/tab'; -import * as properties from '../../shared/settings/properties'; - -const COMPLETION_ITEM_LIMIT = 10; - -export default class CompletionsInteractor { - constructor() { - this.tabPresenter = new TabPresenter(); - this.completionRepository = new CompletionRepository(); - this.settingRepository = new SettingRepository(); - } - - queryConsoleCommand(prefix) { - let keys = Object.keys(CommandDocs); - let items = keys - .filter(name => name.startsWith(prefix)) - .map(name => ({ - caption: name, - content: name, - url: CommandDocs[name], - })); - - if (items.length === 0) { - return Promise.resolve(Completions.empty()); - } - return Promise.resolve( - new Completions([new CompletionGroup('Console Command', items)]) - ); - } - - async queryOpen(name, keywords) { - let settings = await this.settingRepository.get(); - let groups = []; - - let complete = settings.properties.complete || properties.defaults.complete; - for (let c of complete) { - if (c === 's') { - // eslint-disable-next-line no-await-in-loop - let engines = await this.querySearchEngineItems(name, keywords); - if (engines.length > 0) { - groups.push(new CompletionGroup('Search Engines', engines)); - } - } else if (c === 'h') { - // eslint-disable-next-line no-await-in-loop - let histories = await this.queryHistoryItems(name, keywords); - if (histories.length > 0) { - groups.push(new CompletionGroup('History', histories)); - } - } else if (c === 'b') { - // eslint-disable-next-line no-await-in-loop - let bookmarks = await this.queryBookmarkItems(name, keywords); - if (bookmarks.length > 0) { - groups.push(new CompletionGroup('Bookmarks', bookmarks)); - } - } - } - return new Completions(groups); - } - - // eslint-disable-next-line max-statements - async queryBuffer(name, keywords) { - let lastId = await this.tabPresenter.getLastSelectedId(); - let trimmed = keywords.trim(); - let tabs = []; - if (trimmed.length > 0 && !isNaN(trimmed)) { - let all = await this.tabPresenter.getAll(); - let index = parseInt(trimmed, 10) - 1; - if (index >= 0 && index < all.length) { - tabs = [all[index]]; - } - } else if (trimmed === '%') { - let all = await this.tabPresenter.getAll(); - let tab = all.find(t => t.active); - tabs = [tab]; - } else if (trimmed === '#') { - if (typeof lastId !== 'undefined' && lastId !== null) { - let all = await this.tabPresenter.getAll(); - let tab = all.find(t => t.id === lastId); - tabs = [tab]; - } - } else { - tabs = await this.completionRepository.queryTabs(keywords, false); - } - const flag = (tab) => { - if (tab.active) { - return '%'; - } else if (tab.id === lastId) { - return '#'; - } - return ' '; - }; - let items = tabs.map(tab => new CompletionItem({ - caption: tab.index + 1 + ': ' + flag(tab) + ' ' + tab.title, - content: name + ' ' + tab.title, - url: tab.url, - icon: tab.favIconUrl - })); - if (items.length === 0) { - return Promise.resolve(Completions.empty()); - } - return new Completions([new CompletionGroup('Buffers', items)]); - } - - queryBdelete(name, keywords) { - return this.queryTabs(name, true, keywords); - } - - queryBdeleteForce(name, keywords) { - return this.queryTabs(name, false, keywords); - } - - querySet(name, keywords) { - let items = Object.keys(properties.docs).map((key) => { - if (properties.types[key] === 'boolean') { - return [ - new CompletionItem({ - caption: key, - content: name + ' ' + key, - url: 'Enable ' + properties.docs[key], - }), - new CompletionItem({ - caption: 'no' + key, - content: name + ' no' + key, - url: 'Disable ' + properties.docs[key], - }), - ]; - } - return [ - new CompletionItem({ - caption: key, - content: name + ' ' + key, - url: 'Set ' + properties.docs[key], - }) - ]; - }); - items = items.reduce((acc, val) => acc.concat(val), []); - items = items.filter((item) => { - return item.caption.startsWith(keywords); - }); - if (items.length === 0) { - return Promise.resolve(Completions.empty()); - } - return Promise.resolve( - new Completions([new CompletionGroup('Properties', items)]) - ); - } - - async queryTabs(name, excludePinned, args) { - let tabs = await this.completionRepository.queryTabs(args, excludePinned); - let items = tabs.map(tab => new CompletionItem({ - caption: tab.title, - content: name + ' ' + tab.title, - url: tab.url, - icon: tab.favIconUrl - })); - if (items.length === 0) { - return Promise.resolve(Completions.empty()); - } - return new Completions([new CompletionGroup('Buffers', items)]); - } - - async querySearchEngineItems(name, keywords) { - let settings = await this.settingRepository.get(); - let engines = Object.keys(settings.search.engines) - .filter(key => key.startsWith(keywords)); - return engines.map(key => new CompletionItem({ - caption: key, - content: name + ' ' + key, - })); - } - - async queryHistoryItems(name, keywords) { - let histories = await this.completionRepository.queryHistories(keywords); - histories = [histories] - .map(filters.filterBlankTitle) - .map(filters.filterHttp) - .map(filters.filterByTailingSlash) - .map(pages => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT)) - .map(pages => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0] - .sort((x, y) => x.visitCount < y.visitCount) - .slice(0, COMPLETION_ITEM_LIMIT); - return histories.map(page => new CompletionItem({ - caption: page.title, - content: name + ' ' + page.url, - url: page.url - })); - } - - async queryBookmarkItems(name, keywords) { - let bookmarks = await this.completionRepository.queryBookmarks(keywords); - return bookmarks.slice(0, COMPLETION_ITEM_LIMIT) - .map(page => new CompletionItem({ - caption: page.title, - content: name + ' ' + page.url, - url: page.url - })); - } -} 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/link.js b/src/background/usecases/link.js deleted file mode 100644 index b8ed719..0000000 --- a/src/background/usecases/link.js +++ /dev/null @@ -1,19 +0,0 @@ -import SettingRepository from '../repositories/setting'; -import TabPresenter from '../presenters/tab'; - -export default class LinkInteractor { - constructor() { - this.settingRepository = new SettingRepository(); - this.tabPresenter = new TabPresenter(); - } - - openToTab(url, tabId) { - return this.tabPresenter.open(url, tabId); - } - - openNewTab(url, openerId, background) { - return this.tabPresenter.create(url, { - openerTabId: openerId, active: !background - }); - } -} diff --git a/src/background/usecases/mark.js b/src/background/usecases/mark.js deleted file mode 100644 index 34b8a74..0000000 --- a/src/background/usecases/mark.js +++ /dev/null @@ -1,39 +0,0 @@ -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'; - -export default class MarkInteractor { - constructor() { - this.tabPresenter = new TabPresenter(); - this.markRepository = new MarkRepository(); - this.consolePresenter = new ConsolePresenter(); - this.contentMessageClient = new ContentMessageClient(); - } - - async setGlobal(key, x, y) { - let tab = await this.tabPresenter.getCurrent(); - let mark = new GlobalMark(tab.id, tab.url, x, y); - return this.markRepository.setMark(key, mark); - } - - async jumpGlobal(key) { - let current = await this.tabPresenter.getCurrent(); - - let mark = await this.markRepository.getMark(key); - if (!mark) { - return this.consolePresenter.showError(current.id, 'Mark is not set'); - } - - return this.contentMessageClient.scrollTo( - mark.tabId, mark.x, mark.y - ).then(() => { - return this.tabPresenter.select(mark.tabId); - }).catch(async() => { - let tab = await this.tabPresenter.create(mark.url); - let mark2 = new GlobalMark(tab.id, mark.url, mark.x, mark.y); - return this.markRepository.setMark(key, mark2); - }); - } -} 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); - } - } -} - diff --git a/src/background/usecases/setting.js b/src/background/usecases/setting.js deleted file mode 100644 index 656fc3f..0000000 --- a/src/background/usecases/setting.js +++ /dev/null @@ -1,31 +0,0 @@ -import Setting from '../domains/setting'; -import PersistentSettingRepository from '../repositories/persistent-setting'; -import SettingRepository from '../repositories/setting'; - -export default class SettingInteractor { - constructor() { - this.persistentSettingRepository = new PersistentSettingRepository(); - this.settingRepository = new SettingRepository(); - } - - save(settings) { - this.persistentSettingRepository.save(settings); - } - - get() { - return this.settingRepository.get(); - } - - async reload() { - let settings = await this.persistentSettingRepository.load(); - if (!settings) { - settings = Setting.defaultSettings(); - } - - let value = settings.value(); - - this.settingRepository.update(value); - - return value; - } -} diff --git a/src/background/usecases/version.js b/src/background/usecases/version.js deleted file mode 100644 index a71f90d..0000000 --- a/src/background/usecases/version.js +++ /dev/null @@ -1,41 +0,0 @@ -import manifest from '../../../manifest.json'; -import VersionRepository from '../repositories/version'; -import TabPresenter from '../presenters/tab'; -import Notifier from '../infrastructures/notifier'; - -export default class VersionInteractor { - constructor() { - this.versionRepository = new VersionRepository(); - this.tabPresenter = new TabPresenter(); - this.notifier = new Notifier(); - } - - async notifyIfUpdated() { - if (!await this.checkUpdated()) { - return; - } - - let title = 'Vim Vixen ' + manifest.version + ' has been installed'; - let message = 'Click here to see release notes'; - this.notifier.notify(title, message, () => { - let url = this.releaseNoteUrl(manifest.version); - this.tabPresenter.create(url); - }); - this.versionRepository.update(manifest.version); - } - - async checkUpdated() { - let prev = await this.versionRepository.get(); - if (!prev) { - return true; - } - return manifest.version !== prev; - } - - releaseNoteUrl(version) { - if (version) { - return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; - } - return 'https://github.com/ueokande/vim-vixen/releases/'; - } -} -- cgit v1.2.3