Mark keys as a clean archtecture

jh-changes
Shin'ya Ueoka 6 years ago
parent efc48dc742
commit 2ec912c262
  1. 19
      src/content/controllers/AddonEnabledController.ts
  2. 15
      src/content/controllers/KeymapController.ts
  3. 16
      src/content/controllers/MarkController.ts
  4. 31
      src/content/controllers/MarkKeyController.ts
  5. 30
      src/content/index.ts
  6. 52
      src/content/repositories/MarkKeyRepository.ts
  7. 36
      src/content/usecases/MarkKeyUseCase.ts
  8. 8
      src/content/usecases/MarkUseCase.ts

@ -0,0 +1,19 @@
import * as messages from '../../shared/messages';
import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase';
export default class AddonEnabledController {
private addonEnabledUseCase: AddonEnabledUseCase;
constructor({
addonEnabledUseCase = new AddonEnabledUseCase(),
} = {}) {
this.addonEnabledUseCase = addonEnabledUseCase;
}
getAddonEnabled(
_message: messages.AddonEnabledQueryMessage,
): Promise<boolean> {
let enabled = this.addonEnabledUseCase.getEnabled();
return Promise.resolve(enabled);
}
}

@ -7,6 +7,7 @@ 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';
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
import { Key } from '../../shared/utils/keys'; import { Key } from '../../shared/utils/keys';
export default class KeymapController { export default class KeymapController {
@ -26,6 +27,8 @@ export default class KeymapController {
private backgroundClient: BackgroundClient; private backgroundClient: BackgroundClient;
private markKeyUseCase: MarkKeyyUseCase;
constructor({ constructor({
keymapUseCase = new KeymapUseCase(), keymapUseCase = new KeymapUseCase(),
addonEnabledUseCase = new AddonEnabledUseCase(), addonEnabledUseCase = new AddonEnabledUseCase(),
@ -35,6 +38,7 @@ export default class KeymapController {
focusUseCase = new FocusUseCase(), focusUseCase = new FocusUseCase(),
clipbaordUseCase = new ClipboardUseCase(), clipbaordUseCase = new ClipboardUseCase(),
backgroundClient = new BackgroundClient(), backgroundClient = new BackgroundClient(),
markKeyUseCase = new MarkKeyyUseCase(),
} = {}) { } = {}) {
this.keymapUseCase = keymapUseCase; this.keymapUseCase = keymapUseCase;
this.addonEnabledUseCase = addonEnabledUseCase; this.addonEnabledUseCase = addonEnabledUseCase;
@ -44,6 +48,7 @@ export default class KeymapController {
this.focusUseCase = focusUseCase; this.focusUseCase = focusUseCase;
this.clipbaordUseCase = clipbaordUseCase; this.clipbaordUseCase = clipbaordUseCase;
this.backgroundClient = backgroundClient; this.backgroundClient = backgroundClient;
this.markKeyUseCase = markKeyUseCase;
} }
// eslint-disable-next-line complexity, max-lines-per-function // eslint-disable-next-line complexity, max-lines-per-function
@ -98,10 +103,12 @@ export default class KeymapController {
// background: operation.background, // background: operation.background,
// }), '*'); // }), '*');
// break; // break;
// case operations.MARK_SET_PREFIX: case operations.MARK_SET_PREFIX:
// return markActions.startSet(); this.markKeyUseCase.enableSetMode();
// case operations.MARK_JUMP_PREFIX: break;
// return markActions.startJump(); case operations.MARK_JUMP_PREFIX:
this.markKeyUseCase.enableJumpMode();
break;
case operations.NAVIGATE_HISTORY_PREV: case operations.NAVIGATE_HISTORY_PREV:
this.navigateUseCase.openHistoryPrev(); this.navigateUseCase.openHistoryPrev();
break; break;

@ -0,0 +1,16 @@
import * as messages from '../../shared/messages';
import MarkUseCase from '../usecases/MarkUseCase';
export default class MarkController {
private markUseCase: MarkUseCase;
constructor({
markUseCase = new MarkUseCase(),
} = {}) {
this.markUseCase = markUseCase;
}
scrollTo(message: messages.TabScrollToMessage) {
this.markUseCase.scroll(message.x, message.y);
}
}

@ -0,0 +1,31 @@
import MarkUseCase from '../usecases/MarkUseCase';
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
import * as keys from '../../shared/utils/keys';
export default class MarkKeyController {
private markUseCase: MarkUseCase;
private markKeyUseCase: MarkKeyyUseCase;
constructor({
markUseCase = new MarkUseCase(),
markKeyUseCase = new MarkKeyyUseCase(),
} = {}) {
this.markUseCase = markUseCase;
this.markKeyUseCase = markKeyUseCase;
}
press(key: keys.Key): boolean {
if (this.markKeyUseCase.isSetMode()) {
this.markUseCase.set(key.key);
this.markKeyUseCase.disableSetMode();
return true;
}
if (this.markKeyUseCase.isJumpMode()) {
this.markUseCase.jump(key.key);
this.markKeyUseCase.disableJumpMode();
return true;
}
return false;
}
}

@ -4,12 +4,15 @@ import consoleFrameStyle from './site-style';
// import { newStore } from './store'; // import { newStore } from './store';
import MessageListener from './MessageListener'; import MessageListener from './MessageListener';
import FindController from './controllers/FindController'; import FindController from './controllers/FindController';
import MarkController from './controllers/MarkController';
import * as messages from '../shared/messages'; import * as messages from '../shared/messages';
import InputDriver from './InputDriver'; import InputDriver from './InputDriver';
import KeymapController from './controllers/KeymapController'; import KeymapController from './controllers/KeymapController';
import AddonEnabledUseCase from './usecases/AddonEnabledUseCase'; import AddonEnabledUseCase from './usecases/AddonEnabledUseCase';
import SettingUseCase from './usecases/SettingUseCase'; import SettingUseCase from './usecases/SettingUseCase';
import * as blacklists from '../shared/blacklists'; import * as blacklists from '../shared/blacklists';
import MarkKeyController from './controllers/MarkKeyController';
import AddonEnabledController from './controllers/AddonEnabledController';
// const store = newStore(); // const store = newStore();
@ -28,14 +31,28 @@ if (window.self === window.top) {
} }
return undefined; return undefined;
}); });
let markController = new MarkController();
let addonEnabledController = new AddonEnabledController();
new MessageListener().onBackgroundMessage((message: messages.Message) => {
switch (message.type) {
case messages.ADDON_ENABLED_QUERY:
return addonEnabledController.getAddonEnabled(message);
case messages.TAB_SCROLL_TO:
return markController.scrollTo(message);
}
return undefined;
});
} else { } else {
// new FrameContentComponent(window, store); // eslint-disable-line no-new // new FrameContentComponent(window, store); // eslint-disable-line no-new
} }
let keymapController = new KeymapController(); let keymapController = new KeymapController();
let markKeyController = new MarkKeyController();
let inputDriver = new InputDriver(document.body); let inputDriver = new InputDriver(document.body);
// inputDriver.onKey(key => followSlaveController.pressKey(key)); // inputDriver.onKey(key => followSlaveController.pressKey(key));
// inputDriver.onKey(key => markController.pressKey(key)); inputDriver.onKey(key => markKeyController.press(key));
inputDriver.onKey(key => keymapController.press(key)); inputDriver.onKey(key => keymapController.press(key));
let style = window.document.createElement('style'); let style = window.document.createElement('style');
@ -64,3 +81,14 @@ const reloadSettings = async() => {
} }
}; };
reloadSettings(); reloadSettings();
new MessageListener().onBackgroundMessage((message: messages.Message): any => {
let addonEnabledUseCase = new AddonEnabledUseCase();
switch (message.type) {
case messages.SETTINGS_CHANGED:
return reloadSettings();
case messages.ADDON_TOGGLE_ENABLED:
return addonEnabledUseCase.toggle();
}
});

