web/ is the js-target MoonBit program: it renders app.view into the DOM with a keyed patcher, turns pointer/touch/key events into app input, runs effects against window.Android or demo stand-ins, and exposes window.launcher.* for the Android shell. tests/e2e.cjs ports the legacy Playwright flows and passes against the build; scripts/build-web.sh assembles dist/web. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
189 lines
4.3 KiB
MoonBit
189 lines
4.3 KiB
MoonBit
///|
|
|
/// Turns `vdom` trees into DOM and patches the DOM to match the next tree: keyed
|
|
/// children are reused (and moved) so a reordered tile keeps its element, and the CSS
|
|
/// transitions on it. Elements with handlers get `data-n`, their number in document
|
|
/// order, which pointer events report back (see `@app.handler_table`).
|
|
|
|
///|
|
|
priv struct RNode {
|
|
dom : Dom
|
|
is_text : Bool
|
|
tag : String
|
|
key : Int
|
|
mut text : String
|
|
mut n : Int
|
|
mut classes : String
|
|
mut attrs : Map[String, String]
|
|
mut props : Map[String, String]
|
|
mut style : String
|
|
mut children : Array[RNode]
|
|
}
|
|
|
|
///|
|
|
fn new_rnode(n : @vdom.Node[@app.Msg]) -> RNode {
|
|
match n {
|
|
Text(s) =>
|
|
{
|
|
dom: dom_text(s),
|
|
is_text: true,
|
|
tag: "",
|
|
key: 0,
|
|
text: s,
|
|
n: -1,
|
|
classes: "",
|
|
attrs: {},
|
|
props: {},
|
|
style: "",
|
|
children: [],
|
|
}
|
|
El(e) =>
|
|
{
|
|
dom: dom_create(e.tag),
|
|
is_text: false,
|
|
tag: e.tag,
|
|
key: e.key,
|
|
text: "",
|
|
n: -1,
|
|
classes: "",
|
|
attrs: {},
|
|
props: {},
|
|
style: "",
|
|
children: [],
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn update_rnode(r : RNode, n : @vdom.Node[@app.Msg], counter : Ref[Int]) -> Unit {
|
|
match n {
|
|
Text(s) =>
|
|
if s != r.text {
|
|
r.text = s
|
|
dom_set_text(r.dom, s)
|
|
}
|
|
El(e) => {
|
|
// numbered first: children come after their parent in document order
|
|
let id = if e.handlers.is_empty() {
|
|
-1
|
|
} else {
|
|
let id = counter.val
|
|
counter.val = id + 1
|
|
id
|
|
}
|
|
if id != r.n {
|
|
r.n = id
|
|
if id < 0 {
|
|
dom_rm_attr(r.dom, "data-n")
|
|
} else {
|
|
dom_attr(r.dom, "data-n", id.to_string())
|
|
}
|
|
}
|
|
let classes = e.classes.filter(fn(c) { c != "" }).join(" ")
|
|
if classes != r.classes {
|
|
r.classes = classes
|
|
dom_class(r.dom, classes)
|
|
}
|
|
let attrs : Map[String, String] = {}
|
|
for kv in e.attrs {
|
|
attrs[kv.0] = kv.1
|
|
}
|
|
for kv in attrs {
|
|
if r.attrs.get(kv.0) != Some(kv.1) {
|
|
dom_attr(r.dom, kv.0, kv.1)
|
|
}
|
|
}
|
|
for kv in r.attrs {
|
|
if !attrs.contains(kv.0) {
|
|
dom_rm_attr(r.dom, kv.0)
|
|
}
|
|
}
|
|
r.attrs = attrs
|
|
for kv in e.props {
|
|
dom_prop(r.dom, kv.0, kv.1)
|
|
}
|
|
if e.style != r.style {
|
|
r.style = e.style
|
|
if e.style == "" {
|
|
dom_rm_attr(r.dom, "style")
|
|
} else {
|
|
dom_attr(r.dom, "style", e.style)
|
|
}
|
|
}
|
|
r.children = patch_children(r.dom, r.children, e.children, counter)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn compatible(r : RNode, n : @vdom.Node[@app.Msg]) -> Bool {
|
|
match n {
|
|
Text(_) => r.is_text
|
|
El(e) => !r.is_text && r.tag == e.tag && r.key == e.key
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn patch_children(
|
|
parent : Dom,
|
|
old : Array[RNode],
|
|
new : Array[@vdom.Node[@app.Msg]],
|
|
counter : Ref[Int],
|
|
) -> Array[RNode] {
|
|
let used : Array[Bool] = Array::make(old.length(), false)
|
|
let by_key : Map[Int, Int] = {}
|
|
for i in 0..<old.length() {
|
|
if old[i].key != 0 {
|
|
by_key[old[i].key] = i
|
|
}
|
|
}
|
|
let out : Array[RNode] = []
|
|
for n in new {
|
|
let mut found = -1
|
|
match n {
|
|
El(e) if e.key != 0 =>
|
|
if by_key.get(e.key) is Some(i) && !used[i] && compatible(old[i], n) {
|
|
found = i
|
|
}
|
|
_ =>
|
|
for i in 0..<old.length() {
|
|
if !used[i] && old[i].key == 0 && compatible(old[i], n) {
|
|
found = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
let r = if found >= 0 {
|
|
used[found] = true
|
|
old[found]
|
|
} else {
|
|
new_rnode(n)
|
|
}
|
|
update_rnode(r, n, counter)
|
|
out.push(r)
|
|
}
|
|
for i in 0..<old.length() {
|
|
if !used[i] {
|
|
dom_remove(old[i].dom)
|
|
}
|
|
}
|
|
// put the elements in order, last to first, each before the one after it
|
|
for j = out.length() - 1; j >= 0; j = j - 1 {
|
|
if j == out.length() - 1 {
|
|
dom_place(parent, out[j].dom, out[j].dom, false)
|
|
} else {
|
|
dom_place(parent, out[j].dom, out[j + 1].dom, true)
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
/// Patch the children of `root` to `tree`; returns the new state to pass next time.
|
|
fn patch_root(
|
|
root : Dom,
|
|
old : Array[RNode],
|
|
tree : Array[@vdom.Node[@app.Msg]],
|
|
) -> Array[RNode] {
|
|
patch_children(root, old, tree, Ref::new(0))
|
|
}
|