Set the card's fields from a textarea

This commit is contained in:
Tangent Wantwight 2023-10-15 23:59:04 -04:00
parent e98e9e1de7
commit b357d13e2c

View file

@ -1,7 +1,7 @@
import { ALL as HTML } from "./lib/html";
import { parse } from "./parser";
import { runNoctl, Vm } from "./vm";
import { AsHtml } from "./words";
import { AsHtml, AsText, TextPiece } from "./words";
/**
* Basic unit of information, also an "actor" in the programming system
@ -15,6 +15,29 @@ type Card = {
code: string;
};
/**
* Updates a card's fields
* @param state VM state
* @param card
* @param fields
*/
function parseFields(card: Card, fields: string) {
const script = parse(fields);
const newFields: Record<string, string> = {};
if (script[0]) {
script[1].forEach(([name, value = undefined]) => {
if (name != undefined) {
newFields[AsText(name as TextPiece)] = value
? AsText(value as TextPiece)
: "";
}
});
card.fields = newFields;
} else {
console.error(script[1]);
}
}
/**
* @param state VM state
* @param code Script to run
@ -44,6 +67,12 @@ const TEXTAREA_STYLE: Partial<CSSStyleDeclaration> = {
width: "100%",
};
const fieldInput = document.createElement("textarea");
Object.assign(fieldInput.style, TEXTAREA_STYLE, { height: "8em" });
fieldInput.value = String.raw`
title "Hello, World!"
`.trim();
const codeInput = document.createElement("textarea");
Object.assign(codeInput.style, TEXTAREA_STYLE, { height: "20em" });
codeInput.value = String.raw`
@ -96,6 +125,7 @@ const display = document.createElement("blockquote");
const debugDisplay = document.createElement("pre");
function render() {
parseFields(theCard, fieldInput.value);
theCard.code = codeInput.value;
const vm: Vm = {