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
// before updating in response to a change.
var nonce = 0;
@ -28,23 +14,28 @@ function ifNoFurtherChanges(callback) {
// Initialize editor
window.onload = function() {
loadedArticleInfo.character = params.characterId;
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;
document.getElementById("editor-content").value = params.article.contents;
}
// this.onContentChange();
onContentChange();
};
function getArticleObj() {
// aid
// lexicon
// character
// title
// turn
// status
// contents
var title = document.getElementById("editor-title").value;
var contents = document.getElementById("editor-content").value;
return {
aid: params.article.aid,
lexicon: params.article.lexicon,
character: params.article.character,
title: title,
turn: params.article.turn,
status: params.article.status,
contents: contents
};
}
function update(article) {
@ -52,37 +43,24 @@ function update(article) {
req.open("POST", params.updateURL, true);
req.setRequestHeader("Content-type", "application/json");
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200)
return;
if (req.readyState == 4 && req.status == 200) {
params.article = article;
document.getElementById("preview-control").innerHTML = JSON.stringify(req.response);
}
};
req.send(article)
req.send(JSON.stringify(article));
}
function onContentChange() {
setTimeout(() => {
if (nonce == nonce_local) {
// Get the new content
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);
ifNoFurtherChanges(() => {
var article = getArticleObj();
update(article);
});
}
window.addEventListener("beforeunload", function(e) {
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) {
e.returnValue = "Are you sure?";
}
@ -94,7 +72,7 @@ window.addEventListener("keydown", function(event) {
if (String.fromCharCode(event.which).toLowerCase() == 's')
{
event.preventDefault();
document.getElementById("preview").innerHTML += "<p>wrong</p>";
onContentChange();
}
}
});

View File

@ -234,6 +234,12 @@ def get_bp():
@lexicon_param
@player_required
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

View File

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