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

421 lines
11 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";
const $noteTitle = $("#note-title");
const $noteDetail = $('#note-detail');
const $noteDetailCode = $('#note-detail-code');
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");
const $executeScriptButton = $("#execute-script-button");
let editor = null;
let codeEditor = null;
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 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') {
let content = editor.getData();
// if content is only tags/whitespace (typically <p>&nbsp;</p>), then just make it empty
// this is important when setting new note to code
if (jQuery(content).text().trim() === '' && !content.includes("<img")) {
content = '';
}
2018-03-26 11:25:17 +08:00
note.content = content;
}
2018-03-26 11:25:17 +08:00
else if (note.type === 'code') {
note.content = codeEditor.getValue();
}
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 setContent(content) {
2018-03-26 11:25:17 +08:00
if (currentNote.type === 'text') {
if (!editor) {
await utils.requireLibrary(utils.CKEDITOR);
2017-11-05 05:54:27 +08:00
editor = await BalloonEditor.create($noteDetail[0], {});
2017-11-05 05:54:27 +08:00
editor.document.on('change', noteChanged);
}
2017-11-05 05:54:27 +08:00
// temporary workaround for https://github.com/ckeditor/ckeditor5-enter/issues/49
editor.setData(content ? content : "<p></p>");
$noteDetail.show();
}
2018-03-26 11:25:17 +08:00
else if (currentNote.type === 'code') {
if (!codeEditor) {
await utils.requireLibrary(utils.CODE_MIRROR);
CodeMirror.keyMap.default["Shift-Tab"] = "indentLess";
CodeMirror.keyMap.default["Tab"] = "indentMore";
CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js';
codeEditor = CodeMirror($("#note-detail-code")[0], {
value: "",
viewportMargin: Infinity,
indentUnit: 4,
matchBrackets: true,
matchTags: { bothTags: true },
highlightSelectionMatches: { showToken: /\w/, annotateScrollbar: false },
lint: true,
gutters: ["CodeMirror-lint-markers"],
lineNumbers: true
});
codeEditor.on('change', noteChanged);
}
$noteDetailCode.show();
// this needs to happen after the element is shown, otherwise the editor won't be refresheds
codeEditor.setValue(content);
2018-03-26 11:25:17 +08:00
const info = CodeMirror.findModeByMIME(currentNote.mime);
if (info) {
codeEditor.setOption("mode", info.mime);
CodeMirror.autoLoadMode(codeEditor, info.mode);
}
codeEditor.refresh();
}
2018-03-26 11:25:17 +08:00
else if (currentNote.type === 'search') {
$noteDetailSearch.show();
try {
const json = JSON.parse(content);
$searchString.val(json.searchString);
}
catch (e) {
console.log(e);
$searchString.val('');
}
$searchString.on('input', noteChanged);
}
}
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
2018-03-26 11:25:17 +08:00
await protectedSessionService.ensureProtectedSession(currentNote.isProtected, false);
2018-03-11 22:49:22 +08:00
2018-03-26 11:25:17 +08:00
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();
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
2018-03-26 11:25:17 +08:00
$noteTitle.val(currentNote.title);
2018-03-26 11:25:17 +08:00
noteTypeService.setNoteType(currentNote.type);
noteTypeService.setNoteMime(currentNote.mime);
2017-11-05 05:54:27 +08:00
$noteDetail.hide();
$noteDetailSearch.hide();
$noteDetailCode.hide();
$noteDetailRender.html('').hide();
$noteDetailAttachment.hide();
2018-03-26 11:25:17 +08:00
if (currentNote.type === 'render') {
$noteDetailRender.show();
const bundle = await server.get('script/bundle/' + getCurrentNoteId());
2017-11-05 05:54:27 +08:00
$noteDetailRender.html(bundle.html);
2017-11-05 05:54:27 +08:00
bundleService.executeBundle(bundle);
}
2018-03-26 11:25:17 +08:00
else if (currentNote.type === 'file') {
const labels = await server.get('notes/' + currentNote.noteId + '/labels');
const labelMap = utils.toObject(labels, l => [l.name, l.value]);
$noteDetailAttachment.show();
2018-03-26 11:25:17 +08:00
$attachmentFileName.text(labelMap.original_file_name);
$attachmentFileSize.text(labelMap.file_size + " bytes");
$attachmentFileType.text(currentNote.mime);
}
else {
2018-03-26 11:25:17 +08:00
await setContent(currentNote.content);
}
noteChangeDisabled = false;
2018-02-15 12:31:20 +08:00
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
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 getEditor() {
return editor;
}
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') {
$noteDetail.focus();
2017-11-05 05:54:27 +08:00
}
2018-03-26 11:25:17 +08:00
else if (note.type === 'code') {
codeEditor.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);
}
}
function getCurrentNoteType() {
const currentNote = getCurrentNote();
2018-03-26 11:25:17 +08:00
return currentNote ? currentNote.type : null;
}
async function executeCurrentNote() {
if (getCurrentNoteType() === 'code') {
// make sure note is saved so we load latest changes
await saveNoteIfChanged();
2018-03-26 11:25:17 +08:00
if (currentNote.mime.endsWith("env=frontend")) {
const bundle = await server.get('script/bundle/' + getCurrentNoteId());
bundleService.executeBundle(bundle);
}
2018-03-26 11:25:17 +08:00
if (currentNote.mime.endsWith("env=backend")) {
await server.post('script/run/' + getCurrentNoteId());
}
2018-03-26 09:29:35 +08:00
infoService.showMessage("Note executed");
}
}
$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
});
// so that tab jumps from note title (which has tabindex 1)
$noteDetail.attr("tabindex", 2);
});
$(document).bind('keydown', "ctrl+return", executeCurrentNote);
$executeScriptButton.click(executeCurrentNote());
setInterval(saveNoteIfChanged, 5000);
export default {
reload,
switchToNote,
saveNoteIfChanged,
updateNoteFromInputs,
saveNoteToServer,
setNoteBackgroundIfProtected,
loadNote,
getCurrentNote,
getCurrentNoteType,
getCurrentNoteId,
newNoteCreated,
getEditor,
focus,
executeCurrentNote,
loadLabelList,
setContent
};