Port to TS

This commit is contained in:
Tangent 2023-08-05 01:09:33 -04:00
parent 8b4584eb48
commit 8fc2892630
5 changed files with 191 additions and 197 deletions

View file

@ -1,22 +1,35 @@
/**
* @typedef {object} Card Basic unit of information, also an "actor" in the programming system
* @property {number} id Unique identifier
* @property {Record<string, string>} fields Key-value properties on the card
* @property {string} code Eventually: a markdown string containing code, but for now, just code
*/
import { parse } from "./notcl";
/**
* @typedef {"action" | "render"} ScriptType "Mode" of the environment a script runs in; determines access to mutability features and such.
* Basic unit of information, also an "actor" in the programming system
*/
type Card = {
/** Unique identifier */
id: number;
/** Key-value properties on the card */
fields: Record<string, string>;
/** Eventually: a markdown string containing code, but for now, just code */
code: string;
};
/**
* "Mode" of the environment a script runs in; determines access to mutability features and such.
*
* "action": response to a UI action; allowed to modify card fields and access time and random numbers.
*
* "render": deterministic generation of display markup from card and workspace state; can only modify temporary variables.
*/
type ScriptType = "action" | "render";
/**
* @typedef {object} Vm State for running a script in.
* @property {ScriptType} mode Mutability status
* @property {string} output Markup to render / output
* State for running a script in.
*/
type Vm = {
/** Mutability status */
mode: ScriptType;
/** Markup to render / output */
output: string;
};
/**
* @param {Vm} state VM state
@ -24,7 +37,7 @@
* @returns {string} Markup to render / output
*/
function renderCard(state, code) {
const script = Notcl.parse(code);
const script = parse(code);
if (script[0]) {
state.output = JSON.stringify(script[1], null, 2);
} else {
@ -88,7 +101,7 @@ const debugDisplay = document.createElement("pre");
function render() {
const vm = {
mode: /** @type {ScriptType} */ ("render"),
mode: /** @type {ScriptType} */ "render",
output: "",
};
const html = renderCard(vm, theCard.code);