From 5a7db96febd4e3cb2aa519610da75e0bccd69a7f Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 23 May 2019 21:30:07 +0900 Subject: Move some navigation operations to background --- src/background/controllers/OperationController.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/background/controllers') diff --git a/src/background/controllers/OperationController.ts b/src/background/controllers/OperationController.ts index de6f8cb..cb5c043 100644 --- a/src/background/controllers/OperationController.ts +++ b/src/background/controllers/OperationController.ts @@ -5,6 +5,7 @@ import ConsoleUseCase from '../usecases/ConsoleUseCase'; import TabUseCase from '../usecases/TabUseCase'; import TabSelectUseCase from '../usecases/TabSelectUseCase'; import ZoomUseCase from '../usecases/ZoomUseCase'; +import NavigateUseCase from '../usecases/NavigateUseCase'; @injectable() export default class OperationController { @@ -14,6 +15,7 @@ export default class OperationController { private tabUseCase: TabUseCase, private tabSelectUseCase: TabSelectUseCase, private zoomUseCase: ZoomUseCase, + private navigateUseCase: NavigateUseCase, ) { } @@ -74,6 +76,14 @@ export default class OperationController { return this.findUseCase.findStart(); case operations.CANCEL: return this.consoleUseCase.hideConsole(); + case operations.NAVIGATE_HISTORY_PREV: + return this.navigateUseCase.openHistoryPrev(); + case operations.NAVIGATE_HISTORY_NEXT: + return this.navigateUseCase.openHistoryNext(); + case operations.NAVIGATE_LINK_PREV: + return this.navigateUseCase.openLinkPrev(); + case operations.NAVIGATE_LINK_NEXT: + return this.navigateUseCase.openLinkNext(); } throw new Error('unknown operation: ' + operation.type); } -- cgit v1.2.3 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 +++++-- src/content/controllers/KeymapController.ts | 8 --- src/content/presenters/NavigationPresenter.ts | 27 ------- src/content/usecases/NavigateUseCase.ts | 8 --- test/background/usecases/NavigateUseCase.test.ts | 82 ++++++++++++++++++++++ .../content/presenters/NavigationPresenter.test.ts | 9 --- 8 files changed, 108 insertions(+), 57 deletions(-) create mode 100644 test/background/usecases/NavigateUseCase.test.ts (limited to 'src/background/controllers') 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); } } diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts index 4be8f9d..4eb6955 100644 --- a/src/content/controllers/KeymapController.ts +++ b/src/content/controllers/KeymapController.ts @@ -4,7 +4,6 @@ import KeymapUseCase from '../usecases/KeymapUseCase'; import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase'; import FindSlaveUseCase from '../usecases/FindSlaveUseCase'; import ScrollUseCase from '../usecases/ScrollUseCase'; -import NavigateUseCase from '../usecases/NavigateUseCase'; import FocusUseCase from '../usecases/FocusUseCase'; import ClipboardUseCase from '../usecases/ClipboardUseCase'; import BackgroundClient from '../client/BackgroundClient'; @@ -19,7 +18,6 @@ export default class KeymapController { private addonEnabledUseCase: AddonEnabledUseCase, private findSlaveUseCase: FindSlaveUseCase, private scrollUseCase: ScrollUseCase, - private navigateUseCase: NavigateUseCase, private focusUseCase: FocusUseCase, private clipbaordUseCase: ClipboardUseCase, private backgroundClient: BackgroundClient, @@ -84,12 +82,6 @@ export default class KeymapController { case operations.MARK_JUMP_PREFIX: this.markKeyUseCase.enableJumpMode(); break; - case operations.NAVIGATE_PARENT: - this.navigateUseCase.openParent(); - break; - case operations.NAVIGATE_ROOT: - this.navigateUseCase.openRoot(); - break; case operations.FOCUS_INPUT: this.focusUseCase.focusFirstInput(); break; diff --git a/src/content/presenters/NavigationPresenter.ts b/src/content/presenters/NavigationPresenter.ts index c141112..11d96ec 100644 --- a/src/content/presenters/NavigationPresenter.ts +++ b/src/content/presenters/NavigationPresenter.ts @@ -6,10 +6,6 @@ export default interface NavigationPresenter { openLinkPrev(): void; openLinkNext(): void; - - openParent(): void; - - openRoot(): void; } const REL_PATTERN: {[key: string]: RegExp} = { @@ -51,29 +47,6 @@ export class NavigationPresenterImpl implements NavigationPresenter { 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. private linkRel(rel: 'prev' | 'next'): void { let link = selectLast(`link[rel~=${rel}][href]`); diff --git a/src/content/usecases/NavigateUseCase.ts b/src/content/usecases/NavigateUseCase.ts index 4711c5e..7adccfd 100644 --- a/src/content/usecases/NavigateUseCase.ts +++ b/src/content/usecases/NavigateUseCase.ts @@ -24,12 +24,4 @@ export default class NavigateUseCase { openLinkNext(): void { this.navigationPresenter.openLinkNext(); } - - openParent(): void { - this.navigationPresenter.openParent(); - } - - openRoot(): void { - this.navigationPresenter.openRoot(); - } } diff --git a/test/background/usecases/NavigateUseCase.test.ts b/test/background/usecases/NavigateUseCase.test.ts new file mode 100644 index 0000000..13f3e99 --- /dev/null +++ b/test/background/usecases/NavigateUseCase.test.ts @@ -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(); + }); + }); +}); diff --git a/test/content/presenters/NavigationPresenter.test.ts b/test/content/presenters/NavigationPresenter.test.ts index c1aca9a..5b6a8ca 100644 --- a/test/content/presenters/NavigationPresenter.test.ts +++ b/test/content/presenters/NavigationPresenter.test.ts @@ -132,13 +132,4 @@ describe('NavigationPresenter', () => { 'next page' )); }); - - 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; - }); - }); }); -- cgit v1.2.3 From ccbe08cf66e16084c919f0b2fa2da81258c01d41 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 25 May 2019 21:33:33 +0900 Subject: Repeat last operation --- src/background/controllers/OperationController.ts | 19 ++++++++- src/background/repositories/RepeatRepository.ts | 22 ++++++++++ src/background/usecases/RepeatUseCase.ts | 49 +++++++++++++++++++++++ src/content/client/BackgroundClient.ts | 13 ------ src/content/client/OperationClient.ts | 15 +++++++ src/content/controllers/KeymapController.ts | 6 ++- src/content/di.ts | 2 + src/shared/operations.ts | 11 ++++- 8 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 src/background/repositories/RepeatRepository.ts create mode 100644 src/background/usecases/RepeatUseCase.ts delete mode 100644 src/content/client/BackgroundClient.ts create mode 100644 src/content/client/OperationClient.ts (limited to 'src/background/controllers') diff --git a/src/background/controllers/OperationController.ts b/src/background/controllers/OperationController.ts index 1e80f54..7233c0e 100644 --- a/src/background/controllers/OperationController.ts +++ b/src/background/controllers/OperationController.ts @@ -6,6 +6,7 @@ import TabUseCase from '../usecases/TabUseCase'; import TabSelectUseCase from '../usecases/TabSelectUseCase'; import ZoomUseCase from '../usecases/ZoomUseCase'; import NavigateUseCase from '../usecases/NavigateUseCase'; +import RepeatUseCase from '../usecases/RepeatUseCase'; @injectable() export default class OperationController { @@ -16,11 +17,19 @@ export default class OperationController { private tabSelectUseCase: TabSelectUseCase, private zoomUseCase: ZoomUseCase, private navigateUseCase: NavigateUseCase, + private repeatUseCase: RepeatUseCase, ) { } + async exec(op: operations.Operation): Promise { + await this.doOperation(op); + if (this.repeatUseCase.isRepeatable(op)) { + this.repeatUseCase.storeLastOperation(op); + } + } + // eslint-disable-next-line complexity, max-lines-per-function - exec(operation: operations.Operation): Promise { + doOperation(operation: operations.Operation): Promise { switch (operation.type) { case operations.TAB_CLOSE: return this.tabUseCase.close(false); @@ -88,6 +97,14 @@ export default class OperationController { return this.navigateUseCase.openParent(); case operations.NAVIGATE_ROOT: return this.navigateUseCase.openRoot(); + case operations.REPEAT_LAST: + { + let last = this.repeatUseCase.getLastOperation(); + if (typeof last !== 'undefined') { + return this.doOperation(last); + } + return Promise.resolve(); + } } throw new Error('unknown operation: ' + operation.type); } diff --git a/src/background/repositories/RepeatRepository.ts b/src/background/repositories/RepeatRepository.ts new file mode 100644 index 0000000..c7f7a71 --- /dev/null +++ b/src/background/repositories/RepeatRepository.ts @@ -0,0 +1,22 @@ +import { injectable } from 'tsyringe'; +import { Operation } from '../../shared/operations'; +import MemoryStorage from '../infrastructures/MemoryStorage'; + +const REPEAT_KEY = 'repeat'; + +@injectable() +export default class RepeatRepository { + private cache: MemoryStorage; + + constructor() { + this.cache = new MemoryStorage(); + } + + getLastOperation(): Operation | undefined { + return this.cache.get(REPEAT_KEY); + } + + setLastOperation(op: Operation): void { + this.cache.set(REPEAT_KEY, op); + } +} diff --git a/src/background/usecases/RepeatUseCase.ts b/src/background/usecases/RepeatUseCase.ts new file mode 100644 index 0000000..a005682 --- /dev/null +++ b/src/background/usecases/RepeatUseCase.ts @@ -0,0 +1,49 @@ +import { injectable } from 'tsyringe'; +import * as operations from '../../shared/operations'; +import RepeatRepository from '../repositories/RepeatRepository'; + +type Operation = operations.Operation; + +@injectable() +export default class RepeatUseCase { + constructor( + private repeatRepository: RepeatRepository, + ) { + } + + storeLastOperation(op: Operation): void { + this.repeatRepository.setLastOperation(op); + } + + getLastOperation(): operations.Operation | undefined { + return this.repeatRepository.getLastOperation(); + } + + // eslint-disable-next-line complexity + isRepeatable(op: Operation): boolean { + switch (op.type) { + case operations.NAVIGATE_HISTORY_PREV: + case operations.NAVIGATE_HISTORY_NEXT: + case operations.NAVIGATE_LINK_PREV: + case operations.NAVIGATE_LINK_NEXT: + case operations.NAVIGATE_PARENT: + case operations.NAVIGATE_ROOT: + case operations.PAGE_SOURCE: + case operations.PAGE_HOME: + case operations.TAB_CLOSE: + case operations.TAB_CLOSE_FORCE: + case operations.TAB_CLOSE_RIGHT: + case operations.TAB_REOPEN: + case operations.TAB_RELOAD: + case operations.TAB_PIN: + case operations.TAB_UNPIN: + case operations.TAB_TOGGLE_PINNED: + case operations.TAB_DUPLICATE: + case operations.ZOOM_IN: + case operations.ZOOM_OUT: + case operations.ZOOM_NEUTRAL: + return true; + } + return false; + } +} diff --git a/src/content/client/BackgroundClient.ts b/src/content/client/BackgroundClient.ts deleted file mode 100644 index 4a41184..0000000 --- a/src/content/client/BackgroundClient.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { injectable } from 'tsyringe'; -import * as operations from '../../shared/operations'; -import * as messages from '../../shared/messages'; - -@injectable() -export default class BackgroundClient { - execBackgroundOp(op: operations.Operation): Promise { - return browser.runtime.sendMessage({ - type: messages.BACKGROUND_OPERATION, - operation: op, - }); - } -} diff --git a/src/content/client/OperationClient.ts b/src/content/client/OperationClient.ts new file mode 100644 index 0000000..d699fec --- /dev/null +++ b/src/content/client/OperationClient.ts @@ -0,0 +1,15 @@ +import * as operations from '../../shared/operations'; +import * as messages from '../../shared/messages'; + +export default interface OperationClient { + execBackgroundOp(op: operations.Operation): Promise; +} + +export class OperationClientImpl implements OperationClient { + execBackgroundOp(op: operations.Operation): Promise { + return browser.runtime.sendMessage({ + type: messages.BACKGROUND_OPERATION, + operation: op, + }); + } +} diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts index 4eb6955..fcfaff1 100644 --- a/src/content/controllers/KeymapController.ts +++ b/src/content/controllers/KeymapController.ts @@ -6,7 +6,7 @@ import FindSlaveUseCase from '../usecases/FindSlaveUseCase'; import ScrollUseCase from '../usecases/ScrollUseCase'; import FocusUseCase from '../usecases/FocusUseCase'; import ClipboardUseCase from '../usecases/ClipboardUseCase'; -import BackgroundClient from '../client/BackgroundClient'; +import OperationClient from '../client/OperationClient'; import MarkKeyyUseCase from '../usecases/MarkKeyUseCase'; import FollowMasterClient from '../client/FollowMasterClient'; import Key from '../domains/Key'; @@ -20,9 +20,11 @@ export default class KeymapController { private scrollUseCase: ScrollUseCase, private focusUseCase: FocusUseCase, private clipbaordUseCase: ClipboardUseCase, - private backgroundClient: BackgroundClient, private markKeyUseCase: MarkKeyyUseCase, + @inject('OperationClient') + private backgroundClient: OperationClient, + @inject('FollowMasterClient') private followMasterClient: FollowMasterClient, ) { diff --git a/src/content/di.ts b/src/content/di.ts index 23be027..e18806a 100644 --- a/src/content/di.ts +++ b/src/content/di.ts @@ -21,6 +21,7 @@ import { MarkClientImpl } from './client/MarkClient'; import { MarkKeyRepositoryImpl } from './repositories/MarkKeyRepository'; import { MarkRepositoryImpl } from './repositories/MarkRepository'; import { NavigationPresenterImpl } from './presenters/NavigationPresenter'; +import { OperationClientImpl } from './client/OperationClient'; import { ScrollPresenterImpl } from './presenters/ScrollPresenter'; import { SettingClientImpl } from './client/SettingClient'; import { SettingRepositoryImpl } from './repositories/SettingRepository'; @@ -48,6 +49,7 @@ container.register('MarkClient', { useClass: MarkClientImpl }); container.register('MarkKeyRepository', { useClass: MarkKeyRepositoryImpl }); container.register('MarkRepository', { useClass: MarkRepositoryImpl }); container.register('NavigationPresenter', { useClass: NavigationPresenterImpl }); +container.register('OperationClient', { useClass: OperationClientImpl }); container.register('ScrollPresenter', { useClass: ScrollPresenterImpl }); container.register('SettingClient', { useClass: SettingClientImpl }); container.register('SettingRepository', { useClass: SettingRepositoryImpl }); diff --git a/src/shared/operations.ts b/src/shared/operations.ts index 688c240..0f0d0c0 100644 --- a/src/shared/operations.ts +++ b/src/shared/operations.ts @@ -75,6 +75,9 @@ export const FIND_PREV = 'find.prev'; export const MARK_SET_PREFIX = 'mark.set.prefix'; export const MARK_JUMP_PREFIX = 'mark.jump.prefix'; +// Repeat +export const REPEAT_LAST = 'repeat.last'; + export interface CancelOperation { type: typeof CANCEL; } @@ -291,6 +294,10 @@ export interface MarkJumpPrefixOperation { type: typeof MARK_JUMP_PREFIX; } +export interface RepeatLastOperation { + type: typeof REPEAT_LAST; +} + export type Operation = CancelOperation | AddonEnableOperation | @@ -342,7 +349,8 @@ export type Operation = FindNextOperation | FindPrevOperation | MarkSetPrefixOperation | - MarkJumpPrefixOperation; + MarkJumpPrefixOperation | + RepeatLastOperation; const assertOptionalBoolean = (obj: any, name: string) => { if (Object.prototype.hasOwnProperty.call(obj, name) && @@ -441,6 +449,7 @@ export const valueOf = (o: any): Operation => { case FIND_PREV: case MARK_SET_PREFIX: case MARK_JUMP_PREFIX: + case REPEAT_LAST: return { type: o.type }; } throw new TypeError('unknown operation type: ' + o.type); -- cgit v1.2.3 From 48e005dc825a5211b4d3e92ed06ad15e01c23d13 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 25 May 2019 21:55:45 +0900 Subject: Repeat open, tabopen and winopen command --- src/background/controllers/OperationController.ts | 3 +++ src/background/usecases/CommandUseCase.ts | 17 ++++++++++++ src/background/usecases/RepeatUseCase.ts | 1 + src/background/usecases/TabUseCase.ts | 15 +++++++++++ src/content/client/OperationClient.ts | 18 +++++++++++++ src/content/usecases/ClipboardUseCase.ts | 10 ++++--- src/shared/operations.ts | 33 ++++++++++++++++++++++- 7 files changed, 93 insertions(+), 4 deletions(-) (limited to 'src/background/controllers') diff --git a/src/background/controllers/OperationController.ts b/src/background/controllers/OperationController.ts index 7233c0e..51cff28 100644 --- a/src/background/controllers/OperationController.ts +++ b/src/background/controllers/OperationController.ts @@ -105,6 +105,9 @@ export default class OperationController { } return Promise.resolve(); } + case operations.INTERNAL_OPEN_URL: + return this.tabUseCase.openURL( + operation.url, operation.newTab, operation.newWindow); } throw new Error('unknown operation: ' + operation.type); } diff --git a/src/background/usecases/CommandUseCase.ts b/src/background/usecases/CommandUseCase.ts index 921a779..a526cfc 100644 --- a/src/background/usecases/CommandUseCase.ts +++ b/src/background/usecases/CommandUseCase.ts @@ -1,4 +1,5 @@ import { injectable } from 'tsyringe'; +import * as operations from '../../shared/operations'; import * as parsers from './parsers'; import * as urls from '../../shared/urls'; import TabPresenter from '../presenters/TabPresenter'; @@ -7,6 +8,7 @@ import SettingRepository from '../repositories/SettingRepository'; import BookmarkRepository from '../repositories/BookmarkRepository'; import ConsoleClient from '../infrastructures/ConsoleClient'; import ContentMessageClient from '../infrastructures/ContentMessageClient'; +import RepeatUseCase from '../usecases/RepeatUseCase'; @injectable() export default class CommandIndicator { @@ -17,21 +19,36 @@ export default class CommandIndicator { private bookmarkRepository: BookmarkRepository, private consoleClient: ConsoleClient, private contentMessageClient: ContentMessageClient, + private repeatUseCase: RepeatUseCase, ) { } async open(keywords: string): Promise { let url = await this.urlOrSearch(keywords); + this.repeatUseCase.storeLastOperation({ + type: operations.INTERNAL_OPEN_URL, + url, + }); return this.tabPresenter.open(url); } async tabopen(keywords: string): Promise { let url = await this.urlOrSearch(keywords); + this.repeatUseCase.storeLastOperation({ + type: operations.INTERNAL_OPEN_URL, + url, + newTab: true, + }); return this.tabPresenter.create(url); } async winopen(keywords: string): Promise { let url = await this.urlOrSearch(keywords); + this.repeatUseCase.storeLastOperation({ + type: operations.INTERNAL_OPEN_URL, + url, + newWindow: true, + }); return this.windowPresenter.create(url); } diff --git a/src/background/usecases/RepeatUseCase.ts b/src/background/usecases/RepeatUseCase.ts index a005682..d78de34 100644 --- a/src/background/usecases/RepeatUseCase.ts +++ b/src/background/usecases/RepeatUseCase.ts @@ -42,6 +42,7 @@ export default class RepeatUseCase { case operations.ZOOM_IN: case operations.ZOOM_OUT: case operations.ZOOM_NEUTRAL: + case operations.INTERNAL_OPEN_URL: return true; } return false; diff --git a/src/background/usecases/TabUseCase.ts b/src/background/usecases/TabUseCase.ts index 0239a87..31112a9 100644 --- a/src/background/usecases/TabUseCase.ts +++ b/src/background/usecases/TabUseCase.ts @@ -1,11 +1,13 @@ import { injectable } from 'tsyringe'; import TabPresenter from '../presenters/TabPresenter'; +import WindowPresenter from '../presenters/WindowPresenter'; import BrowserSettingRepository from '../repositories/BrowserSettingRepository'; @injectable() export default class TabUseCase { constructor( private tabPresenter: TabPresenter, + private windowPresenter: WindowPresenter, private browserSettingRepository: BrowserSettingRepository, ) { } @@ -77,4 +79,17 @@ export default class TabUseCase { this.tabPresenter.create(url); } } + + async openURL( + url: string, newTab?: boolean, newWindow?: boolean, + ): Promise { + if (newWindow) { + await this.windowPresenter.create(url); + } else if (newTab) { + await this.tabPresenter.create(url); + } else { + let tab = await this.tabPresenter.getCurrent(); + await this.tabPresenter.open(url, tab.id); + } + } } diff --git a/src/content/client/OperationClient.ts b/src/content/client/OperationClient.ts index d699fec..5dbe555 100644 --- a/src/content/client/OperationClient.ts +++ b/src/content/client/OperationClient.ts @@ -3,6 +3,10 @@ import * as messages from '../../shared/messages'; export default interface OperationClient { execBackgroundOp(op: operations.Operation): Promise; + + internalOpenUrl( + url: string, newTab?: boolean, background?: boolean, + ): Promise; } export class OperationClientImpl implements OperationClient { @@ -12,4 +16,18 @@ export class OperationClientImpl implements OperationClient { operation: op, }); } + + internalOpenUrl( + url: string, newTab?: boolean, background?: boolean, + ): Promise { + return browser.runtime.sendMessage({ + type: messages.BACKGROUND_OPERATION, + operation: { + type: operations.INTERNAL_OPEN_URL, + url, + newTab, + background, + }, + }); + } } diff --git a/src/content/usecases/ClipboardUseCase.ts b/src/content/usecases/ClipboardUseCase.ts index 8c4d621..c8fe719 100644 --- a/src/content/usecases/ClipboardUseCase.ts +++ b/src/content/usecases/ClipboardUseCase.ts @@ -2,16 +2,16 @@ import { injectable, inject } from 'tsyringe'; import * as urls from '../../shared/urls'; import ClipboardRepository from '../repositories/ClipboardRepository'; import SettingRepository from '../repositories/SettingRepository'; -import TabsClient from '../client/TabsClient'; import ConsoleClient from '../client/ConsoleClient'; +import OperationClient from '../client/OperationClient'; @injectable() export default class ClipboardUseCase { constructor( @inject('ClipboardRepository') private repository: ClipboardRepository, @inject('SettingRepository') private settingRepository: SettingRepository, - @inject('TabsClient') private client: TabsClient, @inject('ConsoleClient') private consoleClient: ConsoleClient, + @inject('OperationClient') private operationClinet: OperationClient, ) { } @@ -26,6 +26,10 @@ export default class ClipboardUseCase { let search = this.settingRepository.get().search; let text = this.repository.read(); let url = urls.searchUrl(text, search); - await this.client.openUrl(url, newTab); + + // TODO: Repeat pasting from clipboard instead of opening a certain url. + // 'Repeat last' command is implemented in the background script and cannot + // access to clipboard until Firefox 63. + await this.operationClinet.internalOpenUrl(url, newTab); } } diff --git a/src/shared/operations.ts b/src/shared/operations.ts index 0f0d0c0..2b03d9d 100644 --- a/src/shared/operations.ts +++ b/src/shared/operations.ts @@ -78,6 +78,9 @@ export const MARK_JUMP_PREFIX = 'mark.jump.prefix'; // Repeat export const REPEAT_LAST = 'repeat.last'; +// Internal +export const INTERNAL_OPEN_URL = 'internal.open.url'; + export interface CancelOperation { type: typeof CANCEL; } @@ -298,6 +301,14 @@ export interface RepeatLastOperation { type: typeof REPEAT_LAST; } +export interface InternalOpenUrl { + type: typeof INTERNAL_OPEN_URL; + url: string; + newTab?: boolean; + newWindow?: boolean; + background?: boolean; +} + export type Operation = CancelOperation | AddonEnableOperation | @@ -350,7 +361,8 @@ export type Operation = FindPrevOperation | MarkSetPrefixOperation | MarkJumpPrefixOperation | - RepeatLastOperation; + RepeatLastOperation | + InternalOpenUrl; const assertOptionalBoolean = (obj: any, name: string) => { if (Object.prototype.hasOwnProperty.call(obj, name) && @@ -366,6 +378,13 @@ const assertRequiredNumber = (obj: any, name: string) => { } }; +const assertRequiredString = (obj: any, name: string) => { + if (!Object.prototype.hasOwnProperty.call(obj, name) || + typeof obj[name] !== 'string') { + throw new TypeError(`Missing string parameter '${name}`); + } +}; + // eslint-disable-next-line complexity, max-lines-per-function export const valueOf = (o: any): Operation => { if (!Object.prototype.hasOwnProperty.call(o, 'type')) { @@ -409,6 +428,18 @@ export const valueOf = (o: any): Operation => { type: URLS_PASTE, newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), }; + case INTERNAL_OPEN_URL: + assertOptionalBoolean(o, 'newTab'); + assertOptionalBoolean(o, 'newWindow'); + assertOptionalBoolean(o, 'background'); + assertRequiredString(o, 'url'); + return { + type: INTERNAL_OPEN_URL, + url: o.url, + newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), + newWindow: Boolean(typeof o.newWindow === undefined ? false : o.newWindow), // eslint-disable-line max-len + background: Boolean(typeof o.background === undefined ? true : o.background), // eslint-disable-line max-len + }; case CANCEL: case ADDON_ENABLE: case ADDON_DISABLE: -- cgit v1.2.3