diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-12-22 10:47:00 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-22 10:47:00 +0900 |
commit | b2dcdedad729ff7087867da50e20578f9fc8fb29 (patch) | |
tree | 033ecffbd7db9b6db8000464a68d748fcae1dc3d /src/background | |
parent | 3c7230c3036e8bb2b2e9a752be9b0ef4a0a7349d (diff) | |
parent | 75f86907fc2699c0f0661d4780c38249a18f849b (diff) |
Merge pull request #689 from ueokande/n-times-repeat-operations
Repeat commands n-times
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/controllers/OperationController.ts | 175 | ||||
-rw-r--r-- | src/background/infrastructures/ContentMessageListener.ts | 9 |
2 files changed, 99 insertions, 85 deletions
diff --git a/src/background/controllers/OperationController.ts b/src/background/controllers/OperationController.ts index 7a10ad6..2f5d4a6 100644 --- a/src/background/controllers/OperationController.ts +++ b/src/background/controllers/OperationController.ts @@ -21,95 +21,108 @@ export default class OperationController { ) { } - async exec(op: operations.Operation): Promise<any> { - await this.doOperation(op); + async exec(repeat: number, op: operations.Operation): Promise<any> { + await this.doOperation(repeat, op); if (this.repeatUseCase.isRepeatable(op)) { this.repeatUseCase.storeLastOperation(op); } } // eslint-disable-next-line complexity, max-lines-per-function - doOperation(operation: operations.Operation): Promise<any> { - switch (operation.type) { - case operations.TAB_CLOSE: - return this.tabUseCase.close(false, operation.select === 'left'); - case operations.TAB_CLOSE_RIGHT: - return this.tabUseCase.closeRight(); - case operations.TAB_CLOSE_FORCE: - return this.tabUseCase.close(true); - case operations.TAB_REOPEN: - return this.tabUseCase.reopen(); - case operations.TAB_PREV: - return this.tabSelectUseCase.selectPrev(1); - case operations.TAB_NEXT: - return this.tabSelectUseCase.selectNext(1); - case operations.TAB_FIRST: - return this.tabSelectUseCase.selectFirst(); - case operations.TAB_LAST: - return this.tabSelectUseCase.selectLast(); - case operations.TAB_PREV_SEL: - return this.tabSelectUseCase.selectPrevSelected(); - case operations.TAB_RELOAD: - return this.tabUseCase.reload(operation.cache); - case operations.TAB_PIN: - return this.tabUseCase.setPinned(true); - case operations.TAB_UNPIN: - return this.tabUseCase.setPinned(false); - case operations.TAB_TOGGLE_PINNED: - return this.tabUseCase.togglePinned(); - case operations.TAB_DUPLICATE: - return this.tabUseCase.duplicate(); - case operations.PAGE_SOURCE: - return this.tabUseCase.openPageSource(); - case operations.PAGE_HOME: - return this.tabUseCase.openHome(operation.newTab); - case operations.ZOOM_IN: - return this.zoomUseCase.zoomIn(); - case operations.ZOOM_OUT: - return this.zoomUseCase.zoomOut(); - case operations.ZOOM_NEUTRAL: - return this.zoomUseCase.zoomNutoral(); - case operations.COMMAND_SHOW: - return this.consoleUseCase.showCommand(); - case operations.COMMAND_SHOW_OPEN: - return this.consoleUseCase.showOpenCommand(operation.alter); - case operations.COMMAND_SHOW_TABOPEN: - return this.consoleUseCase.showTabopenCommand(operation.alter); - case operations.COMMAND_SHOW_WINOPEN: - return this.consoleUseCase.showWinopenCommand(operation.alter); - case operations.COMMAND_SHOW_BUFFER: - return this.consoleUseCase.showBufferCommand(); - case operations.COMMAND_SHOW_ADDBOOKMARK: - return this.consoleUseCase.showAddbookmarkCommand(operation.alter); - case operations.FIND_START: - 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(); - case operations.NAVIGATE_PARENT: - 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); + async doOperation( + repeat: number, + operation: operations.Operation, + ): Promise<any> { + // eslint-disable-next-line complexity, max-lines-per-function + const opFunc = (() => { + switch (operation.type) { + case operations.TAB_CLOSE: + return () => this.tabUseCase.close(false, operation.select === 'left'); + case operations.TAB_CLOSE_RIGHT: + return () => this.tabUseCase.closeRight(); + case operations.TAB_CLOSE_FORCE: + return () => this.tabUseCase.close(true); + case operations.TAB_REOPEN: + return () => this.tabUseCase.reopen(); + case operations.TAB_PREV: + return () => this.tabSelectUseCase.selectPrev(1); + case operations.TAB_NEXT: + return () => this.tabSelectUseCase.selectNext(1); + case operations.TAB_FIRST: + return () => this.tabSelectUseCase.selectFirst(); + case operations.TAB_LAST: + return () => this.tabSelectUseCase.selectLast(); + case operations.TAB_PREV_SEL: + return () => this.tabSelectUseCase.selectPrevSelected(); + case operations.TAB_RELOAD: + return () => this.tabUseCase.reload(operation.cache); + case operations.TAB_PIN: + return () => this.tabUseCase.setPinned(true); + case operations.TAB_UNPIN: + return () => this.tabUseCase.setPinned(false); + case operations.TAB_TOGGLE_PINNED: + return () => this.tabUseCase.togglePinned(); + case operations.TAB_DUPLICATE: + return () => this.tabUseCase.duplicate(); + case operations.PAGE_SOURCE: + return () => this.tabUseCase.openPageSource(); + case operations.PAGE_HOME: + return () => this.tabUseCase.openHome(operation.newTab); + case operations.ZOOM_IN: + return () => this.zoomUseCase.zoomIn(); + case operations.ZOOM_OUT: + return () => this.zoomUseCase.zoomOut(); + case operations.ZOOM_NEUTRAL: + return () => this.zoomUseCase.zoomNutoral(); + case operations.COMMAND_SHOW: + return () => this.consoleUseCase.showCommand(); + case operations.COMMAND_SHOW_OPEN: + return () => this.consoleUseCase.showOpenCommand(operation.alter); + case operations.COMMAND_SHOW_TABOPEN: + return () => this.consoleUseCase.showTabopenCommand(operation.alter); + case operations.COMMAND_SHOW_WINOPEN: + return () => this.consoleUseCase.showWinopenCommand(operation.alter); + case operations.COMMAND_SHOW_BUFFER: + return () => this.consoleUseCase.showBufferCommand(); + case operations.COMMAND_SHOW_ADDBOOKMARK: + return () => this.consoleUseCase.showAddbookmarkCommand( + operation.alter); + case operations.FIND_START: + 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(); + case operations.NAVIGATE_PARENT: + return () => this.navigateUseCase.openParent(); + case operations.NAVIGATE_ROOT: + return () => this.navigateUseCase.openRoot(); + case operations.REPEAT_LAST: + return () => { + let last = this.repeatUseCase.getLastOperation(); + if (typeof last !== 'undefined') { + return this.doOperation(1, last); + } + return Promise.resolve(); + }; + case operations.INTERNAL_OPEN_URL: + return () => this.tabUseCase.openURL( + operation.url, operation.newTab, operation.newWindow); + default: + throw new Error('unknown operation: ' + operation.type); } - return Promise.resolve(); - } - case operations.INTERNAL_OPEN_URL: - return this.tabUseCase.openURL( - operation.url, operation.newTab, operation.newWindow); + })(); + + for (let i = 0; i < repeat; ++i) { + // eslint-disable-next-line no-await-in-loop + await opFunc(); } - throw new Error('unknown operation: ' + operation.type); } } diff --git a/src/background/infrastructures/ContentMessageListener.ts b/src/background/infrastructures/ContentMessageListener.ts index f80d686..f20340b 100644 --- a/src/background/infrastructures/ContentMessageListener.ts +++ b/src/background/infrastructures/ContentMessageListener.ts @@ -1,5 +1,6 @@ import { injectable } from 'tsyringe'; import * as messages from '../../shared/messages'; +import * as operations from '../../shared/operations'; import CompletionGroup from '../domains/CompletionGroup'; import CommandController from '../controllers/CommandController'; import SettingController from '../controllers/SettingController'; @@ -19,7 +20,7 @@ export default class ContentMessageListener { private findController: FindController, private addonEnabledController: AddonEnabledController, private linkController: LinkController, - private backgroundOperationController: OperationController, + private operationController: OperationController, private markController: MarkController, ) { this.consolePorts = {}; @@ -79,7 +80,7 @@ export default class ContentMessageListener { senderTab.id as number, message.background); case messages.BACKGROUND_OPERATION: - return this.onBackgroundOperation(message.operation); + return this.onBackgroundOperation(message.repeat, message.operation); case messages.MARK_SET_GLOBAL: return this.onMarkSetGlobal(message.key, message.x, message.y); case messages.MARK_JUMP_GLOBAL: @@ -126,8 +127,8 @@ export default class ContentMessageListener { return this.linkController.openToTab(url, openerId); } - onBackgroundOperation(operation: any): Promise<any> { - return this.backgroundOperationController.exec(operation); + onBackgroundOperation(count: number, op: operations.Operation): Promise<any> { + return this.operationController.exec(count, op); } onMarkSetGlobal(key: string, x: number, y: number): Promise<any> { |