Add basic, no-validation autosave

This commit is contained in:
Tim Van Baak 2020-02-23 20:08:21 -08:00
parent dc340a1d5e
commit 3a5a8557e0
3 changed files with 37 additions and 54 deletions

View File

@ -1,17 +1,3 @@
// Editor state
var loadedArticleInfo = {
aid: null,
lexicon: null,
character: null,
title: null,
turn: null,
status: {
ready: null,
approved: null,
},
contents: null,
};
// Reduce unnecessary requests by checking for no further changes being made // Reduce unnecessary requests by checking for no further changes being made
// before updating in response to a change. // before updating in response to a change.
var nonce = 0; var nonce = 0;
@ -28,23 +14,28 @@ function ifNoFurtherChanges(callback) {
// Initialize editor // Initialize editor
window.onload = function() { window.onload = function() {
loadedArticleInfo.character = params.characterId;
document.getElementById("preview").innerHTML = "<p>&nbsp;</p>"; document.getElementById("preview").innerHTML = "<p>&nbsp;</p>";
// document.getElementById("editor-content").value = "\n\n" + params.default_signature; if (params.article != null) {
document.getElementById("editor-title").value = params.article.title;
// this.onContentChange(); document.getElementById("editor-content").value = params.article.contents;
}
onContentChange();
}; };
function getArticleObj() { function getArticleObj() {
// aid var title = document.getElementById("editor-title").value;
// lexicon var contents = document.getElementById("editor-content").value;
// character return {
// title aid: params.article.aid,
// turn lexicon: params.article.lexicon,
// status character: params.article.character,
// contents title: title,
turn: params.article.turn,
status: params.article.status,
contents: contents
};
} }
function update(article) { function update(article) {
@ -52,37 +43,24 @@ function update(article) {
req.open("POST", params.updateURL, true); req.open("POST", params.updateURL, true);
req.setRequestHeader("Content-type", "application/json"); req.setRequestHeader("Content-type", "application/json");
req.onreadystatechange = function () { req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) if (req.readyState == 4 && req.status == 200) {
return; params.article = article;
document.getElementById("preview-control").innerHTML = JSON.stringify(req.response);
}
}; };
req.send(article) req.send(JSON.stringify(article));
} }
function onContentChange() { function onContentChange() {
setTimeout(() => { ifNoFurtherChanges(() => {
if (nonce == nonce_local) { var article = getArticleObj();
// Get the new content update(article);
var articleTitle = document.getElementById("editor-title").value; });
var articleBody = document.getElementById("editor-content").value;
// Pass the draft text to the parser to get the preview html and citations
var parseResult = parseLexipythonMarkdown(articleBody);
// Build the citation block
var citeblockContent = makeCiteblock(parseResult);
// Compute warnings and build the control block
var controlContent = checkWarnings(parseResult);
// Fill in the content blocks
document.getElementById("preview").innerHTML = (
"<h1>" + articleTitle + "</h1>\n"
+ parseResult.html);
document.getElementById("preview-citations").innerHTML = citeblockContent;
document.getElementById("preview-control").innerHTML = controlContent;
}
}, 3000);
} }
window.addEventListener("beforeunload", function(e) { window.addEventListener("beforeunload", function(e) {
var content = document.getElementById("editor-content").value var content = document.getElementById("editor-content").value
var hasText = content.length > 0 && content != "\n\n" + params.default_signature; var hasText = content.length > 0 && content != params.article.contents;
if (hasText) { if (hasText) {
e.returnValue = "Are you sure?"; e.returnValue = "Are you sure?";
} }
@ -94,7 +72,7 @@ window.addEventListener("keydown", function(event) {
if (String.fromCharCode(event.which).toLowerCase() == 's') if (String.fromCharCode(event.which).toLowerCase() == 's')
{ {
event.preventDefault(); event.preventDefault();
document.getElementById("preview").innerHTML += "<p>wrong</p>"; onContentChange();
} }
} }
}); });

View File

@ -234,6 +234,12 @@ def get_bp():
@lexicon_param @lexicon_param
@player_required @player_required
def editor_update(name): def editor_update(name):
pass article = request.json
# TODO verification
filename = f'{article["character"]}.{article["aid"]}'
with g.lexicon.ctx.draft.edit(filename) as a:
a.update(article)
# TODO return more info
return {'hello': 'world'}
return bp return bp

View File

@ -18,7 +18,7 @@
character: null, character: null,
{% endif %} {% endif %}
{% if article %} {% if article %}
article: {{ jsonfmt(character) }}, article: {{ jsonfmt(article) }},
{% else %} {% else %}
article: null, article: null,
{% endif %} {% endif %}
@ -73,8 +73,7 @@
{% endfor %} {% endfor %}
{% if article %} {% if article %}
<input id="editor-title" placeholder="Title" oninput="onContentChange()"> <input id="editor-title" placeholder="Title" oninput="onContentChange()">
<textarea id="editor-content" class="fullwidth" oninput="onContentChange()"> <textarea id="editor-content" class="fullwidth" oninput="onContentChange()"></textarea>
</textarea>
{% endif %} {% endif %}
</div> </div>
</div> </div>