aboutsummaryrefslogtreecommitdiff
path: root/src/content/usecases/FollowSlaveUseCase.ts
blob: eb011de63b005206ff7b5ae8df3c5b3d562f38b3 (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
import FollowSlaveRepository, { FollowSlaveRepositoryImpl }
  from '../repositories/FollowSlaveRepository';
import FollowPresenter, { FollowPresenterImpl }
  from '../presenters/FollowPresenter';
import TabsClient, { TabsClientImpl } from '../client/TabsClient';
import { LinkHint, InputHint } from '../presenters/Hint';
import FollowMasterClient, { FollowMasterClientImpl }
  from '../client/FollowMasterClient';
import Key from '../domains/Key';

interface Size {
  width: number;
  height: number;
}

interface Point {
  x: number;
  y: number;
}

export default class FollowSlaveUseCase {
  private presenter: FollowPresenter;

  private tabsClient: TabsClient;

  private followMasterClient: FollowMasterClient;

  private followSlaveRepository: FollowSlaveRepository;

  constructor({
    presenter = new FollowPresenterImpl(),
    tabsClient = new TabsClientImpl(),
    followMasterClient = new FollowMasterClientImpl(window.top),
    followSlaveRepository = new FollowSlaveRepositoryImpl(),
  } = {}) {
    this.presenter = presenter;
    this.tabsClient = tabsClient;
    this.followMasterClient = followMasterClient;
    this.followSlaveRepository = followSlaveRepository;
  }

  countTargets(viewSize: Size, framePosition: Point): void {
    let count = this.presenter.getTargetCount(viewSize, framePosition);
    this.followMasterClient.responseHintCount(count);
  }

  createHints(viewSize: Size, framePosition: Point, tags: string[]): void {
    this.followSlaveRepository.enableFollowMode();
    this.presenter.createHints(viewSize, framePosition, tags);
  }

  showHints(prefix: string) {
    this.presenter.filterHints(prefix);
  }

  sendKey(key: Key): void {
    this.followMasterClient.sendKey(key);
  }

  isFollowMode(): boolean {
    return this.followSlaveRepository.isFollowMode();
  }

  async activate(tag: string, newTab: boolean, background: boolean) {
    let hint = this.presenter.getHint(tag);
    if (!hint) {
      return;
    }

    if (hint instanceof LinkHint) {
      let url = hint.getLink();
      // ignore taget='_blank'
      if (!newTab && hint.getLinkTarget() === '_blank') {
        hint.click();
        return;
      }
      // eslint-disable-next-line no-script-url
      if (!url || url === '#' || url.toLowerCase().startsWith('javascript:')) {
        return;
      }
      await this.tabsClient.openUrl(url, newTab, background);
    } else if (hint instanceof InputHint) {
      hint.activate();
    }
  }

  clear(): void {
    this.followSlaveRepository.disableFollowMode();
    this.presenter.clearHints();
  }
}