Add "get card field" command
This commit is contained in:
parent
68213df945
commit
de4495a997
2 changed files with 35 additions and 20 deletions
30
src/3x5.ts
30
src/3x5.ts
|
@ -1,19 +1,8 @@
|
|||
import { ALL as HTML } from "./lib/html";
|
||||
import { parse } from "./parser";
|
||||
import { runNoctl, Vm } from "./vm";
|
||||
import { AsHtml, AsText, TextPiece } from "./words";
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
import { Card, CardVm, GetField } from './lib/card';
|
||||
import { ALL as HTML } from './lib/html';
|
||||
import { parse } from './parser';
|
||||
import { runNoctl, Vm } from './vm';
|
||||
import { AsHtml, AsText, TextPiece } from './words';
|
||||
|
||||
/**
|
||||
* Updates a card's fields
|
||||
|
@ -43,7 +32,7 @@ function parseFields(card: Card, fields: string) {
|
|||
* @param code Script to run
|
||||
* @returns Markup to render / output
|
||||
*/
|
||||
function renderCard(state: Vm, code: string) {
|
||||
function renderCard(state: CardVm, code: string) {
|
||||
const script = parse(code);
|
||||
if (script[0]) {
|
||||
runNoctl(state, script[1], (word) => (state.output += AsHtml(word) + "\n"));
|
||||
|
@ -128,10 +117,11 @@ function render() {
|
|||
parseFields(theCard, fieldInput.value);
|
||||
theCard.code = codeInput.value;
|
||||
|
||||
const vm: Vm = {
|
||||
const vm: CardVm = {
|
||||
mode: "render",
|
||||
commands: { ...HTML },
|
||||
commands: { ...HTML, get: GetField },
|
||||
output: "",
|
||||
card: theCard,
|
||||
};
|
||||
const html = renderCard(vm, theCard.code);
|
||||
|
||||
|
@ -145,7 +135,7 @@ document.body.append(
|
|||
fieldInput,
|
||||
codeInput,
|
||||
rerender,
|
||||
state,
|
||||
display,
|
||||
state,
|
||||
debugDisplay
|
||||
);
|
||||
|
|
25
src/lib/card.ts
Normal file
25
src/lib/card.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { Vm } from '../vm';
|
||||
import { TextPiece } from '../words';
|
||||
import { getOpt } from './options';
|
||||
|
||||
/**
|
||||
* Basic unit of information, also an "actor" in the programming system
|
||||
*/
|
||||
export 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;
|
||||
};
|
||||
|
||||
export type CardVm = Vm<{
|
||||
card: Card;
|
||||
}>;
|
||||
|
||||
export function GetField(vm: CardVm, argv: TextPiece[]): TextPiece {
|
||||
const [{ error = false }, fieldName] = getOpt(argv, { min: 1, max: 1 });
|
||||
// TODO: handle error
|
||||
return { text: vm.card.fields[fieldName] ?? "" };
|
||||
}
|
Loading…
Reference in a new issue