2019-02-19 08:38:17 +01:00
|
|
|
<div id="toolbar" class="d-flex p-1">
|
2019-02-20 23:13:59 +01:00
|
|
|
<i class="align-self-center fa fa-terminal pr-1"></i>
|
|
|
|
<div id="message" class="mr-auto"></div>
|
|
|
|
<div id="draft-button" class="pr-2 selected">Draft</div>
|
|
|
|
<div id="delete-button" class="pr-2">Delete</div>
|
2019-02-19 08:38:17 +01:00
|
|
|
</div>
|
2019-02-08 08:53:56 +01:00
|
|
|
<script>
|
2019-02-19 08:38:17 +01:00
|
|
|
var _options = {
|
|
|
|
'alertTimeout': 5, // Second in dissapear the alert
|
2019-02-20 23:13:59 +01:00
|
|
|
'autosaveTimeout': 3 // Second to activate before call the autosave
|
2019-02-19 08:38:17 +01:00
|
|
|
};
|
2019-02-08 08:53:56 +01:00
|
|
|
|
2019-02-19 08:38:17 +01:00
|
|
|
function showAlert(text) {
|
|
|
|
$("#message").html(text).fadeIn();
|
|
|
|
setTimeout(function(){
|
|
|
|
$("#message").html("");
|
|
|
|
},_options['alertTimeout']*1000);
|
2019-02-08 08:53:56 +01:00
|
|
|
}
|
2019-02-19 08:38:17 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<textarea id="editor"></textarea>
|
|
|
|
<script>
|
|
|
|
var _editor = null;
|
|
|
|
var _key = null; // Current page key in the editor
|
|
|
|
var _tags = []; // Current tags from the content
|
|
|
|
var _content = ""; // Current content, this variable helps to know when the content was changed
|
|
|
|
var _autosaveTimer = null; // Timer object for the autosave
|
2019-02-20 23:13:59 +01:00
|
|
|
var _draft = true;
|
2019-02-08 08:53:56 +01:00
|
|
|
|
2019-02-19 08:38:17 +01:00
|
|
|
function editorInitialize(content) {
|
|
|
|
_editor = new EasyMDE({
|
|
|
|
autofocus: true,
|
|
|
|
toolbar: false,
|
|
|
|
spellChecker: false,
|
|
|
|
status: false,
|
|
|
|
tabSize: 4,
|
|
|
|
initialValue: content
|
2019-02-08 08:53:56 +01:00
|
|
|
});
|
|
|
|
|
2019-02-20 08:45:55 +01:00
|
|
|
// Get the tags from the content
|
|
|
|
// When the content is setted the tags need to be setted
|
|
|
|
_tags = parser.tags(content);
|
|
|
|
|
2019-02-19 08:38:17 +01:00
|
|
|
// Editor event change
|
|
|
|
_editor.codemirror.on("change", function(){
|
|
|
|
// Reset timer
|
|
|
|
if (_autosaveTimer != null) {
|
|
|
|
clearTimeout(_autosaveTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activate timer
|
|
|
|
_autosaveTimer = setTimeout(function() {
|
2019-02-20 23:13:59 +01:00
|
|
|
updatePage("Saved");
|
|
|
|
}, _options["autosaveTimeout"]*1000);
|
2019-02-08 08:53:56 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-19 08:38:17 +01:00
|
|
|
function editorGetContent() {
|
|
|
|
return _editor.value();
|
|
|
|
}
|
2019-02-08 08:53:56 +01:00
|
|
|
|
2019-02-20 23:13:59 +01:00
|
|
|
function updatePage(alertMessage) {
|
|
|
|
log('Updating page...', '');
|
|
|
|
_content = editorGetContent();
|
|
|
|
var tags = parser.tags(_content);
|
|
|
|
var title = parser.title(_content);
|
|
|
|
var newContent = parser.removeFirstLine(_content);
|
|
|
|
|
|
|
|
// Update the page because was a change in the content
|
|
|
|
ajax.updatePage(_key, title, newContent, tags, _draft).then(function(key) {
|
|
|
|
_key = key;
|
|
|
|
showAlert(alertMessage);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check if there are new tags in the editor
|
|
|
|
// If there are new tags get the new tags for the sidebar
|
|
|
|
if (JSON.stringify(_tags) != JSON.stringify(tags)) {
|
|
|
|
_tags = tags;
|
|
|
|
displayTags();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 08:45:55 +01:00
|
|
|
function createPage() {
|
2019-02-20 23:13:59 +01:00
|
|
|
// New pages is draft by default
|
|
|
|
setDraft(true);
|
2019-02-20 08:45:55 +01:00
|
|
|
let response = ajax.createPage();
|
|
|
|
response.then(function(key) {
|
|
|
|
// Log
|
|
|
|
log('createPage() => ajax.createPage => key',key);
|
|
|
|
_key = key;
|
|
|
|
editorInitialize('# Title \n');
|
|
|
|
});
|
|
|
|
}
|
2019-02-08 08:53:56 +01:00
|
|
|
|
2019-02-20 23:13:59 +01:00
|
|
|
function setDraft(value) {
|
|
|
|
let message = "";
|
|
|
|
if (value) {
|
|
|
|
_draft = true;
|
|
|
|
$("#draft-button").addClass("selected");
|
|
|
|
message = "Page saved as draft";
|
|
|
|
} else {
|
|
|
|
_draft = false;
|
|
|
|
$("#draft-button").removeClass("selected");
|
|
|
|
message = "Page published";
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePage(message);
|
|
|
|
}
|
|
|
|
|
2019-02-20 08:45:55 +01:00
|
|
|
// MAIN
|
2019-02-19 08:38:17 +01:00
|
|
|
$(document).ready(function() {
|
2019-02-20 23:13:59 +01:00
|
|
|
// Click on draft button
|
|
|
|
$(document).on("click", "#draft-button", function() {
|
|
|
|
if (_draft) {
|
|
|
|
setDraft(false);
|
|
|
|
} else {
|
|
|
|
setDraft(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-19 08:38:17 +01:00
|
|
|
showAlert("Welcome to Bludit");
|
|
|
|
});
|
2019-02-08 08:53:56 +01:00
|
|
|
|
|
|
|
</script>
|