import renderService from "./render.js"; import protectedSessionService from "./protected_session.js"; import protectedSessionHolder from "./protected_session_holder.js"; import libraryLoader from "./library_loader.js"; import openService from "./open.js"; import froca from "./froca.js"; import utils from "./utils.js"; import linkService from "./link.js"; import treeService from "./tree.js"; import FNote from "../entities/fnote.js"; import FAttachment from "../entities/fattachment.js"; let idCounter = 1; /** * @param {FNote|FAttachment} entity * @param {object} options * @return {Promise<{type: string, $renderedContent: jQuery}>} */ async function getRenderedContent(entity, options = {}) { options = Object.assign({ trim: false, tooltip: false }, options); const type = getRenderingType(entity); // attachment supports only image and file/pdf/audio/video const $renderedContent = $('
Content of this note cannot be displayed
")); } if (entity instanceof FNote) { $renderedContent.addClass(entity.getCssClass()); } return { $renderedContent, type }; } async function renderText(note, options, $renderedContent) { // entity must be FNote const blob = await note.getBlob({preview: options.trim}); if (!utils.isHtmlEmpty(blob.content)) { $renderedContent.append($('").text(trim(blob.content, options.trim))); } function renderImage(entity, $renderedContent) { const sanitizedTitle = entity.title.replace(/[^a-z0-9-.]/gi, ""); let url; if (entity instanceof FNote) { url = `api/images/${entity.noteId}/${sanitizedTitle}?${entity.utcDateModified}`; } else if (entity instanceof FAttachment) { url = `api/attachments/${entity.attachmentId}/image/${sanitizedTitle}?${entity.utcDateModified}">`; } $renderedContent.append( $("") .attr("src", url) .css("max-width", "100%") ); } function renderFile(entity, type, $renderedContent) { let entityType, entityId; if (entity instanceof FNote) { entityType = 'notes'; entityId = entity.noteId; } else if (entity instanceof FAttachment) { entityType = 'attachments'; entityId = entity.attachmentId; } else { throw new Error(`Can't recognize entity type of '${entity}'`); } const $downloadButton = $(''); const $openButton = $(''); $downloadButton.on('click', () => openService.downloadFileNote(entity.noteId)); $openButton.on('click', () => openService.openNoteExternally(entity.noteId, entity.mime)); // open doesn't work for protected notes since it works through a browser which isn't in protected session $openButton.toggle(!entity.isProtected); const $content = $('
'); if (type === 'pdf') { const $pdfPreview = $(''); $pdfPreview.attr("src", openService.getUrlForDownload(`api/${entityType}/${entityId}/open`)); $content.append($pdfPreview); } else if (type === 'audio') { const $audioPreview = $('') .attr("src", openService.getUrlForStreaming(`api/${entityType}/${entityId}/open-partial`)) .attr("type", entity.mime) .css("width", "100%"); $content.append($audioPreview); } else if (type === 'video') { const $videoPreview = $('') .attr("src", openService.getUrlForDownload(`api/${entityType}/${entityId}/open-partial`)) .attr("type", entity.mime) .css("width", "100%"); $content.append($videoPreview); } $content.append( $('') .append($downloadButton) .append($openButton) ); $renderedContent.append($content); } async function renderMermaid(note, $renderedContent) { await libraryLoader.requireLibrary(libraryLoader.MERMAID); const blob = await note.getBlob(); const content = blob.content || ""; $renderedContent .css("display", "flex") .css("justify-content", "space-around"); const documentStyle = window.getComputedStyle(document.documentElement); const mermaidTheme = documentStyle.getPropertyValue('--mermaid-theme'); mermaid.mermaidAPI.initialize({startOnLoad: false, theme: mermaidTheme.trim(), securityLevel: 'antiscript'}); try { mermaid.mermaidAPI.render("in-mermaid-graph-" + idCounter++, content, content => $renderedContent.append($(content))); } catch (e) { const $error = $("The diagram could not displayed.
"); $renderedContent.append($error); } } async function renderCanvas(note, $renderedContent) { // make sure surrounding container has size of what is visible. Then image is shrinked to its boundaries $renderedContent.css({height: "100%", width: "100%"}); const blob = await note.getBlob(); const content = blob.content || ""; try { const placeHolderSVG = ""; const data = JSON.parse(content) const svg = data.svg || placeHolderSVG; /** * maxWidth: size down to 100% (full) width of container but do not enlarge! * height:auto to ensure that height scales with width */ $renderedContent.append($(svg).css({maxWidth: "100%", maxHeight: "100%", height: "auto", width: "auto"})); } catch (err) { console.error("error parsing content as JSON", content, err); $renderedContent.append($("").text("Error parsing content. Please check console.error() for more details.")); } } /** * @param {jQuery} $renderedContent * @param {FNote} note * @returns {Promise} */ async function renderChildrenList($renderedContent, note) { $renderedContent.css("padding", "10px"); $renderedContent.addClass("text-with-ellipsis"); let childNoteIds = note.getChildNoteIds(); if (childNoteIds.length > 10) { childNoteIds = childNoteIds.slice(0, 10); } // just load the first 10 child notes const childNotes = await froca.getNotes(childNoteIds); for (const childNote of childNotes) { $renderedContent.append(await linkService.createNoteLink(`${note.noteId}/${childNote.noteId}`, { showTooltip: false, showNoteIcon: true })); $renderedContent.append("
"); } } function trim(text, doTrim) { if (!doTrim) { return text; } else { return text.substr(0, Math.min(text.length, 2000)); } } function getRenderingType(entity) { let type = entity.type || entity.role; const mime = entity.mime; if (type === 'file' && mime === 'application/pdf') { type = 'pdf'; } else if (type === 'file' && mime.startsWith('audio/')) { type = 'audio'; } else if (type === 'file' && mime.startsWith('video/')) { type = 'video'; } if (entity.isProtected) { if (protectedSessionHolder.isProtectedSessionAvailable()) { protectedSessionHolder.touchProtectedSession(); } else { type = 'protectedSession'; } } return type; } export default { getRenderedContent };