diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-12-10 12:52:17 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-10 12:52:17 +0000 |
commit | 5a0444d7bb7eae27fdca5c2be8fc3ec6c36d53bd (patch) | |
tree | 46d70e19f9720d237f4423c1debfcacdd088ce0b /src/content/operators | |
parent | a3c34a309c4b1421eb4914c3fbeba327a5400021 (diff) | |
parent | d2fb674566393d9a8b88d71dba9f5081786b118c (diff) |
Merge pull request #917 from ueokande/operation-as-a-operator
refactor: Make each operation as an operator
Diffstat (limited to 'src/content/operators')
32 files changed, 662 insertions, 0 deletions
diff --git a/src/content/operators/Operator.ts b/src/content/operators/Operator.ts new file mode 100644 index 0000000..3b1fe03 --- /dev/null +++ b/src/content/operators/Operator.ts @@ -0,0 +1,5 @@ +interface Operator { + run(): Promise<void>; +} + +export default Operator; diff --git a/src/content/operators/OperatorFactory.ts b/src/content/operators/OperatorFactory.ts new file mode 100644 index 0000000..f45973b --- /dev/null +++ b/src/content/operators/OperatorFactory.ts @@ -0,0 +1,6 @@ +import * as operations from "../../shared/operations"; +import Operator from "./Operator"; + +export default interface OperatorFactory { + create(op: operations.Operation, repeat: number): Operator; +} diff --git a/src/content/operators/OperatorFactoryChain.ts b/src/content/operators/OperatorFactoryChain.ts new file mode 100644 index 0000000..98dedb5 --- /dev/null +++ b/src/content/operators/OperatorFactoryChain.ts @@ -0,0 +1,6 @@ +import * as operations from "../../shared/operations"; +import Operator from "./Operator"; + +export default interface OperatorFactoryChain { + create(op: operations.Operation, repeat: number): Operator | null; +} diff --git a/src/content/operators/impls/AbstractScrollOperator.ts b/src/content/operators/impls/AbstractScrollOperator.ts new file mode 100644 index 0000000..f8d9f70 --- /dev/null +++ b/src/content/operators/impls/AbstractScrollOperator.ts @@ -0,0 +1,10 @@ +import SettingRepository from "../../repositories/SettingRepository"; + +export default class AbstractScrollOperator { + constructor(private readonly settingRepository: SettingRepository) {} + + protected getSmoothScroll(): boolean { + const settings = this.settingRepository.get(); + return settings.properties.smoothscroll; + } +} diff --git a/src/content/operators/impls/AddonOperatorFactoryChain.ts b/src/content/operators/impls/AddonOperatorFactoryChain.ts new file mode 100644 index 0000000..54880c4 --- /dev/null +++ b/src/content/operators/impls/AddonOperatorFactoryChain.ts @@ -0,0 +1,40 @@ +import { inject, injectable } from "tsyringe"; +import OperatorFactoryChain from "../OperatorFactoryChain"; +import AddonIndicatorClient from "../../client/AddonIndicatorClient"; +import AddonEnabledRepository from "../../repositories/AddonEnabledRepository"; +import * as operations from "../../../shared/operations"; +import Operator from "../Operator"; +import EnableAddonOperator from "./EnableAddonOperator"; +import DisableAddonOperator from "./DisableAddonOperator"; +import ToggleAddonOperator from "./ToggleAddonOperator"; + +@injectable() +export default class AddonOperatorFactoryChain implements OperatorFactoryChain { + constructor( + @inject("AddonIndicatorClient") + private readonly addonIndicatorClient: AddonIndicatorClient, + @inject("AddonEnabledRepository") + private readonly addonEnabledRepository: AddonEnabledRepository + ) {} + + create(op: operations.Operation, _repeat: number): Operator | null { + switch (op.type) { + case operations.ADDON_ENABLE: + return new EnableAddonOperator( + this.addonIndicatorClient, + this.addonEnabledRepository + ); + case operations.ADDON_DISABLE: + return new DisableAddonOperator( + this.addonIndicatorClient, + this.addonEnabledRepository + ); + case operations.ADDON_TOGGLE_ENABLED: + return new ToggleAddonOperator( + this.addonIndicatorClient, + this.addonEnabledRepository + ); + } + return null; + } +} diff --git a/src/content/operators/impls/BackgroundOperationOperator.ts b/src/content/operators/impls/BackgroundOperationOperator.ts new file mode 100644 index 0000000..dd86559 --- /dev/null +++ b/src/content/operators/impls/BackgroundOperationOperator.ts @@ -0,0 +1,15 @@ +import Operator from "../Operator"; +import OperationClient from "../../client/OperationClient"; +import * as operations from "../../../shared/operations"; + +export default class BackgroundOperationOperator implements Operator { + constructor( + private readonly operationClient: OperationClient, + private readonly repeat: number, + private readonly op: operations.Operation + ) {} + + async run(): Promise<void> { + await this.operationClient.execBackgroundOp(this.repeat, this.op); + } +} diff --git a/src/content/operators/impls/ClipboardOperatorFactoryChain.ts b/src/content/operators/impls/ClipboardOperatorFactoryChain.ts new file mode 100644 index 0000000..454aea1 --- /dev/null +++ b/src/content/operators/impls/ClipboardOperatorFactoryChain.ts @@ -0,0 +1,47 @@ +import { inject, injectable } from "tsyringe"; +import YankURLOperator from "./YankURLOperator"; +import PasteOperator from "./PasteOperator"; +import Operator from "../Operator"; +import OperatorFactoryChain from "../OperatorFactoryChain"; +import ClipboardRepository from "../../repositories/ClipboardRepository"; +import ConsoleClient from "../../client/ConsoleClient"; +import OperationClient from "../../client/OperationClient"; +import SettingRepository from "../../repositories/SettingRepository"; +import * as operations from "../../../shared/operations"; +import URLRepository from "./URLRepository"; + +@injectable() +export default class ClipboardOperatorFactoryChain + implements OperatorFactoryChain { + constructor( + @inject("ClipboardRepository") + private readonly clipboardRepository: ClipboardRepository, + @inject("ConsoleClient") + private readonly consoleClient: ConsoleClient, + @inject("OperationClient") + private readonly operationClinet: OperationClient, + @inject("SettingRepository") + private readonly settingRepository: SettingRepository, + @inject("URLRepository") + private readonly urlRepository: URLRepository + ) {} + + create(op: operations.Operation, _repeat: number): Operator | null { + switch (op.type) { + case operations.URLS_YANK: + return new YankURLOperator( + this.clipboardRepository, + this.consoleClient, + this.urlRepository + ); + case operations.URLS_PASTE: + return new PasteOperator( + this.clipboardRepository, + this.settingRepository, + this.operationClinet, + op.newTab + ); + } + return null; + } +} diff --git a/src/content/operators/impls/DisableAddonOperator.ts b/src/content/operators/impls/DisableAddonOperator.ts new file mode 100644 index 0000000..28811fe --- /dev/null +++ b/src/content/operators/impls/DisableAddonOperator.ts @@ -0,0 +1,15 @@ +import Operator from "../Operator"; +import AddonIndicatorClient from "../../client/AddonIndicatorClient"; +import AddonEnabledRepository from "../../repositories/AddonEnabledRepository"; + +export default class DisableAddonOperator implements Operator { + constructor( + private readonly indicator: AddonIndicatorClient, + private readonly repository: AddonEnabledRepository + ) {} + + async run(): Promise<void> { + this.repository.set(false); + await this.indicator.setEnabled(false); + } +} diff --git a/src/content/operators/impls/EnableAddonOperator.ts b/src/content/operators/impls/EnableAddonOperator.ts new file mode 100644 index 0000000..b5b1d79 --- /dev/null +++ b/src/content/operators/impls/EnableAddonOperator.ts @@ -0,0 +1,15 @@ +import Operator from "../Operator"; +import AddonIndicatorClient from "../../client/AddonIndicatorClient"; +import AddonEnabledRepository from "../../repositories/AddonEnabledRepository"; + +export default class EnableAddonOperator implements Operator { + constructor( + private readonly indicator: AddonIndicatorClient, + private readonly repository: AddonEnabledRepository + ) {} + + async run(): Promise<void> { + this.repository.set(true); + await this.indicator.setEnabled(true); + } +} diff --git a/src/content/operators/impls/EnableJumpMarkOperator.ts b/src/content/operators/impls/EnableJumpMarkOperator.ts new file mode 100644 index 0000000..42ca8ee --- /dev/null +++ b/src/content/operators/impls/EnableJumpMarkOperator.ts @@ -0,0 +1,10 @@ +import Operator from "../Operator"; +import MarkKeyRepository from "../../repositories/MarkKeyRepository"; + +export default class EnableJumpMarkOperator implements Operator { + constructor(private readonly repository: MarkKeyRepository) {} + + async run(): Promise<void> { + this.repository.enableJumpMode(); + } +} diff --git a/src/content/operators/impls/EnableSetMarkOperator.ts b/src/content/operators/impls/EnableSetMarkOperator.ts new file mode 100644 index 0000000..3d0daf4 --- /dev/null +++ b/src/content/operators/impls/EnableSetMarkOperator.ts @@ -0,0 +1,10 @@ +import Operator from "../Operator"; +import MarkKeyRepository from "../../repositories/MarkKeyRepository"; + +export default class EnableSetMarkOperator implements Operator { + constructor(private readonly repository: MarkKeyRepository) {} + + async run(): Promise<void> { + this.repository.enableSetMode(); + } +} diff --git a/src/content/operators/impls/FindNextOperator.ts b/src/content/operators/impls/FindNextOperator.ts new file mode 100644 index 0000000..c67f6d9 --- /dev/null +++ b/src/content/operators/impls/FindNextOperator.ts @@ -0,0 +1,15 @@ +import Operator from "../Operator"; +import FindMasterClient from "../../client/FindMasterClient"; + +export default class FindNextOperator implements Operator { + constructor( + private readonly findMasterClient: FindMasterClient, + private readonly repeat: number + ) {} + + async run(): Promise<void> { + for (let i = 0; i < this.repeat; ++i) { + this.findMasterClient.findNext(); + } + } +} diff --git a/src/content/operators/impls/FindOperatorFactoryChain.ts b/src/content/operators/impls/FindOperatorFactoryChain.ts new file mode 100644 index 0000000..b3524c1 --- /dev/null +++ b/src/content/operators/impls/FindOperatorFactoryChain.ts @@ -0,0 +1,25 @@ +import { inject, injectable } from "tsyringe"; +import Operator from "../Operator"; +import OperatorFactoryChain from "../OperatorFactoryChain"; +import FindNextOperator from "./FindNextOperator"; +import FindPrevOperator from "./FindPrevOperator"; +import FindMasterClient from "../../client/FindMasterClient"; +import * as operations from "../../../shared/operations"; + +@injectable() +export default class FindOperatorFactoryChain implements OperatorFactoryChain { + constructor( + @inject("FindMasterClient") + private readonly findMasterClient: FindMasterClient + ) {} + + create(op: operations.Operation, repeat: number): Operator | null { + switch (op.type) { + case operations.FIND_NEXT: + return new FindNextOperator(this.findMasterClient, repeat); + case operations.FIND_PREV: + return new FindPrevOperator(this.findMasterClient, repeat); + } + return null; + } +} diff --git a/src/content/operators/impls/FindPrevOperator.ts b/src/content/operators/impls/FindPrevOperator.ts new file mode 100644 index 0000000..f73e605 --- /dev/null +++ b/src/content/operators/impls/FindPrevOperator.ts @@ -0,0 +1,15 @@ +import Operator from "../Operator"; +import FindMasterClient from "../../client/FindMasterClient"; + +export default class FindPrevOperator implements Operator { + constructor( + private readonly findMasterClient: FindMasterClient, + private readonly repeat: number + ) {} + + async run(): Promise<void> { + for (let i = 0; i < this.repeat; ++i) { + this.findMasterClient.findPrev(); + } + } +} diff --git a/src/content/operators/impls/FocusOperator.ts b/src/content/operators/impls/FocusOperator.ts new file mode 100644 index 0000000..51d6fec --- /dev/null +++ b/src/content/operators/impls/FocusOperator.ts @@ -0,0 +1,10 @@ +import Operator from "../Operator"; +import FocusPresenter from "../../presenters/FocusPresenter"; + +export default class FocusOperator implements Operator { + constructor(private readonly presenter: FocusPresenter) {} + + async run(): Promise<void> { + this.presenter.focusFirstElement(); + } +} diff --git a/src/content/operators/impls/FocusOperatorFactoryChain.ts b/src/content/operators/impls/FocusOperatorFactoryChain.ts new file mode 100644 index 0000000..c89c1e5 --- /dev/null +++ b/src/content/operators/impls/FocusOperatorFactoryChain.ts @@ -0,0 +1,22 @@ +import { inject, injectable } from "tsyringe"; +import OperatorFactoryChain from "../OperatorFactoryChain"; +import Operator from "../Operator"; +import FocusOperator from "./FocusOperator"; +import FocusPresenter from "../../presenters/FocusPresenter"; +import * as operations from "../../../shared/operations"; + +@injectable() +export default class FocusOperatorFactoryChain implements OperatorFactoryChain { + constructor( + @inject("FocusPresenter") + private readonly focusPresenter: FocusPresenter + ) {} + + create(op: operations.Operation, _repeat: number): Operator | null { + switch (op.type) { + case operations.FOCUS_INPUT: + return new FocusOperator(this.focusPresenter); + } + return null; + } +} diff --git a/src/content/operators/impls/FollowOperatorFactoryChain.ts b/src/content/operators/impls/FollowOperatorFactoryChain.ts new file mode 100644 index 0000000..588e1a4 --- /dev/null +++ b/src/content/operators/impls/FollowOperatorFactoryChain.ts @@ -0,0 +1,27 @@ +import { inject, injectable } from "tsyringe"; +import StartFollowOperator from "./StartFollowOperator"; +import Operator from "../Operator"; +import OperatorFactoryChain from "../OperatorFactoryChain"; +import FollowMasterClient from "../../client/FollowMasterClient"; +import * as operations from "../../../shared/operations"; + +@injectable() +export default class FollowOperatorFactoryChain + implements OperatorFactoryChain { + constructor( + @inject("FollowMasterClient") + private followMasterClient: FollowMasterClient + ) {} + + create(op: operations.Operation, _repeat: number): Operator | null { + switch (op.type) { + case operations.FOLLOW_START: + return new StartFollowOperator( + this.followMasterClient, + op.newTab, + op.background + ); + } + return null; + } +} diff --git a/src/content/operators/impls/HorizontalScrollOperator.ts b/src/content/operators/impls/HorizontalScrollOperator.ts new file mode 100644 index 0000000..f813f85 --- /dev/null +++ b/src/content/operators/impls/HorizontalScrollOperator.ts @@ -0,0 +1,21 @@ +import AbstractScrollOperator from "./AbstractScrollOperator"; +import Operator from "../Operator"; +import ScrollPresenter from "../../presenters/ScrollPresenter"; +import SettingRepository from "../../repositories/SettingRepository"; + +export default class HorizontalScrollOperator + extends AbstractScrollOperator + implements Operator { + constructor( + private readonly presenter: ScrollPresenter, + settingRepository: SettingRepository, + private readonly count: number + ) { + super(settingRepository); + } + + async run(): Promise<void> { + const smooth = this.getSmoothScroll(); + this.presenter.scrollHorizonally(this.count, smooth); + } +} diff --git a/src/content/operators/impls/MarkOperatorFactoryChain.ts b/src/content/operators/impls/MarkOperatorFactoryChain.ts new file mode 100644 index 0000000..7e6c513 --- /dev/null +++ b/src/content/operators/impls/MarkOperatorFactoryChain.ts @@ -0,0 +1,25 @@ +import { inject, injectable } from "tsyringe"; +import EnableSetMarkOperator from "./EnableSetMarkOperator"; +import EnableJumpMarkOperator from "./EnableJumpMarkOperator"; +import Operator from "../Operator"; +import OperatorFactoryChain from "../OperatorFactoryChain"; +import MarkKeyRepository from "../../repositories/MarkKeyRepository"; +import * as operations from "../../../shared/operations"; + +@injectable() +export default class MarkOperatorFactoryChain implements OperatorFactoryChain { + constructor( + @inject("MarkKeyRepository") + private readonly markKeyRepository: MarkKeyRepository + ) {} + + create(op: operations.Operation, _repeat: number): Operator | null { + switch (op.type) { + case operations.MARK_SET_PREFIX: + return new EnableSetMarkOperator(this.markKeyRepository); + case operations.MARK_JUMP_PREFIX: + return new EnableJumpMarkOperator(this.markKeyRepository); + } + return null; + } +} diff --git a/src/content/operators/impls/OperatorFactoryImpl.ts b/src/content/operators/impls/OperatorFactoryImpl.ts new file mode 100644 index 0000000..22b35c8 --- /dev/null +++ b/src/content/operators/impls/OperatorFactoryImpl.ts @@ -0,0 +1,51 @@ +import { inject, injectable } from "tsyringe"; +import OperatorFactory from "../OperatorFactory"; +import BackgroundOperationOperator from "./BackgroundOperationOperator"; +import Operator from "../Operator"; +import OperatorFactoryChain from "../OperatorFactoryChain"; +import { Operation } from "../../../shared/operations"; +import OperationClient from "../../client/OperationClient"; +import AddonOperatorFactoryChain from "./AddonOperatorFactoryChain"; +import ClipboardOperatorFactoryChain from "./ClipboardOperatorFactoryChain"; +import FindOperatorFactoryChain from "./FindOperatorFactoryChain"; +import FocusOperatorFactoryChain from "./FocusOperatorFactoryChain"; +import FollowOperatorFactoryChain from "./FollowOperatorFactoryChain"; +import MarkOperatorFactoryChain from "./MarkOperatorFactoryChain"; +import ScrollOperatorFactoryChain from "./ScrollOperatorFactoryChain"; + +@injectable() +export default class OperatorFactoryImpl implements OperatorFactory { + private readonly factoryChains: OperatorFactoryChain[]; + + constructor( + addonOperatorFactoryChain: AddonOperatorFactoryChain, + clipboardOperatorFactoryChain: ClipboardOperatorFactoryChain, + findOperatorFactoryChain: FindOperatorFactoryChain, + focusOperatorFactoryChain: FocusOperatorFactoryChain, + followOperatorFactoryChain: FollowOperatorFactoryChain, + markOperatorFactoryChain: MarkOperatorFactoryChain, + scrollOperatorFactoryChain: ScrollOperatorFactoryChain, + @inject("OperationClient") + private readonly operationClient: OperationClient + ) { + this.factoryChains = [ + addonOperatorFactoryChain, + clipboardOperatorFactoryChain, + findOperatorFactoryChain, + focusOperatorFactoryChain, + followOperatorFactoryChain, + markOperatorFactoryChain, + scrollOperatorFactoryChain, + ]; + } + + create(op: Operation, repeat: number): Operator { + for (const chain of this.factoryChains) { + const operator = chain.create(op, repeat); + if (operator != null) { + return operator; + } + } + return new BackgroundOperationOperator(this.operationClient, repeat, op); + } +} diff --git a/src/content/operators/impls/PageScrollOperator.ts b/src/content/operators/impls/PageScrollOperator.ts new file mode 100644 index 0000000..377bf92 --- /dev/null +++ b/src/content/operators/impls/PageScrollOperator.ts @@ -0,0 +1,21 @@ +import AbstractScrollOperator from "./AbstractScrollOperator"; +import Operator from "../Operator"; +import ScrollPresenter from "../../presenters/ScrollPresenter"; +import SettingRepository from "../../repositories/SettingRepository"; + +export default class PageScrollOperator + extends AbstractScrollOperator + implements Operator { + constructor( + private readonly presenter: ScrollPresenter, + settingRepository: SettingRepository, + private readonly count: number + ) { + super(settingRepository); + } + + async run(): Promise<void> { + const smooth = this.getSmoothScroll(); + this.presenter.scrollPages(this.count, smooth); + } +} diff --git a/src/content/operators/impls/PasteOperator.ts b/src/content/operators/impls/PasteOperator.ts new file mode 100644 index 0000000..592da66 --- /dev/null +++ b/src/content/operators/impls/PasteOperator.ts @@ -0,0 +1,25 @@ +import Operator from "../Operator"; +import ClipboardRepository from "../../repositories/ClipboardRepository"; +import SettingRepository from "../../repositories/SettingRepository"; +import OperationClient from "../../client/OperationClient"; +import * as urls from "../../../shared/urls"; + +export default class PasteOperator implements Operator { + constructor( + private readonly repository: ClipboardRepository, + private readonly settingRepository: SettingRepository, + private readonly operationClient: OperationClient, + private readonly newTab: boolean + ) {} + + async run(): Promise<void> { + const search = this.settingRepository.get().search; + const text = this.repository.read(); + const url = urls.searchUrl(text, search); + + // NOTE: 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.operationClient.internalOpenUrl(url, this.newTab); + } +} diff --git a/src/content/operators/impls/ScrollOperatorFactoryChain.ts b/src/content/operators/impls/ScrollOperatorFactoryChain.ts new file mode 100644 index 0000000..6847aea --- /dev/null +++ b/src/content/operators/impls/ScrollOperatorFactoryChain.ts @@ -0,0 +1,68 @@ +import { inject, injectable } from "tsyringe"; +import OperatorFactoryChain from "../OperatorFactoryChain"; +import ScrollPresenter from "../../presenters/ScrollPresenter"; +import SettingRepository from "../../repositories/SettingRepository"; +import * as operations from "../../../shared/operations"; +import Operator from "../Operator"; +import VerticalScrollOperator from "./VerticalScrollOperator"; +import HorizontalScrollOperator from "./HorizontalScrollOperator"; +import PageScrollOperator from "./PageScrollOperator"; +import ScrollToTopOperator from "./ScrollToTopOperator"; +import ScrollToBottomOperator from "./ScrollToBottomOperator"; +import ScrollToHomeOperator from "./ScrollToHomeOperator"; +import ScrollToEndOperator from "./ScrollToEndOperator"; + +@injectable() +export default class ScrollOperatorFactoryChain + implements OperatorFactoryChain { + constructor( + @inject("ScrollPresenter") + private readonly scrollPresenter: ScrollPresenter, + @inject("SettingRepository") + private readonly settingRepository: SettingRepository + ) {} + + create(op: operations.Operation, repeat: number): Operator | null { + switch (op.type) { + case operations.SCROLL_VERTICALLY: + return new VerticalScrollOperator( + this.scrollPresenter, + this.settingRepository, + op.count * repeat + ); + case operations.SCROLL_HORIZONALLY: + return new HorizontalScrollOperator( + this.scrollPresenter, + this.settingRepository, + op.count * repeat + ); + case operations.SCROLL_PAGES: + return new PageScrollOperator( + this.scrollPresenter, + this.settingRepository, + op.count * repeat + ); + case operations.SCROLL_TOP: + return new ScrollToTopOperator( + this.scrollPresenter, + this.settingRepository + ); + case operations.SCROLL_BOTTOM: + return new ScrollToBottomOperator( + this.scrollPresenter, + this.settingRepository + ); + case operations.SCROLL_HOME: + return new ScrollToHomeOperator( + this.scrollPresenter, + this.settingRepository + ); + case operations.SCROLL_END: + return new ScrollToEndOperator( + this.scrollPresenter, + this.settingRepository + ); + } + return null; + } +} diff --git a/src/content/operators/impls/ScrollToBottomOperator.ts b/src/content/operators/impls/ScrollToBottomOperator.ts new file mode 100644 index 0000000..4db521b --- /dev/null +++ b/src/content/operators/impls/ScrollToBottomOperator.ts @@ -0,0 +1,20 @@ +import AbstractScrollOperator from "./AbstractScrollOperator"; +import Operator from "../Operator"; +import ScrollPresenter from "../../presenters/ScrollPresenter"; +import SettingRepository from "../../repositories/SettingRepository"; + +export default class ScrollToBottomOperator + extends AbstractScrollOperator + implements Operator { + constructor( + private readonly presenter: ScrollPresenter, + settingRepository: SettingRepository + ) { + super(settingRepository); + } + + async run(): Promise<void> { + const smooth = this.getSmoothScroll(); + this.presenter.scrollToBottom(smooth); + } +} diff --git a/src/content/operators/impls/ScrollToEndOperator.ts b/src/content/operators/impls/ScrollToEndOperator.ts new file mode 100644 index 0000000..8217e15 --- /dev/null +++ b/src/content/operators/impls/ScrollToEndOperator.ts @@ -0,0 +1,20 @@ +import AbstractScrollOperator from "./AbstractScrollOperator"; +import Operator from "../Operator"; +import ScrollPresenter from "../../presenters/ScrollPresenter"; +import SettingRepository from "../../repositories/SettingRepository"; + +export default class ScrollToEndOperator + extends AbstractScrollOperator + implements Operator { + constructor( + private readonly presenter: ScrollPresenter, + settingRepository: SettingRepository + ) { + super(settingRepository); + } + + async run(): Promise<void> { + const smooth = this.getSmoothScroll(); + this.presenter.scrollToEnd(smooth); + } +} diff --git a/src/content/operators/impls/ScrollToHomeOperator.ts b/src/content/operators/impls/ScrollToHomeOperator.ts new file mode 100644 index 0000000..a0d7701 --- /dev/null +++ b/src/content/operators/impls/ScrollToHomeOperator.ts @@ -0,0 +1,20 @@ +import AbstractScrollOperator from "./AbstractScrollOperator"; +import Operator from "../Operator"; +import ScrollPresenter from "../../presenters/ScrollPresenter"; +import SettingRepository from "../../repositories/SettingRepository"; + +export default class ScrollToHomeOperator + extends AbstractScrollOperator + implements Operator { + constructor( + private readonly presenter: ScrollPresenter, + settingRepository: SettingRepository + ) { + super(settingRepository); + } + + async run(): Promise<void> { + const smooth = this.getSmoothScroll(); + this.presenter.scrollToHome(smooth); + } +} diff --git a/src/content/operators/impls/ScrollToTopOperator.ts b/src/content/operators/impls/ScrollToTopOperator.ts new file mode 100644 index 0000000..6075758 --- /dev/null +++ b/src/content/operators/impls/ScrollToTopOperator.ts @@ -0,0 +1,20 @@ +import AbstractScrollOperator from "./AbstractScrollOperator"; +import Operator from "../Operator"; +import ScrollPresenter from "../../presenters/ScrollPresenter"; +import SettingRepository from "../../repositories/SettingRepository"; + +export default class ScrollToTopOperator + extends AbstractScrollOperator + implements Operator { + constructor( + private readonly presenter: ScrollPresenter, + settingRepository: SettingRepository + ) { + super(settingRepository); + } + + async run(): Promise<void> { + const smooth = this.getSmoothScroll(); + this.presenter.scrollToTop(smooth); + } +} diff --git a/src/content/operators/impls/StartFollowOperator.ts b/src/content/operators/impls/StartFollowOperator.ts new file mode 100644 index 0000000..6f30058 --- /dev/null +++ b/src/content/operators/impls/StartFollowOperator.ts @@ -0,0 +1,14 @@ +import Operator from "../Operator"; +import FollowMasterClient from "../../client/FollowMasterClient"; + +export default class StartFollowOperator implements Operator { + constructor( + private readonly followMasterClient: FollowMasterClient, + private readonly newTab: boolean, + private readonly background: boolean + ) {} + + async run(): Promise<void> { + this.followMasterClient.startFollow(this.newTab, this.background); + } +} diff --git a/src/content/operators/impls/ToggleAddonOperator.ts b/src/content/operators/impls/ToggleAddonOperator.ts new file mode 100644 index 0000000..2a249d6 --- /dev/null +++ b/src/content/operators/impls/ToggleAddonOperator.ts @@ -0,0 +1,16 @@ +import Operator from "../Operator"; +import AddonIndicatorClient from "../../client/AddonIndicatorClient"; +import AddonEnabledRepository from "../../repositories/AddonEnabledRepository"; + +export default class ToggleAddonOperator implements Operator { + constructor( + private readonly indicator: AddonIndicatorClient, + private readonly repository: AddonEnabledRepository + ) {} + + async run(): Promise<void> { + const current = this.repository.get(); + this.repository.set(!current); + await this.indicator.setEnabled(!current); + } +} diff --git a/src/content/operators/impls/URLRepository.ts b/src/content/operators/impls/URLRepository.ts new file mode 100644 index 0000000..a1efc2e --- /dev/null +++ b/src/content/operators/impls/URLRepository.ts @@ -0,0 +1,9 @@ +export default interface URLRepository { + getCurrentURL(): string; +} + +export class URLRepositoryImpl implements URLRepository { + getCurrentURL(): string { + return window.location.href; + } +} diff --git a/src/content/operators/impls/VerticalScrollOperator.ts b/src/content/operators/impls/VerticalScrollOperator.ts new file mode 100644 index 0000000..4ab336c --- /dev/null +++ b/src/content/operators/impls/VerticalScrollOperator.ts @@ -0,0 +1,21 @@ +import AbstractScrollOperator from "./AbstractScrollOperator"; +import Operator from "../Operator"; +import ScrollPresenter from "../../presenters/ScrollPresenter"; +import SettingRepository from "../../repositories/SettingRepository"; + +export default class VerticalScrollOperator + extends AbstractScrollOperator + implements Operator { + constructor( + private readonly presenter: ScrollPresenter, + settingRepository: SettingRepository, + private readonly count: number + ) { + super(settingRepository); + } + + async run(): Promise<void> { + const smooth = this.getSmoothScroll(); + this.presenter.scrollVertically(this.count, smooth); + } +} diff --git a/src/content/operators/impls/YankURLOperator.ts b/src/content/operators/impls/YankURLOperator.ts new file mode 100644 index 0000000..2e774eb --- /dev/null +++ b/src/content/operators/impls/YankURLOperator.ts @@ -0,0 +1,18 @@ +import Operator from "../Operator"; +import ClipboardRepository from "../../repositories/ClipboardRepository"; +import ConsoleClient from "../../client/ConsoleClient"; +import URLRepository from "./URLRepository"; + +export default class YankURLOperator implements Operator { + constructor( + private readonly repository: ClipboardRepository, + private readonly consoleClient: ConsoleClient, + private readonly urlRepository: URLRepository + ) {} + + async run(): Promise<void> { + const url = this.urlRepository.getCurrentURL(); + this.repository.write(url); + await this.consoleClient.info("Yanked " + url); + } +} |