aboutsummaryrefslogtreecommitdiff
path: root/src/content/usecases/MarkUseCase.ts
blob: ec63f2b76814a4edc334126ddb86cc1fee3fb47b (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
import ScrollPresenter, { ScrollPresenterImpl }
  from '../presenters/ScrollPresenter';
import MarkClient, { MarkClientImpl } from '../client/MarkClient';
import MarkRepository, { MarkRepositoryImpl }
  from '../repositories/MarkRepository';
import SettingRepository, { SettingRepositoryImpl }
  from '../repositories/SettingRepository';
import ConsoleClient, { ConsoleClientImpl } from '../client/ConsoleClient';

export default class MarkUseCase {
  private scrollPresenter: ScrollPresenter;

  private client: MarkClient;

  private repository: MarkRepository;

  private settingRepository: SettingRepository;

  private consoleClient: ConsoleClient;

  constructor({
    scrollPresenter = new ScrollPresenterImpl(),
    client = new MarkClientImpl(),
    repository = new MarkRepositoryImpl(),
    settingRepository = new SettingRepositoryImpl(),
    consoleClient = new ConsoleClientImpl(),
  } = {}) {
    this.scrollPresenter = scrollPresenter;
    this.client = client;
    this.repository = repository;
    this.settingRepository = settingRepository;
    this.consoleClient = consoleClient;
  }

  async set(key: string): Promise<void> {
    let pos = this.scrollPresenter.getScroll();
    if (this.globalKey(key)) {
      this.client.setGloablMark(key, pos);
      await this.consoleClient.info(`Set global mark to '${key}'`);
    } else {
      this.repository.set(key, pos);
      await this.consoleClient.info(`Set local mark to '${key}'`);
    }
  }

  async jump(key: string): Promise<void> {
    if (this.globalKey(key)) {
      await this.client.jumpGlobalMark(key);
    } else {
      let pos = this.repository.get(key);
      if (!pos) {
        throw new Error('Mark is not set');
      }
      let smooth = this.settingRepository.get().properties.smoothscroll;
      this.scrollPresenter.scrollTo(pos.x, pos.y, smooth);
    }
  }

  private globalKey(key: string) {
    return (/^[A-Z0-9]$/).test(key);
  }
}