chore(client/ts): port type_widget

This commit is contained in:
Elian Doran 2025-01-14 19:12:29 +02:00
parent 353156e625
commit e16f4a1a71
No known key found for this signature in database
3 changed files with 16 additions and 15 deletions

View File

@ -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}. * 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; 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]; type FilterByValueType<T, ValueType> = { [K in keyof T]: T[K] extends ValueType ? K : never }[keyof T];

View File

@ -1,5 +1,5 @@
import utils from "../services/utils.js"; 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. * 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); return this.parent?.triggerEvent(name, data);
} }
handleEventInChildren(name: string, data: unknown = {}) { handleEventInChildren<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown[] | unknown> | null {
const promises = []; const promises: Promise<unknown>[] = [];
for (const child of this.children) { for (const child of this.children) {
const ret = child.handleEvent(name, data); const ret = child.handleEvent(name, data) as Promise<void>;
if (ret) { if (ret) {
promises.push(ret); promises.push(ret);

View File

@ -1,7 +1,9 @@
import NoteContextAwareWidget from "../note_context_aware_widget.js"; 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 // for overriding
static getType() {} static getType() {}
@ -11,12 +13,11 @@ export default class TypeWidget extends NoteContextAwareWidget {
return super.doRender(); return super.doRender();
} }
/** @param {FNote} note */ abstract doRefresh(note: FNote | null | undefined): Promise<void>;
async doRefresh(note) {}
async refresh() { async refresh() {
const thisWidgetType = this.constructor.getType(); const thisWidgetType = (this.constructor as any).getType();
const noteWidgetType = await this.parent.getWidgetType(); const noteWidgetType = await (this.parent as NoteDetailWidget).getWidgetType();
if (thisWidgetType !== noteWidgetType) { if (thisWidgetType !== noteWidgetType) {
this.toggleInt(false); this.toggleInt(false);
@ -27,7 +28,7 @@ export default class TypeWidget extends NoteContextAwareWidget {
await this.doRefresh(this.note); 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() {} focus() {}
async readOnlyTemporarilyDisabledEvent({ noteContext }) { async readOnlyTemporarilyDisabledEvent({ noteContext }: EventData<"readOnlyTemporarilyDisabled">) {
if (this.isNoteContext(noteContext.ntxId)) { if (this.isNoteContext(noteContext.ntxId)) {
await this.refresh(); await this.refresh();
@ -49,10 +50,10 @@ export default class TypeWidget extends NoteContextAwareWidget {
} }
// events should be propagated manually to the children widgets // 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)) { if (["activeContextChanged", "setNoteContext"].includes(name)) {
// won't trigger .refresh(); // won't trigger .refresh();
return super.handleEventInChildren("setNoteContext", data); return super.handleEventInChildren("setNoteContext", data as EventData<"activeContextChanged">);
} else if (name === "entitiesReloaded") { } else if (name === "entitiesReloaded") {
return super.handleEventInChildren(name, data); return super.handleEventInChildren(name, data);
} else { } else {