diff options
| author | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-06-14 23:14:51 +0900 | 
|---|---|---|
| committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-07-05 21:32:43 +0900 | 
| commit | 65cf6f0842d8d5933dc13b3767b1baf398d68cd5 (patch) | |
| tree | df9a8b139fd98adb79f075ba655d1303bdf3fd1d /src/background/usecases | |
| parent | caced372415a944c4297157397d0027ba629fff0 (diff) | |
Implement FindNextOperator
Diffstat (limited to 'src/background/usecases')
| -rw-r--r-- | src/background/usecases/StartFindUseCase.ts | 57 | 
1 files changed, 57 insertions, 0 deletions
diff --git a/src/background/usecases/StartFindUseCase.ts b/src/background/usecases/StartFindUseCase.ts new file mode 100644 index 0000000..066d930 --- /dev/null +++ b/src/background/usecases/StartFindUseCase.ts @@ -0,0 +1,57 @@ +import { inject, injectable } from "tsyringe"; +import ConsoleClient from "../infrastructures/ConsoleClient"; +import FindRepositoryImpl from "../repositories/FindRepository"; +import FindClient from "../clients/FindClient"; +import FramePresenter from "../presenters/FramePresenter"; + +@injectable() +export default class StartFindUseCase { +  constructor( +    @inject("FindClient") +    private readonly findClient: FindClient, +    @inject("FindRepository") +    private readonly findRepository: FindRepositoryImpl, +    @inject("ConsoleClient") +    private readonly consoleClient: ConsoleClient, +    @inject("FramePresenter") +    private readonly framePresenter: FramePresenter +  ) {} + +  async startFind(tabId: number, keyword?: string): Promise<void> { +    if (typeof keyword === "undefined") { +      keyword = (await this.findRepository.getLocalState(tabId))?.keyword; +    } +    if (typeof keyword === "undefined") { +      keyword = await this.findRepository.getGlobalKeyword(); +    } +    if (typeof keyword === "undefined") { +      await this.consoleClient.showError(tabId, "No previous search keywords"); +      return; +    } + +    this.findRepository.setGlobalKeyword(keyword); + +    const frameIds = await this.framePresenter.getAllFrameIds(tabId); +    for (const frameId of frameIds) { +      await this.findClient.clearSelection(tabId, frameId); +    } + +    for (let framePos = 0; framePos < frameIds.length; ++framePos) { +      const found = await this.findClient.findNext( +        tabId, +        frameIds[framePos], +        keyword +      ); +      if (found) { +        await this.findRepository.setLocalState(tabId, { +          frameIds, +          framePos, +          keyword, +        }); +        await this.consoleClient.showInfo(tabId, "Pattern found: " + keyword); +        return; +      } +    } +    this.consoleClient.showError(tabId, "Pattern not found: " + keyword); +  } +}  | 
