Skip to content

Build your personal knowledge base with Trilium Notes

refactor(react): allow binding multiple events at once

Elian Doranbea352855a4e
diff --git a/apps/client/src/widgets/note_title.tsx b/apps/client/src/widgets/note_title.tsx
index d824d796e1..9d1522740d 100644
--- a/apps/client/src/widgets/note_title.tsx
+++ b/apps/client/src/widgets/note_title.tsx
@@ -52,16 +52,10 @@ export default function NoteTitleWidget() {
// Manage focus.
const textBoxRef = useRef<HTMLInputElement>(null);
const isNewNote = useRef<boolean>();
- useTriliumEventBeta("focusOnTitle", () => {
+ useTriliumEventBeta([ "focusOnTitle", "focusAndSelectTitle" ], (e) => {
if (noteContext?.isActive() && textBoxRef.current) {
- console.log(textBoxRef.current);
textBoxRef.current.focus();
- }
- });
- useTriliumEventBeta("focusAndSelectTitle", ({ isNewNote: _isNewNote } ) => {
- if (noteContext?.isActive() && textBoxRef.current) {
- textBoxRef.current.focus();
- isNewNote.current = _isNewNote;
+ isNewNote.current = ("isNewNote" in e ? e.isNewNote : false);
}
});
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 4f06041fef..9269e06054 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -87,11 +87,22 @@ export default function useTriliumEvent<T extends EventNames>(eventName: T, hand
}, [ eventName, parentWidget, handler ]);
}
-export function useTriliumEventBeta<T extends EventNames>(eventName: T, handler: TriliumEventHandler<T>) {
+export function useTriliumEventBeta<T extends EventNames>(eventName: T | T[], handler: TriliumEventHandler<T>) {
const parentComponent = useContext(ParentComponent) as ReactWrappedWidget;
- parentComponent.registerHandler(eventName, handler);
- return (() => parentComponent.removeHandler(eventName, handler));
+ if (Array.isArray(eventName)) {
+ for (const eventSingleName of eventName) {
+ parentComponent.registerHandler(eventSingleName, handler);
+ }
+ return (() => {
+ for (const eventSingleName of eventName) {
+ parentComponent.removeHandler(eventSingleName, handler)
+ }
+ });
+ } else {
+ parentComponent.registerHandler(eventName, handler);
+ return (() => parentComponent.removeHandler(eventName, handler));
+ }
}
export function useSpacedUpdate(callback: () => Promise<void>, interval = 1000) {