Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board/search & filtering: fix card placement on empty columns (cards filtered out)

Adorian Doran67980af94dd1
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index 2f80cf6d31..d4562d0604 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -15,7 +15,7 @@ import ws from "../../../services/ws";
import { buildNote } from "../../../test/easy-froca";
import { BoardViewData } from ".";
import BoardApi, { getPendingWrites, PendingColumnWrites } from "./api";
-import { ColumnMap } from "./data";
+import { ColumnItem, ColumnMap } from "./data";
import { BOARD_TEMPLATE_ID, DEFAULT_COLUMN_ICON, getStatusDefinition, INBOX_COLUMN } from "./columns";
import { DEFAULT_CARD_TEMPLATES } from "./card_templates";
@@ -554,12 +554,82 @@ describe("BoardApi card operations", () => {
// The target column is empty, so there is nothing to place it against.
await api.moveWithinBoard(first.note.noteId, first.branch.branchId, 0, 1, "Done", "To Do");
expect(branches.moveBeforeBranch).not.toHaveBeenCalled();
+ expect(branches.moveAfterBranch).not.toHaveBeenCalled();
await api.moveWithinBoard(first.note.noteId, first.branch.branchId, 0, 1, "To Do", "Done");
expect(branches.moveBeforeBranch)
.toHaveBeenCalledWith([ first.branch.branchId ], items[1].branch.branchId);
});
+ /** A board whose "Done" column holds three cards, with a fourth waiting in "To Do". */
+ function createBoardWithSpareCard(shownInDone?: (done: ColumnItem[]) => ColumnItem[]) {
+ const board = buildNote({
+ title: "Board",
+ children: [
+ { title: "First", "#status": "Done" },
+ { title: "Second", "#status": "Done" },
+ { title: "Third", "#status": "Done" },
+ { title: "Spare", "#status": "To Do" }
+ ]
+ });
+
+ const items = board.getChildBranches().flatMap(branch => {
+ const note = froca.getNoteFromCache(branch.noteId);
+ return note ? [ { branch, note } ] : [];
+ });
+ const done = items.slice(0, 3);
+ const spare = items[3];
+ const allByColumn: ColumnMap = new Map([ [ "To Do", [ spare ] ], [ "Done", done ] ]);
+ const byColumn: ColumnMap = shownInDone
+ ? new Map([ [ "To Do", [ spare ] ], [ "Done", shownInDone(done) ] ])
+ : allByColumn;
+
+ const created = createApi(
+ {}, [ "To Do", "Done" ], board, "status", byColumn, allByColumn);
+ return { ...created, done, spare };
+ }
+
+ /**
+ * Crossing columns writes a placement too. Left with the tree position it came with, the card
+ * would rank among the target's cards by wherever it happened to sit before the drop.
+ */
+ it("files a card dropped past the last card of another column after it", async () => {
+ const { api, done, spare } = createBoardWithSpareCard();
+
+ await api.moveWithinBoard(spare.note.noteId, spare.branch.branchId, 0, 3, "To Do", "Done");
+
+ expect(branches.moveBeforeBranch).not.toHaveBeenCalled();
+ expect(branches.moveAfterBranch)
+ .toHaveBeenCalledWith([ spare.branch.branchId ], done[2].branch.branchId);
+ });
+
+ it("files a card dropped into a column a filter empties after the cards it hides", async () => {
+ const { api, done, spare } = createBoardWithSpareCard(() => []);
+
+ await api.moveWithinBoard(spare.note.noteId, spare.branch.branchId, 0, 0, "To Do", "Done");
+
+ expect(branches.moveAfterBranch)
+ .toHaveBeenCalledWith([ spare.branch.branchId ], done[2].branch.branchId);
+ });
+
+ it("files a card dropped past the last card shown after that one, not the hidden", async () => {
+ const { api, done, spare } = createBoardWithSpareCard((all) => [ all[0] ]);
+
+ await api.moveWithinBoard(spare.note.noteId, spare.branch.branchId, 0, 1, "To Do", "Done");
+
+ expect(branches.moveAfterBranch)
+ .toHaveBeenCalledWith([ spare.branch.branchId ], done[0].branch.branchId);
+ });
+
+ it("sends a card to the true end of a column a filter narrows", async () => {
+ const { api, done, spare } = createBoardWithSpareCard((all) => [ all[0] ]);
+
+ await api.moveToColumnEnd(spare.note.noteId, spare.branch.branchId, "Done");
+
+ expect(branches.moveAfterBranch)
+ .toHaveBeenCalledWith([ spare.branch.branchId ], done[2].branch.branchId);
+ });
+
it("reorders within a column, and places past the last card after it", async () => {
const { api, items } = createBoardWithCards();
const [ first, , third ] = items;
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 8f01449bdf..5ffa2d6e94 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -1126,18 +1126,19 @@ export default class BoardApi {
/**
* Moves a card to the end of another column, where a new one would go.
*
- * {@link moveWithinBoard} leaves a card crossing columns where the tree already had it, which
- * is where a drop between two cards wants it. A card sent across by the keyboard is aimed at no
- * card in particular, so it goes where the reader would look for it.
+ * A card sent across by the keyboard is aimed at no card in particular, unlike a drop, so it
+ * goes where the reader would look for it.
*/
async moveToColumnEnd(noteId: string, branchId: string, targetColumn: string) {
// What is already at the end, as far as this instance can know: nothing waits for the board
// to redraw between two keystrokes, so the column map still shows the target as it was
// before the card the last press sent. Anything sent since is remembered here instead, and
// the memory lasts exactly as long as the map it stands in for, both being rebuilt by the
- // refresh that catches up.
+ // refresh that catches up. Read off the unfiltered map, since the end of a column is behind
+ // the cards a filter is not showing.
+ const columnItems = (this.allByColumn ?? this.byColumn)?.get(targetColumn) ?? [];
const last = this.sentToColumnEnd.get(targetColumn)
- ?? (this.byColumn?.get(targetColumn) ?? []).at(-1)?.branch.branchId;
+ ?? columnItems.at(-1)?.branch.branchId;
await this.changeColumn(noteId, targetColumn);
if (last && last !== branchId) {
@@ -1170,6 +1171,11 @@ export default class BoardApi {
async moveWithinBoard(noteId: string, sourceBranchId: string, sourceIndex: number, targetIndex: number, sourceColumn: string, targetColumn: string) {
const targetItems = this.byColumn?.get(targetColumn) ?? [];
+ // What a card dropped past the last one drawn is placed after: the last card shown, or the
+ // last one the column holds when a filter is showing none of them. Read before the writes
+ // below, since a redraw between them hands this instance the map with the card moved.
+ const lastInTarget = (targetItems.at(-1)
+ ?? this.allByColumn?.get(targetColumn)?.at(-1))?.branch.branchId;
const note = froca.getNoteFromCache(noteId);
if (!note) return;
@@ -1178,10 +1184,11 @@ export default class BoardApi {
// 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) {
+ if (targetIndex < targetItems.length) {
const targetBranch = targetItems[targetIndex].branch;
await branches.moveBeforeBranch([ sourceBranchId ], targetBranch.branchId);
+ } else if (lastInTarget && lastInTarget !== sourceBranchId) {
+ await branches.moveAfterBranch([ sourceBranchId ], lastInTarget);
}
} else if (sourceIndex !== targetIndex) {
// Reordering within the same column