feat: StringRenderer
This commit is contained in:
parent
6515854b30
commit
0c0ed26408
5 changed files with 68 additions and 43 deletions
16
build.sbt
16
build.sbt
|
@ -27,18 +27,13 @@ generate := DomGenerator.generate()
|
|||
|
||||
(Compile / compile) := ((Compile / compile) dependsOn generate).value
|
||||
|
||||
lazy val root = crossProject(JSPlatform, JVMPlatform)
|
||||
lazy val dom = crossProject(JSPlatform, JVMPlatform)
|
||||
.crossType(CrossType.Pure)
|
||||
.in(file("./dom"))
|
||||
.settings(
|
||||
name := "sfs-dom",
|
||||
version := "0.1.0-SNAPSHOT"
|
||||
)
|
||||
// .settings(
|
||||
// libraryDependencies ++= Seq(
|
||||
// "org.scala-js" % "scalajs-dom" % "2.8.0" % "sjs1_3"
|
||||
// )
|
||||
// )
|
||||
.jvmSettings(
|
||||
libraryDependencies ++= Seq(
|
||||
"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"
|
||||
)
|
||||
)
|
||||
|
||||
lazy val sfs = crossProject(JSPlatform, JVMPlatform)
|
||||
.crossType(CrossType.Pure)
|
||||
.in(file("./sfs"))
|
||||
.settings(
|
||||
name := "sfs",
|
||||
version := "0.1.0-SNAPSHOT"
|
||||
)
|
||||
.dependsOn(dom)
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package top.davidon.sfs.dom
|
||||
|
||||
trait AsValue[F, T] {
|
||||
extension (from: F) {
|
||||
def asStringValue(): Value[F, String]
|
||||
}
|
||||
}
|
|
@ -20,39 +20,23 @@ trait ScalaFullStack
|
|||
|
||||
object svg extends SvgTags with SvgAttrs with ComplexSvgKeys
|
||||
|
||||
given AsValue[String, String] with {
|
||||
extension (from: String) {
|
||||
def asStringValue(): Value[String, String] = {
|
||||
given strToVal: Conversion[String, Value[String, String]] with {
|
||||
def apply(from: String): Value[String, String] =
|
||||
Value(from, StringAsIsCodec)
|
||||
}
|
||||
given intToVal: Conversion[Int, Value[Int, String]] with {
|
||||
def apply(from: Int): Value[Int, String] = Value(from, IntAsStringCodec)
|
||||
}
|
||||
}
|
||||
given AsValue[Int, String] with {
|
||||
extension (from: Int) {
|
||||
def asStringValue(): Value[Int, String] = {
|
||||
Value(from, IntAsStringCodec)
|
||||
}
|
||||
}
|
||||
}
|
||||
given AsValue[Double, String] with {
|
||||
extension (from: Double) {
|
||||
def asStringValue(): Value[Double, String] = {
|
||||
given doubleToVal: Conversion[Double, Value[Double, String]] with {
|
||||
def apply(from: Double): Value[Double, String] =
|
||||
Value(from, DoubleAsStringCodec)
|
||||
}
|
||||
given longToVal: Conversion[Long, Value[Long, String]] with {
|
||||
def apply(from: Long): Value[Long, String] = Value(from, LongAsStringCodec)
|
||||
}
|
||||
}
|
||||
given AsValue[Boolean, String] with {
|
||||
extension (from: Boolean) {
|
||||
def asStringValue(): Value[Boolean, String] = {
|
||||
Value(from, BooleanAsTrueFalseStringCodec)
|
||||
}
|
||||
}
|
||||
}
|
||||
given AsValue[Iterable[String], String] with {
|
||||
extension (from: Iterable[String]) {
|
||||
def asStringValue(): Value[Iterable[String], String] = {
|
||||
given iterToVal: Conversion[Iterable[String], Value[Iterable[String], String]]
|
||||
with {
|
||||
def apply(from: Iterable[String]): Value[Iterable[String], String] =
|
||||
Value(from, IterableAsSpaceSeparatedStringCodec)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,17 @@ package object codecs {
|
|||
|
||||
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] =
|
||||
new Codec[Boolean, String] {
|
||||
|
||||
|
|
|
@ -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}>"
|
||||
}"
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue