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";
|
2020-12-03 22:54:24 +01:00
|
|
|
import noteContentRenderer from "./note_content_renderer.js";
|
2018-03-26 22:29:14 -04:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
function setupGlobalTooltip() {
|
|
|
|
$(document).on("mouseenter", "a", mouseEnterHandler);
|
|
|
|
$(document).on("mouseleave", "a", mouseLeaveHandler);
|
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
|
|
|
|
$(document).on("click", () => $('.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);
|
|
|
|
$el.on('mouseleave', mouseLeaveHandler);
|
|
|
|
}
|
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
|
|
|
|
2019-11-01 20:00:56 +01:00
|
|
|
if ($link.hasClass("no-tooltip-preview")
|
2022-06-16 19:29:18 +02:00
|
|
|
|| $link.hasClass("disabled")) {
|
2018-12-22 20:57:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-11-06 19:35:42 +01:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
// this is to avoid showing tooltip from inside CKEditor link editor dialog
|
|
|
|
if ($link.closest(".ck-link-actions").length) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-26 22:29:14 -04:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
let notePath = linkService.getNotePathFromUrl($link.attr("href"));
|
2018-03-26 22:29:14 -04:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
if (!notePath) {
|
|
|
|
notePath = $link.attr("data-note-path");
|
|
|
|
}
|
2018-08-22 15:31:36 +02:00
|
|
|
|
2018-12-22 20:57:09 +01:00
|
|
|
if (!notePath) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-06 19:35:42 +01:00
|
|
|
|
2020-01-25 09:56:08 +01:00
|
|
|
const noteId = treeService.getNoteIdFromNotePath(notePath);
|
2018-11-06 19:35:42 +01:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
const note = await froca.getNote(noteId);
|
2020-12-03 22:54:24 +01:00
|
|
|
const content = await renderTooltip(note);
|
2020-08-15 21:24:17 +02:00
|
|
|
|
|
|
|
if (utils.isHtmlEmpty(content)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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({
|
|
|
|
delay: {"show": 300, "hide": 100},
|
|
|
|
container: 'body',
|
|
|
|
placement: 'auto',
|
|
|
|
trigger: 'manual',
|
|
|
|
boundary: 'window',
|
2022-04-16 15:51:30 +02:00
|
|
|
offset: "0, 20", // workaround for https://github.com/zadam/trilium/issues/2794
|
2018-12-22 20:57:09 +01:00
|
|
|
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>',
|
|
|
|
sanitize: false
|
2018-12-22 20:57:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$(this).tooltip('show');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mouseLeaveHandler() {
|
|
|
|
$(this).tooltip('dispose');
|
2018-03-26 22:29:14 -04:00
|
|
|
}
|
|
|
|
|
2020-12-03 22:54:24 +01:00
|
|
|
async function renderTooltip(note) {
|
2020-03-23 16:39:03 +01:00
|
|
|
if (note.isDeleted) {
|
|
|
|
return '<div>Note has been deleted.</div>';
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:45:07 +02:00
|
|
|
const someNotePath = treeService.getSomeNotePath(note);
|
2020-09-13 22:23:03 +02:00
|
|
|
|
|
|
|
if (!someNotePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:48:06 +01:00
|
|
|
let content = '<h5 class="note-tooltip-title">' + (await treeService.getNoteTitleWithPathAsSuffix(someNotePath)).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
|
|
|
|
2020-12-03 22:54:24 +01:00
|
|
|
const {$renderedContent} = await noteContentRenderer.getRenderedContent(note, {
|
|
|
|
tooltip: true,
|
|
|
|
trim: true
|
|
|
|
});
|
2020-10-29 21:06:30 +01:00
|
|
|
|
2020-12-03 22:54:24 +01:00
|
|
|
content = content
|
2021-01-21 21:48:06 +01:00
|
|
|
+ '<div class="note-tooltip-attributes">' + $renderedAttributes[0].outerHTML + '</div>'
|
2020-12-03 22:54:24 +01:00
|
|
|
+ $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
|
|
|
}
|