trilium/public/javascripts/note_editor.js

184 lines
4.4 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 05:54:27 +08:00
const noteEditor = (function() {
const noteTitleEl = $("#note-title");
const noteDetailEl = $('#note-detail');
const protectButton = $("#protect-button");
const unprotectButton = $("#unprotect-button");
2017-11-05 05:54:27 +08:00
const noteDetailWrapperEl = $("#note-detail-wrapper");
2017-12-02 23:37:12 +08:00
let editor = null;
2017-11-05 05:54:27 +08:00
let currentNote = null;
let noteChangeDisabled = false;
let isNoteChanged = false;
function getCurrentNote() {
return currentNote;
}
function getCurrentNoteId() {
return currentNote ? currentNote.detail.note_id : null;
}
function noteChanged() {
if (noteChangeDisabled) {
return;
}
isNoteChanged = true;
}
async function reload() {
// no saving here
await loadNoteToEditor(getCurrentNoteId());
}
async function switchToNote(noteId) {
if (getCurrentNoteId() !== noteId) {
await saveNoteIfChanged();
await loadNoteToEditor(noteId);
}
}
2017-11-05 05:54:27 +08:00
async function saveNoteIfChanged() {
if (!isNoteChanged) {
return;
}
const note = noteEditor.getCurrentNote();
updateNoteFromInputs(note);
await saveNoteToServer(note);
if (note.detail.is_protected) {
protected_session.touchProtectedSession();
}
2017-11-05 05:54:27 +08:00
}
function updateNoteFromInputs(note) {
2017-12-02 23:37:12 +08:00
note.detail.note_text = editor.getData();
2017-11-05 05:54:27 +08:00
const title = noteTitleEl.val();
note.detail.note_title = title;
noteTree.setNoteTitle(note.detail.note_id, title);
2017-11-05 05:54:27 +08:00
}
async function saveNoteToServer(note) {
await server.put('notes/' + note.detail.note_id, note);
2017-11-05 05:54:27 +08:00
isNoteChanged = false;
2017-11-09 11:33:08 +08:00
showMessage("Saved!");
2017-11-05 05:54:27 +08:00
}
function setNoteBackgroundIfProtected(note) {
if (note.detail.is_protected) {
2017-12-17 10:27:42 +08:00
$("#note-detail-wrapper").addClass("protected");
protectButton.hide();
unprotectButton.show();
2017-11-05 05:54:27 +08:00
}
else {
2017-12-17 10:27:42 +08:00
$("#note-detail-wrapper").removeClass("protected");
protectButton.show();
unprotectButton.hide();
2017-11-05 05:54:27 +08:00
}
noteTree.setCurrentNoteTreeBasedOnProtectedStatus();
2017-11-05 05:54:27 +08:00
}
let isNewNoteCreated = false;
function newNoteCreated() {
isNewNoteCreated = true;
}
2017-11-05 05:54:27 +08:00
async function loadNoteToEditor(noteId) {
currentNote = await loadNote(noteId);
2017-11-05 05:54:27 +08:00
if (isNewNoteCreated) {
isNewNoteCreated = false;
2017-11-05 05:54:27 +08:00
noteTitleEl.focus().select();
}
await protected_session.ensureProtectedSession(currentNote.detail.is_protected, false);
2017-11-05 05:54:27 +08:00
if (currentNote.detail.is_protected) {
protected_session.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.
protected_session.ensureDialogIsClosed();
2017-11-05 05:54:27 +08:00
noteDetailWrapperEl.show();
noteChangeDisabled = true;
noteTitleEl.val(currentNote.detail.note_title);
2017-12-02 23:37:12 +08:00
editor.setData(currentNote.detail.note_text);
2017-11-05 05:54:27 +08:00
noteChangeDisabled = false;
setNoteBackgroundIfProtected(currentNote);
showAppIfHidden();
2017-11-05 05:54:27 +08:00
}
async function loadNote(noteId) {
return await server.get('notes/' + noteId);
2017-11-05 05:54:27 +08:00
}
2017-12-03 02:54:16 +08:00
function getEditor() {
return editor;
}
2017-11-05 05:54:27 +08:00
$(document).ready(() => {
noteTitleEl.on('input', () => {
noteChanged();
const title = noteTitleEl.val();
noteTree.setNoteTitle(getCurrentNoteId(), title);
});
2017-11-05 05:54:27 +08:00
2017-12-02 23:37:12 +08:00
BalloonEditor
.create(document.querySelector('#note-detail'), {
})
.then(edit => {
editor = edit;
editor.document.on('change', noteChanged);
2017-12-02 23:37:12 +08:00
})
.catch(error => {
console.error(error);
});
2017-11-05 05:54:27 +08:00
// so that tab jumps from note title (which has tabindex 1)
2017-12-02 23:37:12 +08:00
noteDetailEl.attr("tabindex", 2);
2017-11-05 05:54:27 +08:00
});
setInterval(saveNoteIfChanged, 5000);
return {
reload,
switchToNote,
2017-11-05 05:54:27 +08:00
saveNoteIfChanged,
updateNoteFromInputs,
saveNoteToServer,
setNoteBackgroundIfProtected,
2017-11-05 05:54:27 +08:00
loadNote,
getCurrentNote,
getCurrentNoteId,
2017-12-03 02:54:16 +08:00
newNoteCreated,
getEditor
2017-11-05 05:54:27 +08:00
};
})();