Move open parent and open root to background

jh-changes
Shin'ya Ueoka 6 years ago
parent e779fb1779
commit 8d0739463d
  1. 4
      src/background/controllers/OperationController.ts
  2. 2
      src/background/presenters/TabPresenter.ts
  3. 25
      src/background/usecases/NavigateUseCase.ts
  4. 8
      src/content/controllers/KeymapController.ts
  5. 27
      src/content/presenters/NavigationPresenter.ts
  6. 8
      src/content/usecases/NavigateUseCase.ts
  7. 82
      test/background/usecases/NavigateUseCase.test.ts
  8. 9
      test/content/presenters/NavigationPresenter.test.ts

@ -84,6 +84,10 @@ export default class OperationController {
return this.navigateUseCase.openLinkPrev(); return this.navigateUseCase.openLinkPrev();
case operations.NAVIGATE_LINK_NEXT: case operations.NAVIGATE_LINK_NEXT:
return this.navigateUseCase.openLinkNext(); 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); throw new Error('unknown operation: ' + operation.type);
} }

@ -36,7 +36,7 @@ export default class TabPresenter {
return tabId; return tabId;
} }
async getByKeyword(keyword: string, excludePinned = false): Promise<Tab[]> { async getByKeyword(keyword: string, excludePinned: boolean = false): Promise<Tab[]> {
let tabs = await browser.tabs.query({ currentWindow: true }); let tabs = await browser.tabs.query({ currentWindow: true });
return tabs.filter((t) => { return tabs.filter((t) => {
return t.url && t.url.toLowerCase().includes(keyword.toLowerCase()) || return t.url && t.url.toLowerCase().includes(keyword.toLowerCase()) ||

@ -30,11 +30,28 @@ export default class NavigateUseCase {
await this.navigateClient.linkPrev(tab.id!!); await this.navigateClient.linkPrev(tab.id!!);
} }
openParent(): Promise<void> { async openParent(): Promise<void> {
throw new Error('not implemented'); 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<void> { async openRoot(): Promise<void> {
throw new Error('not implemented'); let tab = await this.tabPresenter.getCurrent();
let url = new URL(tab.url!!);
await this.tabPresenter.open(url.origin);
} }
} }

@ -4,7 +4,6 @@ import KeymapUseCase from '../usecases/KeymapUseCase';
import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase'; import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase';
import FindSlaveUseCase from '../usecases/FindSlaveUseCase'; import FindSlaveUseCase from '../usecases/FindSlaveUseCase';
import ScrollUseCase from '../usecases/ScrollUseCase'; import ScrollUseCase from '../usecases/ScrollUseCase';
import NavigateUseCase from '../usecases/NavigateUseCase';
import FocusUseCase from '../usecases/FocusUseCase'; import FocusUseCase from '../usecases/FocusUseCase';
import ClipboardUseCase from '../usecases/ClipboardUseCase'; import ClipboardUseCase from '../usecases/ClipboardUseCase';
import BackgroundClient from '../client/BackgroundClient'; import BackgroundClient from '../client/BackgroundClient';
@ -19,7 +18,6 @@ export default class KeymapController {
private addonEnabledUseCase: AddonEnabledUseCase, private addonEnabledUseCase: AddonEnabledUseCase,
private findSlaveUseCase: FindSlaveUseCase, private findSlaveUseCase: FindSlaveUseCase,
private scrollUseCase: ScrollUseCase, private scrollUseCase: ScrollUseCase,
private navigateUseCase: NavigateUseCase,
private focusUseCase: FocusUseCase, private focusUseCase: FocusUseCase,
private clipbaordUseCase: ClipboardUseCase, private clipbaordUseCase: ClipboardUseCase,
private backgroundClient: BackgroundClient, private backgroundClient: BackgroundClient,
@ -84,12 +82,6 @@ export default class KeymapController {
case operations.MARK_JUMP_PREFIX: case operations.MARK_JUMP_PREFIX:
this.markKeyUseCase.enableJumpMode(); this.markKeyUseCase.enableJumpMode();
break; break;
case operations.NAVIGATE_PARENT:
this.navigateUseCase.openParent();
break;
case operations.NAVIGATE_ROOT:
this.navigateUseCase.openRoot();
break;
case operations.FOCUS_INPUT: case operations.FOCUS_INPUT:
this.focusUseCase.focusFirstInput(); this.focusUseCase.focusFirstInput();
break; break;

@ -6,10 +6,6 @@ export default interface NavigationPresenter {
openLinkPrev(): void; openLinkPrev(): void;
openLinkNext(): void; openLinkNext(): void;
openParent(): void;
openRoot(): void;
} }
const REL_PATTERN: {[key: string]: RegExp} = { const REL_PATTERN: {[key: string]: RegExp} = {
@ -51,29 +47,6 @@ export class NavigationPresenterImpl implements NavigationPresenter {
this.linkRel('next'); this.linkRel('next');
} }
openParent(): void {
const loc = window.location;
if (loc.hash !== '') {
loc.hash = '';
return;
} else if (loc.search !== '') {
loc.search = '';
return;
}
const basenamePattern = /\/[^/]+$/;
const lastDirPattern = /\/[^/]+\/$/;
if (basenamePattern.test(loc.pathname)) {
loc.pathname = loc.pathname.replace(basenamePattern, '/');
} else if (lastDirPattern.test(loc.pathname)) {
loc.pathname = loc.pathname.replace(lastDirPattern, '/');
}
}
openRoot(): void {
window.location.href = window.location.origin;
}
// Code common to linkPrev and linkNext which navigates to the specified page. // Code common to linkPrev and linkNext which navigates to the specified page.
private linkRel(rel: 'prev' | 'next'): void { private linkRel(rel: 'prev' | 'next'): void {
let link = selectLast<HTMLLinkElement>(`link[rel~=${rel}][href]`); let link = selectLast<HTMLLinkElement>(`link[rel~=${rel}][href]`);

@ -24,12 +24,4 @@ export default class NavigateUseCase {
openLinkNext(): void { openLinkNext(): void {
this.navigationPresenter.openLinkNext(); this.navigationPresenter.openLinkNext();
} }
openParent(): void {
this.navigationPresenter.openParent();
}
openRoot(): void {
this.navigationPresenter.openRoot();
}
} }

@ -0,0 +1,82 @@
import TabPresenter from '../../../src/background/presenters/TabPresenter';
import NavigateUseCase from '../../../src/background/usecases/NavigateUseCase';
import NavigateClient from '../../../src/background/clients/NavigateClient';
// import { expect } from 'chai';
import * as sinon from 'sinon';
describe('NavigateUseCase', () => {
let sut: NavigateUseCase;
let tabPresenter: TabPresenter;
let navigateClient: NavigateClient;
beforeEach(() => {
tabPresenter = new TabPresenter();
navigateClient = new NavigateClient();
sut = new NavigateUseCase(tabPresenter, navigateClient);
});
describe('#openParent()', async () => {
it.only('opens parent directory of file', async() => {
var stub = sinon.stub(tabPresenter, 'getCurrent');
stub.returns(Promise.resolve({ url: 'https://google.com/fruits/yellow/banana' }))
var mock = sinon.mock(tabPresenter);
mock.expects('open').withArgs('https://google.com/fruits/yellow/');
await sut.openParent();
mock.verify();
});
it.only('opens parent directory of directory', async() => {
var stub = sinon.stub(tabPresenter, 'getCurrent');
stub.returns(Promise.resolve({ url: 'https://google.com/fruits/yellow/' }))
var mock = sinon.mock(tabPresenter);
mock.expects('open').withArgs('https://google.com/fruits/');
await sut.openParent();
mock.verify();
});
it.only('removes hash', async() => {
var stub = sinon.stub(tabPresenter, 'getCurrent');
stub.returns(Promise.resolve({ url: 'https://google.com/#top' }))
var mock = sinon.mock(tabPresenter);
mock.expects('open').withArgs('https://google.com/');
await sut.openParent();
mock.verify();
});
it.only('removes search query', async() => {
var stub = sinon.stub(tabPresenter, 'getCurrent');
stub.returns(Promise.resolve({ url: 'https://google.com/search?q=apple' }))
var mock = sinon.mock(tabPresenter);
mock.expects('open').withArgs('https://google.com/search');
await sut.openParent();
mock.verify();
});
});
describe('#openRoot()', () => {
it.only('opens root direectory', async() => {
var stub = sinon.stub(tabPresenter, 'getCurrent');
stub.returns(Promise.resolve({
url: 'https://google.com/seach?q=apple',
}))
var mock = sinon.mock(tabPresenter);
mock.expects('open').withArgs('https://google.com');
await sut.openRoot();
mock.verify();
});
});
});

@ -132,13 +132,4 @@ describe('NavigationPresenter', () => {
'<a href="#dummy">next page</a><a rel="next" href="#next">click me</a>' '<a href="#dummy">next page</a><a rel="next" href="#next">click me</a>'
)); ));
}); });
describe('#parent', () => {
// NOTE: not able to test location
it('removes hash', () => {
window.location.hash = '#section-1';
sut.openParent();
expect(document.location.hash).to.be.empty;
});
});
}); });