From fcd15f4f09e412cb66f29649572b9d8ae6d50363 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 11 May 2019 13:55:25 +0900 Subject: [PATCH] Find as a controller --- .eslintrc | 2 +- src/console/actions/console.ts | 2 +- src/console/components/Console.tsx | 3 +- src/content/components/top-content/find.ts | 36 ----------- src/content/components/top-content/index.ts | 2 - src/content/controllers/FindController.ts | 24 ++++++++ src/content/index.ts | 16 +++++ src/content/usecases/FindUseCase.ts | 2 +- src/shared/messages.ts | 66 ++++++++++----------- 9 files changed, 78 insertions(+), 75 deletions(-) delete mode 100644 src/content/components/top-content/find.ts create mode 100644 src/content/controllers/FindController.ts diff --git a/.eslintrc b/.eslintrc index 7845ca5..48826fa 100644 --- a/.eslintrc +++ b/.eslintrc @@ -79,6 +79,6 @@ "react/jsx-indent": ["error", 2], "react/prop-types": "off", "react/react-in-jsx-scope": "off", - "@typescript-eslint/no-unused-vars": "error" + "@typescript-eslint/no-unused-vars": ["error", { args: "none" }], } } diff --git a/src/console/actions/console.ts b/src/console/actions/console.ts index b1494b0..d03f52c 100644 --- a/src/console/actions/console.ts +++ b/src/console/actions/console.ts @@ -53,7 +53,7 @@ const enterCommand = async( return hideCommand(); }; -const enterFind = (text: string): actions.ConsoleAction => { +const enterFind = (text?: string): actions.ConsoleAction => { window.top.postMessage(JSON.stringify({ type: messages.CONSOLE_ENTER_FIND, text, diff --git a/src/console/components/Console.tsx b/src/console/components/Console.tsx index 3274047..68cc523 100644 --- a/src/console/components/Console.tsx +++ b/src/console/components/Console.tsx @@ -38,7 +38,8 @@ class Console extends React.Component { if (this.props.mode === 'command') { return this.props.dispatch(consoleActions.enterCommand(value)); } else if (this.props.mode === 'find') { - return this.props.dispatch(consoleActions.enterFind(value)); + return this.props.dispatch(consoleActions.enterFind( + value === '' ? undefined : value)); } } diff --git a/src/content/components/top-content/find.ts b/src/content/components/top-content/find.ts deleted file mode 100644 index c25cbeb..0000000 --- a/src/content/components/top-content/find.ts +++ /dev/null @@ -1,36 +0,0 @@ -import * as messages from '../../../shared/messages'; -import MessageListener from '../../MessageListener'; - -import FindUseCase from '../../usecases/FindUseCase'; - -let findUseCase = new FindUseCase(); - -export default class FindComponent { - constructor() { - new MessageListener().onWebMessage(this.onMessage.bind(this)); - } - - onMessage(message: messages.Message) { - switch (message.type) { - case messages.CONSOLE_ENTER_FIND: - return this.start(message.text); - case messages.FIND_NEXT: - return this.next(); - case messages.FIND_PREV: - return this.prev(); - } - return Promise.resolve(); - } - - start(text: string) { - return findUseCase.startFind(text.length === 0 ? null : text); - } - - next() { - return findUseCase.findNext(); - } - - prev() { - return findUseCase.findPrev(); - } -} diff --git a/src/content/components/top-content/index.ts b/src/content/components/top-content/index.ts index de14b3f..0f07653 100644 --- a/src/content/components/top-content/index.ts +++ b/src/content/components/top-content/index.ts @@ -1,6 +1,5 @@ import CommonComponent from '../common'; import FollowController from './follow-controller'; -import FindComponent from './find'; import * as consoleFrames from '../../console-frames'; import * as messages from '../../../shared/messages'; import MessageListener from '../../MessageListener'; @@ -18,7 +17,6 @@ export default class TopContent { new CommonComponent(win, store); // eslint-disable-line no-new new FollowController(win, store); // eslint-disable-line no-new - new FindComponent(); // eslint-disable-line no-new // TODO make component consoleFrames.initialize(this.win.document); diff --git a/src/content/controllers/FindController.ts b/src/content/controllers/FindController.ts new file mode 100644 index 0000000..cf27a8d --- /dev/null +++ b/src/content/controllers/FindController.ts @@ -0,0 +1,24 @@ +import * as messages from '../../shared/messages'; +import FindUseCase from '../usecases/FindUseCase'; + +export default class FindController { + private findUseCase: FindUseCase; + + constructor({ + findUseCase = new FindUseCase(), + } = {}) { + this.findUseCase = findUseCase; + } + + async start(m: messages.ConsoleEnterFindMessage): Promise { + await this.findUseCase.startFind(m.text); + } + + async next(_: messages.FindNextMessage): Promise { + await this.findUseCase.findNext(); + } + + async prev(_: messages.FindPrevMessage): Promise { + await this.findUseCase.findPrev(); + } +} diff --git a/src/content/index.ts b/src/content/index.ts index 9d791fc..4024b98 100644 --- a/src/content/index.ts +++ b/src/content/index.ts @@ -2,11 +2,27 @@ import TopContentComponent from './components/top-content'; import FrameContentComponent from './components/frame-content'; import consoleFrameStyle from './site-style'; import { newStore } from './store'; +import MessageListener from './MessageListener'; +import FindController from './controllers/FindController'; +import * as messages from '../shared/messages'; const store = newStore(); if (window.self === window.top) { new TopContentComponent(window, store); // eslint-disable-line no-new + + let findController = new FindController(); + new MessageListener().onWebMessage((message: messages.Message) => { + switch (message.type) { + case messages.CONSOLE_ENTER_FIND: + return findController.start(message); + case messages.FIND_NEXT: + return findController.next(message); + case messages.FIND_PREV: + return findController.prev(message); + } + return undefined; + }); } else { new FrameContentComponent(window, store); // eslint-disable-line no-new } diff --git a/src/content/usecases/FindUseCase.ts b/src/content/usecases/FindUseCase.ts index 4fda323..74cbc97 100644 --- a/src/content/usecases/FindUseCase.ts +++ b/src/content/usecases/FindUseCase.ts @@ -25,7 +25,7 @@ export default class FindUseCase { this.consoleClient = consoleClient; } - async startFind(keyword: string | null): Promise { + async startFind(keyword?: string): Promise { this.presenter.clearSelection(); if (keyword) { this.saveKeyword(keyword); diff --git a/src/shared/messages.ts b/src/shared/messages.ts index 41b0f0b..75df798 100644 --- a/src/shared/messages.ts +++ b/src/shared/messages.ts @@ -42,162 +42,162 @@ export const SETTINGS_QUERY = 'settings.query'; export const CONSOLE_FRAME_MESSAGE = 'console.frame.message'; -interface BackgroundOperationMessage { +export interface BackgroundOperationMessage { type: typeof BACKGROUND_OPERATION; operation: operations.Operation; } -interface ConsoleUnfocusMessage { +export interface ConsoleUnfocusMessage { type: typeof CONSOLE_UNFOCUS; } -interface ConsoleEnterCommandMessage { +export interface ConsoleEnterCommandMessage { type: typeof CONSOLE_ENTER_COMMAND; text: string; } -interface ConsoleEnterFindMessage { +export interface ConsoleEnterFindMessage { type: typeof CONSOLE_ENTER_FIND; - text: string; + text?: string; } -interface ConsoleQueryCompletionsMessage { +export interface ConsoleQueryCompletionsMessage { type: typeof CONSOLE_QUERY_COMPLETIONS; text: string; } -interface ConsoleShowCommandMessage { +export interface ConsoleShowCommandMessage { type: typeof CONSOLE_SHOW_COMMAND; command: string; } -interface ConsoleShowErrorMessage { +export interface ConsoleShowErrorMessage { type: typeof CONSOLE_SHOW_ERROR; text: string; } -interface ConsoleShowInfoMessage { +export interface ConsoleShowInfoMessage { type: typeof CONSOLE_SHOW_INFO; text: string; } -interface ConsoleShowFindMessage { +export interface ConsoleShowFindMessage { type: typeof CONSOLE_SHOW_FIND; } -interface ConsoleHideMessage { +export interface ConsoleHideMessage { type: typeof CONSOLE_HIDE; } -interface FollowStartMessage { +export interface FollowStartMessage { type: typeof FOLLOW_START; newTab: boolean; background: boolean; } -interface FollowRequestCountTargetsMessage { +export interface FollowRequestCountTargetsMessage { type: typeof FOLLOW_REQUEST_COUNT_TARGETS; viewSize: { width: number, height: number }; framePosition: { x: number, y: number }; } -interface FollowResponseCountTargetsMessage { +export interface FollowResponseCountTargetsMessage { type: typeof FOLLOW_RESPONSE_COUNT_TARGETS; count: number; } -interface FollowCreateHintsMessage { +export interface FollowCreateHintsMessage { type: typeof FOLLOW_CREATE_HINTS; keysArray: string[]; newTab: boolean; background: boolean; } -interface FollowShowHintsMessage { +export interface FollowShowHintsMessage { type: typeof FOLLOW_SHOW_HINTS; keys: string; } -interface FollowRemoveHintsMessage { +export interface FollowRemoveHintsMessage { type: typeof FOLLOW_REMOVE_HINTS; } -interface FollowActivateMessage { +export interface FollowActivateMessage { type: typeof FOLLOW_ACTIVATE; keys: string; } -interface FollowKeyPressMessage { +export interface FollowKeyPressMessage { type: typeof FOLLOW_KEY_PRESS; key: string; ctrlKey: boolean; } -interface MarkSetGlobalMessage { +export interface MarkSetGlobalMessage { type: typeof MARK_SET_GLOBAL; key: string; x: number; y: number; } -interface MarkJumpGlobalMessage { +export interface MarkJumpGlobalMessage { type: typeof MARK_JUMP_GLOBAL; key: string; } -interface TabScrollToMessage { +export interface TabScrollToMessage { type: typeof TAB_SCROLL_TO; x: number; y: number; } -interface FindNextMessage { +export interface FindNextMessage { type: typeof FIND_NEXT; } -interface FindPrevMessage { +export interface FindPrevMessage { type: typeof FIND_PREV; } -interface FindGetKeywordMessage { +export interface FindGetKeywordMessage { type: typeof FIND_GET_KEYWORD; } -interface FindSetKeywordMessage { +export interface FindSetKeywordMessage { type: typeof FIND_SET_KEYWORD; keyword: string; found: boolean; } -interface AddonEnabledQueryMessage { +export interface AddonEnabledQueryMessage { type: typeof ADDON_ENABLED_QUERY; } -interface AddonEnabledResponseMessage { +export interface AddonEnabledResponseMessage { type: typeof ADDON_ENABLED_RESPONSE; enabled: boolean; } -interface AddonToggleEnabledMessage { +export interface AddonToggleEnabledMessage { type: typeof ADDON_TOGGLE_ENABLED; } -interface OpenUrlMessage { +export interface OpenUrlMessage { type: typeof OPEN_URL; url: string; newTab: boolean; background: boolean; } -interface SettingsChangedMessage { +export interface SettingsChangedMessage { type: typeof SETTINGS_CHANGED; } -interface SettingsQueryMessage { +export interface SettingsQueryMessage { type: typeof SETTINGS_QUERY; } -interface ConsoleFrameMessageMessage { +export interface ConsoleFrameMessageMessage { type: typeof CONSOLE_FRAME_MESSAGE; message: any; }