Files
launcher/vdom/vdom.mbt
T
SpriteandClaude Sonnet 5.5 28e0284d9b Add the pure MoonBit core: grid, gestures, updater, vdom, app
grid: packing, pinning, resize steps (port of Layout.js and app.py's grid logic).
gesture: pointer state machine (tap, long press, drag, release, swipe).
updater: release/version parsing and the update decision.
vdom: pure virtual DOM with an HTML-text renderer for tests.
app: Elm-style Model/Msg/update/view with effects as data, persistence (JSON
layout, import of the old WebView build's localStorage), drawer/search, menus,
drag and resize. 60 headless tests drive it through pointer input.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 00:55:16 +00:00

169 lines
4.3 KiB
MoonBit

///|
/// A tiny virtual DOM, pure data, generic over the message type `M` the handlers send.
/// The app's `view` builds a tree; a platform layer (web/) turns it into real DOM and
/// patches it by key. Because the tree is plain data, views are unit-tested by
/// rendering them to HTML text (`to_html`).
///|
/// What the user can do to an element. Pointer gestures are recognised by the gesture
/// package; the platform layer reports them against the chain of elements that have
/// handlers.
pub(all) struct Handlers[M] {
/// Pointer went down (acted on at once, e.g. grabbing a widget).
down : M?
/// A tap (down and up without a hold or a move).
tap : M?
/// A long press.
press : M?
/// Enter pressed in an input.
enter : M?
/// Escape pressed in an input.
escape : M?
/// An input's text changed.
input : ((String) -> M)?
}
///|
pub fn[M] Handlers::none() -> Handlers[M] {
{ down: None, tap: None, press: None, enter: None, escape: None, input: None }
}
///|
pub fn[M] Handlers::is_empty(self : Handlers[M]) -> Bool {
self.down is None &&
self.tap is None &&
self.press is None &&
self.enter is None &&
self.escape is None &&
self.input is None
}
///|
pub(all) enum Node[M] {
Text(String)
El(Element[M])
}
///|
pub(all) struct Element[M] {
tag : String
/// Identity among siblings for the keyed diff (0: none).
key : Int
classes : Array[String]
attrs : Array[(String, String)]
/// DOM properties (not attributes), e.g. an input's `value`.
props : Array[(String, String)]
style : String
handlers : Handlers[M]
children : Array[Node[M]]
}
///|
/// An element builder: `el("div", classes=["a"], children=[...])`.
pub fn[M] el(
tag : String,
key? : Int = 0,
classes? : Array[String] = [],
attrs? : Array[(String, String)] = [],
props? : Array[(String, String)] = [],
style? : String = "",
handlers? : Handlers[M] = Handlers::none(),
children? : Array[Node[M]] = [],
) -> Node[M] {
El({ tag, key, classes, attrs, props, style, handlers, children })
}
///|
pub fn[M] text(s : String) -> Node[M] {
Text(s)
}
///|
/// `cls("a", on=false)` is the empty class, so callers can build lists with `filter`.
pub fn cls_if(name : String, on : Bool) -> String {
if on { name } else { "" }
}
///|
fn escape_html(s : String) -> String {
let b = StringBuilder()
for c in s {
match c {
'&' => b.write_string("&amp;")
'<' => b.write_string("&lt;")
'>' => b.write_string("&gt;")
'"' => b.write_string("&quot;")
_ => b.write_char(c)
}
}
b.to_string()
}
///|
fn[M] handler_names(h : Handlers[M]) -> String {
let names : Array[String] = []
if h.down is Some(_) {
names.push("down")
}
if h.tap is Some(_) {
names.push("tap")
}
if h.press is Some(_) {
names.push("press")
}
if h.enter is Some(_) {
names.push("enter")
}
if h.escape is Some(_) {
names.push("escape")
}
if h.input is Some(_) {
names.push("input")
}
names.join(" ")
}
///|
/// Render a node as HTML text, one element per line (children indented), for tests.
/// Handlers show as an `on="tap press"` attribute.
pub fn[M] to_html(node : Node[M], indent? : Int = 0) -> String {
let pad = " ".repeat(indent)
match node {
Text(s) => pad + escape_html(s)
El(e) => {
let b = StringBuilder()
b.write_string("\{pad}<\{e.tag}")
let classes = e.classes.filter(fn(c) { c != "" })
if classes.length() > 0 {
b.write_string(" class=\"\{classes.join(" ")}\"")
}
for kv in e.attrs {
b.write_string(" \{kv.0}=\"\{escape_html(kv.1)}\"")
}
for kv in e.props {
b.write_string(" .\{kv.0}=\"\{escape_html(kv.1)}\"")
}
if e.style != "" {
b.write_string(" style=\"\{e.style}\"")
}
let on = handler_names(e.handlers)
if on != "" {
b.write_string(" on=\"\{on}\"")
}
b.write_string(">")
if e.children.length() == 1 && e.children[0] is Text(s) {
b.write_string("\{escape_html(s)}</\{e.tag}>")
} else if e.children.length() == 0 {
b.write_string("</\{e.tag}>")
} else {
for c in e.children {
b.write_string("\n")
b.write_string(to_html(c, indent=indent + 1))
}
b.write_string("\n\{pad}</\{e.tag}>")
}
b.to_string()
}
}
}