From 8d0739463d970deae2ebdd88eedac29e9c4379ff Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 24 May 2019 21:51:18 +0900 Subject: Move open parent and open root to background --- src/background/controllers/OperationController.ts | 4 ++++ src/background/presenters/TabPresenter.ts | 2 +- src/background/usecases/NavigateUseCase.ts | 25 +++++++++++++++++++---- 3 files changed, 26 insertions(+), 5 deletions(-) (limited to 'src/background') diff --git a/src/background/controllers/OperationController.ts b/src/background/controllers/OperationController.ts index cb5c043..1e80f54 100644 --- a/src/background/controllers/OperationController.ts +++ b/src/background/controllers/OperationController.ts @@ -84,6 +84,10 @@ export default class OperationController { return this.navigateUseCase.openLinkPrev(); case operations.NAVIGATE_LINK_NEXT: return this.navigateUseCase.openLinkNext(); + case operations.NAVIGATE_PARENT: + return this.navigateUseCase.openParent(); + case operations.NAVIGATE_ROOT: + return this.navigateUseCase.openRoot(); } throw new Error('unknown operation: ' + operation.type); } diff --git a/src/background/presenters/TabPresenter.ts b/src/background/presenters/TabPresenter.ts index 5665bf0..5e6e56e 100644 --- a/src/background/presenters/TabPresenter.ts +++ b/src/background/presenters/TabPresenter.ts @@ -36,7 +36,7 @@ export default class TabPresenter { return tabId; } - async getByKeyword(keyword: string, excludePinned = false): Promise { + async getByKeyword(keyword: string, excludePinned: boolean = false): Promise { let tabs = await browser.tabs.query({ currentWindow: true }); return tabs.filter((t) => { return t.url && t.url.toLowerCase().includes(keyword.toLowerCase()) || diff --git a/src/background/usecases/NavigateUseCase.ts b/src/background/usecases/NavigateUseCase.ts index 76e1c8e..ced2a0b 100644 --- a/src/background/usecases/NavigateUseCase.ts +++ b/src/background/usecases/NavigateUseCase.ts @@ -30,11 +30,28 @@ export default class NavigateUseCase { await this.navigateClient.linkPrev(tab.id!!); } - openParent(): Promise { - throw new Error('not implemented'); + async openParent(): Promise { + let tab = await this.tabPresenter.getCurrent(); + let url = new URL(tab.url!!); + if (url.hash !== '') { + url.hash = ''; + } else if (url.search !== '') { + 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); } - openRoot(): Promise { - throw new Error('not implemented'); + async openRoot(): Promise { + let tab = await this.tabPresenter.getCurrent(); + let url = new URL(tab.url!!); + await this.tabPresenter.open(url.origin); } } -- cgit v1.2.3