Skip to content

Build your personal knowledge base with Trilium Notes

fix(quick-search): pin the full search link to the bottom of the popup

BeatLinkbb75b9ee766e
diff --git a/apps/client/src/widgets/quick_search.spec.ts b/apps/client/src/widgets/quick_search.spec.ts
new file mode 100644
index 0000000000..2184f8ceb7
--- /dev/null
+++ b/apps/client/src/widgets/quick_search.spec.ts
@@ -0,0 +1,76 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import server from "../services/server.js";
+import QuickSearchWidget from "./quick_search.js";
+
+describe("QuickSearchWidget", () => {
+ beforeEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it("keeps the full search link pinned below the scrolling results as more load", async () => {
+ const widget = renderWidget();
+ mockResults(30);
+
+ await widget.search();
+
+ const $menu = widget.$widget.find(".dropdown-menu");
+ const $results = $menu.find(".quick-search-results");
+ const $footer = $menu.find(".quick-search-footer");
+
+ // The results scroll on their own, so the footer stays visible at the bottom of the menu.
+ expect($results.children(".dropdown-item").length).toBe(15);
+ expect($results.find(".show-in-full-search").length).toBe(0);
+ expect($menu.children().last().is($footer)).toBe(true);
+ expect($footer.hasClass("hidden-ext")).toBe(false);
+ expect($footer.find(".show-in-full-search").length).toBe(1);
+
+ // happy-dom reports zero scroll metrics, which reads as "scrolled to the bottom".
+ $results.trigger("scroll");
+ await vi.waitFor(() => expect($results.children(".dropdown-item").length).toBe(25));
+
+ expect($menu.children().last().is($footer)).toBe(true);
+ expect($footer.find(".show-in-full-search").length).toBe(1);
+ });
+
+ it("hides the full search link while searching and when nothing matches", async () => {
+ const widget = renderWidget();
+ mockResults(3);
+ await widget.search();
+
+ const $footer = widget.$widget.find(".quick-search-footer");
+ expect($footer.hasClass("hidden-ext")).toBe(false);
+
+ mockResults(0);
+ const search = widget.search();
+ expect($footer.hasClass("hidden-ext")).toBe(true);
+
+ await search;
+ expect($footer.hasClass("hidden-ext")).toBe(true);
+ expect(widget.$widget.find(".quick-search-results .dropdown-item.disabled").length).toBe(1);
+ });
+});
+
+function renderWidget() {
+ const widget = new QuickSearchWidget();
+ widget.render();
+ widget.$widget.find(".search-string").val("hello");
+ return widget;
+}
+
+function mockResults(count: number) {
+ const searchResults = Array.from({ length: count }, (_, index) => ({
+ notePath: `note${index}`,
+ noteTitle: `Note ${index}`,
+ notePathTitle: `Note ${index}`,
+ highlightedNotePathTitle: `Note ${index}`,
+ icon: "bx bx-note"
+ }));
+
+ vi.spyOn(server, "get").mockResolvedValue({
+ searchResultNoteIds: searchResults.map((result) => result.notePath),
+ searchResults,
+ highlightedTokens: ["hello"],
+ error: ""
+ } as never);
+}
diff --git a/apps/client/src/widgets/quick_search.ts b/apps/client/src/widgets/quick_search.ts
index b4f28cd47f..9de0868604 100644
--- a/apps/client/src/widgets/quick_search.ts
+++ b/apps/client/src/widgets/quick_search.ts
@@ -28,12 +28,28 @@ const TPL = /*html*/`
max-height: 80vh;
min-width: 400px;
max-width: 720px;
- overflow-y: auto;
- overflow-x: hidden;
+ overflow: hidden;
text-overflow: ellipsis;
box-shadow: -30px 50px 93px -50px black;
}
+ /* Bootstrap opens the menu by switching display, so the column only applies to the open menu. */
+ .quick-search .dropdown-menu.show {
+ display: flex;
+ flex-direction: column;
+ }
+
+ .quick-search .quick-search-results {
+ flex: 1 1 auto;
+ min-height: 0;
+ overflow-y: auto;
+ overflow-x: hidden;
+ }
+
+ .quick-search .quick-search-footer {
+ flex: 0 0 auto;
+ }
+
.quick-search .dropdown-item {
white-space: normal;
padding: 12px 16px;
@@ -59,10 +75,6 @@ const TPL = /*html*/`
display: none;
}
- .quick-search .dropdown-item.show-in-full-search::after {
- display: none;
- }
-
.quick-search-item.dropdown-item:hover {
background-color: #f8f9fa;
}
@@ -133,7 +145,13 @@ const TPL = /*html*/`
<button class="btn btn-outline-secondary search-button" type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="bx bx-search"></span>
</button>
- <div class="dropdown-menu tn-dropdown-list"></div>
+ <div class="dropdown-menu tn-dropdown-list">
+ <div class="quick-search-results"></div>
+ <div class="quick-search-footer hidden-ext">
+ <div class="dropdown-divider"></div>
+ <a class="dropdown-item show-in-full-search" tabindex="0">${t("quick-search.show-in-full-search")}</a>
+ </div>
+ </div>
</div>
<input type="text" class="form-control form-control-sm search-string" placeholder="${t("quick-search.placeholder")}">
</div>`;
@@ -166,6 +184,8 @@ export default class QuickSearchWidget extends BasicWidget {
private dropdown!: bootstrap.Dropdown;
private $searchString!: JQuery<HTMLElement>;
private $dropdownMenu!: JQuery<HTMLElement>;
+ private $searchResults!: JQuery<HTMLElement>;
+ private $footer!: JQuery<HTMLElement>;
// State for infinite scrolling
private allSearchResults: Array<any> = [];
@@ -181,6 +201,8 @@ export default class QuickSearchWidget extends BasicWidget {
this.$widget = $(TPL);
this.$searchString = this.$widget.find(".search-string");
this.$dropdownMenu = this.$widget.find(".dropdown-menu");
+ this.$searchResults = this.$dropdownMenu.find(".quick-search-results");
+ this.$footer = this.$dropdownMenu.find(".quick-search-footer");
this.dropdown = Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']")[0], {
reference: this.$searchString[0],
@@ -193,10 +215,14 @@ export default class QuickSearchWidget extends BasicWidget {
this.$widget.find(".input-group-prepend").on("shown.bs.dropdown", () => this.search());
// Add scroll event listener for infinite scrolling
- this.$dropdownMenu.on("scroll", () => {
+ this.$searchResults.on("scroll", () => {
this.handleScroll();
});
+ const $showInFullSearchButton = this.$footer.find(".show-in-full-search");
+ $showInFullSearchButton.on("click", () => this.showInFullSearch());
+ shortcutService.bindElShortcut($showInFullSearchButton, "return", () => this.showInFullSearch());
+
if (utils.isMobile()) {
this.$searchString.keydown((e) => {
// Skip processing if IME is composing to prevent interference
@@ -230,7 +256,7 @@ export default class QuickSearchWidget extends BasicWidget {
});
shortcutService.bindElShortcut(this.$searchString, "down", () => {
- this.$dropdownMenu.find(".dropdown-item:not(.disabled):first").focus();
+ this.$searchResults.find(".dropdown-item:not(.disabled):first").focus();
});
shortcutService.bindElShortcut(this.$searchString, "esc", () => {
@@ -254,8 +280,9 @@ export default class QuickSearchWidget extends BasicWidget {
this.currentDisplayedCount = 0;
this.isLoadingMore = false;
- this.$dropdownMenu.empty();
- this.$dropdownMenu.append(`
+ this.$footer.addClass("hidden-ext");
+ this.$searchResults.empty();
+ this.$searchResults.append(`
<span class="dropdown-item disabled">
<span class="bx bx-loader bx-spin"></span>
${t("quick-search.searching")}
@@ -281,16 +308,18 @@ export default class QuickSearchWidget extends BasicWidget {
this.allSearchResults = searchResults || [];
this.allSearchResultNoteIds = searchResultNoteIds || [];
- this.$dropdownMenu.empty();
+ this.$searchResults.empty();
if (this.allSearchResults.length === 0 && this.allSearchResultNoteIds.length === 0) {
- this.$dropdownMenu.append(`<span class="dropdown-item disabled">${t("quick-search.no-results")}</span>`);
+ this.$searchResults.append(`<span class="dropdown-item disabled">${t("quick-search.no-results")}</span>`);
return;
}
// Display initial batch
await this.displayMoreResults(INITIAL_DISPLAYED_NOTES);
- this.addShowInFullSearchButton();
+
+ this.$footer.removeClass("hidden-ext");
+ shortcutService.bindElShortcut(this.$searchResults.find(".dropdown-item:first"), "up", () => this.$searchString.focus());
this.dropdown.update();
}
@@ -299,10 +328,6 @@ export default class QuickSearchWidget extends BasicWidget {
if (this.isLoadingMore) return;
this.isLoadingMore = true;
- // Remove the "Show in full search" button temporarily
- this.$dropdownMenu.find('.show-in-full-search').remove();
- this.$dropdownMenu.find('.dropdown-divider').remove();
-
// Use highlighted search results if available, otherwise fall back to basic display
if (this.allSearchResults.length > 0) {
const startIndex = this.currentDisplayedCount;
@@ -349,7 +374,7 @@ export default class QuickSearchWidget extends BasicWidget {
$item[0].click();
});
- this.$dropdownMenu.append($item);
+ this.$searchResults.append($item);
}
this.currentDisplayedCount = endIndex;
@@ -377,7 +402,7 @@ export default class QuickSearchWidget extends BasicWidget {
$link.find("a")[0]?.click();
});
- this.$dropdownMenu.append($link);
+ this.$searchResults.append($link);
}
this.currentDisplayedCount = endIndex;
@@ -389,42 +414,21 @@ export default class QuickSearchWidget extends BasicWidget {
private handleScroll() {
if (this.isLoadingMore) return;
- const dropdown = this.$dropdownMenu[0];
- const scrollTop = dropdown.scrollTop;
- const scrollHeight = dropdown.scrollHeight;
- const clientHeight = dropdown.clientHeight;
+ const results = this.$searchResults[0];
+ const scrollTop = results.scrollTop;
+ const scrollHeight = results.scrollHeight;
+ const clientHeight = results.clientHeight;
// Trigger loading more when user scrolls near the bottom (within 50px)
if (scrollTop + clientHeight >= scrollHeight - 50) {
const totalResults = this.allSearchResults.length > 0 ? this.allSearchResults.length : this.allSearchResultNoteIds.length;
if (this.currentDisplayedCount < totalResults) {
- this.displayMoreResults(LOAD_MORE_BATCH_SIZE).then(() => {
- this.addShowInFullSearchButton();
- });
+ this.displayMoreResults(LOAD_MORE_BATCH_SIZE).then(() => this.dropdown.update());
}
}
}
- private addShowInFullSearchButton() {
- // Remove existing button if it exists
- this.$dropdownMenu.find('.show-in-full-search').remove();
- this.$dropdownMenu.find('.dropdown-divider').remove();
-
- const $showInFullButton = $('<a class="dropdown-item show-in-full-search" tabindex="0">').text(t("quick-search.show-in-full-search"));
-
- this.$dropdownMenu.append($(`<div class="dropdown-divider">`));
- this.$dropdownMenu.append($showInFullButton);
-
- $showInFullButton.on("click", () => this.showInFullSearch());
-
- shortcutService.bindElShortcut($showInFullButton, "return", () => this.showInFullSearch());
-
- shortcutService.bindElShortcut(this.$dropdownMenu.find(".dropdown-item:first"), "up", () => this.$searchString.focus());
-
- this.dropdown.update();
- }
-
async showInFullSearch() {
this.dropdown.hide();