aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/NavigateUseCase.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-26 16:24:14 +0900
committerGitHub <noreply@github.com>2019-05-26 16:24:14 +0900
commitcd584c8e243bafa8fc284279f716e8113607cd65 (patch)
treebc39bc30369f149e4ba4b6dc9c353b2906c4ef90 /src/background/usecases/NavigateUseCase.ts
parent07897df636ca3e732490d53fd2acf947738bf16e (diff)
parent34a96cdc9c5d7c8a11c6f1ae512fbc97724f61c4 (diff)
Merge pull request #592 from ueokande/repeat-last-operation
Add "repeat last operation" command
Diffstat (limited to 'src/background/usecases/NavigateUseCase.ts')
-rw-r--r--src/background/usecases/NavigateUseCase.ts57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/background/usecases/NavigateUseCase.ts b/src/background/usecases/NavigateUseCase.ts
new file mode 100644
index 0000000..152339a
--- /dev/null
+++ b/src/background/usecases/NavigateUseCase.ts
@@ -0,0 +1,57 @@
+import { injectable } from 'tsyringe';
+import NavigateClient from '../clients/NavigateClient';
+import TabPresenter from '../presenters/TabPresenter';
+
+@injectable()
+export default class NavigateUseCase {
+ constructor(
+ private tabPresenter: TabPresenter,
+ private navigateClient: NavigateClient,
+ ) {
+ }
+
+ async openHistoryNext(): Promise<void> {
+ let tab = await this.tabPresenter.getCurrent();
+ await this.navigateClient.historyNext(tab.id!!);
+ }
+
+ async openHistoryPrev(): Promise<void> {
+ let tab = await this.tabPresenter.getCurrent();
+ await this.navigateClient.historyPrev(tab.id!!);
+ }
+
+ async openLinkNext(): Promise<void> {
+ let tab = await this.tabPresenter.getCurrent();
+ await this.navigateClient.linkNext(tab.id!!);
+ }
+
+ async openLinkPrev(): Promise<void> {
+ let tab = await this.tabPresenter.getCurrent();
+ await this.navigateClient.linkPrev(tab.id!!);
+ }
+
+ async openParent(): Promise<void> {
+ let tab = await this.tabPresenter.getCurrent();
+ let url = new URL(tab.url!!);
+ if (url.hash.length > 0) {
+ url.hash = '';
+ } else if (url.search.length > 0) {
+ url.search = '';
+ } else {
+ const basenamePattern = /\/[^/]+$/;
+ const lastDirPattern = /\/[^/]+\/$/;
+ if (basenamePattern.test(url.pathname)) {
+ url.pathname = url.pathname.replace(basenamePattern, '/');
+ } else if (lastDirPattern.test(url.pathname)) {
+ url.pathname = url.pathname.replace(lastDirPattern, '/');
+ }
+ }
+ await this.tabPresenter.open(url.href);
+ }
+
+ async openRoot(): Promise<void> {
+ let tab = await this.tabPresenter.getCurrent();
+ let url = new URL(tab.url!!);
+ await this.tabPresenter.open(url.origin);
+ }
+}