You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
NotePostApp/www/js/editnote.js

92 lines
2.7 KiB
JavaScript

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
var save_in_progress = false;
function saveme(callback) {
if (save_in_progress) {
console.log("Warning: save already in progress, doing nothing.");
return;
}
app.toast.create({
text: '<i class="fas fa-sync fa-spin"></i> &nbsp; Saving...',
closeTimeout: 2 * 60 * 1000 // two whole minutes should be enough for *any* connection.
}).open();
save_in_progress = true;
sync(); // update textareas with correct content
var noteid = $("#note_content").data("noteid");
var note = new Note();
if (noteid != "") {
note = NOTES.get(noteid);
if (note.getSyncStatus() != "LOCAL_ONLY") {
note.setSyncStatus("LOCAL_EDITED");
}
}
note.setText($("#note_content").val());
note.setModified();
NOTES.set(note);
NOTES.syncAll(function () {
app.toast.create({
text: '<i class="fas fa-check"></i> &nbsp; Note saved.',
closeTimeout: 3000
}).open();
$("#orig_content").val(note.content);
save_in_progress = false;
if (typeof callback == "function") {
callback();
}
}, function () {
app.toast.create({
text: '<i class="fas fa-save"></i> &nbsp; Note saved locally.',
closeTimeout: 3000
}).open();
$("#orig_content").val(note.content);
save_in_progress = false;
if (typeof callback == "function") {
callback();
}
});
}
function init() {
document.getElementById("noteframe").contentWindow.initEditor($("#note_content").val());
}
/**
* Get the text from the editor iframe and put it in the hidden textarea
* @returns {undefined}
*/
function sync() {
$("#note_content").val(document.getElementById("noteframe").contentWindow.getMarkdown());
}
function exiteditor() {
sync();
if ($("#note_content").val() == "" || $("#note_content").val() === $("#orig_content").val()) {
router.back({force: true, ignoreCache: true, reload: true});
} else {
saveme(function () {
router.back({force: true, ignoreCache: true, reload: true});
});
}
}
$(window).bind('keydown', function (event) {
if (event.ctrlKey || event.metaKey) {
switch (String.fromCharCode(event.which).toLowerCase()) {
case 's':
event.preventDefault();
sync();
saveme();
break;
}
}
});
$("#noteframe").on("load", function () {
init();
});