trilium/src/public/javascripts/services/note_detail.js

346 lines
8.8 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
import noteTypeService from './note_type.js';
import protectedSessionService from './protected_session.js';
import protectedSessionHolder from './protected_session_holder.js';
import utils from './utils.js';
import server from './server.js';
import messagingService from "./messaging.js";
import bundleService from "./bundle.js";
2018-03-26 09:29:35 +08:00
import infoService from "./info.js";
2018-03-26 11:25:17 +08:00
import treeCache from "./tree_cache.js";
import NoteFull from "../entities/note_full.js";
import noteDetailCode from './note_detail_code.js';
import noteDetailText from './note_detail_text.js';
const $noteTitle = $("#note-title");
const $noteDetailComponents = $(".note-detail-component");
const $noteDetailSearch = $('#note-detail-search');
const $noteDetailRender = $('#note-detail-render');
const $noteDetailAttachment = $('#note-detail-attachment');
const $protectButton = $("#protect-button");
const $unprotectButton = $("#unprotect-button");
const $noteDetailWrapper = $("#note-detail-wrapper");
const $noteIdDisplay = $("#note-id-display");
const $labelList = $("#label-list");
const $labelListInner = $("#label-list-inner");
const $attachmentFileName = $("#attachment-filename");
const $attachmentFileType = $("#attachment-filetype");
const $attachmentFileSize = $("#attachment-filesize");
const $attachmentDownload = $("#attachment-download");
const $attachmentOpen = $("#attachment-open");
const $searchString = $("#search-string");
let currentNote = null;
let noteChangeDisabled = false;
let isNoteChanged = false;
function getCurrentNote() {
return currentNote;
}
function getCurrentNoteId() {
2018-03-26 11:25:17 +08:00
return currentNote ? currentNote.noteId : null;
}
function getCurrentNoteType() {
const currentNote = getCurrentNote();
return currentNote ? currentNote.type : null;
}
function noteChanged() {
if (noteChangeDisabled) {
return;
}
isNoteChanged = true;
}
2017-11-05 05:54:27 +08:00
async function reload() {
// no saving here
2017-11-05 05:54:27 +08:00
await loadNoteToEditor(getCurrentNoteId());
}
2017-11-05 05:54:27 +08:00
async function switchToNote(noteId) {
if (getCurrentNoteId() !== noteId) {
await saveNoteIfChanged();
2017-11-05 05:54:27 +08:00
await loadNoteToEditor(noteId);
2017-11-05 05:54:27 +08:00
}
}
2017-11-05 05:54:27 +08:00
async function saveNoteIfChanged() {
if (!isNoteChanged) {
return;
2017-11-05 05:54:27 +08:00
}
const note = getCurrentNote();
2017-11-05 05:54:27 +08:00
updateNoteFromInputs(note);
2017-11-05 05:54:27 +08:00
await saveNoteToServer(note);
2018-03-26 11:25:17 +08:00
if (note.isProtected) {
protectedSessionHolder.touchProtectedSession();
}
}
function updateNoteFromInputs(note) {
2018-03-26 11:25:17 +08:00
if (note.type === 'text') {
note.content = noteDetailText.getContent();
}
2018-03-26 11:25:17 +08:00
else if (note.type === 'code') {
note.content = noteDetailCode.getContent();
}
2018-03-26 11:25:17 +08:00
else if (note.type === 'search') {
note.content = JSON.stringify({
searchString: $searchString.val()
});
}
2018-03-26 11:25:17 +08:00
else if (note.type === 'render' || note.type === 'file') {
// nothing
}
else {
2018-03-26 11:25:17 +08:00
infoService.throwError("Unrecognized type: " + note.type);
}
const title = $noteTitle.val();
2017-11-05 05:54:27 +08:00
2018-03-26 11:25:17 +08:00
note.title = title;
2017-11-05 05:54:27 +08:00
2018-03-26 11:25:17 +08:00
treeService.setNoteTitle(note.noteId, title);
}
2017-11-05 05:54:27 +08:00
async function saveNoteToServer(note) {
2018-03-26 11:25:17 +08:00
await server.put('notes/' + note.noteId, note);
isNoteChanged = false;
2017-11-05 05:54:27 +08:00
2018-03-26 09:29:35 +08:00
infoService.showMessage("Saved!");
}
function setNoteBackgroundIfProtected(note) {
2018-03-26 11:25:17 +08:00
const isProtected = !!note.isProtected;
$noteDetailWrapper.toggleClass("protected", isProtected);
$protectButton.toggle(!isProtected);
$unprotectButton.toggle(isProtected);
}
2017-11-05 05:54:27 +08:00
let isNewNoteCreated = false;
2017-11-05 05:54:27 +08:00
function newNoteCreated() {
isNewNoteCreated = true;
}
2017-11-05 05:54:27 +08:00
async function showRenderNote() {
$noteDetailRender.show();
2017-11-05 05:54:27 +08:00
const bundle = await server.get('script/bundle/' + getCurrentNoteId());
2017-11-05 05:54:27 +08:00
$noteDetailRender.html(bundle.html);
await bundleService.executeBundle(bundle);
}
2017-11-05 05:54:27 +08:00
async function showFileNote() {
const labels = await server.get('notes/' + currentNote.noteId + '/labels');
const labelMap = utils.toObject(labels, l => [l.name, l.value]);
$noteDetailAttachment.show();
$attachmentFileName.text(labelMap.original_file_name);
$attachmentFileSize.text(labelMap.file_size + " bytes");
$attachmentFileType.text(currentNote.mime);
}
function showSearchNote() {
$noteDetailSearch.show();
try {
const json = JSON.parse(currentNote.content);
$searchString.val(json.searchString);
}
catch (e) {
console.log(e);
$searchString.val('');
}
$searchString.on('input', noteChanged);
}
async function handleProtectedSession() {
await protectedSessionService.ensureProtectedSession(currentNote.isProtected, false);
if (currentNote.isProtected) {
protectedSessionHolder.touchProtectedSession();
}
// this might be important if we focused on protected note when not in protected note and we got a dialog
// to login, but we chose instead to come to another node - at that point the dialog is still visible and this will close it.
protectedSessionService.ensureDialogIsClosed();
}
async function loadNoteToEditor(noteId) {
currentNote = await loadNote(noteId);
if (isNewNoteCreated) {
isNewNoteCreated = false;
$noteTitle.focus().select();
}
2018-03-11 22:49:22 +08:00
$noteIdDisplay.html(noteId);
2018-03-11 22:49:22 +08:00
await handleProtectedSession();
2017-11-05 05:54:27 +08:00
$noteDetailWrapper.show();
2017-11-05 05:54:27 +08:00
noteChangeDisabled = true;
2017-11-05 05:54:27 +08:00
try {
$noteTitle.val(currentNote.title);
noteTypeService.setNoteType(currentNote.type);
noteTypeService.setNoteMime(currentNote.mime);
2017-11-05 05:54:27 +08:00
$noteDetailComponents.hide();
if (currentNote.type === 'render') {
await showRenderNote();
}
else if (currentNote.type === 'file') {
await showFileNote();
}
else if (currentNote.type === 'text') {
await noteDetailText.showTextNote();
}
else if (currentNote.type === 'code') {
await noteDetailCode.showCodeNote();
}
else if (currentNote.type === 'search') {
showSearchNote();
}
}
finally {
noteChangeDisabled = false;
}
setNoteBackgroundIfProtected(currentNote);
treeService.setBranchBackgroundBasedOnProtectedStatus(noteId);
2018-01-24 12:41:22 +08:00
// after loading new note make sure editor is scrolled to the top
$noteDetailWrapper.scrollTop(0);
2018-01-24 12:41:22 +08:00
await loadLabelList();
}
2018-03-07 13:17:18 +08:00
async function loadLabelList() {
const noteId = getCurrentNoteId();
const labels = await server.get('notes/' + noteId + '/labels');
2017-11-05 05:54:27 +08:00
$labelListInner.html('');
if (labels.length > 0) {
for (const attr of labels) {
$labelListInner.append(utils.formatLabel(attr) + " ");
}
2018-02-05 09:23:30 +08:00
$labelList.show();
2018-02-05 09:23:30 +08:00
}
else {
$labelList.hide();
}
}
2018-02-05 09:23:30 +08:00
async function loadNote(noteId) {
2018-03-26 11:25:17 +08:00
const row = await server.get('notes/' + noteId);
return new NoteFull(treeCache, row);
}
2018-02-05 09:23:30 +08:00
function focus() {
const note = getCurrentNote();
2018-02-05 09:23:30 +08:00
2018-03-26 11:25:17 +08:00
if (note.type === 'text') {
noteDetailText.focus();
2017-11-05 05:54:27 +08:00
}
2018-03-26 11:25:17 +08:00
else if (note.type === 'code') {
noteDetailCode.focus();
2017-11-05 05:54:27 +08:00
}
2018-03-26 11:25:17 +08:00
else if (note.type === 'render' || note.type === 'file' || note.type === 'search') {
// do nothing
2017-12-03 02:54:16 +08:00
}
else {
2018-03-26 11:25:17 +08:00
infoService.throwError('Unrecognized type: ' + note.type);
}
}
$attachmentDownload.click(() => utils.download(getAttachmentUrl()));
$attachmentOpen.click(() => {
if (utils.isElectron()) {
const open = require("open");
open(getAttachmentUrl());
}
else {
window.location.href = getAttachmentUrl();
}
});
function getAttachmentUrl() {
// electron needs absolute URL so we extract current host, port, protocol
return utils.getHost() + "/api/attachments/download/" + getCurrentNoteId()
+ "?protectedSessionId=" + encodeURIComponent(protectedSessionHolder.getProtectedSessionId());
}
messagingService.subscribeToMessages(syncData => {
if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === getCurrentNoteId())) {
2018-03-26 09:29:35 +08:00
infoService.showMessage('Reloading note because of background changes');
reload();
}
});
$(document).ready(() => {
$noteTitle.on('input', () => {
noteChanged();
const title = $noteTitle.val();
2017-11-05 05:54:27 +08:00
treeService.setNoteTitle(getCurrentNoteId(), title);
2017-11-05 05:54:27 +08:00
});
noteDetailText.focus();
});
// this makes sure that when user e.g. reloads the page or navigates away from the page, the note's content is saved
// this sends the request asynchronously and doesn't wait for result
$(window).on('beforeunload', saveNoteIfChanged);
setInterval(saveNoteIfChanged, 5000);
export default {
reload,
switchToNote,
updateNoteFromInputs,
saveNoteToServer,
setNoteBackgroundIfProtected,
loadNote,
getCurrentNote,
getCurrentNoteType,
getCurrentNoteId,
newNoteCreated,
focus,
loadLabelList,
saveNoteIfChanged,
noteChanged
};