trilium/static/js/note.js

217 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-08-30 10:25:58 +08:00
const tags = {
2017-06-12 04:04:07 +08:00
1: "<b>",
2: "</b>",
3: "<i>",
4: "</i>",
5: "<u>",
6: "</u>",
9: "<s>",
10: "</s>"
};
2017-06-12 09:04:04 +08:00
let noteChangeDisabled = false;
let isNoteChanged = false;
2017-06-12 04:04:07 +08:00
function noteChanged() {
2017-06-12 09:04:04 +08:00
if (noteChangeDisabled) {
return;
}
isNoteChanged = true;
}
function saveNoteIfChanged(callback) {
if (!isNoteChanged) {
if (callback) {
callback();
}
return;
}
const note = globalCurrentNote;
updateNoteFromInputs(note);
encryptNoteIfNecessary(note);
saveNoteToServer(note, callback);
}
setInterval(saveNoteIfChanged, 5000);
2017-06-12 04:04:07 +08:00
$(document).ready(function() {
$("#noteTitle").on('input', function() {
noteChanged();
});
$('#noteDetail').summernote({
airMode: true,
2017-06-12 09:04:04 +08:00
height: 300,
2017-06-12 04:04:07 +08:00
callbacks: {
onChange: noteChanged
}
});
// so that tab jumps from note title (which has tabindex 1)
$(".note-editable").attr("tabindex", 2);
2017-06-12 04:04:07 +08:00
});
function updateNoteFromInputs(note) {
let contents = $('#noteDetail').summernote('code');
html2notecase(contents, note);
let title = $('#noteTitle').val();
getNodeByKey(note.detail.note_id).setTitle(title);
note.detail.note_title = title;
}
function saveNoteToServer(note, callback) {
$.ajax({
url: baseUrl + 'notes/' + note.detail.note_id,
type: 'PUT',
data: JSON.stringify(note),
contentType: "application/json",
success: function () {
isNoteChanged = false;
message("Saved!");
if (callback) {
callback();
}
},
error: function () {
error("Error saving the note!");
}
});
}
let globalCurrentNote;
2017-06-12 04:04:07 +08:00
2017-06-12 09:04:04 +08:00
function createNewTopLevelNote() {
2017-09-07 09:34:54 +08:00
let rootNode = globalTree.fancytree("getRootNode");
2017-06-12 09:04:04 +08:00
createNote(rootNode, "root", "into");
}
let newNoteCreated = false;
function createNote(node, parentKey, target, encryption) {
// if encryption isn't available (user didn't enter password yet), then note is created as unencrypted
// but this is quite weird since user doesn't see where the note is being created so it shouldn't occur often
if (!encryption || !isEncryptionAvailable()) {
encryption = 0;
}
const newNoteName = "new note";
const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryptString(newNoteName) : newNoteName;
2017-06-12 04:04:07 +08:00
$.ajax({
url: baseUrl + 'notes/' + parentKey + '/children' ,
type: 'POST',
data: JSON.stringify({
note_title: newNoteNameEncryptedIfNecessary,
2017-06-12 04:04:07 +08:00
target: target,
target_note_id: node.key,
encryption: encryption
2017-06-12 04:04:07 +08:00
}),
contentType: "application/json",
success: function(result) {
const newNode = {
title: newNoteName,
key: result.note_id,
note_id: result.note_id,
encryption: encryption,
extraClasses: encryption ? "encrypted" : ""
2017-06-12 04:04:07 +08:00
};
2017-09-04 06:50:56 +08:00
globalAllNoteIds.push(result.note_id);
newNoteCreated = true;
if (target === 'after') {
2017-06-12 04:04:07 +08:00
node.appendSibling(newNode).setActive(true);
}
else {
node.addChildren(newNode).setActive(true);
node.folder = true;
node.renderTitle();
}
message("Created!");
}
});
}
function setTreeBasedOnEncryption(note) {
const node = getNodeByKey(note.detail.note_id);
node.toggleClass("encrypted", note.detail.encryption > 0);
}
function setNoteBackgroundIfEncrypted(note) {
if (note.detail.encryption > 0) {
$(".note-editable").addClass("encrypted");
2017-09-06 10:01:22 +08:00
$("#encryptButton").hide();
$("#decryptButton").show();
}
else {
$(".note-editable").removeClass("encrypted");
2017-09-06 10:01:22 +08:00
$("#encryptButton").show();
$("#decryptButton").hide();
}
setTreeBasedOnEncryption(note);
}
2017-06-12 04:04:07 +08:00
function loadNote(noteId) {
$.get(baseUrl + 'notes/' + noteId).then(function(note) {
globalCurrentNote = note;
2017-06-12 04:04:07 +08:00
if (newNoteCreated) {
newNoteCreated = false;
$("#noteTitle").focus().select();
}
handleEncryption(note.detail.encryption > 0, false, () => {
$("#noteDetailWrapper").show();
2017-09-07 10:03:53 +08:00
// this may fal if the dialog has not been previously opened
try {
$("#encryptionPasswordDialog").dialog('close');
}
catch(e) {}
$("#encryptionPassword").val('');
decryptNoteIfNecessary(note);
$("#noteTitle").val(note.detail.note_title);
2017-06-12 09:04:04 +08:00
let noteText = notecase2html(note);
noteChangeDisabled = true;
2017-06-12 09:04:04 +08:00
// Clear contents and remove all stored history. This is to prevent undo from going across notes
$('#noteDetail').summernote('reset');
$('#noteDetail').summernote('code', noteText);
document.location.hash = noteId;
2017-08-23 10:40:54 +08:00
$(window).resize(); // to trigger resizing of editor
addRecentNote(noteId, note.detail.note_id);
noteChangeDisabled = false;
setNoteBackgroundIfEncrypted(note);
});
2017-06-12 04:04:07 +08:00
});
}