mirror of
https://github.com/zadam/trilium.git
synced 2025-10-29 18:49:00 +01:00
refactor(react): allow binding multiple events at once
This commit is contained in:
parent
51e8a80ca3
commit
bea352855a
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user