mirror of
https://github.com/zadam/trilium.git
synced 2026-02-20 21:37:06 +01:00
feat(react): basic implementation of note title
This commit is contained in:
@@ -5,6 +5,7 @@ import { t } from "../services/i18n.js";
|
||||
import toastService from "../services/toast.js";
|
||||
import { renderReactWidget } from "./react/react_utils.jsx";
|
||||
import { EventNames, EventData } from "../components/app_context.js";
|
||||
import { Handler } from "leaflet";
|
||||
|
||||
export class TypedBasicWidget<T extends TypedComponent<any>> extends TypedComponent<T> {
|
||||
protected attrs: Record<string, string>;
|
||||
@@ -277,10 +278,12 @@ export function wrapReactWidgets<T extends TypedComponent<any>>(components: (T |
|
||||
return wrappedResult;
|
||||
}
|
||||
|
||||
type EventHandler = ((data: any) => void);
|
||||
|
||||
export class ReactWrappedWidget extends BasicWidget {
|
||||
|
||||
private el: VNode;
|
||||
listeners: Record<string, (data: any) => void> = {};
|
||||
private listeners: Record<string, EventHandler[]> = {};
|
||||
|
||||
constructor(el: VNode) {
|
||||
super();
|
||||
@@ -292,8 +295,38 @@ export class ReactWrappedWidget extends BasicWidget {
|
||||
}
|
||||
|
||||
handleEvent<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown[] | unknown> | null | undefined {
|
||||
const listener = this.listeners[name];
|
||||
listener?.(data);
|
||||
if (!this.listeners[name]) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const listener of this.listeners[name]) {
|
||||
listener(data);
|
||||
}
|
||||
}
|
||||
|
||||
registerHandler<T extends EventNames>(name: T, handler: EventHandler) {
|
||||
if (!this.listeners[name]) {
|
||||
this.listeners[name] = [];
|
||||
}
|
||||
|
||||
if (this.listeners[name].includes(handler)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.listeners[name].push(handler);
|
||||
}
|
||||
|
||||
removeHandler<T extends EventNames>(name: T, handler: EventHandler) {
|
||||
if (!this.listeners[name]?.includes(handler)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.listeners[name] = this.listeners[name]
|
||||
.filter(listener => listener !== handler);
|
||||
|
||||
if (!this.listeners[name].length) {
|
||||
delete this.listeners[name];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user