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
refactor(autocomplete): draw the contained note lists with the popup's rows
Elian Doran6a21e146bf88
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 996d6538ff..136e25c010 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -989,11 +989,6 @@ table.promoted-attributes-in-tooltip th {opacity: 1;}-.algolia-autocomplete-container .aa-dropdown-menu {- position: inherit !important;- overflow: auto;-}-.help-button,.custom-title-bar-button {float: inline-end;diff --git a/apps/client/src/stylesheets/theme-next/base.css b/apps/client/src/stylesheets/theme-next/base.cssindex 032e17c73d..d358a6d078 100644--- a/apps/client/src/stylesheets/theme-next/base.css+++ b/apps/client/src/stylesheets/theme-next/base.css@@ -812,24 +812,6 @@ li.dropdown-item a.dropdown-item-button:focus-visible {transform: translateY(4%);}-/*- * NOTE SEARCH SUGGESTIONS- */--/* List body */-.jump-to-note-dialog .jump-to-note-results .aa-suggestions,-.note-detail-empty .aa-suggestions {- padding: 0;-}--/* List item */-.jump-to-note-dialog .aa-suggestion,-.note-detail-empty .aa-suggestion {- border-radius: 6px;- color: var(--menu-text-color);- cursor: default;-}-/** Alert bars*/diff --git a/apps/client/src/stylesheets/theme-next/dialogs.css b/apps/client/src/stylesheets/theme-next/dialogs.cssindex 97fb65352b..6919594cd6 100644--- a/apps/client/src/stylesheets/theme-next/dialogs.css+++ b/apps/client/src/stylesheets/theme-next/dialogs.css@@ -156,7 +156,7 @@ div.tn-tool-dialog {padding: 0 !important;}-.jump-to-note-dialog .jump-to-note-results .aa-dropdown-menu {+.jump-to-note-dialog .jump-to-note-results .note-suggestion-list {/* Auto, as a fixed width would shift with the negative margin rather than grow by it. */width: auto;margin-inline-end: calc(0px - var(--jump-to-note-padding));diff --git a/apps/client/src/stylesheets/theme-next/pages.css b/apps/client/src/stylesheets/theme-next/pages.cssindex 5a53b9a3d5..87f8f6abb0 100644--- a/apps/client/src/stylesheets/theme-next/pages.css+++ b/apps/client/src/stylesheets/theme-next/pages.css@@ -104,7 +104,7 @@}/* The search results list */-.note-detail-empty span.aa-dropdown-menu {+.note-detail-empty menu.note-suggestion-list {margin-top: 1em;border: unset;}diff --git a/apps/client/src/widgets/dialogs/jump_to_note.css b/apps/client/src/widgets/dialogs/jump_to_note.cssindex fdd58f90a6..e228e5feaf 100644--- a/apps/client/src/widgets/dialogs/jump_to_note.css+++ b/apps/client/src/widgets/dialogs/jump_to_note.css@@ -16,73 +16,17 @@min-height: 200px;}-.jump-to-note-results .aa-dropdown-menu {+.jump-to-note-results .note-suggestion-list {max-height: calc(80vh - 200px);width: 100%;- max-width: none;overflow-y: auto;overflow-x: hidden;- text-overflow: ellipsis;- box-shadow: none;}.jump-to-note-results {width: 100%;}-.jump-to-note-results .aa-suggestions {- padding: 0;- width: 100%;-}--/* Command palette styling */-.jump-to-note-dialog .command-suggestion {- display: flex;- align-items: center;- gap: 0.75rem;- font-size: 0.9em;-}--.jump-to-note-dialog .aa-suggestion .command-suggestion,-.jump-to-note-dialog .aa-suggestion .command-suggestion div {- padding: 0;-}--.jump-to-note-dialog .aa-cursor .command-suggestion,-.jump-to-note-dialog .aa-suggestion:hover .command-suggestion {- background-color: transparent;-}---.jump-to-note-dialog .command-icon {- color: var(--muted-text-color);- font-size: 1.125rem;- flex-shrink: 0;- margin-top: 0.125rem;-}--.jump-to-note-dialog .command-content {- flex-grow: 1;- min-width: 0;-}--.jump-to-note-dialog .command-name {- font-weight: bold;-}--.jump-to-note-dialog .command-description {- font-size: 0.8em;- line-height: 1.3;- opacity: 0.75;-}--.jump-to-note-dialog kbd.command-shortcut {- background-color: transparent;- color: inherit;- opacity: 0.75;- font-family: inherit !important;-}-@media (max-width: 991px) {body.mobile .jump-to-note-dialog {.modal-header {@@ -97,12 +41,14 @@overflow-y: auto;}- .aa-dropdown-menu {+ .note-suggestion-list {max-height: unset;overflow: auto;}- .aa-suggestion {+ .note-suggestion-list > .dropdown-item {+ --menu-item-start-padding: 0px;+ --menu-item-end-padding: 0px;padding-inline: 0;}}diff --git a/apps/client/src/widgets/dialogs/jump_to_note.tsx b/apps/client/src/widgets/dialogs/jump_to_note.tsxindex ccb52a0fbf..a17391a33d 100644--- a/apps/client/src/widgets/dialogs/jump_to_note.tsx+++ b/apps/client/src/widgets/dialogs/jump_to_note.tsx@@ -119,7 +119,7 @@ export default function JumpToNoteDialogComponent() {onHidden={() => setShown(false)}show={shown}>- <div className="algolia-autocomplete-container jump-to-note-results" ref={containerRef} />+ <div className="jump-to-note-results" ref={containerRef} /></Modal>);}diff --git a/apps/client/src/widgets/react/NoteAutocomplete.css b/apps/client/src/widgets/react/NoteAutocomplete.cssindex 39b08928dd..c18261aa05 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.css+++ b/apps/client/src/widgets/react/NoteAutocomplete.css@@ -54,69 +54,32 @@}/*- * The lists in a host's container, such as the empty tab's and Jump to Note's.+ * The lists in a host's container, such as the Empty tab's and Jump to Note's: the popup's rows,+ * outside a `.dropdown-menu`, which defines Bootstrap's item and divider variables.*/-span.aa-dropdown-menu,-span.aa-dropdown-menu .aa-suggestions {- display: block;-}--span.aa-dropdown-menu .aa-suggestion {- cursor: pointer;- padding: 6px 16px;+:where(menu.note-suggestion-list) {margin: 0;+ padding: 0;+ list-style: none;+}- .note-suggestion {- display: flex;- }-- .icon {- flex: none;- line-height: inherit;- }-- /* The rest of the row, so the keys floated in it end at the row's padding. */- .text {- flex: 1;- min-width: 0;- padding-inline-start: 4px;- }-- .search-result-title {- display: block;- }-- .search-result-attributes {- display: block;- font-size: 0.8em;- color: var(--muted-text-color);- opacity: 0.6;- line-height: 1;- word-wrap: break-word;- }-- p {- padding: 0;- margin: 0;- }+.note-suggestion-list {+ /* The theme draws a divider's line this far past each side of its row. */+ --padding: 0px;- &.aa-cursor {- color: var(--suggestion-active-text-color);- background-color: var(--suggestion-active-background-color);+ > .dropdown-item {+ --menu-item-vertical-padding: 6px;+ --menu-item-start-padding: 16px;+ --menu-item-end-padding: 16px;+ padding: 6px 16px;}- /* A row saying something about the list, not one to pick. */- &.disabled {- cursor: default;- color: var(--muted-text-color);+ > .dropdown-divider {+ margin-block: 8px;+ border-top: 1px solid var(--main-border-color);}}-span.aa-dropdown-menu .note-suggestion-separator {- margin-block: 8px;- border-top: 1px solid var(--main-border-color);-}-/** The keys of a search row, at the end of its title line. One line tall, as the title is, so the row* keeps the height of the others.@@ -129,15 +92,11 @@ span.aa-dropdown-menu .note-suggestion-separator {color: var(--muted-text-color);}-span.aa-dropdown-menu .note-suggestion-shortcut {- float: inline-end;-}-/** Each key drawn as the Next theme draws a button's (`button.btn kbd` in forms.css), plainly in the* themes without its variables. The `!important`s outrank the Next theme's `.dropdown-item kbd`.*/-:is(.dropdown-item, .aa-suggestion) .note-suggestion-shortcut > kbd {+.dropdown-item .note-suggestion-shortcut > kbd {padding: 0.1875rem 0.375rem;border: 0;box-shadow: none;diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsxindex eb047d3380..3c3fe00be2 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -436,17 +436,22 @@ describe("NoteAutocomplete's suggestion list", () => {const input = await mount({ container: { current: host } });await type(input, "a");- const menu = host.querySelector<HTMLElement>(":scope > span.aa-dropdown-menu");- expect(menu?.querySelectorAll(".aa-suggestion")).toHaveLength(3);+ const menu = host.querySelector<HTMLElement>(":scope > menu.note-suggestion-list");+ expect(menu?.querySelectorAll(":scope > .dropdown-item")).toHaveLength(3);// In the host's flow, with no position of its own.expect(menu?.hasAttribute("style")).toBe(false);- expect(document.querySelector(".note-autocomplete-menu")).toBeNull();+ expect(document.querySelector(".dropdown-menu.note-autocomplete-menu")).toBeNull();+ // Drawn as the popup draws its rows.+ const beta = menu?.querySelectorAll<HTMLElement>(".dropdown-item")[1];+ expect(beta?.querySelector(".tn-icon")?.className).toBe("bx bx-note tn-icon");+ expect(beta?.querySelector(".search-result-title")?.innerHTML).toBe("X / Beta");+ expect(beta?.querySelector(".search-result-attributes")?.textContent).toBe("#tag");await act(async () => {input.focus();input.blur();});- expect(host.querySelectorAll(".aa-suggestion")).toHaveLength(3);+ expect(host.querySelectorAll(".dropdown-item")).toHaveLength(3);// Both are left to the host, as a dialog closes on Escape.const reachedHost: string[] = [];@@ -455,15 +460,15 @@ describe("NoteAutocomplete's suggestion list", () => {await press(input, "Escape");await press(input, "Tab");document.body.removeEventListener("keydown", listen);- expect(host.querySelectorAll(".aa-suggestion")).toHaveLength(3);+ expect(host.querySelectorAll(".dropdown-item")).toHaveLength(3);expect(reachedHost).toEqual([ "Escape", "Tab" ]);await press(input, "Enter");- expect(host.querySelector(".aa-dropdown-menu")).toBeNull();+ expect(host.querySelector(".note-suggestion-list")).toBeNull();host.remove();});- it("lists the commands for a `>` in a command palette, in the plugin's markup", async () => {+ it("lists the commands for a `>` in a command palette, as menu rows", async () => {// In a host's container, as Jump to Note lists them.const host = document.createElement("div");document.body.append(host);@@ -472,13 +477,13 @@ describe("NoteAutocomplete's suggestion list", () => {expect(getCommandSuggestions).toHaveBeenLastCalledWith("> cmd");expect(getNoteSuggestions).not.toHaveBeenCalled();- const [ described, bare ] = host.querySelectorAll<HTMLElement>(".aa-suggestion");- expect(described.querySelector(".command-suggestion > .command-icon")?.className).toBe("command-icon bx bx-cog");- expect(described.querySelector(".command-content > .command-name")?.textContent).toBe("Cmd One");- expect(described.querySelector(".command-content > .command-description")?.textContent).toBe("Does a thing");- expect(described.querySelector(".command-suggestion > kbd.command-shortcut")?.textContent).toBe("Ctrl+1");- expect(bare.querySelector(".command-icon")?.className).toBe("command-icon bx bx-terminal");- expect(bare.querySelector(".command-description, .command-shortcut")).toBeNull();+ const [ described, bare ] = host.querySelectorAll<HTMLElement>(".note-suggestion-list > .dropdown-item");+ expect(described.querySelector(".tn-icon")?.className).toBe("bx bx-cog tn-icon");+ expect(described.querySelector(".search-result-title")?.textContent).toBe("Cmd One");+ expect(described.querySelector(".search-result-attributes")?.textContent).toBe("Does a thing");+ expect(described.querySelector("kbd")?.textContent).toBe("Ctrl+1");+ expect(bare.querySelector(".tn-icon")?.className).toBe("bx bx-terminal tn-icon");+ expect(bare.querySelector(".search-result-attributes, kbd")).toBeNull();host.remove();// Elsewhere a `>` is only text to search for.@@ -728,9 +733,9 @@ describe("NoteAutocomplete's suggestion list", () => {document.body.append(host);const contained = await mount({ container: { current: host } });await type(contained, "New");- const firstContained = host.querySelector(".aa-suggestions")?.firstElementChild;+ const firstContained = host.querySelector(".note-suggestion-list")?.firstElementChild;expect(firstContained?.classList.contains("disabled")).toBe(true);- expect(firstContained?.nextElementSibling?.className).toBe("note-suggestion-separator");+ expect(firstContained?.nextElementSibling?.className).toBe("dropdown-divider");host.remove();// A list with notes needs no such row.@@ -766,8 +771,8 @@ describe("NoteAutocomplete's suggestion list", () => {const input = await mount({ container: { current: host } });await type(input, "al");- const separators = [ ...host.querySelectorAll(".note-suggestion-separator") ];- const suggestions = [ ...host.querySelectorAll(".aa-suggestion") ];+ const separators = [ ...host.querySelectorAll(".note-suggestion-list > .dropdown-divider") ];+ const suggestions = [ ...host.querySelectorAll(".note-suggestion-list > .dropdown-item") ];expect(separators).toHaveLength(1);expect(separators[0].previousElementSibling).toBe(suggestions[1]);expect(separators[0].nextElementSibling?.querySelector(".bx-search")).not.toBeNull();diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex 2e522f3c08..ef19d45cad 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -320,7 +320,13 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,{(autocomplete.isShown || (autocomplete.isOpen && isSearchingFullText)) && (container? container.current && createPortal(- <NoteSuggestionList autocomplete={autocomplete} searchingFor={isSearchingFullText ? value : undefined} />,+ <NoteSuggestionMenu+ autocomplete={autocomplete}+ searchingFor={isSearchingFullText ? value : undefined}+ // The popup's rows, in the host's panel. Faded at an edge only where the host+ // sets `--scroll-fade-top` or `--scroll-fade-bottom`.+ className="note-autocomplete-menu note-suggestion-list tn-menu-keyboard scroll-edge-fade"+ />,container.current): anchor && (<Popup@@ -336,7 +342,11 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,escapeDismisses={false}onDismiss={autocomplete.close}>- <NoteSuggestionMenu autocomplete={autocomplete} searchingFor={isSearchingFullText ? value : undefined} />+ <NoteSuggestionMenu+ autocomplete={autocomplete}+ searchingFor={isSearchingFullText ? value : undefined}+ className="tn-menu-scroll"+ /></Popup>))}</div>@@ -344,67 +354,18 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,}/**- * The list of suggestions, in the `aa-*` markup that the Empty tab and Jump to Note style. With- * `searchingFor`, the query of a search in progress, it shows a row saying so in place of the- * suggestions.+ * The list of suggestions as the rows of a menu, so it looks like the app's other dropdowns, whether+ * in the popup or in a host's container. With `searchingFor`, the query of a search in progress, it+ * shows a row saying so in place of the suggestions.*/-function NoteSuggestionList({ autocomplete, searchingFor }: {- autocomplete: ReturnType<typeof useAutocomplete<Suggestion>>;- searchingFor?: string;-}) {- return (- <span- // Faded at an edge only where a host sets `--scroll-fade-top` or `--scroll-fade-bottom`.- className="aa-dropdown-menu scroll-edge-fade"- role="listbox"- // Keeps the input focused, so its blur does not close the list before the click lands- // on a suggestion.- onMouseDown={(e) => e.preventDefault()}- >- <div className="aa-dataset-0">- <span className="aa-suggestions">- {searchingFor !== undefined && (- <div className="aa-suggestion">- <NoteSuggestion suggestion={{ noteTitle: searchingFor, highlightedNotePathTitle: t("quick-search.searching") }} />- </div>- )}- {searchingFor === undefined && listsNoNote(autocomplete.items) && <>- <div className="aa-suggestion disabled">- <NoteSuggestion suggestion={noNotesRow()} />- </div>- <div className="note-suggestion-separator" role="separator" />- </>}- {searchingFor === undefined && autocomplete.items.map((suggestion, index) => [- startsGroup(autocomplete.items, index) && <div key={`separator-${index}`} className="note-suggestion-separator" role="separator" />,- <div- key={suggestionKey(suggestion, index)}- id={autocomplete.itemId(index)}- className={clsx("aa-suggestion", index === autocomplete.activeIndex && "aa-cursor")}- role="option"- aria-selected={index === autocomplete.activeIndex}- onMouseMove={(e) => autocomplete.hover(index, e)}- onClick={() => autocomplete.pick(suggestion)}- >- <NoteSuggestion suggestion={suggestion} />- </div>- ])}- </span>- </div>- </span>- );-}--/**- * The list of suggestions as the rows of a menu, for the popup, so it looks like the app's other- * dropdowns. With `searchingFor`, it shows a row saying a search is in progress instead.- */-function NoteSuggestionMenu({ autocomplete, searchingFor }: {+function NoteSuggestionMenu({ autocomplete, searchingFor, className }: {autocomplete: ReturnType<typeof useAutocomplete<Suggestion>>;searchingFor?: string;+ className: string;}) {return (<menu- className="tn-menu-scroll"+ className={className}role="listbox"// Keeps the input focused, so its blur does not close the list before the click lands// on a suggestion.@@ -475,37 +436,6 @@ export function renderNoteSuggestion(suggestion: Suggestion): HTMLElement {return element;}-/** One row of the list. */-function NoteSuggestion({ suggestion }: { suggestion: Suggestion }) {- if (suggestion.action === "command") {- return (- <div className="command-suggestion">- <span className={clsx("command-icon", suggestion.icon || "bx bx-terminal")} />- <div className="command-content">- <div className="command-name">{suggestion.highlightedNotePathTitle}</div>- {suggestion.commandDescription && (- <div className="command-description">{suggestion.commandDescription}</div>- )}- </div>- {suggestion.commandShortcut && <kbd className="command-shortcut">{suggestion.commandShortcut}</kbd>}- </div>- );- }-- return (- <div className="note-suggestion">- <span className={clsx("icon", suggestionIcon(suggestion))} />- <span className="text">- <SuggestionShortcut suggestion={suggestion} />- <RawHtml className="search-result-title" html={suggestion.highlightedNotePathTitle ?? ""} />- {suggestion.highlightedAttributeSnippet && (- <RawHtml className="search-result-attributes" html={suggestion.highlightedAttributeSnippet} />- )}- </span>- </div>- );-}-/*** Which group of the list a row belongs to: the notes (with an external link or a command), the* rows searching further for the query, or the rows creating a note from it.diff --git a/apps/client/src/widgets/type_widgets/Empty.css b/apps/client/src/widgets/type_widgets/Empty.cssindex 5efed63b62..0f05994ec5 100644--- a/apps/client/src/widgets/type_widgets/Empty.css+++ b/apps/client/src/widgets/type_widgets/Empty.css@@ -50,7 +50,7 @@ body.desktop {border-radius: 8px;}-.note-detail-empty-results .aa-dropdown-menu {+.note-detail-empty-results .note-suggestion-list {border: var(--bs-border-width) solid var(--bs-border-color);border-top: 0;}diff --git a/packages/trilium-e2e/src/layout/split_pane.spec.ts b/packages/trilium-e2e/src/layout/split_pane.spec.tsindex cf573afb95..cf0e65d7cc 100644--- a/packages/trilium-e2e/src/layout/split_pane.spec.ts+++ b/packages/trilium-e2e/src/layout/split_pane.spec.ts@@ -33,7 +33,8 @@ test("Open the note in the correct split pane", async ({ page, context }) => {// Click the search result in the second split. The notes come ahead of the search and// creation rows, which also carry the title.- await resultsSelector.locator(".aa-suggestion", { hasText: CODE_NOTE_TITLE })+ await resultsSelector+ .locator(".note-suggestion-list > .dropdown-item", { hasText: CODE_NOTE_TITLE }).first().click();await expect(split2.locator(".note-title").first()).toHaveValue(CODE_NOTE_TITLE);diff --git a/packages/trilium-e2e/src/support/app.ts b/packages/trilium-e2e/src/support/app.tsindex bc28f97198..f939cac486 100644--- a/packages/trilium-e2e/src/support/app.ts+++ b/packages/trilium-e2e/src/support/app.ts@@ -126,7 +126,7 @@ export default class App {// `.note-detail-empty-results`, which also contains the recent-notes list) ensures the// dropdown actually opened.const suggestionSelector = this.currentNoteSplit- .locator(".note-detail-empty-results .aa-suggestion")+ .locator(".note-detail-empty-results .note-suggestion-list > .dropdown-item").first();await expect(suggestionSelector).toContainText(noteTitle);await suggestionSelector.click();