From 3a5a8557e0e90746cbfaff6401cbc3cb752cece6 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 23 Feb 2020 20:08:21 -0800 Subject: [PATCH] Add basic, no-validation autosave --- amanuensis/resources/editor.js | 78 +++++++++--------------- amanuensis/server/lexicon.py | 8 ++- amanuensis/templates/lexicon/editor.html | 5 +- 3 files changed, 37 insertions(+), 54 deletions(-) diff --git a/amanuensis/resources/editor.js b/amanuensis/resources/editor.js index 1c45372..fa75aeb 100644 --- a/amanuensis/resources/editor.js +++ b/amanuensis/resources/editor.js @@ -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 = "

 

"; - // document.getElementById("editor-content").value = "\n\n" + params.default_signature; - - // this.onContentChange(); + if (params.article != null) { + document.getElementById("editor-title").value = params.article.title; + document.getElementById("editor-content").value = params.article.contents; + } + + 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 = ( - "

" + articleTitle + "

\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 += "

wrong

"; + onContentChange(); } } }); \ No newline at end of file diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index c59607c..3058194 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -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 diff --git a/amanuensis/templates/lexicon/editor.html b/amanuensis/templates/lexicon/editor.html index db2b0ed..29d291e 100644 --- a/amanuensis/templates/lexicon/editor.html +++ b/amanuensis/templates/lexicon/editor.html @@ -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 %} - + {% endif %}