2023-08-21 17:01:20 +02:00
|
|
|
import BaseModel from '@joplin/lib/BaseModel';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
|
|
|
import { NoteEntity } from '@joplin/lib/services/database/types';
|
|
|
|
import { useCallback } from 'react';
|
|
|
|
import canManuallySortNotes from './canManuallySortNotes';
|
2024-03-26 13:45:15 +02:00
|
|
|
import { FocusNote } from './useFocusNote';
|
|
|
|
import { Dispatch } from 'redux';
|
2023-08-21 17:01:20 +02:00
|
|
|
|
2024-03-26 13:45:15 +02:00
|
|
|
const useMoveNote = (notesParentType: string, noteSortOrder: string, selectedNoteIds: string[], selectedFolderId: string, uncompletedTodosOnTop: boolean, showCompletedTodos: boolean, notes: NoteEntity[], selectedFolderInTrash: boolean, makeItemIndexVisible: (itemIndex: number)=> void, focusNote: FocusNote, dispatch: Dispatch) => {
|
2023-08-21 17:01:20 +02:00
|
|
|
const moveNote = useCallback((direction: number, inc: number) => {
|
2024-03-02 16:25:27 +02:00
|
|
|
if (!canManuallySortNotes(notesParentType, noteSortOrder, selectedFolderInTrash)) return;
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
const noteId = selectedNoteIds[0];
|
|
|
|
let targetNoteIndex = BaseModel.modelIndexById(notes, noteId);
|
|
|
|
if ((direction === 1)) {
|
|
|
|
targetNoteIndex += inc + 1;
|
|
|
|
}
|
|
|
|
if ((direction === -1)) {
|
|
|
|
targetNoteIndex -= inc;
|
|
|
|
}
|
|
|
|
void Note.insertNotesAt(selectedFolderId, selectedNoteIds, targetNoteIndex, uncompletedTodosOnTop, showCompletedTodos);
|
2024-03-26 13:45:15 +02:00
|
|
|
|
|
|
|
// The note will be moved to the target index, so we need to update the scroll amount to make it visible
|
|
|
|
dispatch({
|
|
|
|
type: 'NOTE_SELECT',
|
|
|
|
id: noteId,
|
|
|
|
});
|
|
|
|
|
|
|
|
makeItemIndexVisible(targetNoteIndex);
|
|
|
|
|
|
|
|
focusNote(noteId);
|
|
|
|
}, [selectedFolderId, noteSortOrder, notes, notesParentType, selectedNoteIds, uncompletedTodosOnTop, showCompletedTodos, selectedFolderInTrash, makeItemIndexVisible, focusNote, dispatch]);
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
return moveNote;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useMoveNote;
|