aboutsummaryrefslogtreecommitdiff
path: root/src/content/usecases/MarkUseCase.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-11 16:38:08 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-05-11 16:38:08 +0900
commitc6288f19d93a05f96274dd172450b8350389c39f (patch)
tree75b5a168af535aa640bc21e70281497915dade63 /src/content/usecases/MarkUseCase.ts
parentebfb172520f7077a15cd5d4e865e5d86593c55ac (diff)
Mark set/jump as a clean architecture
Diffstat (limited to 'src/content/usecases/MarkUseCase.ts')
-rw-r--r--src/content/usecases/MarkUseCase.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/content/usecases/MarkUseCase.ts b/src/content/usecases/MarkUseCase.ts
new file mode 100644
index 0000000..ec63f2b
--- /dev/null
+++ b/src/content/usecases/MarkUseCase.ts
@@ -0,0 +1,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);
+ }
+}