From b357d13e2c0cc00fe9e2501f45e0b0d55e2c889f Mon Sep 17 00:00:00 2001
From: Tangent Wantwight <tangent128@gmail.com>
Date: Sun, 15 Oct 2023 23:59:04 -0400
Subject: [PATCH] Set the card's fields from a textarea

---
 src/3x5.ts | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/3x5.ts b/src/3x5.ts
index 7ed606c..dbe127b 100644
--- a/src/3x5.ts
+++ b/src/3x5.ts
@@ -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 = {