refactor(react): allow binding multiple events at once

This commit is contained in:
Elian Doran 2025-08-21 13:17:28 +03:00
parent 51e8a80ca3
commit bea352855a
No known key found for this signature in database
2 changed files with 16 additions and 11 deletions

View File

@ -52,16 +52,10 @@ export default function NoteTitleWidget() {
// Manage focus.
const textBoxRef = useRef<HTMLInputElement>(null);
const isNewNote = useRef<boolean>();
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);
}
});

View File

@ -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);
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) {