From e19a2a5b26e346024d17559b2970bc49180b5d82 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 30 Mar 2020 21:34:43 -0700 Subject: [PATCH] Change editor client/server comm structure --- amanuensis/resources/editor.js | 17 ++++++++++------- amanuensis/server/lexicon.py | 15 ++++++++++----- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/amanuensis/resources/editor.js b/amanuensis/resources/editor.js index a8c77fa..87d4cc9 100644 --- a/amanuensis/resources/editor.js +++ b/amanuensis/resources/editor.js @@ -12,8 +12,9 @@ function ifNoFurtherChanges(callback, timeout=2000) { }, timeout); } -// Initialize editor +// Read data out of params and initialize editor window.onload = function() { + // Kill noscript message first document.getElementById("preview").innerHTML = "

 

"; if (params.article != null) { @@ -24,7 +25,7 @@ window.onload = function() { onContentChange(0); }; -function getArticleObj() { +function buildArticleObject() { var title = document.getElementById("editor-title").value; var contents = document.getElementById("editor-content").value; return { @@ -45,19 +46,21 @@ function update(article) { req.responseType = "json"; req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { - // params.article = article; + params.article = req.response.article; + var title = document.getElementById("editor-title").value; - var previewHtml = "

" + title + "

\n" + req.response.rendered + var previewHtml = "

" + title + "

\n" + req.response.info.rendered; document.getElementById("preview").innerHTML = previewHtml; - document.getElementById("preview-control").innerHTML = req.response.word_count; + document.getElementById("preview-control").innerHTML = req.response.info.word_count; } }; - req.send(JSON.stringify(article)); + var payload = { article: article }; + req.send(JSON.stringify(payload)); } function onContentChange(timeout=2000) { ifNoFurtherChanges(() => { - var article = getArticleObj(); + var article = buildArticleObject(); update(article); }, timeout); } diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index f902071..ec5710d 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -1,4 +1,5 @@ import json +import uuid from flask import ( Blueprint, render_template, url_for, redirect, g, flash, request, Markup) @@ -210,9 +211,10 @@ def get_bp(): @lexicon_param @player_required def editor_new(name): - import uuid new_aid = uuid.uuid4().hex + # TODO harden this cid = request.args.get("cid") + character = g.lexicon.character.get(cid) article = { "version": "0", "aid": new_aid, @@ -224,7 +226,7 @@ def get_bp(): "ready": False, "approved": False }, - "contents": "" + "contents": f"\n\n{character.signature}", } filename = f"{cid}.{new_aid}" with g.lexicon.ctx.draft.new(filename) as j: @@ -235,7 +237,7 @@ def get_bp(): @lexicon_param @player_required def editor_update(name): - article = request.json + article = request.json['article'] # TODO verification parsed_draft = parse_raw_markdown(article['contents']) rendered_html = parsed_draft.render(PreviewHtmlRenderer()) @@ -247,8 +249,11 @@ def get_bp(): # TODO return more info return { - 'rendered': rendered_html, - 'word_count': features.word_count, + 'article': article, + 'info': { + 'rendered': rendered_html, + 'word_count': features.word_count, + } } return bp