feat: StringRenderer

This commit is contained in:
DavidOnTop 2024-09-21 09:38:02 +02:00
parent 6515854b30
commit 0c0ed26408
No known key found for this signature in database
GPG key ID: 5D05538A45D5149F
5 changed files with 68 additions and 43 deletions

View file

@ -27,18 +27,13 @@ generate := DomGenerator.generate()
(Compile / compile) := ((Compile / compile) dependsOn generate).value (Compile / compile) := ((Compile / compile) dependsOn generate).value
lazy val root = crossProject(JSPlatform, JVMPlatform) lazy val dom = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure) .crossType(CrossType.Pure)
.in(file("./dom")) .in(file("./dom"))
.settings( .settings(
name := "sfs-dom", name := "sfs-dom",
version := "0.1.0-SNAPSHOT" version := "0.1.0-SNAPSHOT"
) )
// .settings(
// libraryDependencies ++= Seq(
// "org.scala-js" % "scalajs-dom" % "2.8.0" % "sjs1_3"
// )
// )
.jvmSettings( .jvmSettings(
libraryDependencies ++= Seq( libraryDependencies ++= Seq(
"org.scala-js" % "scalajs-dom_sjs1_3" % "2.8.0" "org.scala-js" % "scalajs-dom_sjs1_3" % "2.8.0"
@ -49,3 +44,12 @@ lazy val root = crossProject(JSPlatform, JVMPlatform)
"org.scala-js" % "scalajs-dom_sjs1_3" % "2.8.0" "org.scala-js" % "scalajs-dom_sjs1_3" % "2.8.0"
) )
) )
lazy val sfs = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("./sfs"))
.settings(
name := "sfs",
version := "0.1.0-SNAPSHOT"
)
.dependsOn(dom)

View file

@ -1,7 +0,0 @@
package top.davidon.sfs.dom
trait AsValue[F, T] {
extension (from: F) {
def asStringValue(): Value[F, String]
}
}

View file

@ -20,39 +20,23 @@ trait ScalaFullStack
object svg extends SvgTags with SvgAttrs with ComplexSvgKeys object svg extends SvgTags with SvgAttrs with ComplexSvgKeys
given AsValue[String, String] with { given strToVal: Conversion[String, Value[String, String]] with {
extension (from: String) { def apply(from: String): Value[String, String] =
def asStringValue(): Value[String, String] = { Value(from, StringAsIsCodec)
Value(from, StringAsIsCodec)
}
}
} }
given AsValue[Int, String] with { given intToVal: Conversion[Int, Value[Int, String]] with {
extension (from: Int) { def apply(from: Int): Value[Int, String] = Value(from, IntAsStringCodec)
def asStringValue(): Value[Int, String] = {
Value(from, IntAsStringCodec)
}
}
} }
given AsValue[Double, String] with { given doubleToVal: Conversion[Double, Value[Double, String]] with {
extension (from: Double) { def apply(from: Double): Value[Double, String] =
def asStringValue(): Value[Double, String] = { Value(from, DoubleAsStringCodec)
Value(from, DoubleAsStringCodec)
}
}
} }
given AsValue[Boolean, String] with { given longToVal: Conversion[Long, Value[Long, String]] with {
extension (from: Boolean) { def apply(from: Long): Value[Long, String] = Value(from, LongAsStringCodec)
def asStringValue(): Value[Boolean, String] = {
Value(from, BooleanAsTrueFalseStringCodec)
}
}
} }
given AsValue[Iterable[String], String] with { given iterToVal: Conversion[Iterable[String], Value[Iterable[String], String]]
extension (from: Iterable[String]) { with {
def asStringValue(): Value[Iterable[String], String] = { def apply(from: Iterable[String]): Value[Iterable[String], String] =
Value(from, IterableAsSpaceSeparatedStringCodec) Value(from, IterableAsSpaceSeparatedStringCodec)
}
}
} }
} }

View file

@ -20,6 +20,17 @@ package object codecs {
override def encode(scalaValue: Double): String = scalaValue.toString override def encode(scalaValue: Double): String = scalaValue.toString
} }
lazy val LongAsIsCodec: Codec[Long, Long] = AsIsCodec()
lazy val LongAsStringCodec: Codec[Long, String] =
new Codec[Long, String] {
override def decode(domValue: String): Long =
domValue.toLong // @TODO this can throw exception. How do we handle this?
override def encode(scalaValue: Long): String = scalaValue.toString
}
lazy val BooleanAsTrueFalseStringCodec: Codec[Boolean, String] = lazy val BooleanAsTrueFalseStringCodec: Codec[Boolean, String] =
new Codec[Boolean, String] { new Codec[Boolean, String] {

View file

@ -0,0 +1,33 @@
package top.davidon.sfs.renderers
import org.scalajs.dom
import top.davidon.sfs.dom.SFS.given
import top.davidon.sfs.dom.{Element, Renderer, Value}
class StringRenderer extends Renderer[String] {
override def render(
elements: Element[dom.Element]*
): String = {
elements.map(renderElement(_)).mkString("")
}
private def renderElement(e: Element[dom.Element]): String = {
val modsStr =
e.mods.map(m => s" ${m.key.name}=\"${m.value()}\"").mkString("")
val bodyStr = e.children
.map {
case e: Element[?] =>
renderElement(e)
case c: Value[?, String] =>
c()
case _ =>
throw Exception(
"attempted to parse child that was neither an Element or Value, this should never happen if it did its a bug"
)
}
.mkString(" ")
s"<${e.tag.name}$modsStr>$bodyStr${
if e.tag.void then "" else s"</${e.tag.name}>"
}"
}
}