2019-12-31 02:32:45 +08:00
|
|
|
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);
|
|
|
|
|
2020-02-25 05:46:27 +08:00
|
|
|
let $rendered;
|
2019-12-31 02:32:45 +08:00
|
|
|
|
|
|
|
if (type === 'text') {
|
|
|
|
const fullNote = await server.get('notes/' + note.noteId);
|
|
|
|
|
2020-02-25 05:55:12 +08:00
|
|
|
$rendered = $("<div>").html(fullNote.content);
|
2019-12-31 02:32:45 +08:00
|
|
|
}
|
|
|
|
else if (type === 'code') {
|
|
|
|
const fullNote = await server.get('notes/' + note.noteId);
|
|
|
|
|
2020-02-25 05:46:27 +08:00
|
|
|
$rendered = $("<pre>").text(fullNote.content);
|
2019-12-31 02:32:45 +08:00
|
|
|
}
|
|
|
|
else if (type === 'image') {
|
2020-02-25 05:46:27 +08:00
|
|
|
$rendered = $("<img>").attr("src", `api/images/${note.noteId}/${note.title}`);
|
2019-12-31 02:32:45 +08:00
|
|
|
}
|
|
|
|
else if (type === 'file') {
|
|
|
|
function getFileUrl() {
|
|
|
|
return utils.getUrlForDownload("api/notes/" + note.noteId + "/download");
|
|
|
|
}
|
|
|
|
|
|
|
|
const $downloadButton = $('<button class="file-download btn btn-primary" type="button">Download</button>');
|
|
|
|
const $openButton = $('<button class="file-open btn btn-primary" type="button">Open</button>');
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2020-02-25 05:46:27 +08:00
|
|
|
$rendered = $('<div>')
|
2019-12-31 02:32:45 +08:00
|
|
|
.append($downloadButton)
|
|
|
|
.append(' ')
|
|
|
|
.append($openButton);
|
|
|
|
}
|
|
|
|
else if (type === 'render') {
|
2020-02-25 05:55:12 +08:00
|
|
|
$rendered = $('<div>');
|
2019-12-31 02:32:45 +08:00
|
|
|
|
2020-02-25 05:55:12 +08:00
|
|
|
await renderService.render(note, $rendered, this.ctx);
|
2019-12-31 02:32:45 +08:00
|
|
|
}
|
|
|
|
else if (type === 'protected-session') {
|
|
|
|
const $button = $(`<button class="btn btn-sm"><span class="bx bx-log-in"></span> Enter protected session</button>`)
|
|
|
|
.on('click', protectedSessionService.enterProtectedSession);
|
|
|
|
|
2020-02-25 05:46:27 +08:00
|
|
|
$rendered = $("<div>")
|
2019-12-31 02:32:45 +08:00
|
|
|
.append("<div>This note is protected and to access it you need to enter password.</div>")
|
|
|
|
.append("<br/>")
|
|
|
|
.append($button);
|
|
|
|
}
|
|
|
|
else {
|
2020-02-25 05:46:27 +08:00
|
|
|
$rendered = $("<em>Content of this note cannot be displayed in the book format</em>");
|
|
|
|
}
|
|
|
|
|
2020-02-25 17:47:49 +08:00
|
|
|
$rendered.addClass(await note.getCssClass());
|
2019-12-31 02:32:45 +08:00
|
|
|
|
|
|
|
return {
|
2020-02-25 05:46:27 +08:00
|
|
|
renderedContent: $rendered,
|
2019-12-31 02:32:45 +08:00
|
|
|
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
|
|
|
|
};
|