import noteDetailService from "./note_detail.js"; import treeUtils from "./tree_utils.js"; import linkService from "./link.js"; import server from "./server.js"; function setupGlobalTooltip() { $(document).on("mouseenter", "a", mouseEnterHandler); $(document).on("mouseleave", "a", mouseLeaveHandler); // close any tooltip after click, this fixes the problem that sometimes tooltips remained on the screen $(document).on("click", () => $('.tooltip').remove()); } function setupElementTooltip($el) { $el.on('mouseenter', mouseEnterHandler); $el.on('mouseleave', mouseLeaveHandler); } async function mouseEnterHandler() { const $link = $(this); if ($link.hasClass("no-tooltip-preview") || $link.hasClass("disabled")) { return; } // 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; } const noteId = treeUtils.getNoteIdFromNotePath(notePath); const notePromise = noteDetailService.loadNote(noteId); const attributePromise = server.get('notes/' + noteId + '/attributes'); const [note, attributes] = await Promise.all([notePromise, attributePromise]); const html = await renderTooltip(note, attributes); // 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, html: true }); $(this).tooltip('show'); } } function mouseLeaveHandler() { $(this).tooltip('dispose'); } async function renderTooltip(note, attributes) { let content = ''; const promoted = attributes.filter(attr => (attr.type === 'label-definition' || attr.type === 'relation-definition') && !attr.name.startsWith("child:") && attr.value.isPromoted); if (promoted.length > 0) { const $table = $("
").text(valueAttr.value); } else if (valueType === 'relation' && valueAttr.value) { $value = $(" | ").append(await linkService.createNoteLink(valueAttr.value)); } const $row = $(" |
").text(definitionAttr.name))
.append($value);
$table.append($row);
}
}
content += $table.prop('outerHTML');
}
if (note.type === 'text') {
// surround with for a case when note's content is pure text (e.g. "[protected]") which
// then fails the jquery non-empty text test
content += ' ' + note.noteContent.content + ' ';
}
else if (note.type === 'code') {
content += $("") .text(note.noteContent.content) .prop('outerHTML'); } else if (note.type === 'image') { content += $("") .prop("src", `/api/images/${note.noteId}/${note.title}`) .prop('outerHTML'); } // other types of notes don't have tooltip preview if (!$(content).text().trim() && note.type !== 'image') { return ""; } return content; } export default { setupGlobalTooltip, setupElementTooltip } |
---|