Notes/src/public/app/services/note_tooltip.js

134 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-01-25 09:56:08 +01:00
import treeService from "./tree.js";
import linkService from "./link.js";
import treeCache from "./tree_cache.js";
import utils from "./utils.js";
import attributeRenderer from "./attribute_renderer.js";
import libraryLoader from "./library_loader.js";
function setupGlobalTooltip() {
$(document).on("mouseenter", "a", mouseEnterHandler);
$(document).on("mouseleave", "a", mouseLeaveHandler);
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());
}
function setupElementTooltip($el) {
$el.on('mouseenter', mouseEnterHandler);
$el.on('mouseleave', mouseLeaveHandler);
}
async function mouseEnterHandler() {
const $link = $(this);
2019-11-01 20:00:56 +01:00
if ($link.hasClass("no-tooltip-preview")
|| $link.hasClass("disabled")
|| $link.attr("data-action") === 'note-revision') {
return;
}
2018-11-06 19:35:42 +01:00
// this is to avoid showing tooltip from inside CKEditor link editor dialog
if ($link.closest(".ck-link-actions").length) {
return;
}
let notePath = linkService.getNotePathFromUrl($link.attr("href"));
if (!notePath) {
notePath = $link.attr("data-note-path");
}
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
const note = await treeCache.getNote(noteId);
const noteComplement = await treeCache.getNoteComplement(noteId);
2018-11-06 19:35:42 +01:00
2020-08-15 21:24:17 +02:00
const content = await renderTooltip(note, noteComplement);
if (utils.isHtmlEmpty(content)) {
return;
}
const html = '<div class="note-tooltip-content">' + content + '</div>';
// 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',
title: html,
2019-04-20 09:39:39 +02:00
html: true,
template: '<div class="tooltip note-tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>',
sanitize: false
});
$(this).tooltip('show');
}
}
function mouseLeaveHandler() {
$(this).tooltip('dispose');
}
2020-02-01 11:15:58 +01:00
async function renderTooltip(note, noteComplement) {
2020-03-23 16:39:03 +01:00
if (note.isDeleted) {
return '<div>Note has been deleted.</div>';
}
const someNotePath = treeService.getSomeNotePath(note);
2020-09-13 22:23:03 +02:00
if (!someNotePath) {
return;
}
let content = $("<h5>").text(await treeService.getNotePathTitle(someNotePath)).prop('outerHTML');
const attributes = note.getAttributes()
.filter(attr => !attr.isDefinition());
if (attributes.length > 0) {
content += '<div class="tooltip-attributes"><strong>Attributes: </strong> '
+ (await attributeRenderer.renderAttributes(attributes)).html()
+ '</div>'
}
if (note.type === 'text' && !utils.isHtmlEmpty(noteComplement.content)) {
const $content = $('<div class="ck-content">').append(noteComplement.content);
if ($content.find('span.math-tex').length > 0) {
await libraryLoader.requireLibrary(libraryLoader.KATEX);
renderMathInElement($content[0], {});
}
content += $content[0].outerHTML;
}
else if (note.type === 'code' && noteComplement.content && noteComplement.content.trim()) {
2018-11-14 08:36:19 +01:00
content += $("<pre>")
2020-02-01 11:15:58 +01:00
.text(noteComplement.content)
2018-11-14 08:36:19 +01:00
.prop('outerHTML');
}
else if (note.type === 'image') {
content += $("<img>")
2019-02-22 23:03:20 +01:00
.prop("src", `api/images/${note.noteId}/${note.title}`)
2018-11-14 08:36:19 +01:00
.prop('outerHTML');
}
// other types of notes don't have tooltip preview
2018-11-06 19:35:42 +01:00
return content;
}
export default {
setupGlobalTooltip,
setupElementTooltip
2020-08-06 00:06:42 +02:00
}