diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-19 15:59:05 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-19 15:59:05 +0900 |
commit | 3f4bc62ed515f1c5da90ee1c3e42f3d435ea6e39 (patch) | |
tree | 8af9f8e5b12d007ce9628b40f3046b73f18e29f8 /src/content/usecases/MarkUseCase.ts | |
parent | 6ec560bca33e774ff7e363270c423c919fdcf4ce (diff) | |
parent | c4dcdff9844e2404e3bc035f4cea9fce2f7770ab (diff) |
Merge pull request #587 from ueokande/refactor-content
Refactor content scripts
Diffstat (limited to 'src/content/usecases/MarkUseCase.ts')
-rw-r--r-- | src/content/usecases/MarkUseCase.ts | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/content/usecases/MarkUseCase.ts b/src/content/usecases/MarkUseCase.ts new file mode 100644 index 0000000..530f141 --- /dev/null +++ b/src/content/usecases/MarkUseCase.ts @@ -0,0 +1,66 @@ +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'); + } + this.scroll(pos.x, pos.y); + } + } + + scroll(x: number, y: number): void { + let smooth = this.settingRepository.get().properties.smoothscroll; + this.scrollPresenter.scrollTo(x, y, smooth); + } + + private globalKey(key: string) { + return (/^[A-Z0-9]$/).test(key); + } +} |