Skip to content

Build your personal knowledge base with Trilium Notes

client: refactor

Adorian Doran5b4ba3fda10c
diff --git a/apps/client/src/menus/context_menu_utils.ts b/apps/client/src/menus/context_menu_utils.ts
index 9ac6f8b6ae..70e6fcc4d8 100644
--- a/apps/client/src/menus/context_menu_utils.ts
+++ b/apps/client/src/menus/context_menu_utils.ts
@@ -1,6 +1,17 @@
import { t } from "../services/i18n"
import attributes from "../services/attributes"
import FNote from "../entities/fnote"
+import { escapeHtml } from "../services/utils"
+
+/**
+ * A name the user wrote, as a menu title.
+ *
+ * A menu reads a title as markup, so the name is escaped into the text it is meant to be, and boxed
+ * so that `style.css` clips a long one rather than letting it widen the menu.
+ */
+export function menuName(name: string) {
+ return `<span class="tn-menu-name">${escapeHtml(name)}</span>`
+}
export function getArchiveMenuItem(note: FNote) {
if (!note.isArchived) {
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index dcaa11c991..feeaf2cabf 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -1702,6 +1702,15 @@ body.mobile .dropdown-submenu > .dropdown-menu {
-webkit-user-select: none;
}
+/* A name the user wrote, boxed by `menuName()`: clipped rather than widening the menu. */
+.dropdown-item .tn-menu-name {
+ display: block;
+ max-width: 300px;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
body.desktop li.dropdown-submenu:hover > ul.dropdown-menu {
display: block;
}
diff --git a/apps/client/src/widgets/collections/attribute_menu.css b/apps/client/src/widgets/collections/attribute_menu.css
deleted file mode 100644
index cbca1c6cb4..0000000000
--- a/apps/client/src/widgets/collections/attribute_menu.css
+++ /dev/null
@@ -1,10 +0,0 @@
-.dropdown-item.attribute-menu-item {
- /* Names come from the user, so a long one is clipped instead of widening the menu. */
- .attribute-menu-name {
- display: block;
- max-width: 300px;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
-}
diff --git a/apps/client/src/widgets/collections/attribute_menu.spec.ts b/apps/client/src/widgets/collections/attribute_menu.spec.ts
index e97496d2c7..f4b215a19c 100644
--- a/apps/client/src/widgets/collections/attribute_menu.spec.ts
+++ b/apps/client/src/widgets/collections/attribute_menu.spec.ts
@@ -32,12 +32,10 @@ describe("buildAttributeMenuItems", () => {
expect(items[0]).toEqual({ kind: "header", title: "attribute_menu.attributes" });
expect(titles(items).slice(1)).toEqual([
- `<span class="attribute-menu-name">Done</span>`,
- `<span class="attribute-menu-name">State</span>`
+ `<span class="tn-menu-name">Done</span>`,
+ `<span class="tn-menu-name">State</span>`
]);
- expect(items[1]).toMatchObject({
- uiIcon: "bx bx-toggle-left", className: "attribute-menu-item"
- });
+ expect(items[1]).toMatchObject({ uiIcon: "bx bx-toggle-left" });
expect(items[2]).toMatchObject({ uiIcon: "bx bx-list-ul" });
});
@@ -111,10 +109,10 @@ describe("buildAttributeMenuItems", () => {
const items = options(buildNote({ title: "Card" }));
expect(titles(items)).toEqual([
- `<span class="attribute-menu-name">attribute_menu.not-set</span>`,
- `<span class="attribute-menu-name">To Do</span>`,
- `<span class="attribute-menu-name">Doing</span>`,
- `<span class="attribute-menu-name">Done</span>`
+ `<span class="tn-menu-name">attribute_menu.not-set</span>`,
+ `<span class="tn-menu-name">To Do</span>`,
+ `<span class="tn-menu-name">Doing</span>`,
+ `<span class="tn-menu-name">Done</span>`
]);
expect(items.every(item => !("uiIcon" in item) || !item.uiIcon)).toBe(true);
});
@@ -122,12 +120,12 @@ describe("buildAttributeMenuItems", () => {
it("marks the option the item holds", () => {
const items = options(buildNote({ title: "Card", "#state": "Doing" }));
- expect(marked(items)).toEqual([ `<span class="attribute-menu-name">Doing</span>` ]);
+ expect(marked(items)).toEqual([ `<span class="tn-menu-name">Doing</span>` ]);
});
it("marks Not set while the item holds none", () => {
expect(marked(options(buildNote({ title: "Card" }))))
- .toEqual([ `<span class="attribute-menu-name">attribute_menu.not-set</span>` ]);
+ .toEqual([ `<span class="tn-menu-name">attribute_menu.not-set</span>` ]);
});
it("marks nothing for a value the definition no longer offers", () => {
@@ -154,7 +152,7 @@ describe("buildAttributeMenuItems", () => {
const entry = items[1];
if (!entry || !("items" in entry)) throw new Error("expected a submenu");
- const escaped = "<span class=\"attribute-menu-name\">"
+ const escaped = "<span class=\"tn-menu-name\">"
+ "&lt;img src&#x3D;x onerror&#x3D;&quot;alert(1)&quot;&gt;</span>";
expect(titles(items)[1]).toBe(escaped);
expect(titles(entry.items ?? [])[1]).toBe(escaped);
diff --git a/apps/client/src/widgets/collections/attribute_menu.ts b/apps/client/src/widgets/collections/attribute_menu.ts
index c40ac6ca02..e0ea8a3d11 100644
--- a/apps/client/src/widgets/collections/attribute_menu.ts
+++ b/apps/client/src/widgets/collections/attribute_menu.ts
@@ -1,10 +1,8 @@
-import "./attribute_menu.css";
-
import type FNote from "../../entities/fnote";
import type { MenuCommandItem, MenuItem } from "../../menus/context_menu";
+import { menuName } from "../../menus/context_menu_utils";
import { setLabelValues } from "../../services/attributes";
import { t } from "../../services/i18n";
-import { escapeHtml } from "../../services/utils";
import { promotedAttributeIcon } from "../attribute_widgets/attribute_types";
import type { PromotedAttribute } from "./promoted_attributes";
@@ -98,14 +96,12 @@ function buildSelectItem<T>(
{
// Boxed like the options: the menu lays a row out as a flex line, so a bare title
// keeps the space that separates it from the icon and reads as indented.
- title: boxed(t("attribute_menu.not-set")),
- className: "attribute-menu-item",
+ title: menuName(t("attribute_menu.not-set")),
trailingIcon: current ? undefined : CHECK,
handler: () => { void setLabelValues(note, attribute.name, []); }
},
...options.map<MenuItem<T>>((option) => ({
- title: boxed(option),
- className: "attribute-menu-item",
+ title: menuName(option),
trailingIcon: option === current ? CHECK : undefined,
handler: () => { void setLabelValues(note, attribute.name, [ option ]); }
}))
@@ -116,16 +112,7 @@ function buildSelectItem<T>(
/** What every entry carries: the attribute's display name, under the icon of its type. */
function attributeEntry<T>(attribute: PromotedAttribute): MenuCommandItem<T> {
return {
- title: boxed(attribute.title),
- className: "attribute-menu-item",
+ title: menuName(attribute.title),
uiIcon: promotedAttributeIcon(attribute)
};
}
-
-/**
- * Boxes a name the user wrote. The menu reads a title as markup, so the name is escaped, and
- * `attribute_menu.css` clips the box rather than letting a long name widen the menu.
- */
-function boxed(name: string) {
- return `<span class="attribute-menu-name">${escapeHtml(name)}</span>`;
-}
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 6ff65d21d7..3249c0d659 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -28,14 +28,11 @@ import {
DEFAULT_GROUP_BY, INBOX_COLUMN, INBOX_COLUMN_ICON
} from "./columns";
import { ColumnMap } from "./data";
+import { SORT_DESCENDING_LABEL, SORT_LABEL } from "./sort";
/** Which end of a column a new card is made at. */
export type CardPlacement = "top" | "bottom";
-/** The labels the board keeps the order it offers its columns in. */
-const SORT_LABEL = "sortColumns";
-const SORT_DESCENDING_LABEL = "sortColumnsDescending";
-
/** One write's claim on a column, held until that write lands or is taken back. */
interface ColumnClaim {
/**
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 2f6a04ac8e..e8c51c3206 100644
--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts
@@ -310,8 +310,7 @@ describe("Board column context menu", () => {
// the box around it is what this is about.
for (const item of after) {
expect(item && "title" in item ? item.title : "")
- .toMatch(/^<span class="board-column-name">.*<\/span>$/);
- expect(item).toMatchObject({ className: "board-column-item" });
+ .toMatch(/^<span class="tn-menu-name">.*<\/span>$/);
}
});
@@ -724,7 +723,7 @@ describe("Board item context menu", () => {
/** A name as the menu writes it, which is what `names` reads back. */
function boxed(name: string) {
- return `<span class="board-column-name">${name}</span>`;
+ return `<span class="tn-menu-name">${name}</span>`;
}
/** Opens the menu a card offers, and hands back what it was given to show. */
@@ -786,8 +785,8 @@ describe("Board item context menu", () => {
&& item.title === "attribute_menu.attributes");
expect(items[at + 1]).toMatchObject({ uiIcon: "bx bx-toggle-left" });
- expect(items.slice(0, at).some(item => item && "className" in item
- && item.className?.includes("board-column-item"))).toBe(true);
+ expect(items.slice(0, at).some(item => item && "title" in item
+ && typeof item.title === "string" && item.title.includes("tn-menu-name"))).toBe(true);
});
/** Reads the run of column entries the menu puts under its Status header. */
@@ -818,15 +817,15 @@ describe("Board item context menu", () => {
// Each name sits in a box of its own, which is what the stylesheet sizes.
expect(columns.map(item => item && "title" in item ? item.title : undefined)).toEqual([
- '<span class="board-column-name">To Do</span>',
- '<span class="board-column-name">Done</span>'
+ '<span class="tn-menu-name">To Do</span>',
+ '<span class="tn-menu-name">Done</span>'
]);
// The tick goes at the trailing edge, leaving each column's own icon where it stands.
expect(columns.map(item => item && "trailingIcon" in item ? item.trailingIcon : undefined))
.toEqual([ "bx bx-check", undefined ]);
- // And carries the class the stylesheet weights it by.
+ // And the one the card is under carries the class the stylesheet weights it by.
expect(columns.map(item => item && "className" in item ? item.className : undefined))
- .toEqual([ "board-column-item board-current-column", "board-column-item" ]);
+ .toEqual([ "board-current-column", undefined ]);
const done = columns[1];
if (done && "handler" in done) done.handler?.(done, {} as never);
@@ -869,11 +868,12 @@ describe("Board item context menu", () => {
// The name sits in a box of its own, which is what the width is set on, and nothing of the
// name itself is left as markup.
- expect(title).toBe('<span class="board-column-name">'
+ expect(title).toBe('<span class="tn-menu-name">'
+ "Done &lt;button id&#x3D;&quot;planted&quot;&gt;press&lt;&#x2F;button&gt;</span>");
});
- it("names every column entry for the stylesheet to size", () => {
+ /** The one the card is under is set apart, which is all the entries are classed for. */
+ it("marks the column the card is under", () => {
const api = {
columns: [ "To Do", "Done" ],
isColumnArchived: () => false,
@@ -883,7 +883,7 @@ describe("Board item context menu", () => {
expect(statusItems(api)
.map(item => item && "className" in item ? item.className : undefined))
- .toEqual([ "board-column-item board-current-column", "board-column-item" ]);
+ .toEqual([ "board-current-column", undefined ]);
});
it("shows each column with the icon and colour it carries", () => {
diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts
index 0bd74b2da4..db62ef7bb3 100644
--- a/apps/client/src/widgets/collections/board/context_menu.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.ts
@@ -7,9 +7,8 @@ import contextMenu, { ContextMenuEvent, MenuItem } from "../../../menus/context_
import link_context_menu from "../../../menus/link_context_menu";
import branches from "../../../services/branches";
import dialog from "../../../services/dialog";
-import { getArchiveMenuItem } from "../../../menus/context_menu_utils";
+import { getArchiveMenuItem, menuName } from "../../../menus/context_menu_utils";
import { t } from "../../../services/i18n";
-import { escapeHtml } from "../../../services/utils";
import ColorPicker from "../../react/ColorPicker";
import { buildAttributeMenuItems } from "../attribute_menu";
import { buildSortMenuItems, type SortMenuOptions } from "../sort_menu";
@@ -366,11 +365,9 @@ function buildMoveColumnItems(api: Api, column: ColumnMenuTarget): MenuItem<stri
const title = api.getColumnTitle(name);
return [ {
- // Boxed as the status list boxes its names, so a long one is clipped rather than
- // widening the menu. `t()` escapes what it interpolates.
- title: `<span class="board-column-name">`
+ // `t()` escapes what it interpolates, so the sentence it builds is boxed as it stands.
+ title: `<span class="tn-menu-name">`
+ `${t("board_view.move-column-after", { column: title })}</span>`,
- className: "board-column-item",
uiIcon: api.getColumnIcon(name),
iconColorClass: api.getColumnColorClass(name),
badges: api.isColumnArchived(name)
@@ -416,18 +413,13 @@ function buildColumnItems(
api: Api, note: FNote, column: string, onFocusCard: (noteId: string) => void
): MenuItem<CommandNames>[] {
const items: MenuItem<CommandNames>[] = api.columns.map((name) => ({
- // The menu reads a title as markup, which is what puts the name in a box of its own: a
- // bare run of text inside the item's flex row is an anonymous box, and nothing can be said
- // about its width. What a crafted name would plant there is escaped into the text it is
- // meant to be; every other title the board builds from a name goes through `t()`, which
- // escapes what it interpolates.
- title: `<span class="board-column-name">${escapeHtml(api.getColumnTitle(name))}</span>`,
+ title: menuName(api.getColumnTitle(name)),
uiIcon: api.getColumnIcon(name),
iconColorClass: api.getColumnColorClass(name),
// The one it is already under is shown rather than hidden, so the list reads as the whole
// set of columns and says which of them this card belongs to.
trailingIcon: name === column ? "bx bx-check" : undefined,
- className: name === column ? "board-column-item board-current-column" : "board-column-item",
+ className: name === column ? "board-current-column" : undefined,
badges: api.isColumnArchived(name)
? [ { title: t("board_view.archived-badge") } ]
: undefined,
diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.css
index 6373fd8d0c..1b617fe9cf 100644
--- a/apps/client/src/widgets/collections/board/index.css
+++ b/apps/client/src/widgets/collections/board/index.css
@@ -892,14 +892,6 @@ body.mobile .column-drop-placeholder {
font-weight: 600;
}
-.dropdown-item.board-column-item .board-column-name {
- display: block;
- max-width: 300px;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
-}
-
/* Outline around a column that is over its limit. */
.board-view-container .board-column.over-limit {
box-shadow: 0 0 0 2px var(--board-limit-exceeded-outline);
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 7ba6e73951..e36c0b7fc5 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -23,13 +23,13 @@ import toast from "../../../services/toast";
import { escapeHtml, isMobile } from "../../../services/utils";
import { type NoteTypeOption, resolveNoteTypeOptions } from "../../../services/note_types";
import { type PromotedAttributeSetting, resolvePromotedAttributes } from "../promoted_attributes";
-import { parseSortKey, type SortContext } from "../sorting";
+import type { SortContext } from "../sorting";
import CollectionProperties from "../../note_bars/CollectionProperties";
import FormTextArea from "../../react/FormTextArea";
import FormTextBox from "../../react/FormTextBox";
import {
- useContextualShortcutHints, useNoteContext, useNoteLabel, useNoteLabelBoolean,
- useNoteLabelWithDefault, useNoteTypeOptions, useTrackedElement, useTriliumEvent
+ useContextualShortcutHints, useNoteContext, useNoteLabelBoolean, useNoteLabelWithDefault,
+ useNoteTypeOptions, useTrackedElement, useTriliumEvent
} from "../../react/hooks";
import Icon from "../../react/Icon";
import NoteAutocomplete from "../../react/NoteAutocomplete";
@@ -53,6 +53,7 @@ import { currentCardTemplate, DEFAULT_CARD_TEMPLATES } from "./card_templates";
import ColumnLimitDialog from "./column_limit";
import BoardProperties from "./properties";
import { openBoardContextMenu, openCreateColumnMenu } from "./context_menu";
+import { useBoardSort } from "./sort";
import {
affectsSortOrder, applyCardMove, ColumnMap, filterColumnMap, getBoardData, resolveColumnSorts,
resolveSortWatch, sortColumnMap, unfilteredCardIndex
@@ -290,8 +291,6 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
const [ statusAttributeWithPrefix ] = useNoteLabelWithDefault(parentNote, "board:groupBy", DEFAULT_GROUP_BY);
const [ includeArchived ] = useNoteLabelBoolean(parentNote, "includeArchived");
const [ inboxEnabled ] = useNoteLabelBoolean(parentNote, "enableInboxColumn");
- const [ storedDefaultSort ] = useNoteLabel(parentNote, "sortColumns");
- const [ isDefaultDescending ] = useNoteLabelBoolean(parentNote, "sortColumnsDescending");
/** Every card the board holds, which an active filter narrows before the cards are drawn. */
const [ allByColumn, setAllByColumn ] = useState<ColumnMap>();
const [ columns, setColumns ] = useState<string[]>();
@@ -397,12 +396,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
() => resolvePromotedAttributes(
parentNote, viewConfig?.promotedAttributes, [ statusAttribute ]),
[ parentNote, viewConfig, statusAttribute, definitionRevision ]);
- // Held rather than read per column: every column taking the board's order asks for it, and the
- // labels change only when the reader picks another one.
- const defaultSort = useMemo(() => {
- const orderBy = parseSortKey(storedDefaultSort);
- return orderBy ? { orderBy, isDescending: isDefaultDescending } : undefined;
- }, [ storedDefaultSort, isDefaultDescending ]);
+ const defaultSort = useBoardSort(parentNote);
const columnSorts = useMemo(
() => resolveColumnSorts(viewConfig?.columns, defaultSort, usableColumns),
[ viewConfig, defaultSort, usableColumns ]);
diff --git a/apps/client/src/widgets/collections/board/properties.tsx b/apps/client/src/widgets/collections/board/properties.tsx
index 633c1afd8f..7576718c39 100644
--- a/apps/client/src/widgets/collections/board/properties.tsx
+++ b/apps/client/src/widgets/collections/board/properties.tsx
@@ -7,7 +7,7 @@ import { t } from "../../../services/i18n";
import ActionButton from "../../react/ActionButton";
import { Card, OptionCardSection } from "../../react/Card";
import FormToggle from "../../react/FormToggle";
-import { useNoteLabel, useNoteLabelBoolean } from "../../react/hooks";
+import { useNoteLabelBoolean } from "../../react/hooks";
import Modal from "../../react/Modal";
import PromotedAttributesCard from "../../react/PromotedAttributesCard";
import TemplateSelectionCard from "../../react/TemplateSelectionCard";
@@ -16,6 +16,7 @@ import SortDropdown from "../SortDropdown";
import { parseSortKey } from "../sorting";
import BoardApi from "./api";
import { openSortActionsMenu } from "./context_menu";
+import { useBoardSort } from "./sort";
/** The board's settings, other than its columns and cards. */
export default function BoardProperties({ api, note, shown, onClose }: {
@@ -67,8 +68,7 @@ function General({ api, note }: { api: BoardApi, note: FNote }) {
const [ inboxShown ] = useNoteLabelBoolean(note, "enableInboxColumn");
const [ archivedShown ] = useNoteLabelBoolean(note, "includeArchived");
// Read off the board's own labels, so the dropdown follows what is picked in it.
- const [ storedSort ] = useNoteLabel(note, "sortColumns");
- const [ isDescending ] = useNoteLabelBoolean(note, "sortColumnsDescending");
+ const defaultSort = useBoardSort(note);
return (
<Card className="board-properties-general" heading={t("board_view.general")}>
@@ -99,8 +99,8 @@ function General({ api, note }: { api: BoardApi, note: FNote }) {
>
<SortDropdown
className="board-sort-picker"
- orderBy={parseSortKey(storedSort)}
- isDescending={isDescending}
+ orderBy={defaultSort?.orderBy}
+ isDescending={!!defaultSort?.isDescending}
attributes={api.getPromotedAttributes()}
noneTitle={t("board_view.sort-manually")}
hideDefault
diff --git a/apps/client/src/widgets/collections/board/sort.ts b/apps/client/src/widgets/collections/board/sort.ts
new file mode 100644
index 0000000000..02fa5cbecc
--- /dev/null
+++ b/apps/client/src/widgets/collections/board/sort.ts
@@ -0,0 +1,26 @@
+import { useMemo } from "preact/hooks";
+
+import type FNote from "../../../entities/fnote";
+import { useNoteLabel, useNoteLabelBoolean } from "../../react/hooks";
+import { parseSortKey } from "../sorting";
+import type { ColumnSort } from "./data";
+
+/** The labels the board keeps the order its columns take by default. */
+export const SORT_LABEL = "sortColumns";
+export const SORT_DESCENDING_LABEL = "sortColumnsDescending";
+
+/**
+ * The order the board holds, absent while it arranges its cards by hand.
+ *
+ * Kept in step with the labels rather than read per column: every column taking the board's order
+ * asks for it, and the labels change only when the reader picks another one.
+ */
+export function useBoardSort(note: FNote): ColumnSort | undefined {
+ const [ stored ] = useNoteLabel(note, SORT_LABEL);
+ const [ isDescending ] = useNoteLabelBoolean(note, SORT_DESCENDING_LABEL);
+
+ return useMemo(() => {
+ const orderBy = parseSortKey(stored);
+ return orderBy ? { orderBy, isDescending } : undefined;
+ }, [ stored, isDescending ]);
+}
diff --git a/apps/client/src/widgets/collections/sort_menu.css b/apps/client/src/widgets/collections/sort_menu.css
deleted file mode 100644
index 615e714f38..0000000000
--- a/apps/client/src/widgets/collections/sort_menu.css
+++ /dev/null
@@ -1,10 +0,0 @@
-.dropdown-item.sort-menu-item {
- /* A field is named by the user, so a long name is clipped instead of widening the menu. */
- .sort-menu-name {
- display: block;
- max-width: 300px;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
-}
diff --git a/apps/client/src/widgets/collections/sort_menu.spec.ts b/apps/client/src/widgets/collections/sort_menu.spec.ts
index 1977e127b4..a5efc4561f 100644
--- a/apps/client/src/widgets/collections/sort_menu.spec.ts
+++ b/apps/client/src/widgets/collections/sort_menu.spec.ts
@@ -75,12 +75,10 @@ describe("buildSortMenuItems", () => {
});
expect(titles(items).slice(4, 6)).toEqual([
- `<span class="sort-menu-name">Due date</span>`,
- `<span class="sort-menu-name">Owner</span>`
+ `<span class="tn-menu-name">Due date</span>`,
+ `<span class="tn-menu-name">Owner</span>`
]);
- expect(items[4]).toMatchObject({
- uiIcon: "bx bx-calendar", className: "sort-menu-item", trailingIcon: undefined
- });
+ expect(items[4]).toMatchObject({ uiIcon: "bx bx-calendar", trailingIcon: undefined });
expect(items[5]).toMatchObject({ uiIcon: "bx bx-transfer", trailingIcon: "bx bx-check" });
});
@@ -89,7 +87,7 @@ describe("buildSortMenuItems", () => {
attributes: [ attribute({ name: "x", title: `<img src=x onerror="alert(1)">` }) ]
});
- expect(titles(items)[4]).toBe("<span class=\"sort-menu-name\">"
+ expect(titles(items)[4]).toBe("<span class=\"tn-menu-name\">"
+ "&lt;img src&#x3D;x onerror&#x3D;&quot;alert(1)&quot;&gt;</span>");
});
diff --git a/apps/client/src/widgets/collections/sort_menu.ts b/apps/client/src/widgets/collections/sort_menu.ts
index bad53b3267..c235138c02 100644
--- a/apps/client/src/widgets/collections/sort_menu.ts
+++ b/apps/client/src/widgets/collections/sort_menu.ts
@@ -1,8 +1,6 @@
-import "./sort_menu.css";
-
import type { MenuItem } from "../../menus/context_menu";
+import { menuName } from "../../menus/context_menu_utils";
import { t } from "../../services/i18n";
-import { escapeHtml } from "../../services/utils";
import { promotedAttributeIcon } from "../attribute_widgets/attribute_types";
import type { PromotedAttribute } from "./promoted_attributes";
import {
@@ -150,12 +148,7 @@ export function buildSortMenuItems<T>(options: SortMenuOptions): MenuItem<T>[] {
/** One entry as a menu reads it: a title of markup, the mark at the trailing edge. */
function toMenuItem<T>(entry: SortEntry): MenuItem<T> {
return {
- // A name the user wrote is escaped into the text it is meant to be, and boxed so that
- // `sort_menu.css` clips a long one instead of widening the menu.
- title: entry.isUserNamed
- ? `<span class="sort-menu-name">${escapeHtml(entry.title)}</span>`
- : entry.title,
- className: entry.isUserNamed ? "sort-menu-item" : undefined,
+ title: entry.isUserNamed ? menuName(entry.title) : entry.title,
uiIcon: entry.icon,
enabled: entry.isEnabled,
trailingIcon: entry.isSelected ? "bx bx-check" : undefined,