aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/NavigateUseCase.ts
blob: 974606c3df5876c0061c3072d6bd21cba84d4f7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { inject, injectable } from "tsyringe";
import NavigateClient from "../clients/NavigateClient";
import TabPresenter from "../presenters/TabPresenter";

@injectable()
export default class NavigateUseCase {
  constructor(
    @inject("TabPresenter") private tabPresenter: TabPresenter,
    private navigateClient: NavigateClient
  ) {}

  async openHistoryNext(): Promise<void> {
    const tab = await this.tabPresenter.getCurrent();
    await this.navigateClient.historyNext(tab.id!);
  }

  async openHistoryPrev(): Promise<void> {
    const tab = await this.tabPresenter.getCurrent();
    await this.navigateClient.historyPrev(tab.id!);
  }

  async openLinkNext(): Promise<void> {
    const tab = await this.tabPresenter.getCurrent();
    await this.navigateClient.linkNext(tab.id!);
  }

  async openLinkPrev(): Promise<void> {
    const tab = await this.tabPresenter.getCurrent();
    await this.navigateClient.linkPrev(tab.id!);
  }

  async openParent(): Promise<void> {
    const tab = await this.tabPresenter.getCurrent();
    const url = new URL(tab.url!);
    if (url.hash.length > 0) {
      url.hash = "";
    } else if (url.search.length > 0) {
      url.search = "";
    } else {
      const basenamePattern = /\/[^/]+$/;
      const lastDirPattern = /\/[^/]+\/$/;
      if (basenamePattern.test(url.pathname)) {
        url.pathname = url.pathname.replace(basenamePattern, "/");
      } else if (lastDirPattern.test(url.pathname)) {
        url.pathname = url.pathname.replace(lastDirPattern, "/");
      }
    }
    await this.tabPresenter.open(url.href);
  }

  async openRoot(): Promise<void> {
    const tab = await this.tabPresenter.getCurrent();
    const url = new URL(tab.url!);
    await this.tabPresenter.open(url.origin);
  }
}