diff --git a/amanuensis/resources/editor.js b/amanuensis/resources/editor.js index 87d4cc9..3b47718 100644 --- a/amanuensis/resources/editor.js +++ b/amanuensis/resources/editor.js @@ -46,18 +46,33 @@ function update(article) { req.responseType = "json"; req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { + // Update internal state with the returned article object params.article = req.response.article; - - var title = document.getElementById("editor-title").value; - var previewHtml = "

" + title + "

\n" + req.response.info.rendered; - document.getElementById("preview").innerHTML = previewHtml; - document.getElementById("preview-control").innerHTML = req.response.info.word_count; + // Set editor editability based on article status + updateEditorStatus(); + // Update the preview with the parse information + updatePreview(req.response.info); } }; var payload = { article: article }; req.send(JSON.stringify(payload)); } +function updateEditorStatus() { + var ready = !!params.article.status.ready + document.getElementById("editor-title").disabled = ready; + document.getElementById("editor-content").disabled = ready; + var submitButton = document.getElementById("button-submit"); + submitButton.innerText = ready ? "Edit article" : "Submit article"; +} + +function updatePreview(info) { + var title = document.getElementById("editor-title").value; + var previewHtml = "

" + title + "

\n" + info.rendered; + document.getElementById("preview").innerHTML = previewHtml; + document.getElementById("preview-control").innerHTML = info.word_count; +} + function onContentChange(timeout=2000) { ifNoFurtherChanges(() => { var article = buildArticleObject(); @@ -65,6 +80,14 @@ function onContentChange(timeout=2000) { }, timeout); } +function submitArticle() { + ifNoFurtherChanges(() => { + params.article.status.ready = !params.article.status.ready; + var article = buildArticleObject(); + update(article); + }, 0); +} + window.addEventListener("beforeunload", function(e) { var content = document.getElementById("editor-content").value var hasText = content.length > 0 && content != params.article.contents; diff --git a/amanuensis/templates/lexicon/editor.html b/amanuensis/templates/lexicon/editor.html index ae550dc..fe9b6ac 100644 --- a/amanuensis/templates/lexicon/editor.html +++ b/amanuensis/templates/lexicon/editor.html @@ -34,11 +34,9 @@ {{ g.lexicon.title }} - + {% if article %} + + {% endif %} {% if character %}