Skip to content

Instantly share code, notes, and snippets.

@psttf
Created December 22, 2021 21:11
Show Gist options
  • Save psttf/dec7017b162431f54f669956088c7821 to your computer and use it in GitHub Desktop.
Save psttf/dec7017b162431f54f669956088c7821 to your computer and use it in GitHub Desktop.
Sharing scala-dom-types based markup between JVM (scalatags) and JS (OutWatch)
import outwatch.helpers.AttributeBuilder
import outwatch.{Attr, HtmlVNode, VDomModifier}
import scala.language.implicitConversions
object TestComponent extends TestViews(outwatch.dsl) {
type TModifier = VDomModifier
type TRes = VDomModifier
type AtPr = Attr
implicit def tTagOps[A](x: HtmlVNode): TagOps = new TagOps {
override def apply(t: VDomModifier*) = x(t: _*)
}
implicit def stringT(x: String): VDomModifier =
VDomModifier.renderToVDomModifier(x)
implicit def raStringAttrOps(
x: AttributeBuilder[String, Attr],
): AttrOps[String] =
(string: String) => x.assign(string)
}
import com.raquo.domtypes.generic.builders._
import com.raquo.domtypes.generic.codecs.Codec
import com.raquo.domtypes.generic.defs._
import com.raquo.domtypes.generic.defs.complex.canonical.CanonicalComplexHtmlKeys
import scalatags.Text.all._
import scalatags.Text.{Aggregate, Modifier, TypedTag}
import scala.language.implicitConversions
object DomTypes
extends HtmlTagBuilder[TypedTag, String]
with ReflectedHtmlAttrBuilder[SCAttr2]
with HtmlAttrBuilder[SCAttr1]
with PropBuilder[SCAttr2]
with sameRefTags.DocumentTags[TypedTag, String]
with sameRefTags.TextTags[TypedTag, String]
with CanonicalComplexHtmlKeys[SCAttr2, SCAttr1, SCAttr2]
with Aggregate {
def htmlTag[Ref <: String](tagName: String, void: Boolean): TypedTag[Ref] =
TypedTag(tagName, Nil)
def reflectedAttr[V, DomPropV](
attrKey: String,
propKey: String,
attrCodec: Codec[V, String],
propCodec: Codec[V, DomPropV],
): SCAttr2[V, DomPropV] =
scalatags.generic.Attr(attrKey)
def htmlAttr[V](key: String, codec: Codec[V, String]): SCAttr1[V] =
scalatags.generic.Attr(key)
def prop[V, DomV](key: String, codec: Codec[V, DomV]): SCAttr2[V, DomV] =
scalatags.generic.Attr(key)
}
object DTView extends TestViews(DomTypes) {
type TModifier = Modifier
type TRes = Frag
type AtPr = AttrPair
implicit def tTagOps[A <: String](x: TypedTag[A]): DTView.TagOps =
new DTView.TagOps {
override def apply(t: Modifier*) = x(t: _*)
}
implicit def stringT(x: String): Frag =
stringFrag(x)
implicit def raStringAttrOps(
x: SCAttr2[String, String],
): DTView.AttrOps[String] =
(string: String) => x := string
}
import com.raquo.domtypes.generic.defs.complex.canonical.CanonicalComplexHtmlKeys
import com.raquo.domtypes.generic.defs.tags.TextTags
abstract class TestViews[T[
_ <: DomHtmlElement,
], DomHtmlElement, HtmlAnchor <: DomHtmlElement, HtmlElement <: DomHtmlElement, HtmlSpan <: DomHtmlElement, HtmlBr <: DomHtmlElement, HtmlMod <: DomHtmlElement, RA[
_,
_,
], A[_], P[_, _]](
dsl: TextTags[
T,
DomHtmlElement,
HtmlAnchor,
HtmlElement,
HtmlSpan,
HtmlBr,
HtmlMod,
] with CanonicalComplexHtmlKeys[RA, A, P],
) {
type TModifier
type TRes <: TModifier
type AtPr <: TModifier
import dsl._
trait TagOps {
def apply(t: TModifier*): TRes
}
trait AttrOps[V] {
def :=(string: V): AtPr
}
implicit def tTagOps[S <: DomHtmlElement](x: T[S]): TagOps
implicit def stringT(x: String): TModifier
implicit def raStringAttrOps(x: RA[String, String]): AttrOps[String]
def test =
span(cls := "bg-info", "test: ", i("italic"), " ", em("em"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment