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>
This commit is contained in:
1 parent
66e8e90da1
commit
28e0284d9b
28 files changed
+4278
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,45 @@
|
||||
// Generated using `moon info`, DON'T EDIT IT
|
||||
package "experiments/launcher/vdom"
|
||||
|
||||
// Values
|
||||
pub fn cls_if(String, Bool) -> String
|
||||
|
||||
pub fn[M] el(String, key? : Int32, classes? : Array[String], attrs? : Array[(String, String)], props? : Array[(String, String)], style? : String, handlers? : Handlers[M], children? : Array[Node[M]]) -> Node[M]
|
||||
|
||||
pub fn[M] text(String) -> Node[M]
|
||||
|
||||
pub fn[M] to_html(Node[M], indent? : Int32) -> String
|
||||
|
||||
// Errors
|
||||
|
||||
// Types and methods
|
||||
pub(all) struct Element[M] {
|
||||
tag : String
|
||||
key : Int32
|
||||
classes : Array[String]
|
||||
attrs : Array[(String, String)]
|
||||
props : Array[(String, String)]
|
||||
style : String
|
||||
handlers : Handlers[M]
|
||||
children : Array[Node[M]]
|
||||
}
|
||||
|
||||
pub(all) struct Handlers[M] {
|
||||
down : M?
|
||||
tap : M?
|
||||
press : M?
|
||||
enter : M?
|
||||
escape : M?
|
||||
input : ((String) -> M)?
|
||||
}
|
||||
pub fn[M] Handlers::is_empty(Self[M]) -> Bool
|
||||
pub fn[M] Handlers::none() -> Self[M]
|
||||
|
||||
pub(all) enum Node[M] {
|
||||
Text(String)
|
||||
El(Element[M])
|
||||
}
|
||||
|
||||
// Type aliases
|
||||
|
||||
// Traits
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
///|
|
||||
/// 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("&")
|
||||
'<' => b.write_string("<")
|
||||
'>' => b.write_string(">")
|
||||
'"' => b.write_string(""")
|
||||
_ => 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
///|
|
||||
test "to_html" {
|
||||
let n : Node[Int] = el(
|
||||
"div",
|
||||
classes=["a", cls_if("b", false), cls_if("c", true)],
|
||||
handlers={ ..Handlers::none(), tap: Some(1) },
|
||||
children=[el("span", children=[text("x < y")]), text("tail")],
|
||||
)
|
||||
inspect(
|
||||
to_html(n),
|
||||
content=(
|
||||
#|<div class="a c" on="tap">
|
||||
#| <span>x < y</span>
|
||||
#| tail
|
||||
#|</div>
|
||||
),
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user