aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/TabUseCase.ts
blob: 31112a98c3b405ec0e9631a68769aea41e9c2591 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { injectable } from 'tsyringe';
import TabPresenter from '../presenters/TabPresenter';
import WindowPresenter from '../presenters/WindowPresenter';
import BrowserSettingRepository from '../repositories/BrowserSettingRepository';

@injectable()
export default class TabUseCase {
  constructor(
    private tabPresenter: TabPresenter,
    private windowPresenter: WindowPresenter,
    private browserSettingRepository: BrowserSettingRepository,
  ) {
  }

  async close(force: boolean): Promise<any> {
    let tab = await this.tabPresenter.getCurrent();
    if (!force && tab.pinned) {
      return Promise.resolve();
    }
    return this.tabPresenter.remove([tab.id as number]);
  }

  async closeRight(): Promise<any> {
    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 as number]);
      }
    }
  }

  reopen(): Promise<any> {
    return this.tabPresenter.reopen();
  }

  async reload(cache: boolean): Promise<any> {
    let tab = await this.tabPresenter.getCurrent();
    return this.tabPresenter.reload(tab.id as number, cache);
  }

  async setPinned(pinned: boolean): Promise<any> {
    let tab = await this.tabPresenter.getCurrent();
    return this.tabPresenter.setPinned(tab.id as number, pinned);
  }

  async togglePinned(): Promise<any> {
    let tab = await this.tabPresenter.getCurrent();
    return this.tabPresenter.setPinned(tab.id as number, !tab.pinned);
  }

  async duplicate(): Promise<any> {
    let tab = await this.tabPresenter.getCurrent();
    return this.tabPresenter.duplicate(tab.id as number);
  }

  async openPageSource(): Promise<any> {
    let tab = await this.tabPresenter.getCurrent();
    let url = 'view-source:' + tab.url;
    return this.tabPresenter.create(url);
  }

  async openHome(newTab: boolean): Promise<any> {
    let tab = await this.tabPresenter.getCurrent();
    let urls = await this.browserSettingRepository.getHomepageUrls();
    if (urls.length === 1 && urls[0] === 'about:home') {
      // eslint-disable-next-line max-len
      throw new Error('Cannot open Firefox Home (about:home) by WebExtensions, set your custom URLs');
    }
    if (urls.length === 1 && !newTab) {
      return this.tabPresenter.open(urls[0], tab.id);
    }
    for (let url of urls) {
      this.tabPresenter.create(url);
    }
  }

  async openURL(
    url: string, newTab?: boolean, newWindow?: boolean,
  ): Promise<void> {
    if (newWindow) {
      await this.windowPresenter.create(url);
    } else if (newTab) {
      await this.tabPresenter.create(url);
    } else {
      let tab = await this.tabPresenter.getCurrent();
      await this.tabPresenter.open(url, tab.id);
    }
  }
}