@ -0,0 +1,52 @@
export default interface MarkKeyRepository {
isSetMode(): boolean;
enableSetMode(): void;
disabeSetMode(): void;
isJumpMode(): boolean;
enableJumpMode(): void;
disabeJumpMode(): void;
// eslint-disable-next-line semi
}
interface Mode {
setMode: boolean;
jumpMode: boolean;
}
let current: Mode = {
setMode: false,
jumpMode: false,
};
export class MarkKeyRepositoryImpl implements MarkKeyRepository {
isSetMode(): boolean {
return current.setMode;
}
enableSetMode(): void {
current.setMode = true;
}
disabeSetMode(): void {
current.setMode = false;
}
isJumpMode(): boolean {
return current.jumpMode;
}
enableJumpMode(): void {
current.jumpMode = true;
}
disabeJumpMode(): void {
current.jumpMode = false;
}
}

@ -0,0 +1,36 @@
import MarkKeyRepository, { MarkKeyRepositoryImpl }
from '../repositories/MarkKeyRepository';
export default class MarkKeyUseCase {
private repository: MarkKeyRepository;
constructor({
repository = new MarkKeyRepositoryImpl()
} = {}) {
this.repository = repository;
}
isSetMode(): boolean {
return this.repository.isSetMode();
}
isJumpMode(): boolean {
return this.repository.isJumpMode();
}
enableSetMode(): void {
this.repository.enableSetMode();
}
disableSetMode(): void {
this.repository.disabeSetMode();
}
enableJumpMode(): void {
this.repository.enableJumpMode();
}
disableJumpMode(): void {
this.repository.disabeJumpMode();
}
}

@ -51,11 +51,15 @@ export default class MarkUseCase {
if (!pos) { if (!pos) {
throw new Error('Mark is not set'); throw new Error('Mark is not set');
} }
let smooth = this.settingRepository.get().properties.smoothscroll; this.scroll(pos.x, pos.y);
this.scrollPresenter.scrollTo(pos.x, pos.y, smooth);
} }
} }
scroll(x: number, y: number): void {
let smooth = this.settingRepository.get().properties.smoothscroll;
this.scrollPresenter.scrollTo(x, y, smooth);
}
private globalKey(key: string) { private globalKey(key: string) {
return (/^[A-Z0-9]$/).test(key); return (/^[A-Z0-9]$/).test(key);
} }