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";
|
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
|
2023-07-25 22:27:15 +02:00
|
|
|
$(document).on("click", e => {
|
|
|
|
if ($(e.target).closest(".note-tooltip").length) {
|
|
|
|
// click within the tooltip shouldn't close it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('.note-tooltip').remove();
|
|
|
|
});
|
2018-12-22 20:57:09 +01:00
|
|
|
}
|
2018-08-15 18:22:02 +02:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
function setupElementTooltip($el) {
|
|
|
|
$el.on('mouseenter', mouseEnterHandler);
|
|
|
|
}
|
2018-10-06 22:00:43 +02:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
async function mouseEnterHandler() {
|
|
|
|
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
|
|
|
|
2023-04-11 17:45:51 +02:00
|
|
|
if (!notePath || 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;
|
|
|
|
}
|
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
const note = await froca.getNote(noteId);
|
2023-07-25 22:27:15 +02:00
|
|
|
|
|
|
|
const [content] = await Promise.all([
|
|
|
|
renderTooltip(note),
|
|
|
|
// to reduce flicker due to accidental mouseover, cursor must stay for a bit over the link for tooltip to appear
|
|
|
|
new Promise(res => setTimeout(res, 500))
|
|
|
|
]);
|
2020-08-15 21:24:17 +02:00
|
|
|
|
|
|
|
if (utils.isHtmlEmpty(content)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
const html = `<div class="note-tooltip-content">${content}</div>`;
|
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
|
|
|
|
if ($(this).is(":hover")) {
|
|
|
|
$(this).tooltip({
|
|
|
|
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
|
|
|
|
placement: 'bottom',
|
2018-12-22 20:57:09 +01:00
|
|
|
trigger: 'manual',
|
|
|
|
boundary: 'window',
|
|
|
|
title: html,
|
2019-04-20 09:39:39 +02:00
|
|
|
html: true,
|
2020-04-08 21:38:11 +02:00
|
|
|
template: '<div class="tooltip note-tooltip" 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
|
|
|
});
|
|
|
|
|
|
|
|
$(this).tooltip('show');
|
|
|
|
|
2023-07-25 22:27:15 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
if (!$(this).is(":hover") && !$(`.${linkId}`).is(":hover")) {
|
|
|
|
// cursor is neither over the link nor over the tooltip, user likely is not interested
|
|
|
|
$(this).tooltip('dispose');
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
}
|
2018-03-26 22:29:14 -04:00
|
|
|
}
|
|
|
|
|
2020-12-03 22:54:24 +01:00
|
|
|
async function renderTooltip(note) {
|
2023-06-05 16:26:05 +02:00
|
|
|
if (!note) {
|
2020-03-23 16:39:03 +01:00
|
|
|
return '<div>Note has been deleted.</div>';
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
let content = `<h5 class="note-tooltip-title">${(await treeService.getNoteTitleWithPathAsSuffix(bestNotePath)).prop('outerHTML')}</h5>`;
|
2020-01-25 14:37:12 +01:00
|
|
|
|
2020-12-03 22:54:24 +01:00
|
|
|
const {$renderedAttributes} = await attributeRenderer.renderNormalAttributes(note);
|
2020-10-29 21:06:30 +01:00
|
|
|
|
2023-05-20 23:46:45 +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
|
|
|
}
|
|
|
|
|
2018-03-26 22:29:14 -04:00
|
|
|
export default {
|
2018-12-22 20:57:09 +01:00
|
|
|
setupGlobalTooltip,
|
|
|
|
setupElementTooltip
|
2020-08-06 00:06:42 +02:00
|
|
|
}
|