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
client/tab bar: tweak tooltips
Adorian Doranfd6d2ed7a941
diff --git a/apps/client/src/services/tab_title.spec.ts b/apps/client/src/services/tab_title.spec.tsindex 2d4a9fd097..9e46aa3f20 100644--- a/apps/client/src/services/tab_title.spec.ts+++ b/apps/client/src/services/tab_title.spec.ts@@ -84,4 +84,30 @@ describe("buildTabTitle tooltipHtml", () => {expect(tooltipHtml).toBe("A & B <c>");});++ it("does not bold the active segment when there is only one split", () => {+ const { tooltipHtml } = buildTabTitle([{ title: "Solo", active: true }], "New tab");++ expect(tooltipHtml).toBe("Solo");+ });++ it("prepends the pinned prefix to both tooltip strings, escaping it in the HTML one", () => {+ const { tooltip, tooltipHtml } = buildTabTitle(+ [+ { title: "Inbox", active: false },+ { title: "Tasks", active: true }+ ],+ "New tab",+ { pinnedPrefix: "Pinned: " }+ );++ expect(tooltip).toBe(`Pinned: Inbox${TAB_TITLE_SEPARATOR}Tasks`);+ expect(tooltipHtml).toBe(`Pinned: Inbox${TAB_TITLE_SEPARATOR}<strong>Tasks</strong>`);+ });++ it("escapes the pinned prefix too", () => {+ const { tooltipHtml } = buildTabTitle([{ title: "Inbox", active: false }], "New tab", { pinnedPrefix: "A & B: " });++ expect(tooltipHtml).toBe("A & B: Inbox");+ });});diff --git a/apps/client/src/services/tab_title.ts b/apps/client/src/services/tab_title.tsindex e61ff88139..cc04550002 100644--- a/apps/client/src/services/tab_title.ts+++ b/apps/client/src/services/tab_title.ts@@ -26,24 +26,33 @@ function escapeHtml(text: string): string {** `tooltip` is plain text; `tooltipHtml` escapes every title (untrusted) and wraps the active split in* trusted `<strong>` so it stands out — only the surrounding markup is trusted, never the titles.+ *+ * `options.pinnedPrefix`, when set, is prepended to the tooltip (e.g. "Pinned: ").*/export function buildTabTitle(splits: { title: string | null | undefined; active: boolean }[],- emptyLabel: string+ emptyLabel: string,+ options: { pinnedPrefix?: string } = {}): { segments: TabTitleSegment[]; tooltip: string; tooltipHtml: string } {const segments = splits.map((split) => ({text: split.title || emptyLabel,active: split.active}));+ const prefix = options.pinnedPrefix ?? "";+ // only emphasize the active split when there's more than one — bolding a lone title is pointless+ const emphasizeActive = segments.length > 1;+ const body = segments.map((segment) => segment.text).join(TAB_TITLE_SEPARATOR);+ const bodyHtml = segments+ .map((segment) => {+ const escaped = escapeHtml(segment.text);+ return segment.active && emphasizeActive ? `<strong>${escaped}</strong>` : escaped;+ })+ .join(TAB_TITLE_SEPARATOR);+return {segments,- tooltip: segments.map((segment) => segment.text).join(TAB_TITLE_SEPARATOR),- tooltipHtml: segments- .map((segment) => {- const escaped = escapeHtml(segment.text);- return segment.active ? `<strong>${escaped}</strong>` : escaped;- })- .join(TAB_TITLE_SEPARATOR)+ tooltip: `${prefix}${body}`,+ tooltipHtml: `${escapeHtml(prefix)}${bodyHtml}`};}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 6affd88383..a9b4a99214 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2003,6 +2003,7 @@"close_all_tabs": "Close all tabs","pin_tab": "Pin tab","unpin_tab": "Unpin tab",+ "pinned_prefix": "Pinned: ","reopen_last_tab": "Reopen last closed tab","move_tab_to_new_window": "Move this tab to a new window","copy_tab_to_new_window": "Copy this tab to a new window",diff --git a/apps/client/src/widgets/tab_row.ts b/apps/client/src/widgets/tab_row.tsindex 7685d4d19d..1509a70b30 100644--- a/apps/client/src/widgets/tab_row.ts+++ b/apps/client/src/widgets/tab_row.ts@@ -476,6 +476,28 @@ export default class TabRowWidget extends BasicWidget {this.$widget.show();}+ /**+ * Enables/disables all tab tooltips. Disabled while dragging so a `mouseenter` from the moving tab+ * can't re-show a tooltip that would be stranded at a stale position (it lives in `<body>`).+ */+ setTooltipsEnabled(enabled: boolean) {+ for (const tabEl of this.tabEls) {+ const wrapperEl = tabEl.querySelector(".note-tab-wrapper");+ const tooltip = wrapperEl ? Tooltip.getInstance(wrapperEl) : null;++ if (!tooltip) {+ continue;+ }++ if (enabled) {+ tooltip.enable();+ } else {+ tooltip.hide();+ tooltip.disable();+ }+ }+ }+get tabEls() {return Array.prototype.slice.call(this.$widget.find(".note-tab"));}@@ -755,12 +777,16 @@ export default class TabRowWidget extends BasicWidget {tabEl.classList.add("note-tab-is-dragging");this.$widget.addClass("tab-row-widget-is-sorting");+ // tooltips live in <body> and won't follow the tab — turn them off for the whole drag+ this.setTooltipsEnabled(false);+initialScrollLeft = this.$tabScrollingContainer?.scrollLeft() ?? 0;draggabilly.positionDrag = () => { };});draggabilly.on("dragEnd", () => {this.isDragging = false;+ this.setTooltipsEnabled(true);const currentScrollLeft = this.$tabScrollingContainer?.scrollLeft() ?? 0;const scrollDelta = currentScrollLeft - initialScrollLeft;const translateX = parseFloat(tabEl.style.left) + scrollDelta;@@ -953,7 +979,7 @@ export default class TabRowWidget extends BasicWidget {const subContexts = mainContext.getSubContexts();const focusedNtxId = this.getFocusedNtxId(mainContext, subContexts);- await this.updateTabTitle($tab, subContexts, focusedNtxId);+ await this.updateTabTitle($tab, subContexts, focusedNtxId, mainContext.pinned);// The icon and type classes follow the tab's focused split (remembered across activations),// falling back to its main (first) split.@@ -978,7 +1004,7 @@ export default class TabRowWidget extends BasicWidget {* Builds the tab title from every split in the tab, e.g. "Note A • Note B • Note C", with the* currently focused split emphasized. The full text is also set as the tooltip.*/- async updateTabTitle($tab: JQuery<HTMLElement>, subContexts: NoteContext[], focusedNtxId: string | null) {+ async updateTabTitle($tab: JQuery<HTMLElement>, subContexts: NoteContext[], focusedNtxId: string | null, pinned: boolean) {const splits = await Promise.all(subContexts.map(async (ctx) => ({title: ctx.note ? await ctx.getNavigationTitle() : null,@@ -986,7 +1012,9 @@ export default class TabRowWidget extends BasicWidget {})));- const { segments, tooltipHtml } = buildTabTitle(splits, t("tab_row.new_tab"));+ const { segments, tooltipHtml } = buildTabTitle(splits, t("tab_row.new_tab"), {+ pinnedPrefix: pinned ? t("tab_row.pinned_prefix") : undefined+ });$tab.attr("data-tab-title", tooltipHtml);