diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-19 15:59:05 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-19 15:59:05 +0900 |
commit | 3f4bc62ed515f1c5da90ee1c3e42f3d435ea6e39 (patch) | |
tree | 8af9f8e5b12d007ce9628b40f3046b73f18e29f8 /src/content/usecases/FindUseCase.ts | |
parent | 6ec560bca33e774ff7e363270c423c919fdcf4ce (diff) | |
parent | c4dcdff9844e2404e3bc035f4cea9fce2f7770ab (diff) |
Merge pull request #587 from ueokande/refactor-content
Refactor content scripts
Diffstat (limited to 'src/content/usecases/FindUseCase.ts')
-rw-r--r-- | src/content/usecases/FindUseCase.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/content/usecases/FindUseCase.ts b/src/content/usecases/FindUseCase.ts new file mode 100644 index 0000000..74cbc97 --- /dev/null +++ b/src/content/usecases/FindUseCase.ts @@ -0,0 +1,81 @@ +import FindPresenter, { FindPresenterImpl } from '../presenters/FindPresenter'; +import FindRepository, { FindRepositoryImpl } + from '../repositories/FindRepository'; +import FindClient, { FindClientImpl } from '../client/FindClient'; +import ConsoleClient, { ConsoleClientImpl } from '../client/ConsoleClient'; + +export default class FindUseCase { + private presenter: FindPresenter; + + private repository: FindRepository; + + private client: FindClient; + + private consoleClient: ConsoleClient; + + constructor({ + presenter = new FindPresenterImpl() as FindPresenter, + repository = new FindRepositoryImpl(), + client = new FindClientImpl(), + consoleClient = new ConsoleClientImpl(), + } = {}) { + this.presenter = presenter; + this.repository = repository; + this.client = client; + this.consoleClient = consoleClient; + } + + async startFind(keyword?: string): Promise<void> { + this.presenter.clearSelection(); + if (keyword) { + this.saveKeyword(keyword); + } else { + let lastKeyword = await this.getKeyword(); + if (!lastKeyword) { + return this.showNoLastKeywordError(); + } + this.saveKeyword(lastKeyword); + } + return this.findNext(); + } + + findNext(): Promise<void> { + return this.findNextPrev(false); + } + + findPrev(): Promise<void> { + return this.findNextPrev(true); + } + + private async findNextPrev( + backwards: boolean, + ): Promise<void> { + let keyword = await this.getKeyword(); + if (!keyword) { + return this.showNoLastKeywordError(); + } + let found = this.presenter.find(keyword, backwards); + if (found) { + this.consoleClient.info('Pattern found: ' + keyword); + } else { + this.consoleClient.error('Pattern not found: ' + keyword); + } + } + + private async getKeyword(): Promise<string | null> { + let keyword = this.repository.getLastKeyword(); + if (!keyword) { + keyword = await this.client.getGlobalLastKeyword(); + } + return keyword; + } + + private async saveKeyword(keyword: string): Promise<void> { + this.repository.setLastKeyword(keyword); + await this.client.setGlobalLastKeyword(keyword); + } + + private async showNoLastKeywordError(): Promise<void> { + await this.consoleClient.error('No previous search keywords'); + } +} |