2023-08-21 17:01:20 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import { useRef, useCallback, MutableRefObject } from 'react';
|
2024-04-01 16:34:22 +02:00
|
|
|
import { focus } from '@joplin/lib/utils/focusHandler';
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
export type FocusNote = (noteId: string)=> void;
|
|
|
|
|
|
|
|
const useFocusNote = (itemRefs: MutableRefObject<Record<string, HTMLDivElement>>) => {
|
|
|
|
const focusItemIID = useRef(null);
|
|
|
|
|
|
|
|
const focusNote: FocusNote = useCallback((noteId: string) => {
|
|
|
|
// - We need to focus the item manually otherwise focus might be lost when the
|
|
|
|
// list is scrolled and items within it are being rebuilt.
|
|
|
|
// - We need to use an interval because when leaving the arrow pressed, the rendering
|
|
|
|
// of items might lag behind and so the ref is not yet available at this point.
|
|
|
|
|
|
|
|
if (!itemRefs.current[noteId]) {
|
|
|
|
if (focusItemIID.current) shim.clearInterval(focusItemIID.current);
|
|
|
|
focusItemIID.current = shim.setInterval(() => {
|
|
|
|
if (itemRefs.current[noteId]) {
|
2024-04-01 16:34:22 +02:00
|
|
|
focus('useFocusNote1', itemRefs.current[noteId]);
|
2023-08-21 17:01:20 +02:00
|
|
|
shim.clearInterval(focusItemIID.current);
|
|
|
|
focusItemIID.current = null;
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
} else {
|
|
|
|
if (focusItemIID.current) shim.clearInterval(focusItemIID.current);
|
2024-04-01 16:34:22 +02:00
|
|
|
focus('useFocusNote2', itemRefs.current[noteId]);
|
2023-08-21 17:01:20 +02:00
|
|
|
}
|
|
|
|
}, [itemRefs]);
|
|
|
|
|
|
|
|
return focusNote;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useFocusNote;
|