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(text): separate the mentioned notes from the creation rows
Elian Dorancff31e0f9f0e
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex c5d7a67103..a6d1839906 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -544,7 +544,7 @@ function noNotesRow(): Suggestion {}/** Whether a line goes above the row at `index`: the first of a group, after the rows of another. */-function startsGroup(items: Suggestion[], index: number) {+export function startsGroup(items: Suggestion[], index: number) {return index > 0 && rowGroup(items[index]) !== rowGroup(items[index - 1]);}diff --git a/apps/client/src/widgets/type_widgets/text/mention_list_view.spec.tsx b/apps/client/src/widgets/type_widgets/text/mention_list_view.spec.tsxindex c88277e496..5691337f72 100644--- a/apps/client/src/widgets/type_widgets/text/mention_list_view.spec.tsx+++ b/apps/client/src/widgets/type_widgets/text/mention_list_view.spec.tsx@@ -63,6 +63,24 @@ describe("createMentionListView", () => {expect(document.querySelector(".mention-list-menu")).toBeNull();});+ it("separates the notes from the creation rows as the note autocomplete does, and scrolls past the line", async () => {+ const scroll = vi.spyOn(Element.prototype, "scrollIntoView").mockImplementation(() => {});+ const entry = (id: string, action?: string) => ({+ item: { id, action, highlightedNotePathTitle: id } as MentionListState["entries"][0]["item"],+ marker: "@",+ render: () => undefined+ });+ const state = stateWith({ entries: [ entry("@Alpha"), entry("@create", "create-note"), entry("@child", "create-child-note") ] });+ const view = await show(state);++ const children = [ ...document.querySelectorAll(".mention-list-menu .tn-menu-scroll > *") ];+ expect(children.map((child) => child.className)).toEqual([ "dropdown-item tn-menu-active", "dropdown-divider", "dropdown-item", "dropdown-item" ]);++ scroll.mockClear();+ await act(async () => view.show({ ...state, selectedIndex: 2 }));+ expect(scroll.mock.contexts[0]).toBe(rows()[2]);+ });+it("reports the row the pointer moves onto and the one clicked, not a row under a still pointer", async () => {const state = stateWith();await show(state);diff --git a/apps/client/src/widgets/type_widgets/text/mention_list_view.tsx b/apps/client/src/widgets/type_widgets/text/mention_list_view.tsxindex 6a9f6d3f2d..265b80b149 100644--- a/apps/client/src/widgets/type_widgets/text/mention_list_view.tsx+++ b/apps/client/src/widgets/type_widgets/text/mention_list_view.tsx@@ -6,7 +6,8 @@ import { render } from "preact";import { useLayoutEffect, useRef } from "preact/hooks";import type { Suggestion } from "../../../services/note_autocomplete";-import { renderNoteSuggestion } from "../../react/NoteAutocomplete";+import { FormDropdownDivider } from "../../react/FormList";+import { renderNoteSuggestion, startsGroup } from "../../react/NoteAutocomplete";import Popup from "../../react/Popup";/**@@ -62,10 +63,12 @@ function MentionMenu({ state, elementRef, scrollsToSelection, onPointerSelect }:const menuRef = useRef<HTMLMenuElement>(null);const lastPointer = useRef<{ x: number; y: number }>();const { entries, selectedIndex } = state;+ // Grouped as the note autocomplete groups its rows; any other feed's entries form one group.+ const suggestions = entries.map(({ item }) => (isNoteSuggestion(item) ? item : {}));useLayoutEffect(() => {if (!scrollsToSelection() || selectedIndex < 0) return;- menuRef.current?.children[selectedIndex]?.scrollIntoView({ block: "nearest" });+ menuRef.current?.querySelectorAll(":scope > .dropdown-item")[selectedIndex]?.scrollIntoView({ block: "nearest" });}, [ selectedIndex, entries ]);return (@@ -83,7 +86,8 @@ function MentionMenu({ state, elementRef, scrollsToSelection, onPointerSelect }:// Keeps the focus in the editor, which closes the list without it.onMouseDown={(e) => e.preventDefault()}>- {entries.map((entry, index) => (+ {entries.map((entry, index) => [+ startsGroup(suggestions, index) && <FormDropdownDivider key={`divider-${index}`} />,<likey={`${entry.marker}:${entry.item.id}`}className={clsx("dropdown-item", index === selectedIndex && "tn-menu-active")}@@ -100,7 +104,7 @@ function MentionMenu({ state, elementRef, scrollsToSelection, onPointerSelect }:><EntryContent entry={entry} /></li>- ))}+ ])}</menu></Popup>);