From c0c69805e34bcaaed1d4b89d5b54a7903314a141 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 14 Feb 2020 14:22:38 -0800 Subject: [PATCH] Add framework for editor live preview warnings --- amanuensis/resources/editor.css | 9 +++ amanuensis/resources/editor.js | 92 +++++++++++++++++------- amanuensis/server/lexicon.py | 9 ++- amanuensis/templates/lexicon/editor.html | 11 +-- 4 files changed, 91 insertions(+), 30 deletions(-) diff --git a/amanuensis/resources/editor.css b/amanuensis/resources/editor.css index 553b356..1944b64 100644 --- a/amanuensis/resources/editor.css +++ b/amanuensis/resources/editor.css @@ -45,6 +45,15 @@ div#editor-right { div#editor-right div.contentblock { margin: 10px 5px 10px 10px; } +div#editor-right p#editor-warnings { + color: #808000; +} +div#editor-right p#editor-errors { + color: #ff0000; +} +span.new { + color: #008000; +} @media only screen and (max-width: 816px) { div#wrapper { max-width: 564px; diff --git a/amanuensis/resources/editor.js b/amanuensis/resources/editor.js index 16d3768..4052bea 100644 --- a/amanuensis/resources/editor.js +++ b/amanuensis/resources/editor.js @@ -5,23 +5,9 @@ function onContentChange() { // Pass the draft text to the parser to get the preview html and citations var parseResult = parseLexipythonMarkdown(articleBody); // Build the citation block - var citeTexts = [] - for (var i = 0; i < parseResult.citations.length; i++) { - var cite = parseResult.citations[i]; - citeTexts.push("[" + cite.id.toString() + "] " + cite.citeTitle); - } - var citeblockContent = citeTexts.join(" / "); + var citeblockContent = makeCiteblock(parseResult); // Compute warnings and build the control block - var flagMissingSignature = !parseResult.hasSignature; - var wordCount = (parseResult.html - // Delete all HTML tags - .replace(/<[^>]+>/g, "") - .trim() - .split(/\s+/) - .length); - var controlContent = ""; - controlContent += "

Signature: " + (!flagMissingSignature).toString() + "

"; - controlContent += "

Word count: " + wordCount.toString() + "

"; + var controlContent = checkWarnings(parseResult); // Fill in the content blocks document.getElementById("preview").innerHTML = ( "

" + articleTitle + "

\n" @@ -91,15 +77,73 @@ function parseLexipythonMarkdown(text) { content += "

" + citationList[i] + "

\n"; } } - // Calculate approximate word count - // var wordCount = text.trim().split(/\s+/).length; - // if (text.trim().length < 1) - // wordCount = 0; - // content += "

Article length: approx. " + wordCount + " words

"; - return result; } +function makeCiteblock(parseResult) { + var citeTexts = [] + for (var i = 0; i < parseResult.citations.length; i++) { + var cite = parseResult.citations[i]; + citeTexts.push("[" + cite.id.toString() + "] " + cite.citeTitle); + } + return citeTexts.join(" / "); +} + +function checkWarnings(parseResult) { + var result = { + errors: [], + warnings: [], + }; + if (!parseResult.hasSignature) { + result.warnings.push("Article has no signature."); + } + // Self-citation + // TODO + // Citation targets + // TODO + if (params.citation.min_total != null && + parseResult.citations.length < params.citation.min_total) { + result.errors.push("Article must have a minimum of " + + params.citation.min_total + " citations."); + } + if (params.citation.max_total != null && + parseResult.citations.length > params.citation.max_total) { + result.errors.push("Article cannot have more than " + + params.citation.max_total + " citations."); + } + // TODO + // Word limits + var wordCount = (parseResult.html + // Delete all HTML tags + .replace(/<[^>]+>/g, "") + .trim() + .split(/\s+/) + .length); + if (params.wordLimit.hard != null && wordCount > params.wordLimit.hard) { + result.errors.push("Article must be shorter than " + params.wordLimit.hard + " words."); + } else if (params.wordLimit.soft != null && wordCount > params.wordLimit.soft) { + result.warnings.push("Article should be shorter than " + params.wordLimit.soft + " words."); + } + + var controlContent = ""; + controlContent += "

Word count: " + wordCount + "

"; + if (result.errors.length > 0) { + controlContent += "

"; + for (var i = 0; i < result.errors.length; i++) { + controlContent += result.errors[i] + "
"; + } + controlContent += "

"; + } + if (result.warnings.length > 0) { + controlContent += "

"; + for (var i = 0; i < result.warnings.length; i++) { + controlContent += result.warnings[i] + "
"; + } + controlContent += "

"; + } + return controlContent; +} + // Parse the article content and update the preview pane @@ -128,13 +172,13 @@ function parseLexipythonMarkdown(text) { // } window.onload = function() { - document.getElementById("editor-content").value = "\n\n" + params.defaultSignature; + document.getElementById("editor-content").value = "\n\n" + params.default_signature; this.onContentChange(); }; window.addEventListener("beforeunload", function(e) { var content = document.getElementById("editor-content").value - var hasText = content.length > 0 && content != "\n\n" + params.defaultSignature; + var hasText = content.length > 0 && content != "\n\n" + params.default_signature; if (hasText) { e.returnValue = "Are you sure?"; } diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index 460ac50..dbbf423 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -1,7 +1,7 @@ import json from flask import ( - Blueprint, render_template, url_for, redirect, g, flash, request) + Blueprint, render_template, url_for, redirect, g, flash, request, Markup) from flask_login import login_required, current_user from amanuensis.config import json_ro, open_ex @@ -138,6 +138,11 @@ def get_bp(): @lexicon_param @player_required def editor(name): - return render_template('lexicon/editor.html') + return render_template( + 'lexicon/editor.html', + current_turn=Markup(json.dumps(g.lexicon.turn.current)), + citation=Markup(json.dumps(dict(g.lexicon.article.citation))), + word_limit=Markup(json.dumps(dict(g.lexicon.article.word_limit))), + addendum=Markup(json.dumps(dict(g.lexicon.article.citation)))) return bp diff --git a/amanuensis/templates/lexicon/editor.html b/amanuensis/templates/lexicon/editor.html index 2c84dce..6b0a2c2 100644 --- a/amanuensis/templates/lexicon/editor.html +++ b/amanuensis/templates/lexicon/editor.html @@ -7,14 +7,17 @@ - {% block title %}{% endblock %} + Editor