Skip to content

Build your personal knowledge base with Trilium Notes

chore(client/ts): port type_widget

Elian Dorane16f4a1a7152
diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.ts
index 15e9b8910a..54041b3d01 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<T, ValueType> = { [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 569b5d4334..64a9597dcd 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<ChildT extends TypedComponent<ChildT>> {
return this.parent?.triggerEvent(name, data);
}
- handleEventInChildren(name: string, data: unknown = {}) {
- const promises = [];
+ handleEventInChildren<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown[] | unknown> | null {
+ const promises: Promise<unknown>[] = [];
for (const child of this.children) {
- const ret = child.handleEvent(name, data);
+ const ret = child.handleEvent(name, data) as Promise<void>;
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 3591c7821c..50afb3c612 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<void>;
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<T extends EventNames>(name: T, data: EventData<T>) {
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 {