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: change the way the column sorting options are reset
Adorian Dorand08b4c31aac9
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 91304ad064..edae7ef7d4 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3521,7 +3521,8 @@"sort-manually": "Manually","sort-cards": "Sort cards","sort-board-default": "Board's default",- "apply-sort-to-columns": "Apply to all columns",+ "sort-actions": "More actions",+ "reset-columns-to-default": "Reset all columns to default","sorted-no-reorder": "Cards cannot be reordered, since this is a sorted column."},"dashboard_view": {diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 8e85922ea7..a44aac5fdd 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -1953,47 +1953,26 @@ describe("the order the board offers its columns", () => {expect(setBoolean).toHaveBeenCalledWith(board, "sortColumnsDescending", true);});- it("hands the order to every column at once, keeping what else each one holds", async () => {+ it("puts every column back to the board's order, keeping what else each one holds", async () => {const { api, saved } = createApi({columns: [- { value: "To Do", icon: "bx bx-list-ul", orderBy: "creationDate" },- { value: "Done", descendingOrder: true }+ { value: "To Do", icon: "bx bx-list-ul", orderBy: "manual" },+ { value: "Done", orderBy: "title", descendingOrder: true }]},- [ "To Do", "Doing", "Done" ],- buildNote({- title: "Board", "#sortColumns": "title", "#sortColumnsDescending": ""- }));+ [ "To Do", "Doing", "Done" ]);- await api.applyDefaultSortToColumns();+ await api.resetColumnSortsToDefault();- // One write for the board, and the column with no entry of its own gains one where it is- // drawn rather than at the end.+ // One write for the board. Strict, so the assertion catches a key stored as undefined+ // rather than dropped, and the column with no entry is left without one.expect(saved.length).toBe(1);- expect(saved.at(-1)?.columns).toEqual([- { value: "To Do", icon: "bx bx-list-ul", orderBy: "title", descendingOrder: true },- { value: "Doing", orderBy: "title", descendingOrder: true },- { value: "Done", orderBy: "title", descendingOrder: true }+ expect(saved.at(-1)?.columns).toStrictEqual([+ { value: "To Do", icon: "bx bx-list-ul" },+ { value: "Done" }]);});-- it("puts every column back to the manual order where that is what the board holds", async () => {- const { api, saved } = createApi(- {- columns: [- { value: "To Do", orderBy: "title" },- { value: "Done", orderBy: "title", descendingOrder: true }- ]- },- [ "To Do", "Done" ]);-- await api.applyDefaultSortToColumns();-- // Strict, so the assertion catches the keys being stored as undefined rather than dropped.- expect(saved.at(-1)?.columns)- .toStrictEqual([ { value: "To Do" }, { value: "Done" } ]);- });});/**diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex 28b6bee7d5..6ff65d21d7 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -689,14 +689,16 @@ export default class BoardApi {}/**- * Gives every column the order the board holds, replacing whatever each column sorted by.+ * Puts every column back to the board's order, dropping the one each column picked for itself.*- * Written in one go: `updateColumn` rewrites the whole config, so a run of them would each- * report the board as it stood before the first.+ * Only the columns the config holds an entry for are written: one with no entry already takes+ * the board's order. Written in one go, since `updateColumn` rewrites the whole config and a+ * run of them would each start from it as it stood before the first.*/- async applyDefaultSortToColumns() {- const { orderBy, isDescending } = this.getDefaultSort();- this.updateColumns(this.columns, { orderBy, descendingOrder: isDescending });+ async resetColumnSortsToDefault() {+ const stored = this.viewConfig?.columns ?? [];+ this.updateColumns(+ stored.map(({ value }) => value), { orderBy: undefined, descendingOrder: false });}/** Whether the inbox also collects notes deeper than the board's direct children. */diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 08c823d41a..0bd74b2da4 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -254,6 +254,27 @@ export function openColumnSortMenu(api: Api, x: number, y: number, column: strin});}+/**+ * What can be done to the orders the columns hold, offered beside the board's own order.+ *+ * Opened at the pointer from the button in the properties card, as the template entries there open+ * theirs.+ */+export function openSortActionsMenu(api: Api, event: { pageX: number, pageY: number }) {+ contextMenu.show({+ x: event.pageX,+ y: event.pageY,+ items: [+ {+ title: t("board_view.reset-columns-to-default"),+ uiIcon: "bx bx-reset",+ handler: () => api.resetColumnSortsToDefault()+ }+ ],+ selectMenuItemHandler() {}+ });+}+/** What the board asks the shared sort menu for, wherever it is opened. */function sortMenuOptions(api: Api, column: string): SortMenuOptions {return {diff --git a/apps/client/src/widgets/collections/board/properties.spec.tsx b/apps/client/src/widgets/collections/board/properties.spec.tsxindex 09f8ad8ffc..ed6225ad40 100644--- a/apps/client/src/widgets/collections/board/properties.spec.tsx+++ b/apps/client/src/widgets/collections/board/properties.spec.tsx@@ -4,6 +4,7 @@ import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import type FNote from "../../../entities/fnote";+import contextMenu, { type MenuCommandItem } from "../../../menus/context_menu";import type { PromotedAttribute } from "../promoted_attributes";import type { SortKey } from "../sorting";import BoardApi from "./api";@@ -101,7 +102,7 @@ describe("Board properties", () => {setDefaultSortDirection: async (isDescending: boolean) => {sorting.push(`descending:${isDescending}`);},- applyDefaultSortToColumns: async () => { sorting.push("apply"); }+ resetColumnSortsToDefault: async () => { sorting.push("reset"); }} as unknown as BoardApi;draw = () => act(() => {@@ -186,19 +187,34 @@ describe("Board properties", () => {expect(picker()?.querySelector(".bx-sort-down")).toBeTruthy();});- it("hands the order to every column when asked to", () => {- act(() => { applyButton()?.click(); });+ it("offers a menu beside the picker for putting the columns back to it", () => {+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});+ // The menu closes itself on any click reaching the document, so the press that opens+ // it must not get there.+ const onDocument = vi.fn();+ document.addEventListener("click", onDocument);- expect(sorting).toEqual([ "apply" ]);+ act(() => { actionsButton()?.click(); });+ document.removeEventListener("click", onDocument);+ expect(onDocument).not.toHaveBeenCalled();++ const items = show.mock.calls.at(-1)?.[0].items ?? [];+ expect(items.map(item => item && "title" in item ? item.title : ""))+ .toEqual([ "board_view.reset-columns-to-default" ]);++ const entry = items[0];+ if (!entry || !("handler" in entry)) throw new Error("expected a menu entry");+ act(() => { entry.handler?.(entry as MenuCommandItem<unknown>, {} as never); });++ expect(sorting).toEqual([ "reset" ]);});function picker() {return general()?.querySelector<HTMLElement>(".board-sort-picker");}- function applyButton() {- return [ ...general()?.querySelectorAll<HTMLElement>("button") ?? [] ]- .find(button => button.textContent?.includes("board_view.apply-sort-to-columns"));+ function actionsButton() {+ return general()?.querySelector<HTMLElement>(".board-sort-actions");}});diff --git a/apps/client/src/widgets/collections/board/properties.tsx b/apps/client/src/widgets/collections/board/properties.tsxindex b19504442a..633c1afd8f 100644--- a/apps/client/src/widgets/collections/board/properties.tsx+++ b/apps/client/src/widgets/collections/board/properties.tsx@@ -4,7 +4,7 @@ import { useCallback } from "preact/hooks";import type FNote from "../../../entities/fnote";import { t } from "../../../services/i18n";-import Button from "../../react/Button";+import ActionButton from "../../react/ActionButton";import { Card, OptionCardSection } from "../../react/Card";import FormToggle from "../../react/FormToggle";import { useNoteLabel, useNoteLabelBoolean } from "../../react/hooks";@@ -15,6 +15,7 @@ import type { PromotedAttribute } from "../promoted_attributes";import SortDropdown from "../SortDropdown";import { parseSortKey } from "../sorting";import BoardApi from "./api";+import { openSortActionsMenu } from "./context_menu";/** The board's settings, other than its columns and cards. */export default function BoardProperties({ api, note, shown, onClose }: {@@ -109,9 +110,16 @@ function General({ api, note }: { api: BoardApi, note: FNote }) {onDirectionChange={(descending) => api.setDefaultSortDirection(descending)}/>- <Button- text={t("board_view.apply-sort-to-columns")}- onClick={() => api.applyDefaultSortToColumns()}+ <ActionButton+ className="board-sort-actions"+ icon="bx bx-dots-vertical-rounded"+ text={t("board_view.sort-actions")}+ onClick={(event) => {+ // The press would otherwise reach the document, where the menu closes+ // itself on any click outside it.+ event.stopPropagation();+ openSortActionsMenu(api, event);+ }}/></OptionCardSection></Card>