From 6703b7845798f4b142a756268650a59b8377f2d6 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 12 Sep 2025 21:50:56 +0300 Subject: [PATCH] refactor(collections/board): move within board to API --- .../src/widgets/collections/board/api.ts | 38 +++++++++++++++++++ .../src/widgets/collections/board/column.tsx | 38 +------------------ 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts index c4cca678a..86936bc67 100644 --- a/apps/client/src/widgets/collections/board/api.ts +++ b/apps/client/src/widgets/collections/board/api.ts @@ -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); + } + } + } + } diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx index c38b49ffe..b9036c7f5 100644 --- a/apps/client/src/widgets/collections/board/column.tsx +++ b/apps/client/src/widgets/collections/board/column.tsx @@ -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 ]);