aboutsummaryrefslogtreecommitdiff
path: root/src/content/usecases/FollowSlaveUseCase.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-19 15:59:05 +0900
committerGitHub <noreply@github.com>2019-05-19 15:59:05 +0900
commit3f4bc62ed515f1c5da90ee1c3e42f3d435ea6e39 (patch)
tree8af9f8e5b12d007ce9628b40f3046b73f18e29f8 /src/content/usecases/FollowSlaveUseCase.ts
parent6ec560bca33e774ff7e363270c423c919fdcf4ce (diff)
parentc4dcdff9844e2404e3bc035f4cea9fce2f7770ab (diff)
Merge pull request #587 from ueokande/refactor-content
Refactor content scripts
Diffstat (limited to 'src/content/usecases/FollowSlaveUseCase.ts')
-rw-r--r--src/content/usecases/FollowSlaveUseCase.ts91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/content/usecases/FollowSlaveUseCase.ts b/src/content/usecases/FollowSlaveUseCase.ts
new file mode 100644
index 0000000..eb011de
--- /dev/null
+++ b/src/content/usecases/FollowSlaveUseCase.ts
@@ -0,0 +1,91 @@
+import FollowSlaveRepository, { FollowSlaveRepositoryImpl }
+ from '../repositories/FollowSlaveRepository';
+import FollowPresenter, { FollowPresenterImpl }
+ from '../presenters/FollowPresenter';
+import TabsClient, { TabsClientImpl } from '../client/TabsClient';
+import { LinkHint, InputHint } from '../presenters/Hint';
+import FollowMasterClient, { FollowMasterClientImpl }
+ from '../client/FollowMasterClient';
+import Key from '../domains/Key';
+
+interface Size {
+ width: number;
+ height: number;
+}
+
+interface Point {
+ x: number;
+ y: number;
+}
+
+export default class FollowSlaveUseCase {
+ private presenter: FollowPresenter;
+
+ private tabsClient: TabsClient;
+
+ private followMasterClient: FollowMasterClient;
+
+ private followSlaveRepository: FollowSlaveRepository;
+
+ constructor({
+ presenter = new FollowPresenterImpl(),
+ tabsClient = new TabsClientImpl(),
+ followMasterClient = new FollowMasterClientImpl(window.top),
+ followSlaveRepository = new FollowSlaveRepositoryImpl(),
+ } = {}) {
+ this.presenter = presenter;
+ this.tabsClient = tabsClient;
+ this.followMasterClient = followMasterClient;
+ this.followSlaveRepository = followSlaveRepository;
+ }
+
+ countTargets(viewSize: Size, framePosition: Point): void {
+ let count = this.presenter.getTargetCount(viewSize, framePosition);
+ this.followMasterClient.responseHintCount(count);
+ }
+
+ createHints(viewSize: Size, framePosition: Point, tags: string[]): void {
+ this.followSlaveRepository.enableFollowMode();
+ this.presenter.createHints(viewSize, framePosition, tags);
+ }
+
+ showHints(prefix: string) {
+ this.presenter.filterHints(prefix);
+ }
+
+ sendKey(key: Key): void {
+ this.followMasterClient.sendKey(key);
+ }
+
+ isFollowMode(): boolean {
+ return this.followSlaveRepository.isFollowMode();
+ }
+
+ async activate(tag: string, newTab: boolean, background: boolean) {
+ let hint = this.presenter.getHint(tag);
+ if (!hint) {
+ return;
+ }
+
+ if (hint instanceof LinkHint) {
+ let url = hint.getLink();
+ // ignore taget='_blank'
+ if (!newTab && hint.getLinkTarget() === '_blank') {
+ hint.click();
+ return;
+ }
+ // eslint-disable-next-line no-script-url
+ if (!url || url === '#' || url.toLowerCase().startsWith('javascript:')) {
+ return;
+ }
+ await this.tabsClient.openUrl(url, newTab, background);
+ } else if (hint instanceof InputHint) {
+ hint.activate();
+ }
+ }
+
+ clear(): void {
+ this.followSlaveRepository.disableFollowMode();
+ this.presenter.clearHints();
+ }
+}