Change editor client/server comm structure

This commit is contained in:
Tim Van Baak 2020-03-30 21:34:43 -07:00
parent 74c4c7dbbf
commit e19a2a5b26
2 changed files with 20 additions and 12 deletions

View File

@ -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 = "<p>&nbsp;</p>";
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 = "<h1>" + title + "</h1>\n" + req.response.rendered
var previewHtml = "<h1>" + title + "</h1>\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);
}

View File

@ -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