Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): draw the editor's icon picker smaller than the note icon's

Elian Doran9500d0056c42
diff --git a/apps/client/src/widgets/react/IconPicker.css b/apps/client/src/widgets/react/IconPicker.css
index a2efd839bd..432206c87d 100644
--- a/apps/client/src/widgets/react/IconPicker.css
+++ b/apps/client/src/widgets/react/IconPicker.css
@@ -38,6 +38,23 @@
border: 1px solid var(--main-border-color);
}
+.icon-picker.compact .filter-row {
+ padding: 6px;
+ gap: 0.5em;
+ font-size: 0.85em;
+}
+
+.icon-picker.compact .filter-row .form-control {
+ padding: 3px 8px;
+ font-size: inherit;
+}
+
+/* The glyph, its padding and its border have to stay inside `COMPACT_ICON_SIZE` in IconPicker.tsx. */
+.icon-picker.compact .icon-list span {
+ padding: 6px;
+ font-size: 24px;
+}
+
.icon-picker .icon-list span.bx-empty {
width: unset;
}
diff --git a/apps/client/src/widgets/react/IconPicker.spec.tsx b/apps/client/src/widgets/react/IconPicker.spec.tsx
index e1153b2e99..6d8a23f938 100644
--- a/apps/client/src/widgets/react/IconPicker.spec.tsx
+++ b/apps/client/src/widgets/react/IconPicker.spec.tsx
@@ -15,8 +15,16 @@ vi.mock("../../services/utils", async (importOriginal) => ({
const openDialogMock = vi.hoisted(() => vi.fn(async (dialog: JQuery<HTMLElement>, _closeActDialog?: boolean) => dialog));
vi.mock("../../services/dialog", () => ({ openDialog: openDialogMock }));
+// The grid asks the server which icons the tree already uses, to lead with those. Anything else
+// asked of the server here is a module loading beside the picker, and wants a list back.
+vi.mock("../../services/server", () => ({
+ default: {
+ get: async (url: string) => (url === "other/icon-usage" ? { iconClassToCountMap: {} } : [])
+ }
+}));
+
import { renderInto } from "../../test/render";
-import { IconPickerButton } from "./IconPicker";
+import IconPicker, { IconPickerButton } from "./IconPicker";
describe("IconPickerButton", () => {
/** Renders the button as the given device would see it. */
@@ -72,3 +80,26 @@ describe("IconPickerButton", () => {
expect(openDialogMock.mock.calls[0][1]).toBe(false);
});
});
+
+describe("IconPicker", () => {
+ it("draws the compact grid smaller than the one a note's icon is picked from", () => {
+ glob.iconRegistry = { sources: [] };
+
+ const regular = renderInto(<IconPicker columnCount={9} onSelect={() => {}} />);
+ const compact = renderInto(<IconPicker columnCount={9} compact onSelect={() => {}} />);
+
+ const regularList = regular.querySelector<HTMLElement>(".icon-picker:not(.compact) .icon-list");
+ const compactList = compact.querySelector<HTMLElement>(".icon-picker.compact .icon-list");
+ expect(regularList).toBeTruthy();
+ expect(compactList).toBeTruthy();
+
+ // Nine columns of the compact cell, and the ten the stylesheet asks for besides.
+ expect(compactList?.style.width).toBe("370px");
+ // Eight rows and a quarter, the quarter being what tells the reader there is more below.
+ expect(compactList?.style.height).toBe("330px");
+
+ // The regular grid keeps the height the stylesheet gives it, which follows the screen.
+ expect(regularList?.style.width).toBe("442px");
+ expect(regularList?.style.height).toBe("");
+ });
+});
diff --git a/apps/client/src/widgets/react/IconPicker.tsx b/apps/client/src/widgets/react/IconPicker.tsx
index f34c810950..f7ce7339fb 100644
--- a/apps/client/src/widgets/react/IconPicker.tsx
+++ b/apps/client/src/widgets/react/IconPicker.tsx
@@ -22,6 +22,15 @@ import Modal from "./Modal";
/** The room one icon takes in the grid, which also decides how many fit across a given width. */
export const ICON_SIZE = isMobile() ? 56 : 48;
+/** The same, for the compact picker, where the icons are drawn with far less room around them. */
+const COMPACT_ICON_SIZE = 40;
+
+/**
+ * How many rows of icons the compact picker shows. The quarter row is deliberate: a row cut off at
+ * the bottom edge is what tells the reader there is more to scroll to.
+ */
+const COMPACT_ROWS = 8.25;
+
// One tooltip on the grid, delegated to the icon tiles. A module constant so the grid re-rendering
// on every keystroke in the search does not tear the tooltip down and build it again each time.
const ICON_TOOLTIP_CONFIG: Partial<Tooltip.Options> = {
@@ -43,6 +52,11 @@ interface IconPickerProps {
resetText?: string;
/** How many icons stand side by side; the host decides from the room it can give the grid. */
columnCount: number;
+ /**
+ * Draw the grid smaller, for a host with less room to give than a menu of its own — the text
+ * editor's balloon, which stands over the note being written rather than beside it.
+ */
+ compact?: boolean;
}
/**
@@ -52,7 +66,8 @@ interface IconPickerProps {
* It only reports what was picked — what that means is the host's own business, whether it is the
* icon of a note or of something else entirely.
*/
-export default function IconPicker({ onSelect, onReset, resetText, columnCount }: IconPickerProps) {
+export default function IconPicker({ onSelect, onReset, resetText, columnCount, compact }: IconPickerProps) {
+ const iconSize = compact ? COMPACT_ICON_SIZE : ICON_SIZE;
const iconListRef = useRef<HTMLDivElement>(null);
const [ search, setSearch ] = useState<string>();
const [ filterByPrefix, setFilterByPrefix ] = useState<string | null>(null);
@@ -64,7 +79,7 @@ export default function IconPicker({ onSelect, onReset, resetText, columnCount }
return (
// The legacy class is kept alongside the picker's own: the themes dress the icon grid
// through it, having known the picker only as a part of the note icon widget.
- <div className="icon-picker note-icon-widget">
+ <div className={clsx("icon-picker", "note-icon-widget", compact && "compact")}>
<FilterRow
filterByPrefix={filterByPrefix}
search={search}
@@ -79,7 +94,10 @@ export default function IconPicker({ onSelect, onReset, resetText, columnCount }
class="icon-list"
ref={iconListRef}
style={{
- width: (columnCount * ICON_SIZE + 10),
+ width: (columnCount * iconSize + 10),
+ // The regular grid is as tall as the screen leaves room for (see the CSS); the
+ // compact one is cut to a set number of rows.
+ ...(compact && { height: COMPACT_ROWS * iconSize })
}}
onClick={(e) => {
// Make sure we are not clicking on something else than a button.
@@ -92,9 +110,9 @@ export default function IconPicker({ onSelect, onReset, resetText, columnCount }
{filteredIcons.length ? (
<Grid
columnCount={columnCount}
- columnWidth={ICON_SIZE}
+ columnWidth={iconSize}
rowCount={Math.ceil(filteredIcons.length / columnCount)}
- rowHeight={ICON_SIZE}
+ rowHeight={iconSize}
cellComponent={IconItemCell}
cellProps={{
filteredIcons,
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index 37926233a1..a6a7777d13 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -506,6 +506,7 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
{iconPickerRequest && createPortal(
<IconPicker
columnCount={ICON_PICKER_COLUMNS}
+ compact
onSelect={iconPickerRequest.onSelect}
/>,
iconPickerRequest.container
@@ -515,7 +516,7 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
}
/** How wide the picker stands in the editor's balloon, in icons. */
-const ICON_PICKER_COLUMNS = 12;
+const ICON_PICKER_COLUMNS = 9;
/**
* Inserts an empty paragraph at the very top of the document and places the cursor in it, giving the