Skip to content

Commit

Permalink
refactor: Update type definitions and usage across handlers
Browse files Browse the repository at this point in the history
- Rename `Options` to `LoadedOptions` to better reflect the loaded
  state of options.
- Introduce `MessageEventData` and `ValueSetEmitOptions` for more
  precise typing in message handling and value setting.
- Update method signatures and implementations to use the new type
  definitions, improving type safety and clarity.
- Adjust imports and usage in various handler classes to align with
  the updated type definitions.
  • Loading branch information
KarimAziev committed Mar 13, 2024
1 parent 5cd6a26 commit e7095c5
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 30 deletions.
17 changes: 9 additions & 8 deletions src/content-script-tools/text-syncer.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -29,7 +29,7 @@ class TextSyncer {
url: string,
title: string,
handler: IHandler,
options?: Options,
options?: LoadedOptions,
) {
const port = chrome.runtime.connect();

Expand All @@ -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);
};
Expand All @@ -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);
}
Expand Down Expand Up @@ -107,7 +108,7 @@ class TextSyncer {
url: string,
title: string,
handler: IHandler,
options?: Options,
options?: LoadedOptions,
) {
options = options || {};

Expand Down
7 changes: 4 additions & 3 deletions src/handlers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
IHandler,
IContentEventsBinder,
UpdateTextPayload,
Options,
LoadedOptions,
ValueSetEmitOptions,
} from '@/handlers/types';

/**
Expand Down Expand Up @@ -33,7 +34,7 @@ export default class BaseHandler extends EventEmitter implements IHandler {
/**
* Handler initialization or data loading.
*/
load(): Promise<Options> {
load(): Promise<LoadedOptions> {
return Promise.resolve({});
}

Expand All @@ -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 || {});
}

Expand Down
3 changes: 2 additions & 1 deletion src/handlers/codemirror5.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import InjectorHandler from '@/handlers/injector';
import { UpdateTextPayload } from '@/handlers/types';
import type { IContentEventsBinder } from '@/handlers/injector';

/**
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/codemirror6.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/injected/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Options,
LoadedOptions,
UpdateTextPayload,
PostToInjectorPayloadMap,
BaseInjectedPostType,
Expand Down Expand Up @@ -139,7 +139,7 @@ export default class BaseInjectedHandler<Elem extends Element> {
/**
* Optionally returns data about the handler's capabilities. Designed to be overridden.
*/
getExtension(): Options['extension'] {
getExtension(): LoadedOptions['extension'] {
return undefined;
}

Expand Down
9 changes: 5 additions & 4 deletions src/handlers/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import BaseHandler from '@/handlers/base';
import {
IContentEventsBinder,
UpdateTextPayload,
Options,
ValueSetEmitOptions,
LoadedOptions,
PostToInjectedPayloadMap,
} from '@/handlers/types';
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;
Expand Down Expand Up @@ -51,7 +52,7 @@ export default class InjectorHandler extends BaseHandler {

async load() {
const elemSelector = getCssSelector(this.elem);
return new Promise<Options>((resolve) => {
return new Promise<LoadedOptions>((resolve) => {
this.injectScript(() => {
this.postToInjected('initialize', {
name: this.name,
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/textarea.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -56,7 +56,7 @@ class TextareaHandler extends BaseHandler {
return estimateParent(this.elem);
}

load(): Promise<Options> {
load(): Promise<LoadedOptions> {
const parentEl = this.getVisualElement();
const rect = parentEl?.getBoundingClientRect();
const screenY = window.screenY;
Expand Down
26 changes: 17 additions & 9 deletions src/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -47,14 +55,14 @@ export interface IHandler {
/**
* Loads data or performs an initialization operation.
*/
load(): Promise<Options>;
load(): Promise<LoadedOptions>;

/**
* 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.
Expand All @@ -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<Elem = Element> {
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;
}

/**
Expand All @@ -104,7 +112,7 @@ export interface IContentEventsBinder {
}

export type PostToInjectorPayloadMap = {
ready: Options;
ready: LoadedOptions;
value: UpdateTextPayload;
change: {};
};
Expand All @@ -113,7 +121,7 @@ export type BaseInjectedPostType = keyof PostToInjectorPayloadMap;

export interface RegisterPayload
extends UpdateTextPayload,
Omit<Options, 'rect'> {
Omit<LoadedOptions, 'rect'> {
url: string;
title: string;
rect?: Record<keyof DOMRect, number>;
Expand Down

0 comments on commit e7095c5

Please sign in to comment.