From bea352855a4e38c15406fac12e2f7a24654ff56d Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 21 Aug 2025 13:17:28 +0300 Subject: [PATCH] refactor(react): allow binding multiple events at once --- apps/client/src/widgets/note_title.tsx | 10 ++-------- apps/client/src/widgets/react/hooks.tsx | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/apps/client/src/widgets/note_title.tsx b/apps/client/src/widgets/note_title.tsx index d824d796e..9d1522740 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(null); const isNewNote = useRef(); - useTriliumEventBeta("focusOnTitle", () => { - if (noteContext?.isActive() && textBoxRef.current) { - console.log(textBoxRef.current); - textBoxRef.current.focus(); - } - }); - useTriliumEventBeta("focusAndSelectTitle", ({ isNewNote: _isNewNote } ) => { + useTriliumEventBeta([ "focusOnTitle", "focusAndSelectTitle" ], (e) => { 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 4f06041fe..9269e0605 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(eventName: T, hand }, [ eventName, parentWidget, handler ]); } -export function useTriliumEventBeta(eventName: T, handler: TriliumEventHandler) { +export function useTriliumEventBeta(eventName: T | T[], handler: TriliumEventHandler) { 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, interval = 1000) {