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
fix(client): keep keyed icon picker in legacy layout
Amp2abd245bad74
diff --git a/apps/client/src/widgets/note_icon.spec.tsx b/apps/client/src/widgets/note_icon.spec.tsxindex 99d20a1137..4df3829ac6 100644--- a/apps/client/src/widgets/note_icon.spec.tsx+++ b/apps/client/src/widgets/note_icon.spec.tsx@@ -5,6 +5,7 @@ import { describe, expect, it, vi } from "vitest";import FNote from "../entities/fnote";import { renderInto } from "../test/render";+import { ReactWrappedWidget } from "./basic_widget";const { dropdownInstances, getOrCreateInstance } = vi.hoisted(() => {const dropdownInstances: Array<{@@ -82,6 +83,27 @@ globalThis.ResizeObserver = globalThis.ResizeObserver?? (ResizeObserverStub as unknown as typeof ResizeObserver);describe("NoteIcon", () => {+ it("stays in the legacy widget tree when the active note changes", () => {+ glob.iconRegistry = { sources: [] };+ noteContext.initialNote = buildNote("note-a");+ const widget = new ReactWrappedWidget(<NoteIcon />);+ widget.doRender();+ const liveContainer = document.createElement("div");+ document.body.appendChild(liveContainer);+ liveContainer.append(...widget.$widget.get());++ try {+ expect(liveContainer.querySelector("button")).not.toBeNull();++ void act(() => noteContext.setNote?.(buildNote("note-b")));++ expect(liveContainer.querySelector("button")?.classList.contains("bx-note-b")).toBe(true);+ } finally {+ widget.cleanup();+ liveContainer.remove();+ }+ });+it("closes an open icon picker when the active note changes", () => {glob.iconRegistry = { sources: [] };noteContext.initialNote = buildNote("note-a");@@ -90,7 +112,7 @@ describe("NoteIcon", () => {const dropdown = container.querySelector<HTMLElement>(".dropdown");expect(toggle).not.toBeNull();expect(dropdown).not.toBeNull();- const pickerDropdown = dropdownInstances[0];+ const pickerDropdown = dropdownInstances.at(-1);expect(pickerDropdown).toBeTruthy();void act(() => {diff --git a/apps/client/src/widgets/note_icon.tsx b/apps/client/src/widgets/note_icon.tsxindex 445c6d7cd4..84829c9678 100644--- a/apps/client/src/widgets/note_icon.tsx+++ b/apps/client/src/widgets/note_icon.tsx@@ -28,23 +28,27 @@ export default function NoteIcon() {const iconLabels = note ? getIconLabels(note) : [];return (- <IconPickerButton- key={note?.noteId}- icon={icon ?? "bx bx-empty"}- title={t("note_icon.change_note_icon")}- disabled={isDisabled}- onSelect={(picked) => {- if (!note) return;- const attributeToSet = note.hasOwnedLabel("workspace") ? "workspaceIconClass" : "iconClass";- attributes.setLabel(note.noteId, attributeToSet, picked);- }}- // Nothing to reset while the note wears the icon its type gives it.- onReset={iconLabels.length ? () => {- for (const label of iconLabels) {- attributes.removeAttributeById(label.noteId, label.attributeId);- }- } : undefined}- />+ // ReactWrappedWidget moves this root out of its render container. Keep it stable so a keyed+ // picker replacement is inserted into the live layout rather than the detached container.+ <div style={{ display: "contents" }}>+ <IconPickerButton+ key={note?.noteId}+ icon={icon ?? "bx bx-empty"}+ title={t("note_icon.change_note_icon")}+ disabled={isDisabled}+ onSelect={(picked) => {+ if (!note) return;+ const attributeToSet = note.hasOwnedLabel("workspace") ? "workspaceIconClass" : "iconClass";+ attributes.setLabel(note.noteId, attributeToSet, picked);+ }}+ // Nothing to reset while the note wears the icon its type gives it.+ onReset={iconLabels.length ? () => {+ for (const label of iconLabels) {+ attributes.removeAttributeById(label.noteId, label.attributeId);+ }+ } : undefined}+ />+ </div>);}