Rewrite the launcher in MoonBit (old code moves to legacy/) (#1)
This commit was merged in pull request #1.
This commit is contained in:
1 parent
7d2bf5996f
commit
2fa7bd745b
150 files changed
+8319
-462
No files matched your search
@@ -0,0 +1 @@
|
||||
|
||||
@@ -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
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
///|
|
||||
/// 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 : Int32
|
||||
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~ : Int32 = 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~ : Int32 = 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[Int32] = 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