Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager/space usage: fix note preview tooltip showing up after invoking the context menu

Adorian Dorandf0cb9418781
diff --git a/apps/client/src/services/note_tooltip.spec.ts b/apps/client/src/services/note_tooltip.spec.ts
index 1c63039455..cadfe5e506 100644
--- a/apps/client/src/services/note_tooltip.spec.ts
+++ b/apps/client/src/services/note_tooltip.spec.ts
@@ -95,6 +95,7 @@ beforeEach(() => {
afterEach(() => {
// Clear any tracked tooltip elements while the .tooltip stub is still installed.
dismissAllTooltips();
+ document.body.classList.remove("context-menu-shown");
$.fn.filter = origFilter;
$.fn.is = origIs;
delete ($.fn as any).tooltip;
@@ -339,6 +340,31 @@ describe("mouseEnterHandler", () => {
expect(($.fn as any).tooltip).not.toHaveBeenCalled();
});
+ it("stays out of the way of a context menu, whether already up or invoked mid-flight", async () => {
+ vi.useFakeTimers();
+ const $link = makeLink('<a data-href="#root/abc">x</a>');
+ parseNavigationStateFromUrl.mockReturnValue({ notePath: "root/abc", noteId: "abc", viewScope: { viewMode: "default" } });
+ // A best path, so the render yields real content and only the guard can hold the tooltip back.
+ froca.getNote = vi.fn(async () => fakeNote({ noteId: "abc", bestPath: "root/abc" })) as unknown as typeof froca.getNote;
+ hoverActive = true;
+
+ // Menu already up when the pointer arrives: nothing is even looked up.
+ document.body.classList.add("context-menu-shown");
+ await mouseEnterHandler.call($link[0], eventFor($link));
+ expect(parseNavigationStateFromUrl).not.toHaveBeenCalled();
+
+ // Menu invoked while the delayed preview was in flight: it resolves, but shows nothing —
+ // the tooltip would otherwise land on top of the menu the right-click just opened.
+ document.body.classList.remove("context-menu-shown");
+ const promise = mouseEnterHandler.call($link[0], eventFor($link));
+ document.body.classList.add("context-menu-shown");
+ await vi.advanceTimersByTimeAsync(600);
+ await promise;
+
+ expect(parseNavigationStateFromUrl).toHaveBeenCalled();
+ expect($.fn.tooltip as unknown as ReturnType<typeof vi.fn>).not.toHaveBeenCalled();
+ });
+
it("short-circuits when a tooltip for this link is already visible", async () => {
const $link = makeLink('<a data-link-id="existing" href="#root/abc">x</a>');
parseNavigationStateFromUrl.mockReturnValue({ notePath: "root/abc", noteId: "abc", viewScope: { viewMode: "default" } });
diff --git a/apps/client/src/services/note_tooltip.ts b/apps/client/src/services/note_tooltip.ts
index e8aba36c23..2c1bca0fc4 100644
--- a/apps/client/src/services/note_tooltip.ts
+++ b/apps/client/src/services/note_tooltip.ts
@@ -41,8 +41,17 @@ function setupElementTooltip($el: JQuery<HTMLElement>) {
$el.on("pointerenter", mouseEnterHandler);
}
+/**
+ * A context menu is what the pointer is dealing with while it is up — the element underneath it is
+ * only the menu's subject. `contextMenu.show()` dismisses the open tooltips, but the preview is
+ * delayed and async, so one already in flight when the menu opened would still land on top of it.
+ */
+function isContextMenuShown() {
+ return document.body.classList.contains("context-menu-shown");
+}
+
export async function mouseEnterHandler<T>(this: HTMLElement, e: JQuery.TriggeredEvent<T, undefined, T, T>) {
- if (e.pointerType !== "mouse") return;
+ if (e.pointerType !== "mouse" || isContextMenuShown()) return;
const $link = $(this);
@@ -120,7 +129,8 @@ export async function mouseEnterHandler<T>(this: HTMLElement, e: JQuery.Triggere
// we need to check if we're still hovering over the element
// since the operation to get tooltip content was async, it is possible that
// we now create tooltip which won't close because it won't receive mouseleave event
- if ($link.filter(":hover").length > 0) {
+ // — or that a context menu was invoked in the meantime, which the preview must not cover.
+ if ($link.filter(":hover").length > 0 && !isContextMenuShown()) {
$link.tooltip({
container: "body",
// https://github.com/zadam/trilium/issues/2794 https://github.com/zadam/trilium/issues/2988