diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.ts index 15e9b8910..54041b3d0 100644 --- a/src/public/app/components/app_context.ts +++ b/src/public/app/components/app_context.ts @@ -282,7 +282,7 @@ type CommandAndEventMappings = CommandMappings & EventMappings; * This type is a discriminated union which contains all the possible commands that can be triggered via {@link AppContext.triggerCommand}. */ export type CommandNames = keyof CommandMappings; -type EventNames = keyof EventMappings; +export type EventNames = keyof EventMappings; type FilterByValueType = { [K in keyof T]: T[K] extends ValueType ? K : never }[keyof T]; diff --git a/src/public/app/components/component.ts b/src/public/app/components/component.ts index 569b5d433..64a9597dc 100644 --- a/src/public/app/components/component.ts +++ b/src/public/app/components/component.ts @@ -1,5 +1,5 @@ import utils from "../services/utils.js"; -import type { CommandMappings, CommandNames } from "./app_context.js"; +import type { CommandMappings, CommandNames, EventData, EventNames } from "./app_context.js"; /** * Abstract class for all components in the Trilium's frontend. @@ -65,11 +65,11 @@ export class TypedComponent> { return this.parent?.triggerEvent(name, data); } - handleEventInChildren(name: string, data: unknown = {}) { - const promises = []; + handleEventInChildren(name: T, data: EventData): Promise | null { + const promises: Promise[] = []; for (const child of this.children) { - const ret = child.handleEvent(name, data); + const ret = child.handleEvent(name, data) as Promise; if (ret) { promises.push(ret); diff --git a/src/public/app/widgets/type_widgets/type_widget.js b/src/public/app/widgets/type_widgets/type_widget.ts similarity index 64% rename from src/public/app/widgets/type_widgets/type_widget.js rename to src/public/app/widgets/type_widgets/type_widget.ts index 3591c7821..50afb3c61 100644 --- a/src/public/app/widgets/type_widgets/type_widget.js +++ b/src/public/app/widgets/type_widgets/type_widget.ts @@ -1,7 +1,9 @@ import NoteContextAwareWidget from "../note_context_aware_widget.js"; -import appContext from "../../components/app_context.js"; +import appContext, { type EventData, type EventNames } from "../../components/app_context.js"; +import type FNote from "../../entities/fnote.js"; +import type NoteDetailWidget from "../note_detail.js"; -export default class TypeWidget extends NoteContextAwareWidget { +export default abstract class TypeWidget extends NoteContextAwareWidget { // for overriding static getType() {} @@ -11,12 +13,11 @@ export default class TypeWidget extends NoteContextAwareWidget { return super.doRender(); } - /** @param {FNote} note */ - async doRefresh(note) {} + abstract doRefresh(note: FNote | null | undefined): Promise; async refresh() { - const thisWidgetType = this.constructor.getType(); - const noteWidgetType = await this.parent.getWidgetType(); + const thisWidgetType = (this.constructor as any).getType(); + const noteWidgetType = await (this.parent as NoteDetailWidget).getWidgetType(); if (thisWidgetType !== noteWidgetType) { this.toggleInt(false); @@ -27,7 +28,7 @@ export default class TypeWidget extends NoteContextAwareWidget { await this.doRefresh(this.note); - this.triggerEvent("noteDetailRefreshed", { ntxId: this.noteContext.ntxId }); + this.triggerEvent("noteDetailRefreshed", { ntxId: this.noteContext?.ntxId }); } } @@ -40,7 +41,7 @@ export default class TypeWidget extends NoteContextAwareWidget { focus() {} - async readOnlyTemporarilyDisabledEvent({ noteContext }) { + async readOnlyTemporarilyDisabledEvent({ noteContext }: EventData<"readOnlyTemporarilyDisabled">) { if (this.isNoteContext(noteContext.ntxId)) { await this.refresh(); @@ -49,10 +50,10 @@ export default class TypeWidget extends NoteContextAwareWidget { } // events should be propagated manually to the children widgets - handleEventInChildren(name, data) { + handleEventInChildren(name: T, data: EventData) { if (["activeContextChanged", "setNoteContext"].includes(name)) { // won't trigger .refresh(); - return super.handleEventInChildren("setNoteContext", data); + return super.handleEventInChildren("setNoteContext", data as EventData<"activeContextChanged">); } else if (name === "entitiesReloaded") { return super.handleEventInChildren(name, data); } else {