refactor(collections/board): move within board to API

This commit is contained in:
Elian Doran
2025-09-12 21:50:56 +03:00
parent 7a61bbc297
commit 6703b78457
2 changed files with 40 additions and 36 deletions

View File

@@ -2,7 +2,9 @@ import { BoardViewData } from ".";
import appContext from "../../../components/app_context";
import FNote from "../../../entities/fnote";
import attributes from "../../../services/attributes";
import branches from "../../../services/branches";
import { executeBulkActions } from "../../../services/bulk_action";
import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
import note_create from "../../../services/note_create";
import server from "../../../services/server";
@@ -154,5 +156,41 @@ export default class BoardApi {
return server.put(`notes/${noteId}/title`, { title: newTitle.trim() });
}
async moveWithinBoard(noteId: string, sourceBranchId: string, sourceIndex: number, targetIndex: number, sourceColumn: string, targetColumn: string) {
const targetItems = this.byColumn?.get(targetColumn) ?? [];
const note = froca.getNoteFromCache(noteId);
if (!note) return;
if (sourceColumn !== targetColumn) {
// Moving to a different column
await this.changeColumn(noteId, targetColumn);
// If there are items in the target column, reorder
if (targetItems.length > 0 && targetIndex < targetItems.length) {
const targetBranch = targetItems[targetIndex].branch;
await branches.moveBeforeBranch([ sourceBranchId ], targetBranch.branchId);
}
} else if (sourceIndex !== targetIndex) {
// Reordering within the same column
let targetBranchId: string | null = null;
if (targetIndex < targetItems.length) {
// Moving before an existing item
const adjustedIndex = sourceIndex < targetIndex ? targetIndex : targetIndex;
if (adjustedIndex < targetItems.length) {
targetBranchId = targetItems[adjustedIndex].branch.branchId;
if (targetBranchId) {
await branches.moveBeforeBranch([ sourceBranchId ], targetBranchId);
}
}
} else if (targetIndex > 0) {
// Moving to the end - place after the last item
const lastItem = targetItems[targetItems.length - 1];
await branches.moveAfterBranch([ sourceBranchId ], lastItem.branch.branchId);
}
}
}
}

View File

@@ -234,42 +234,8 @@ function useDragging({ column, columnIndex, columnItems }: DragContext) {
} else if (targetBranch) {
await branches.moveAfterBranch([ branchId ], targetBranch.branchId);
}
} else {
// From within the board.
if (draggedCard && dropPosition) {
const targetIndex = dropPosition.index;
const targetItems = columnItems || [];
const note = froca.getNoteFromCache(draggedCard.noteId);
if (!note) return;
if (draggedCard.fromColumn !== column || !draggedCard.index) {
// Moving to a different column
await api?.changeColumn(draggedCard.noteId, column);
// If there are items in the target column, reorder
if (targetItems.length > 0 && targetIndex < targetItems.length) {
const targetBranch = targetItems[targetIndex].branch;
await branches.moveBeforeBranch([ draggedCard.branchId ], targetBranch.branchId);
}
} else if (draggedCard.index !== targetIndex) {
// Reordering within the same column
let targetBranchId: string | null = null;
if (targetIndex < targetItems.length) {
// Moving before an existing item
const adjustedIndex = draggedCard.index < targetIndex ? targetIndex : targetIndex;
if (adjustedIndex < targetItems.length) {
targetBranchId = targetItems[adjustedIndex].branch.branchId;
await branches.moveBeforeBranch([ draggedCard.branchId ], targetBranchId);
}
} else if (targetIndex > 0) {
// Moving to the end - place after the last item
const lastItem = targetItems[targetItems.length - 1];
await branches.moveAfterBranch([ draggedCard.branchId ], lastItem.branch.branchId);
}
}
}
} else if (draggedCard && dropPosition) {
api?.moveWithinBoard(draggedCard.noteId, draggedCard.branchId, draggedCard.index, dropPosition.index, draggedCard.fromColumn, column);
}
}, [ api, draggedColumn, dropPosition, columnItems, column, setDropTarget, setDropPosition ]);