import server from "./server.js"; import utils from "./utils.js"; import renderService from "./render.js"; import protectedSessionService from "./protected_session.js"; import protectedSessionHolder from "./protected_session_holder.js"; async function getRenderedContent(note) { const type = getRenderingType(note); let rendered; if (type === 'text') { const fullNote = await server.get('notes/' + note.noteId); const $content = $("
").text(fullNote.content); } else if (type === 'image') { rendered = $("").attr("src", `api/images/${note.noteId}/${note.title}`); } else if (type === 'file') { function getFileUrl() { return utils.getUrlForDownload("api/notes/" + note.noteId + "/download"); } const $downloadButton = $(''); const $openButton = $(''); $downloadButton.on('click', () => utils.download(getFileUrl())); $openButton.on('click', () => { if (utils.isElectron()) { const open = require("open"); open(getFileUrl(), {url: true}); } else { window.location.href = getFileUrl(); } }); // open doesn't work for protected notes since it works through browser which isn't in protected session $openButton.toggle(!note.isProtected); rendered = $('') .append($downloadButton) .append(' ') .append($openButton); } else if (type === 'render') { const $el = $(''); await renderService.render(note, $el, this.ctx); rendered = $el; } else if (type === 'protected-session') { const $button = $(``) .on('click', protectedSessionService.enterProtectedSession); rendered = $("") .append("This note is protected and to access it you need to enter password.") .append("
") .append($button); } else { rendered = "Content of this note cannot be displayed in the book format"; } return { renderedContent: rendered, type }; } function getRenderingType(note) { let type = note.type; if (note.isProtected) { if (protectedSessionHolder.isProtectedSessionAvailable()) { protectedSessionHolder.touchProtectedSession(); } else { type = 'protected-session'; } } return type; } export default { getRenderedContent };