2020-01-25 09:56:08 +01:00
|
|
|
import treeService from "./tree.js";
|
2018-03-26 22:29:14 -04:00
|
|
|
import linkService from "./link.js";
|
2021-04-16 23:01:56 +02:00
|
|
|
import froca from "./froca.js";
|
2020-04-04 22:40:32 +02:00
|
|
|
import utils from "./utils.js";
|
2020-09-08 21:45:07 +02:00
|
|
|
import attributeRenderer from "./attribute_renderer.js";
|
2023-05-20 23:46:45 +02:00
|
|
|
import contentRenderer from "./content_renderer.js";
|
2023-04-15 00:06:13 +02:00
|
|
|
import appContext from "../components/app_context.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type FNote from "../entities/fnote.js";
|
2025-01-07 12:44:30 +02:00
|
|
|
import { t } from "./i18n.js";
|
2018-03-26 22:29:14 -04:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
function setupGlobalTooltip() {
|
|
|
|
$(document).on("mouseenter", "a", mouseEnterHandler);
|
2018-08-15 18:22:02 +02:00
|
|
|
|
2019-04-20 09:39:39 +02:00
|
|
|
// close any note tooltip after click, this fixes the problem that sometimes tooltips remained on the screen
|
2025-01-09 18:07:02 +02:00
|
|
|
$(document).on("click", (e) => {
|
2023-07-25 22:27:15 +02:00
|
|
|
if ($(e.target).closest(".note-tooltip").length) {
|
|
|
|
// click within the tooltip shouldn't close it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-22 22:24:42 +02:00
|
|
|
dismissAllTooltips();
|
2023-07-25 22:27:15 +02:00
|
|
|
});
|
2018-12-22 20:57:09 +01:00
|
|
|
}
|
2018-08-15 18:22:02 +02:00
|
|
|
|
2025-01-22 22:24:42 +02:00
|
|
|
function dismissAllTooltips() {
|
2025-01-09 18:07:02 +02:00
|
|
|
$(".note-tooltip").remove();
|
2024-09-01 00:04:29 +03:00
|
|
|
}
|
|
|
|
|
2024-12-21 14:38:25 +02:00
|
|
|
function setupElementTooltip($el: JQuery<HTMLElement>) {
|
2025-01-09 18:07:02 +02:00
|
|
|
$el.on("mouseenter", mouseEnterHandler);
|
2018-12-22 20:57:09 +01:00
|
|
|
}
|
2018-10-06 22:00:43 +02:00
|
|
|
|
2024-12-21 14:38:25 +02:00
|
|
|
async function mouseEnterHandler(this: HTMLElement) {
|
2018-12-22 20:57:09 +01:00
|
|
|
const $link = $(this);
|
2018-07-28 17:59:55 +02:00
|
|
|
|
2023-07-25 22:27:15 +02:00
|
|
|
if ($link.hasClass("no-tooltip-preview") || $link.hasClass("disabled")) {
|
2018-12-22 20:57:09 +01:00
|
|
|
return;
|
2023-07-25 22:27:15 +02:00
|
|
|
} else if ($link.closest(".ck-link-actions").length) {
|
|
|
|
// this is to avoid showing tooltip from inside the CKEditor link editor dialog
|
|
|
|
return;
|
|
|
|
} else if ($link.closest(".note-tooltip").length) {
|
|
|
|
// don't show tooltip for links within tooltip
|
2018-12-22 20:57:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-26 22:29:14 -04:00
|
|
|
|
2023-05-07 21:18:21 +02:00
|
|
|
const url = $link.attr("href") || $link.attr("data-href");
|
|
|
|
const { notePath, noteId, viewScope } = linkService.parseNavigationStateFromUrl(url);
|
2018-03-26 22:29:14 -04:00
|
|
|
|
2025-01-07 13:40:12 +02:00
|
|
|
if (url?.startsWith("#fnref")) {
|
|
|
|
// The "^" symbol from footnotes within text notes, doesn't require a tooltip.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!notePath || !noteId || viewScope?.viewMode !== "default") {
|
2018-12-22 20:57:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-11-06 19:35:42 +01:00
|
|
|
|
2023-07-25 22:27:15 +02:00
|
|
|
const linkId = $link.attr("data-link-id") || `link-${Math.floor(Math.random() * 1000000)}`;
|
|
|
|
$link.attr("data-link-id", linkId);
|
|
|
|
|
|
|
|
if ($(`.${linkId}`).is(":visible")) {
|
|
|
|
// tooltip is already open for this link
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-07 13:27:19 +02:00
|
|
|
let renderPromise;
|
|
|
|
if (url?.startsWith("#fn")) {
|
|
|
|
renderPromise = renderFootnote($link, url);
|
|
|
|
} else {
|
2025-01-09 18:07:02 +02:00
|
|
|
renderPromise = renderTooltip(await froca.getNote(noteId));
|
2025-01-07 13:27:19 +02:00
|
|
|
}
|
2023-07-25 22:27:15 +02:00
|
|
|
|
|
|
|
const [content] = await Promise.all([
|
2025-01-07 13:27:19 +02:00
|
|
|
renderPromise,
|
2023-07-25 22:27:15 +02:00
|
|
|
// to reduce flicker due to accidental mouseover, cursor must stay for a bit over the link for tooltip to appear
|
2025-01-09 18:07:02 +02:00
|
|
|
new Promise((res) => setTimeout(res, 500))
|
2023-07-25 22:27:15 +02:00
|
|
|
]);
|
2020-08-15 21:24:17 +02:00
|
|
|
|
2024-12-21 14:38:25 +02:00
|
|
|
if (!content || utils.isHtmlEmpty(content)) {
|
2020-08-15 21:24:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
const html = `<div class="note-tooltip-content">${content}</div>`;
|
2025-01-09 18:07:02 +02:00
|
|
|
const tooltipClass = "tooltip-" + Math.floor(Math.random() * 999_999_999);
|
2018-11-14 14:52:05 +01:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
// 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
|
2023-09-14 00:40:49 +02:00
|
|
|
if ($(this).filter(":hover").length > 0) {
|
2018-12-22 20:57:09 +01:00
|
|
|
$(this).tooltip({
|
2025-01-09 18:07:02 +02:00
|
|
|
container: "body",
|
2022-07-15 23:35:17 +02:00
|
|
|
// https://github.com/zadam/trilium/issues/2794 https://github.com/zadam/trilium/issues/2988
|
|
|
|
// with bottom this flickering happens a bit less
|
2025-01-09 18:07:02 +02:00
|
|
|
placement: "bottom",
|
|
|
|
trigger: "manual",
|
2024-12-21 14:38:25 +02:00
|
|
|
//TODO: boundary No longer applicable?
|
|
|
|
//boundary: 'window',
|
2018-12-22 20:57:09 +01:00
|
|
|
title: html,
|
2019-04-20 09:39:39 +02:00
|
|
|
html: true,
|
2023-09-14 00:40:49 +02:00
|
|
|
template: `<div class="tooltip note-tooltip ${tooltipClass}" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>`,
|
2023-07-25 22:27:15 +02:00
|
|
|
sanitize: false,
|
|
|
|
customClass: linkId
|
2018-12-22 20:57:09 +01:00
|
|
|
});
|
|
|
|
|
2025-01-22 22:24:42 +02:00
|
|
|
dismissAllTooltips();
|
2025-01-09 18:07:02 +02:00
|
|
|
$(this).tooltip("show");
|
2025-01-07 12:44:30 +02:00
|
|
|
|
2024-08-31 23:58:26 +03:00
|
|
|
// Dismiss the tooltip immediately if a link was clicked inside the tooltip.
|
|
|
|
$(`.${tooltipClass} a`).on("click", (e) => {
|
2025-01-22 22:24:42 +02:00
|
|
|
dismissAllTooltips();
|
2024-08-31 23:58:26 +03:00
|
|
|
});
|
2018-12-22 20:57:09 +01:00
|
|
|
|
2023-09-14 00:40:49 +02:00
|
|
|
// the purpose of the code below is to:
|
|
|
|
// - allow user to go from hovering the link to hovering the tooltip to be able to scroll,
|
|
|
|
// click on links within tooltip etc. without tooltip disappearing
|
|
|
|
// - once the user moves the cursor away from both link and the tooltip, hide the tooltip
|
|
|
|
const checkTooltip = () => {
|
|
|
|
if (!$(this).filter(":hover").length && !$(`.${linkId}:hover`).length) {
|
2023-07-25 22:27:15 +02:00
|
|
|
// cursor is neither over the link nor over the tooltip, user likely is not interested
|
2025-01-22 22:24:42 +02:00
|
|
|
dismissAllTooltips();
|
2023-09-14 00:40:49 +02:00
|
|
|
} else {
|
|
|
|
setTimeout(checkTooltip, 1000);
|
2023-07-25 22:27:15 +02:00
|
|
|
}
|
2025-01-09 18:07:02 +02:00
|
|
|
};
|
2023-09-14 00:40:49 +02:00
|
|
|
|
|
|
|
setTimeout(checkTooltip, 1000);
|
2023-07-25 22:27:15 +02:00
|
|
|
}
|
2018-03-26 22:29:14 -04:00
|
|
|
}
|
|
|
|
|
2024-12-21 14:38:25 +02:00
|
|
|
async function renderTooltip(note: FNote | null) {
|
2023-06-05 16:26:05 +02:00
|
|
|
if (!note) {
|
2025-01-07 12:44:30 +02:00
|
|
|
return `<div>${t("note_tooltip.note-has-been-deleted")}</div>`;
|
2020-03-23 16:39:03 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId;
|
|
|
|
const bestNotePath = note.getBestNotePathString(hoistedNoteId);
|
2020-09-13 22:23:03 +02:00
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
if (!bestNotePath) {
|
2020-09-13 22:23:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-21 14:38:25 +02:00
|
|
|
const noteTitleWithPathAsSuffix = await treeService.getNoteTitleWithPathAsSuffix(bestNotePath);
|
|
|
|
let content = "";
|
|
|
|
if (noteTitleWithPathAsSuffix) {
|
2025-01-09 18:07:02 +02:00
|
|
|
content = `<h5 class="note-tooltip-title">${noteTitleWithPathAsSuffix.prop("outerHTML")}</h5>`;
|
2024-12-21 14:38:25 +02:00
|
|
|
}
|
2020-01-25 14:37:12 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const { $renderedAttributes } = await attributeRenderer.renderNormalAttributes(note);
|
2020-10-29 21:06:30 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const { $renderedContent } = await contentRenderer.getRenderedContent(note, {
|
2020-12-03 22:54:24 +01:00
|
|
|
tooltip: true,
|
|
|
|
trim: true
|
|
|
|
});
|
2020-10-29 21:06:30 +01:00
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
content = `${content}<div class="note-tooltip-attributes">${$renderedAttributes[0].outerHTML}</div>${$renderedContent[0].outerHTML}`;
|
2018-08-22 15:31:36 +02:00
|
|
|
|
2018-11-06 19:35:42 +01:00
|
|
|
return content;
|
2018-08-22 15:31:36 +02:00
|
|
|
}
|
|
|
|
|
2025-01-07 13:27:19 +02:00
|
|
|
function renderFootnote($link: JQuery<HTMLElement>, url: string) {
|
|
|
|
// A footnote text reference
|
|
|
|
const footnoteRef = url.substring(3);
|
|
|
|
const $footnoteContent = $link
|
2025-01-09 18:07:02 +02:00
|
|
|
.closest(".ck-content") // find the parent CK content
|
|
|
|
.find("> .footnote-section") // find the footnote section
|
|
|
|
.find(`a[href="#fnref${footnoteRef}"]`) // find the footnote link
|
|
|
|
.closest(".footnote-item") // find the parent container of the footnote
|
|
|
|
.find(".footnote-content"); // find the actual text content of the footnote
|
2025-01-07 13:27:19 +02:00
|
|
|
|
|
|
|
return $footnoteContent.html() || "";
|
|
|
|
}
|
|
|
|
|
2018-03-26 22:29:14 -04:00
|
|
|
export default {
|
2018-12-22 20:57:09 +01:00
|
|
|
setupGlobalTooltip,
|
2025-01-22 22:24:42 +02:00
|
|
|
setupElementTooltip,
|
|
|
|
dismissAllTooltips
|
2025-01-09 18:07:02 +02:00
|
|
|
};
|