Skip to content

Build your personal knowledge base with Trilium Notes

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

Elian Doran6703b7845798
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index c4cca678ae..86936bc67f 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 c38b49ffea..b9036c7f59 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 ]);