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
feat(client): show the backlinks in the quick edit popup
Elian Doranc75481a833a3
diff --git a/apps/client/src/widgets/dialogs/PopupEditor.spec.tsx b/apps/client/src/widgets/dialogs/PopupEditor.spec.tsxindex 5eb913d9f3..15702c1bf8 100644--- a/apps/client/src/widgets/dialogs/PopupEditor.spec.tsx+++ b/apps/client/src/widgets/dialogs/PopupEditor.spec.tsx@@ -14,7 +14,10 @@ const mocks = vi.hoisted(() => ({getAttachment: vi.fn(),/** Every context the popup created, newest last — one is built per open. */contexts: [] as { setNote: ReturnType<typeof vi.fn>, viewScope?: ViewScope, noteId: string, ntxId: string }[],- isMobile: vi.fn(() => false)+ isMobile: vi.fn(() => false),+ /** The note of every context the popup builds, read by the badges in its header. */+ contextNote: undefined as unknown,+ backlinkCount: vi.fn(() => 0)}));vi.mock("../../components/app_context", () => ({@@ -36,7 +39,7 @@ vi.mock("../../components/note_context", () => ({noteId = "n1";hoistedNoteId = "root";viewScope: ViewScope | undefined;- note = undefined;+ note = mocks.contextNote;triggerEvent = vi.fn();setNote = vi.fn(async (_notePath: string, opts?: { viewScope?: ViewScope }) => {this.viewScope = opts?.viewScope;@@ -75,6 +78,7 @@ vi.mock("../../services/experimental_features", () => ({ isExperimentalFeatureEn// The modal itself is Bootstrap-driven; the popup's own state is what it hands over.interface ModalStubProps {title?: ComponentChildren;+ header?: ComponentChildren;children?: ComponentChildren;show?: boolean;customTitleBarButtons?: { onClick: () => void }[];@@ -84,7 +88,7 @@ interface ModalStubProps {}vi.mock("../react/Modal", () => ({- default: ({ title, children, show, customTitleBarButtons, modalRef, onShown, onHidden }: ModalStubProps) => (+ default: ({ title, header, children, show, customTitleBarButtons, modalRef, onShown, onHidden }: ModalStubProps) => (<div ref={modalRef} className="modal-stub" data-shown={String(!!show)}><buttonclassName="maximize-stub"@@ -94,6 +98,7 @@ vi.mock("../react/Modal", () => ({<button className="shown-stub" onClick={() => onShown?.()} /><button className="hidden-stub" onClick={() => onHidden?.()} />{title}+ {header}{children}</div>)@@ -104,6 +109,11 @@ vi.mock("../NoteDetail", () => ({ default: () => <div className="note-detail-stuvi.mock("../collections/NoteList", () => ({ default: () => null }));vi.mock("../PromotedAttributes", () => ({ default: () => null }));vi.mock("../layout/NoteActions", () => ({ default: () => null }));+// `BacklinksWidget` has its own spec; this one covers where its links navigate.+vi.mock("../sidebar/Backlinks", () => ({+ useBacklinkCount: mocks.backlinkCount,+ BacklinksWidget: () => <a className="backlink-stub" href="#root/source">Source</a>+}));vi.mock("../layout/NoteBadges", () => ({ default: () => null }));vi.mock("../layout/NoteTypeSwitcher", () => ({default: () => <div className="note-type-switcher-stub" />@@ -471,6 +481,26 @@ describe("PopupEditor", () => {expect(container.querySelector(".note-type-switcher-stub")).toBeNull();});+ it("offers the note's backlinks in its header, opening them in itself rather than the tab behind", async () => {+ mocks.contextNote = { noteId: "n1", getLabelValue: () => null, getColorClass: () => "" };+ await openPopup({ noteIdOrPath: "root/n1" });+ expect(container.querySelector(".popup-editor-backlinks")).toBeNull();++ mocks.backlinkCount.mockReturnValue(2);+ await openPopup({ noteIdOrPath: "root/n1" });+ const toggle = container.querySelector<HTMLElement>(".popup-editor-backlinks button");+ expect(toggle).not.toBeNull();+ await act(async () => toggle?.click());+ const link = document.querySelector<HTMLElement>(".backlink-stub");+ expect(link).not.toBeNull();++ const context = lastContext();+ await act(async () => link?.click());+ expect(context?.setNote).toHaveBeenLastCalledWith("root/source", { viewScope: { viewMode: "default" }, keepActiveDialog: true });+ expect(document.querySelector(".backlink-stub")).toBeNull();+ mocks.contextNote = undefined;+ });+it("gives mobile its own toolbar and hides the buttons that make no sense in a popup", async () => {expect(container.querySelector(".mobile-toolbar-stub")).toBeNull();diff --git a/apps/client/src/widgets/dialogs/PopupEditor.tsx b/apps/client/src/widgets/dialogs/PopupEditor.tsxindex 569876391b..c904be7618 100644--- a/apps/client/src/widgets/dialogs/PopupEditor.tsx+++ b/apps/client/src/widgets/dialogs/PopupEditor.tsx@@ -5,8 +5,10 @@ import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooimport appContext from "../../components/app_context";import NoteContext from "../../components/note_context";+import FNote from "../../entities/fnote";import froca from "../../services/froca";import { t } from "../../services/i18n";+import type { ViewScope } from "../../services/link";import tree from "../../services/tree";import utils from "../../services/utils";import NoteList from "../collections/NoteList";@@ -16,9 +18,11 @@ import NoteTypeSwitcher from "../layout/NoteTypeSwitcher";import TitleRow from "../layout/TitleRow";import NoteDetail from "../NoteDetail";import PromotedAttributes from "../PromotedAttributes";+import { DropdownPanel, type DropdownHandle } from "../react/Dropdown";import { useContainedLinkNavigation, useNoteContext, useNoteLabel, useTriliumEvent } from "../react/hooks";import Modal from "../react/Modal";import { NoteContextContext, ParentComponent, POPUP_EDITOR_NTX_ID } from "../react/react_utils";+import { BacklinksWidget, useBacklinkCount } from "../sidebar/Backlinks";import MobileEditorToolbar from "../type_widgets/text/mobile_editor_toolbar";/** The layer the stylesheet gives this popup while it stands over another modal. */@@ -85,14 +89,15 @@ export default function PopupEditor({ ntxId = POPUP_EDITOR_NTX_ID, openCommand =// Keep navigation that follows internal links inside the popup, rather than letting the global// link handler open the target in the background tab. Settings links open the options dialog.- useContainedLinkNavigation(modalRef, useCallback((notePath, viewScope) => {+ const navigateInPopup = useCallback((notePath: string, viewScope: ViewScope | undefined) => {const targetNoteId = notePath.split("/").at(-1);if (targetNoteId?.startsWith("_options")) {void appContext.triggerCommand("showOptions", { section: targetNoteId });} else {void noteContext.setNote(notePath, { viewScope, keepActiveDialog: true });}- }, [ noteContext ]));+ }, [ noteContext ]);+ useContainedLinkNavigation(modalRef, navigateInPopup);// Add a global class to be able to handle issues with z-index due to rendering in a popup.useEffect(() => {@@ -163,7 +168,10 @@ export default function PopupEditor({ ntxId = POPUP_EDITOR_NTX_ID, openCommand =<ModalmodalRef={modalRef}title={<TitleRow />}- header={<NoteActions paneButtons={false} />}+ header={<>+ <PopupBacklinks onNavigate={navigateInPopup} />+ <NoteActions paneButtons={false} />+ </>}customTitleBarButtons={[{iconClassName: "bx-expand-alt",title: t("popup-editor.maximize"),@@ -205,6 +213,50 @@ export default function PopupEditor({ ntxId = POPUP_EDITOR_NTX_ID, openCommand =);}+/**+ * The note's backlinks, in the dropdown of an icon button whose tooltip gives their count. The+ * dropdown is rendered in the page's body, outside the modal, so `BacklinksInPopup` routes its links+ * into the popup, and following one closes the dropdown.+ */+function PopupBacklinks({ onNavigate }: { onNavigate: (notePath: string, viewScope: ViewScope | undefined) => void }) {+ const { note, viewScope } = useNoteContext();+ const count = useBacklinkCount(note, viewScope?.viewMode === "default");+ const dropdownRef = useRef<DropdownHandle>(null);++ return (note && count > 0 &&+ <DropdownPanel+ dropdownRef={dropdownRef}+ className="popup-editor-backlinks"+ buttonClassName="bx bx-link"+ title={t("status_bar.backlinks_title", { count })}+ dropdownContainerClassName="dropdown-backlinks"+ noSelectButtonStyle+ hideToggleArrow+ iconAction+ scrollable+ >+ <BacklinksInPopup+ note={note}+ onNavigate={(notePath, scope) => {+ dropdownRef.current?.hide();+ onNavigate(notePath, scope);+ }}+ />+ </DropdownPanel>+ );+}++function BacklinksInPopup({ note, onNavigate }: { note: FNote, onNavigate: (notePath: string, viewScope: ViewScope | undefined) => void }) {+ const containerRef = useRef<HTMLDivElement>(null);+ useContainedLinkNavigation(containerRef, onNavigate);++ return (+ <div ref={containerRef}>+ <BacklinksWidget note={note} />+ </div>+ );+}+function syncPopupClasses() {document.body.classList.toggle("popup-editor-open", shownPopups.size > 0);document.body.classList.toggle("popup-editor-stacked", stackedPopups.size > 0);diff --git a/apps/client/src/widgets/layout/StatusBar.css b/apps/client/src/widgets/layout/StatusBar.cssindex b25ba5d93c..93b08dc5b1 100644--- a/apps/client/src/widgets/layout/StatusBar.css+++ b/apps/client/src/widgets/layout/StatusBar.css@@ -124,17 +124,6 @@overflow-wrap: anywhere;}}-- &.dropdown-backlinks {- --menu-padding-size: .9em;-- min-width: 300px;- max-width: 500px;-- > .tn-panel-scroll {- max-height: 60vh;- }- }}.note-info-content {diff --git a/apps/client/src/widgets/sidebar/Backlinks.css b/apps/client/src/widgets/sidebar/Backlinks.cssindex e944dfd289..eba3c082e8 100644--- a/apps/client/src/widgets/sidebar/Backlinks.css+++ b/apps/client/src/widgets/sidebar/Backlinks.css@@ -100,3 +100,15 @@font-size: .85em;}}++.dropdown-menu.dropdown-backlinks {+ --menu-padding-size: .9em;++ width: max-content;+ min-width: 300px;+ max-width: 500px;++ > .tn-panel-scroll {+ max-height: 60vh;+ }+}diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick edit.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick edit.htmlindex 82f6e13089..052c646ae0 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick edit.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick edit.html@@ -27,6 +27,9 @@<li>The note's actions and its menu are shown in the header of the popup,as in the title row of a tab.<ul>+ <li>When other notes link to the note, a button with a link icon lists the <a+ class="reference-link" href="#root/_help_wfGXLmm7x27z">Backlinks</a>. Pressing+ one opens that note in the popup.</li><li>The menu items that only work in a tab are not shown: <em>Search in note</em>, <em>Note map</em>, <em>Full width</em> and <em>Convert into attachment</em>.</li><li>A help page opened from the popup, such as through the help button ofthe note type, opens in a second popup on top of it. Closing that popupdiff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Links/Backlinks.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Links/Backlinks.htmlindex 47d8239a95..edbd600b97 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Links/Backlinks.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Links/Backlinks.html@@ -71,6 +71,10 @@the number of backlinks. The badge only appears when the note is beingread normally (not in a revision or attachment view) and when there isat least one backlink; pressing it opens the section above.</li>+ <li>In <a class="reference-link" href="#root/_help_ZjLYv08Rp3qC">Quick edit</a>,+ as a button with a link icon in the header of the popup, under the same+ conditions; its tooltip gives the number of backlinks. Pressing a backlink+ opens that note in the popup.</li><li>Incoming links are also drawn on the link map, see <a class="reference-link"href="#root/_help_BCkXAVs63Ttv">Note Map (Link map, Tree map)</a>.</li><li>On the old layout, backlinks are showed as a dedicated button in the <adiff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick edit.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick edit.mdindex c6a771ef17..23fee76e08 100644--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick edit.md+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick edit.md@@ -14,6 +14,7 @@ This feature is also well integrated with <a class="reference-link" href="../..* For <a class="reference-link" href="../../Note%20Types/Text.md">Text</a> notes, depending on user preference, both the floating and classic editors are supported. See <a class="reference-link" href="../../Note%20Types/Text/Formatting%20toolbar.md">Formatting toolbar</a>.* The title and the note and the icon are editable, just like a normal tab.* The note's actions and its menu are shown in the header of the popup, as in the title row of a tab.+ * When other notes link to the note, a button with a link icon lists the <a class="reference-link" href="../../Note%20Types/Text/Links/Backlinks.md">Backlinks</a>. Pressing one opens that note in the popup.* The menu items that only work in a tab are not shown: _Search in note_, _Note map_, _Full width_ and _Convert into attachment_.* A help page opened from the popup, such as through the help button of the note type, opens in a second popup on top of it. Closing that popup returns to the note being edited.* The <a class="reference-link" href="../../Advanced%20Usage/Attributes/Promoted%20Attributes.md">Promoted Attributes</a> are also displayed.diff --git a/docs/User Guide/User Guide/Note Types/Text/Links/Backlinks.md b/docs/User Guide/User Guide/Note Types/Text/Links/Backlinks.mdindex 847c6048c1..d9c1bdc6c1 100644--- a/docs/User Guide/User Guide/Note Types/Text/Links/Backlinks.md+++ b/docs/User Guide/User Guide/Note Types/Text/Links/Backlinks.md@@ -36,5 +36,6 @@ Of note:* In the <a class="reference-link" href="../../../Basic%20Concepts%20and%20Features/UI%20Elements/Right%20Sidebar/Connections%20tab.md">Connections tab</a> of the <a class="reference-link" href="../../../Basic%20Concepts%20and%20Features/UI%20Elements/Right%20Sidebar.md">Right Sidebar</a>, as a dedicated section.* As a badge in the <a class="reference-link" href="../../../Basic%20Concepts%20and%20Features/UI%20Elements/New%20Layout/Status%20bar.md">Status bar</a> showing the number of backlinks. The badge only appears when the note is being read normally (not in a revision or attachment view) and when there is at least one backlink; pressing it opens the section above.+* In <a class="reference-link" href="../../../Basic%20Concepts%20and%20Features/Navigation/Quick%20edit.md">Quick edit</a>, as a button with a link icon in the header of the popup, under the same conditions; its tooltip gives the number of backlinks. Pressing a backlink opens that note in the popup.* Incoming links are also drawn on the link map, see <a class="reference-link" href="../../../Advanced%20Usage/Note%20Map%20(Link%20map%2C%20Tree%20map).md">Note Map (Link map, Tree map)</a>.* On the old layout, backlinks are showed as a dedicated button in the <a class="reference-link" href="../../../Basic%20Concepts%20and%20Features/UI%20Elements/Floating%20buttons.md">Floating buttons</a> area.\ No newline at end of file