mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 05:26:59 +01:00
Add HtmlBuilder
This commit is contained in:
parent
c523ba7858
commit
4be9831edd
2 changed files with 32 additions and 0 deletions
30
core/src/scalive/HtmlBuilder.scala
Normal file
30
core/src/scalive/HtmlBuilder.scala
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package scalive
|
||||
|
||||
import java.io.StringWriter
|
||||
|
||||
object HtmlBuilder:
|
||||
|
||||
def build(lv: LiveView[?]): String =
|
||||
val strw = new StringWriter()
|
||||
build(lv.static, lv.dynamic, strw)
|
||||
strw.toString()
|
||||
|
||||
private def build(
|
||||
static: Seq[String],
|
||||
dynamic: Seq[LiveMod[?]],
|
||||
strw: StringWriter
|
||||
): Unit =
|
||||
for i <- dynamic.indices do
|
||||
strw.append(static(i))
|
||||
dynamic(i) match
|
||||
case mod: LiveMod.Dynamic[?, ?] =>
|
||||
strw.append(mod.currentValue.toString)
|
||||
case mod: LiveMod.When[?] => build(mod, strw)
|
||||
case mod: LiveMod.Split[?, ?] => build(mod, strw)
|
||||
strw.append(static.last)
|
||||
|
||||
private def build(mod: LiveMod.When[?], strw: StringWriter): Unit =
|
||||
if mod.displayed then build(mod.nested.static, mod.nested.dynamic, strw)
|
||||
|
||||
private def build(mod: LiveMod.Split[?, ?], strw: StringWriter): Unit =
|
||||
mod.dynamic.foreach(entry => build(mod.static, entry, strw))
|
||||
|
|
@ -17,6 +17,8 @@ def main =
|
|||
)
|
||||
println(lv.fullDiff.toJsonPretty)
|
||||
|
||||
println(HtmlBuilder.build(lv))
|
||||
|
||||
println("Edit first and last")
|
||||
lv.update(
|
||||
MyModel(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue