Skip to content

Commit

Permalink
Allow Markdown in every field, thus making the "Text" type less special
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosuav committed Nov 2, 2024
1 parent df19f0e commit 9da1e3c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion httpstatic/chan_form.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {choc, set_content, DOM, on} from "https://rosuav.github.io/choc/factory.js";
const {BUTTON, DIV, INPUT, LABEL, LI, P, PRE, TD, TEXTAREA, TIME, TR, UL} = choc; //autoimport
const {BR, BUTTON, DIV, INPUT, LABEL, LI, P, PRE, TD, TEXTAREA, TIME, TR, UL} = choc; //autoimport
import {simpleconfirm} from "$$static||utils.js$$";

function format_time(ts) {
Expand All @@ -26,17 +26,20 @@ const render_element = { //Matches _element_types (see Pike code)
//({"twitchid", "Twitch username"}), //If mandatory, will force user to be logged in to submit
simple: el => [ //extcall
P("Text input"),
TEXTAREA({name: "text", value: el.text || "", rows: 2, cols: 80}), BR(),
LABEL(["Label: ", INPUT({name: "label", value: el.label || ""}), " - shown in the form"]),
//Type (numeric/text)?
],
paragraph: el => [ //extcall
P("Paragraph input"),
TEXTAREA({name: "text", value: el.text || "", rows: 2, cols: 80}), BR(),
LABEL(["Label: ", INPUT({name: "label", value: el.label || ""}), " - shown in the form"]),
],
//({"address", "Street address"}),
//({"radio", "Selection (radio) buttons"}),
checkbox: el => [ //extcall
P("Set of checkboxes"),
TEXTAREA({name: "text", value: el.text || "", rows: 2, cols: 80}), BR(),
UL([
(el.label || []).map((l, i) =>
LI([
Expand Down
19 changes: 13 additions & 6 deletions modules/http/chan_form.pike
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ mapping element_attributes = ([ //Matches _element_types
"simple": (["label": type_string]),
"paragraph": (["label": type_string]),
"checkbox": (["label[]": type_string]),
"text": (["text": type_string]),
"text": ([]), //No special attributes, only the universal ones
]);

__async__ mapping(string:mixed) http_request(Protocols.HTTP.Server.Request req) {
Expand Down Expand Up @@ -157,6 +157,11 @@ __async__ mapping(string:mixed) http_request(Protocols.HTTP.Server.Request req)
string formdata = "";
foreach (form->elements, mapping el) {
string|zero elem = 0;
string text = "";
if (el->text && el->text != "") text = Tools.Markdown.parse(el->text, ([
"renderer": Renderer, "lexer": Lexer,
"attributes": 1,
]));
switch (el->type) { //Matches _element_types
case "simple":
elem = sprintf("<label><span>%s</span> <input name=%q></label>",
Expand All @@ -177,13 +182,10 @@ __async__ mapping(string:mixed) http_request(Protocols.HTTP.Server.Request req)
elem += "</ul>";
break;
}
case "text": elem = Tools.Markdown.parse(el->text, ([
"renderer": Renderer, "lexer": Lexer,
"attributes": 1,
])); break;
case "text": elem = ""; break; //Descriptive text - no actual form controls
default: break;
}
if (elem) formdata += sprintf("<section id=%q>%s</section>\n", "field-" + el->name, elem);
if (elem) formdata += sprintf("<section id=%q>%s%s</section>\n", "field-" + el->name, text, elem);
}
return render_template(formview, ([
"formtitle": form->formtitle,
Expand Down Expand Up @@ -326,6 +328,11 @@ __async__ void wscmd_edit_element(object channel, mapping(string:mixed) conn, ma
el->name = msg->value;
form_data = 0; return; //Signal acceptance of the edit
}
else if (msg->field == "text") {
//All fields can have descriptive text.
el->text = msg->value;
form_data = 0; return;
}
else if (msg->field[-1] == ']') {
sscanf(msg->field, "%s[%d]", string basename, int idx);
if (msg->field != sprintf("%s[%d]", basename, idx)) return; //Strict formatting, no extra zeroes or anything
Expand Down

0 comments on commit 9da1e3c

Please sign in to comment.