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); }, timeout);
} }
// Initialize editor // Read data out of params and initialize editor
window.onload = function() { window.onload = function() {
// Kill noscript message first
document.getElementById("preview").innerHTML = "<p>&nbsp;</p>"; document.getElementById("preview").innerHTML = "<p>&nbsp;</p>";
if (params.article != null) { if (params.article != null) {
@ -24,7 +25,7 @@ window.onload = function() {
onContentChange(0); onContentChange(0);
}; };
function getArticleObj() { function buildArticleObject() {
var title = document.getElementById("editor-title").value; var title = document.getElementById("editor-title").value;
var contents = document.getElementById("editor-content").value; var contents = document.getElementById("editor-content").value;
return { return {
@ -45,19 +46,21 @@ function update(article) {
req.responseType = "json"; req.responseType = "json";
req.onreadystatechange = function () { req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) { if (req.readyState == 4 && req.status == 200) {
// params.article = article; params.article = req.response.article;
var title = document.getElementById("editor-title").value; 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").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) { function onContentChange(timeout=2000) {
ifNoFurtherChanges(() => { ifNoFurtherChanges(() => {
var article = getArticleObj(); var article = buildArticleObject();
update(article); update(article);
}, timeout); }, timeout);
} }

View File

@ -1,4 +1,5 @@
import json import json
import uuid
from flask import ( from flask import (
Blueprint, render_template, url_for, redirect, g, flash, request, Markup) Blueprint, render_template, url_for, redirect, g, flash, request, Markup)
@ -210,9 +211,10 @@ def get_bp():
@lexicon_param @lexicon_param
@player_required @player_required
def editor_new(name): def editor_new(name):
import uuid
new_aid = uuid.uuid4().hex new_aid = uuid.uuid4().hex
# TODO harden this
cid = request.args.get("cid") cid = request.args.get("cid")
character = g.lexicon.character.get(cid)
article = { article = {
"version": "0", "version": "0",
"aid": new_aid, "aid": new_aid,
@ -224,7 +226,7 @@ def get_bp():
"ready": False, "ready": False,
"approved": False "approved": False
}, },
"contents": "" "contents": f"\n\n{character.signature}",
} }
filename = f"{cid}.{new_aid}" filename = f"{cid}.{new_aid}"
with g.lexicon.ctx.draft.new(filename) as j: with g.lexicon.ctx.draft.new(filename) as j:
@ -235,7 +237,7 @@ def get_bp():
@lexicon_param @lexicon_param
@player_required @player_required
def editor_update(name): def editor_update(name):
article = request.json article = request.json['article']
# TODO verification # TODO verification
parsed_draft = parse_raw_markdown(article['contents']) parsed_draft = parse_raw_markdown(article['contents'])
rendered_html = parsed_draft.render(PreviewHtmlRenderer()) rendered_html = parsed_draft.render(PreviewHtmlRenderer())
@ -247,8 +249,11 @@ def get_bp():
# TODO return more info # TODO return more info
return { return {
'rendered': rendered_html, 'article': article,
'word_count': features.word_count, 'info': {
'rendered': rendered_html,
'word_count': features.word_count,
}
} }
return bp return bp