2024-08-31 17:05:01 +02:00
|
|
|
import { useRef, useCallback, RefObject } from 'react';
|
2024-04-01 16:34:22 +02:00
|
|
|
import { focus } from '@joplin/lib/utils/focusHandler';
|
2024-05-30 09:38:36 +02:00
|
|
|
import { NoteEntity } from '@joplin/lib/services/database/types';
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
export type FocusNote = (noteId: string)=> void;
|
2024-08-31 17:05:01 +02:00
|
|
|
type ContainerRef = RefObject<HTMLElement>;
|
2024-05-30 09:38:36 +02:00
|
|
|
type OnMakeIndexVisible = (i: number)=> void;
|
2024-08-31 17:05:01 +02:00
|
|
|
type OnSetActiveId = (id: string)=> void;
|
2023-08-21 17:01:20 +02:00
|
|
|
|
2024-08-31 17:05:01 +02:00
|
|
|
const useFocusNote = (
|
|
|
|
containerRef: ContainerRef, notes: NoteEntity[], makeItemIndexVisible: OnMakeIndexVisible, setActiveNoteId: OnSetActiveId,
|
|
|
|
) => {
|
2024-05-30 09:38:36 +02:00
|
|
|
const notesRef = useRef(notes);
|
|
|
|
notesRef.current = notes;
|
|
|
|
|
2023-08-21 17:01:20 +02:00
|
|
|
const focusNote: FocusNote = useCallback((noteId: string) => {
|
2024-08-31 17:05:01 +02:00
|
|
|
if (noteId) {
|
|
|
|
setActiveNoteId(noteId);
|
|
|
|
}
|
2023-08-21 17:01:20 +02:00
|
|
|
|
2024-08-31 17:05:01 +02:00
|
|
|
// The note list container should have focus even when a note list item is visibly selected.
|
|
|
|
// The visibly focused item is determined by activeNoteId and is communicated to accessibility
|
|
|
|
// tools using aria- attributes
|
|
|
|
focus('useFocusNote', containerRef.current);
|
2024-05-30 09:38:36 +02:00
|
|
|
|
2024-08-31 17:05:01 +02:00
|
|
|
const targetIndex = notesRef.current.findIndex(note => note.id === noteId);
|
|
|
|
if (targetIndex > -1) {
|
|
|
|
makeItemIndexVisible(targetIndex);
|
2023-08-21 17:01:20 +02:00
|
|
|
}
|
2024-08-31 17:05:01 +02:00
|
|
|
}, [containerRef, makeItemIndexVisible, setActiveNoteId]);
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
return focusNote;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useFocusNote;
|