Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/collections/board: add a sort button in the column header when the automatic sorting is enabled
Adorian Doran6bab387c27a6
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex ce5a4e1b11..0d9dadd716 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -44,7 +44,8 @@ import Card from "./card";import CardTemplatePill from "./card_template_pill";import { type CardTemplates } from "./card_templates";import { DEFAULT_CARD_ICON, DEFAULT_COLUMN_ICON, INBOX_COLUMN } from "./columns";-import { openColumnContextMenu, openCreateCardMenu } from "./context_menu";+import { openColumnContextMenu, openColumnSortMenu, openCreateCardMenu } from "./context_menu";+import type { ColumnSort } from "./data";import { cardSpacing } from "./drag_measure";import { BoardDropStateContext, useDropIndex, useIsDropTarget } from "./drop_state";@@ -78,7 +79,7 @@ export default function Column({totalCount,isNew,cardTemplates,- isSorted,+ sort,landedNoteId,api,parentNote,@@ -115,10 +116,11 @@ export default function Column({/** The note limit, absent if disabled. */limit?: number,/**- * Whether the column orders its own cards. Such a column places a dropped card itself, so it- * opens no gap for a carried one and offers nothing that would put a card in a chosen place.+ * How the column orders its own cards, absent while the reader arranges them. A sorted column+ * places a dropped card itself, so it opens no gap for a carried one and offers nothing that+ * would put a card in a chosen place.*/- isSorted?: boolean,+ sort?: ColumnSort,/** The card just dropped here, which is revealed where the sort put it. */landedNoteId?: string,/**@@ -136,6 +138,7 @@ export default function Column({onFocusColumn: (column: string) => void,onFocusCard: (noteId: string) => void} & DragContext) {+ const isSorted = !!sort;const [ isCreatingNewItem, setIsCreatingNewItem ] = useState(false);/*** The card a field standing among the cards makes its own above, absent while no such field is@@ -393,6 +396,25 @@ export default function Column({setActiveColumn(isPeeked ? column : undefined);}, [ column, isActive, isPeeked, setActiveColumn ]);+ // Shown only while the column sorts itself, so its absence is what says the order is the+ // reader's own. The arrow says which way the order runs.+ const sortButton = sort && (+ <ActionButton+ className="column-sort"+ icon={sort.isDescending ? "bx bx-sort-down" : "bx bx-sort-up"}+ text={t("board_view.sort")}+ // A strip has room to say which way the order runs but not to be worked in: the menu+ // it would open stands where the column is about to widen.+ disabled={isCollapsed}+ onClick={(e) => {+ // The heading is the column's drag handle and opens its own menu on a right+ // click; neither should also fire from the button.+ e.stopPropagation();+ openColumnSortMenu(api, e.pageX, e.pageY, column);+ }}+ />+ );+const openMenu = useCallback((e: ContextMenuEvent) => {openColumnContextMenu(api, e, {value: column,@@ -573,6 +595,7 @@ export default function Column({}}/><CountBadge items={columnItems} limit={limit} isOver={isOverLimit} />+ {sortButton}<span className="title">{isInRelationMode? <NoteLink notePath={column} />@@ -604,6 +627,7 @@ export default function Column({: api.getColumnTitle(column)}</span><div className="spacer" />+ {sortButton}<CountBadge items={columnItems} limit={limit} isOver={isOverLimit} /><ActionButtonclassName="column-menu"diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 88b0876647..918b724750 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -11,7 +11,7 @@ import { getArchiveMenuItem } from "../../../menus/context_menu_utils";import { t } from "../../../services/i18n";import { escapeHtml } from "../../../services/utils";import ColorPicker from "../../react/ColorPicker";-import { buildSortMenuItems } from "../sort_menu";+import { buildSortMenuItems, type SortMenuOptions } from "../sort_menu";import Api from "./api";import { INBOX_COLUMN } from "./columns";@@ -130,15 +130,7 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:{title: t("board_view.sort"),uiIcon: "bx bx-sort-alt-2",- items: buildSortMenuItems<string>({- ...api.getColumnSort(column.value),- attributes: api.getPromotedAttributes(),- // A board arranges its cards by hand rather than leaving them unsorted.- noneTitle: t("board_view.sort-manually"),- onSelect: (orderBy) => api.setColumnSort(column.value, orderBy),- onDirectionChange: (isDescending) =>- api.setColumnSortDirection(column.value, isDescending)- })+ items: buildSortMenuItems<string>(sortMenuOptions(api, column.value))},{title: t("board_view.set-limit"),@@ -245,6 +237,34 @@ export function openBoardContextMenu(event: ContextMenuEvent, board: BoardMenuTa});}+/**+ * The sort menu on its own, for the button a sorted column shows in its heading.+ *+ * Opened leftwards, since the button sits at the trailing edge of a column that can stand against+ * the window edge.+ */+export function openColumnSortMenu(api: Api, x: number, y: number, column: string) {+ contextMenu.show({+ x,+ y,+ orientation: "left",+ items: buildSortMenuItems<string>(sortMenuOptions(api, column)),+ selectMenuItemHandler() {}+ });+}++/** What the board asks the shared sort menu for, wherever it is opened. */+function sortMenuOptions(api: Api, column: string): SortMenuOptions {+ return {+ ...api.getColumnSort(column),+ attributes: api.getPromotedAttributes(),+ // A board arranges its cards by hand rather than leaving them unsorted.+ noneTitle: t("board_view.sort-manually"),+ onSelect: (orderBy) => api.setColumnSort(column, orderBy),+ onDirectionChange: (isDescending) => api.setColumnSortDirection(column, isDescending)+ };+}+/** Offers both ends of a column for the card its button is about to create. */export function openCreateCardMenu(x: number, y: number, create: (atStart: boolean) => void) {contextMenu.show({diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex fa16ffb86f..e1411d00ea 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -243,7 +243,8 @@ body.mobile .board-view-container .board-add-column {cursor: pointer;}-.board-view-container .board-column h3 > .column-menu {+.board-view-container .board-column h3 > .column-menu,+.board-view-container .board-column h3 > .column-sort {flex-shrink: 0;margin-inline-start: 0.25em;color: var(--muted-text-color);@@ -1039,6 +1040,7 @@ body.mobile .column-drop-placeholder {/* The header stacks, so margins meant for a row would throw these off centre. */.board-view-container .board-column.collapsed h3 > .column-icon,.board-view-container .board-column.collapsed h3 > .column-menu,+.board-view-container .board-column.collapsed h3 > .column-sort,.board-view-container .board-column.collapsed h3 > .counter-badge {margin: 0;}@@ -1051,6 +1053,12 @@ body.mobile .column-drop-placeholder {transform: rotate(180deg);}+/* The turn `writing-mode` gives the title and the count above, which an icon glyph in a button+ does not take from it. */+.board-view-container .board-column.collapsed h3 > .column-sort {+ transform: rotate(-90deg);+}+/* Swapped: `padding` is physical, so it does not turn with the count. */.board-view-container .board-column.collapsed h3 > .counter-badge {padding: 0.6em 0.1em;diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 054803f589..a76cad4781 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -3593,6 +3593,55 @@ describe("a column that sorts its cards", () => {expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);});+ it("shows no sort button while the column is arranged by hand", async () => {+ const { board } = await renderSortedBoard({});++ expect(board.querySelector(".board-column h3 .column-sort")).toBeNull();+ });++ it("shows the way the order runs, and turns the arrow over for a descending one", async () => {+ const { board } = await renderSortedBoard({ orderBy: "title" });++ // `ActionButton` puts the icon on the button itself.+ expect(sortButton(board)?.classList.contains("bx-sort-up")).toBe(true);+ // Only the sorted column carries one.+ expect(board.querySelectorAll(".board-column h3 .column-sort")).toHaveLength(1);++ const descending = await renderSortedBoard({ orderBy: "title", descendingOrder: true });+ expect(sortButton(descending.board)?.classList.contains("bx-sort-down")).toBe(true);+ });++ it("opens the sort menu, and takes the column away once it is arranged by hand", async () => {+ const { board } = await renderSortedBoard({ orderBy: "title" });+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});++ sortButton(board)?.click();+ const items = show.mock.calls.at(-1)?.[0].items ?? [];+ expect(items.map(item => (item && "uiIcon" in item ? item.uiIcon : "separator")))+ .toEqual([+ "bx bx-move-vertical", "bx bx-text", "bx bx-calendar-plus", "separator",+ "bx bx-sort-up", "bx bx-sort-down"+ ]);+ show.mockRestore();++ // Picking the manual order is what takes the button away.+ await pickSort(board, "bx bx-move-vertical");+ expect(board.querySelector(".board-column h3 .column-sort")).toBeNull();+ });++ /** A strip is too narrow to work in, and the menu would stand where it is about to widen. */+ it("shows the button on a strip, with nothing to be done to it", async () => {+ const { board } = await renderSortedBoard({ orderBy: "title", collapsed: true });++ expect(board.querySelector(".board-column.collapsed")).toBeTruthy();+ expect(sortButton(board)?.hasAttribute("disabled")).toBe(true);+ });++ /** The button the first column shows, which is the only sorted one in these boards. */+ function sortButton(board: HTMLElement) {+ return board.querySelector<HTMLElement>(".board-column h3 .column-sort");+ }+/** Opens the first column's menu and picks the sort entry carrying the given icon. */async function pickSort(board: HTMLElement, icon: string) {const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});@@ -3632,7 +3681,7 @@ describe("a column that sorts its cards", () => {}async function renderSortedBoard(- sort: { orderBy?: string, descendingOrder?: boolean },+ sort: { orderBy?: string, descendingOrder?: boolean, collapsed?: boolean },promoted?: { "#label:priority(inheritable)": string, values: Record<string, string> }) {const priorities = promoted?.values ?? {};diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex d4cef3a7bd..24017d8b43 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -1020,7 +1020,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveConFocusCard={focusCard}columnItems={byColumn.get(column)}totalCount={allByColumn?.get(column)?.length}- isSorted={columnSorts.has(column)}+ sort={columnSorts.get(column)}landedNoteId={landedNoteId}isNew={column === createdColumn}/>