diff --git a/src/content-script-tools/text-syncer.ts b/src/content-script-tools/text-syncer.ts index ead13a2..02a2ed6 100644 --- a/src/content-script-tools/text-syncer.ts +++ b/src/content-script-tools/text-syncer.ts @@ -1,10 +1,10 @@ import { IHandler, - Options, - UpdateTextPayload, + LoadedOptions, RegisterPayload, SocketPostPayloadMap, ClosedMessagePayload, + MessageEventData, } from '@/handlers/types'; import { messager } from '@/content-script-tools/message'; import { WS_URL } from '@/background-tools/ws-bridge'; @@ -29,7 +29,7 @@ class TextSyncer { url: string, title: string, handler: IHandler, - options?: Options, + options?: LoadedOptions, ) { const port = chrome.runtime.connect(); @@ -50,9 +50,9 @@ class TextSyncer { * @returns A function to be used as the onMessage event listener. */ private makeMessageListener(handler: IHandler) { - return (msg: any) => { - if ((this as any)[msg.type]) { - return (this as any)[msg.type](handler, msg.payload); + return (msg: MessageEventData) => { + if (this[msg.type]) { + return this[msg.type](handler, msg.payload); } console.warn('Chrome Emacs received unknown message:', msg); }; @@ -62,7 +62,8 @@ class TextSyncer { * @param handler - The handler instance managing the text element. * @param payload - The payload containing the updated text. */ - updateText(handler: IHandler, payload: UpdateTextPayload) { + + updateText(handler: IHandler, payload: MessageEventData['payload']) { if (handler.setValue) { handler.setValue(payload.text, payload); } @@ -107,7 +108,7 @@ class TextSyncer { url: string, title: string, handler: IHandler, - options?: Options, + options?: LoadedOptions, ) { options = options || {}; diff --git a/src/handlers/base.ts b/src/handlers/base.ts index 0f783d0..f7e5876 100644 --- a/src/handlers/base.ts +++ b/src/handlers/base.ts @@ -3,7 +3,8 @@ import { IHandler, IContentEventsBinder, UpdateTextPayload, - Options, + LoadedOptions, + ValueSetEmitOptions, } from '@/handlers/types'; /** @@ -33,7 +34,7 @@ export default class BaseHandler extends EventEmitter implements IHandler { /** * Handler initialization or data loading. */ - load(): Promise { + load(): Promise { return Promise.resolve({}); } @@ -42,7 +43,7 @@ export default class BaseHandler extends EventEmitter implements IHandler { * @param value - The value to set. * @param options - Optional parameters. */ - setValue(value: string, options?: UpdateTextPayload): void { + setValue(value: string, options?: ValueSetEmitOptions): void { this.emit('valueSet', value, options || {}); } diff --git a/src/handlers/codemirror5.ts b/src/handlers/codemirror5.ts index 686f2de..39f0095 100644 --- a/src/handlers/codemirror5.ts +++ b/src/handlers/codemirror5.ts @@ -1,4 +1,5 @@ import InjectorHandler from '@/handlers/injector'; +import { UpdateTextPayload } from '@/handlers/types'; import type { IContentEventsBinder } from '@/handlers/injector'; /** @@ -18,7 +19,7 @@ class CodeMirror5Handler extends InjectorHandler { * @param value - The value to set in the CodeMirror editor. * @param options - Options to customize the value setting, including whether to trigger DOM events. */ - setValue(value: string, options: any) { + setValue(value: string, options: UpdateTextPayload) { options = Object.assign({}, { triggerDOMEvent: false }, options); super.setValue(value, options); } diff --git a/src/handlers/codemirror6.ts b/src/handlers/codemirror6.ts index 37a5dbc..cedd1c5 100644 --- a/src/handlers/codemirror6.ts +++ b/src/handlers/codemirror6.ts @@ -1,5 +1,6 @@ import InjectorHandler from '@/handlers/injector'; import type { IContentEventsBinder } from '@/handlers/injector'; +import { UpdateTextPayload } from '@/handlers/types'; /** * Handler class for interfacing with CodeMirror version 6 editors through InjectorHandler. @@ -18,7 +19,7 @@ class CodeMirror6Handler extends InjectorHandler { * @param value - The value to set in the CodeMirror editor. * @param options - Options to customize the value setting, including whether to trigger DOM events. */ - setValue(value: string, options: any) { + setValue(value: string, options: UpdateTextPayload) { options = Object.assign({}, { triggerDOMEvent: false }, options); super.setValue(value, options); } diff --git a/src/handlers/injected/base.ts b/src/handlers/injected/base.ts index 043abe1..3958744 100644 --- a/src/handlers/injected/base.ts +++ b/src/handlers/injected/base.ts @@ -1,5 +1,5 @@ import { - Options, + LoadedOptions, UpdateTextPayload, PostToInjectorPayloadMap, BaseInjectedPostType, @@ -139,7 +139,7 @@ export default class BaseInjectedHandler { /** * Optionally returns data about the handler's capabilities. Designed to be overridden. */ - getExtension(): Options['extension'] { + getExtension(): LoadedOptions['extension'] { return undefined; } diff --git a/src/handlers/injector.ts b/src/handlers/injector.ts index e46c4ca..652a391 100644 --- a/src/handlers/injector.ts +++ b/src/handlers/injector.ts @@ -3,7 +3,8 @@ import BaseHandler from '@/handlers/base'; import { IContentEventsBinder, UpdateTextPayload, - Options, + ValueSetEmitOptions, + LoadedOptions, PostToInjectedPayloadMap, } from '@/handlers/types'; import { getCssSelector } from '@/util/dom'; @@ -11,7 +12,7 @@ import { getCssSelector } from '@/util/dom'; /** * A specialized handler extending BaseHandler for injecting and communicating with scripts. */ -export default class InjectorHandler extends BaseHandler { +export default class nInjectorHandler extends BaseHandler { private name: string; private uuid: string; private _getValueCallback: ((payload: UpdateTextPayload) => void) | null; @@ -51,7 +52,7 @@ export default class InjectorHandler extends BaseHandler { async load() { const elemSelector = getCssSelector(this.elem); - return new Promise((resolve) => { + return new Promise((resolve) => { this.injectScript(() => { this.postToInjected('initialize', { name: this.name, @@ -67,7 +68,7 @@ export default class InjectorHandler extends BaseHandler { * @param value - The value to set. * @param options - Optional parameters. */ - setValue(value: string, options?: UpdateTextPayload): void { + setValue(value: string, options?: ValueSetEmitOptions): void { this.postToInjected('setValue', { text: value, ...options }); super.setValue(value, options); } diff --git a/src/handlers/textarea.ts b/src/handlers/textarea.ts index 4f22ef6..b1e119c 100644 --- a/src/handlers/textarea.ts +++ b/src/handlers/textarea.ts @@ -1,5 +1,5 @@ import BaseHandler from '@/handlers/base'; -import { UpdateTextPayload, Options } from '@/handlers/types'; +import { UpdateTextPayload, LoadedOptions } from '@/handlers/types'; import { estimateParent, setSelectionRange } from '@/util/dom'; class TextareaHandler extends BaseHandler { @@ -56,7 +56,7 @@ class TextareaHandler extends BaseHandler { return estimateParent(this.elem); } - load(): Promise { + load(): Promise { const parentEl = this.getVisualElement(); const rect = parentEl?.getBoundingClientRect(); const screenY = window.screenY; diff --git a/src/handlers/types.ts b/src/handlers/types.ts index f3631f3..b0b2dc3 100644 --- a/src/handlers/types.ts +++ b/src/handlers/types.ts @@ -29,13 +29,21 @@ export interface IPosition { */ export interface UpdateTextPayload extends IPosition { text: string; - triggerDOMEvent?: boolean; +} + +export interface MessageEventData { + type: 'updateText'; + payload: UpdateTextPayload; } /** * Defines options for handler configuration. */ -export interface Options extends IPosition { +export interface ValueSetEmitOptions extends UpdateTextPayload { + triggerDOMEvent?: boolean; +} + +export interface LoadedOptions extends IPosition { extension?: string | string[] | null; rect?: DOMRect; } @@ -47,14 +55,14 @@ export interface IHandler { /** * Loads data or performs an initialization operation. */ - load(): Promise; + load(): Promise; /** * Sets a value with optional settings. * @param value - The value to set. * @param options - Additional options. */ - setValue(value: string, options?: UpdateTextPayload): void; + setValue(value: string, options?: ValueSetEmitOptions): void; /** * Retrieves the currently set value. @@ -80,15 +88,15 @@ export interface IHandler { /** * Constructs handler objects capable of handling elements. */ -export interface IHandlerConstructor { - new (elem: Element, contentEvents: IContentEventsBinder): IHandler; +export interface IHandlerConstructor { + new (elem: Elem, contentEvents: IContentEventsBinder): IHandler; /** * Determines if the handler can manage the provided element. * @param elem - The element to check. * @returns A boolean indicating if the handler can manage the element. */ - canHandle(elem: Element): boolean; + canHandle(elem: Elem): boolean; } /** @@ -104,7 +112,7 @@ export interface IContentEventsBinder { } export type PostToInjectorPayloadMap = { - ready: Options; + ready: LoadedOptions; value: UpdateTextPayload; change: {}; }; @@ -113,7 +121,7 @@ export type BaseInjectedPostType = keyof PostToInjectorPayloadMap; export interface RegisterPayload extends UpdateTextPayload, - Omit { + Omit { url: string; title: string; rect?: Record;