Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: make the cards moved by context menu to not become focused anymore

Adorian Doran753e46bd3465
diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.ts
index b0a4bcc394..b7ead0ac70 100644
--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts
@@ -850,7 +850,8 @@ describe("Board item context menu", () => {
getPromotedAttributes: () => [],
isFirstInColumn: () => false,
isColumnSorted: () => false,
- getCardColumn: () => column
+ getCardColumn: () => column,
+ getColumnNoteIds: () => []
},
api);
const selected = notes ?? [ buildNote({ title: "Card" }) as FNote ];
@@ -944,23 +945,58 @@ describe("Board item context menu", () => {
expect(api.changeColumn).toHaveBeenCalledWith(expect.any(String), "Done");
});
- /** The card is drawn afresh under the column it lands in, so focus is asked for by name. */
- it("keeps focus on the card it files, as a move by keyboard does", () => {
+ /**
+ * Focus stays in the column being emptied rather than following the card, so that sending a
+ * run of cards off does not carry the reader to wherever each one landed.
+ */
+ it("focuses the card taking its place rather than the one it files", () => {
+ const cards = [ 0, 1, 2, 3 ].map(() => buildNote({ title: "Card" }) as FNote);
+ const ids = cards.map(card => card.noteId);
const api = {
columns: [ "To Do", "Done" ],
isColumnArchived: () => false,
getColumnIcon: () => DEFAULT_COLUMN_ICON,
getColumnColorClass: () => "",
+ getColumnNoteIds: () => ids,
+ changeColumn: vi.fn(async () => {})
+ } as unknown as BoardApi;
+
+ /** Files the card standing at `index` under "Done", and says what was focused after. */
+ const fileFrom = (index: number) => {
+ const focusCard = vi.fn();
+ const items = openItemMenu(
+ api, "To Do", focusCard, vi.fn(), index, vi.fn(), [ cards[index] ]);
+ const header = items.findIndex(item => item && "kind" in item && item.kind === "header");
+ const done = items[header + 2];
+ if (done && "handler" in done) done.handler?.(done, {} as never);
+ return focusCard.mock.calls.map(call => call[0]);
+ };
+
+ // The card that closes the gap is the one after it.
+ expect(fileFrom(1)).toEqual([ ids[2] ]);
+ // From the foot there is none after it, so the column's new last card takes the focus.
+ expect(fileFrom(3)).toEqual([ ids[2] ]);
+ });
+
+ /** The card it is already under writes nothing, so there is no reason to move the focus. */
+ it("leaves the focus alone when the column named is the one it is in", () => {
+ const api = {
+ columns: [ "To Do", "Done" ],
+ isColumnArchived: () => false,
+ getColumnIcon: () => DEFAULT_COLUMN_ICON,
+ getColumnColorClass: () => "",
+ getColumnNoteIds: () => [ "first", "second" ],
changeColumn: vi.fn(async () => {})
} as unknown as BoardApi;
const focusCard = vi.fn();
const items = openItemMenu(api, "To Do", focusCard);
+ // The first column entry, which is the one the card already stands under.
const header = items.findIndex(item => item && "kind" in item && item.kind === "header");
- const done = items[header + 2];
- if (done && "handler" in done) done.handler?.(done, {} as never);
+ const toDo = items[header + 1];
+ if (toDo && "handler" in toDo) toDo.handler?.(toDo, {} as never);
- expect(focusCard).toHaveBeenCalledWith(expect.any(String));
+ expect(focusCard).not.toHaveBeenCalled();
});
/**
diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts
index 110fbd0214..0d5bf07471 100644
--- a/apps/client/src/widgets/collections/board/context_menu.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.ts
@@ -408,7 +408,7 @@ export interface NoteMenuTarget {
notes: FNote[];
/** The branches those notes have on this board, in the order `notes` lists them. */
branchIds: string[];
- /** Refocuses the card after a column change has redrawn it elsewhere. */
+ /** Puts focus on a card by name, once the board has drawn it again. */
onFocusCard: (noteId: string) => void;
/** Opens the new-card editor at an index in the column, above or below this card. */
onInsert: (index: number) => void;
@@ -442,9 +442,19 @@ function buildColumnItems(api: Api, target: NoteMenuTarget): MenuItem<CommandNam
? [ { title: t("board_view.archived-badge") } ]
: undefined,
handler: () => {
- // Asked for before the write: the cards are drawn afresh under the column they land
- // in, so the element the menu was opened from will be gone.
- target.onFocusCard(target.note.noteId);
+ // Focus stays in the column being emptied, on the card that takes this one's place:
+ // a reader sending cards off one after another would otherwise be carried to
+ // wherever each one landed. Asked for before the write, which draws the board again.
+ if (name !== current) {
+ const going = new Set(target.notes.map((note) => note.noteId));
+ const staying = api.getColumnNoteIds(target.column)
+ .filter((noteId) => !going.has(noteId));
+ const next = staying[target.index] ?? staying.at(-1);
+ if (next) {
+ target.onFocusCard(next);
+ }
+ }
+
return Promise.all(
target.notes.map((note) => api.changeColumn(note.noteId, name)));
}