aboutsummaryrefslogtreecommitdiff
path: root/src/content/usecases/ClipboardUseCase.ts
blob: 7f16f68fa631686bde0cabc50baf99208d0ec907 (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
import { injectable, inject } from 'tsyringe';
import * as urls from '../../shared/urls';
import ClipboardRepository from '../repositories/ClipboardRepository';
import SettingRepository from '../repositories/SettingRepository';
import ConsoleClient from '../client/ConsoleClient';
import OperationClient from '../client/OperationClient';

@injectable()
export default class ClipboardUseCase {
  constructor(
    @inject('ClipboardRepository') private repository: ClipboardRepository,
    @inject('SettingRepository') private settingRepository: SettingRepository,
    @inject('ConsoleClient') private consoleClient: ConsoleClient,
    @inject('OperationClient') private operationClinet: OperationClient,
  ) {
  }

  async yankCurrentURL(): Promise<string> {
    const url = window.location.href;
    this.repository.write(url);
    await this.consoleClient.info('Yanked ' + url);
    return Promise.resolve(url);
  }

  async openOrSearch(newTab: boolean): Promise<void> {
    const search = this.settingRepository.get().search;
    const text = this.repository.read();
    const url = urls.searchUrl(text, search);

    // TODO: Repeat pasting from clipboard instead of opening a certain url.
    // 'Repeat last' command is implemented in the background script and cannot
    // access to clipboard until Firefox 63.
    await this.operationClinet.internalOpenUrl(url, newTab);
  }
}