aboutsummaryrefslogtreecommitdiff
path: root/src/content/usecases/MarkUseCase.ts
blob: f5f512ccda3ccea5d9c88834ff9c4767cb1692c1 (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
import { injectable, inject } from "tsyringe";
import ScrollPresenter from "../presenters/ScrollPresenter";
import MarkClient from "../client/MarkClient";
import MarkRepository from "../repositories/MarkRepository";
import SettingRepository from "../repositories/SettingRepository";
import ConsoleClient from "../client/ConsoleClient";

@injectable()
export default class MarkUseCase {
  constructor(
    @inject("ScrollPresenter") private scrollPresenter: ScrollPresenter,
    @inject("MarkClient") private client: MarkClient,
    @inject("MarkRepository") private repository: MarkRepository,
    @inject("SettingRepository") private settingRepository: SettingRepository,
    @inject("ConsoleClient") private consoleClient: ConsoleClient
  ) {}

  async set(key: string): Promise<void> {
    const pos = this.scrollPresenter.getScroll();
    if (this.globalKey(key)) {
      await 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 {
      const pos = this.repository.get(key);
      if (!pos) {
        await this.consoleClient.error("Mark is not set");
        return;
      }
      this.scroll(pos.x, pos.y);
    }
  }

  scroll(x: number, y: number): void {
    const smooth = this.settingRepository.get().properties.smoothscroll;
    this.scrollPresenter.scrollTo(x, y, smooth);
  }

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