Skip to content

Commit 39e3fcf

Browse files
committed
Refactor scaladoc-js to use new utils
1 parent cc26468 commit 39e3fcf

File tree

7 files changed

+181
-302
lines changed

7 files changed

+181
-302
lines changed

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ object Build {
12531253

12541254
lazy val `scaladoc-js-contributors` = project.in(file("scaladoc-js/contributors")).
12551255
enablePlugins(DottyJSPlugin).
1256-
dependsOn(`scala3-library-bootstrappedJS`).
1256+
dependsOn(`scaladoc-js-common`).
12571257
settings(
12581258
Test / fork := false,
12591259
scalaJSUseMainModuleInitializer := true,

scaladoc-js/common/src/code-snippets/CodeSnippets.scala

Lines changed: 45 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import scala.scalajs.js
44
import org.scalajs.dom._
55
import org.scalajs.dom.ext._
66

7+
import utils.HTML._
8+
import scala.util.chaining._
9+
710
import CodeSnippetsGlobals._
811

912
class CodeSnippets:
@@ -35,22 +38,13 @@ class CodeSnippets:
3538
case _ =>
3639
}
3740
def createShowHideButton(toggleRoot: html.Element) = {
38-
val div = document.createElement("div")
39-
div.classList.add("snippet-showhide")
40-
val p = document.createElement("p")
41-
p.textContent = "Show collapsed lines"
42-
val showHideButton = document.createElement("label")
43-
showHideButton.classList.add("snippet-showhide-button")
44-
val checkbox = document.createElement("input").asInstanceOf[html.Input]
45-
checkbox.`type` = "checkbox"
46-
val slider = document.createElement("span")
47-
slider.classList.add("slider")
48-
showHideButton.appendChild(checkbox)
49-
showHideButton.appendChild(slider)
50-
checkbox.addEventListener("change", _ => toggleHide(toggleRoot))
51-
div.appendChild(showHideButton)
52-
div.appendChild(p)
53-
div
41+
div(cls := "snippet-showhide")(
42+
label(cls := "snippet-showhide-button")(
43+
input("type" := "checkbox").tap(_.addEventListener("change", _ => toggleHide(toggleRoot))),
44+
span(cls := "slider")
45+
),
46+
p("Show collapsed lines")
47+
)
5448
}
5549

5650
toggleHide(snippet)
@@ -65,8 +59,7 @@ class CodeSnippets:
6559
private def snippetAnchor(snippet: html.Element): Unit = snippet.querySelector(".snippet-meta .snippet-label") match {
6660
case e: html.Element =>
6761
val name = e.textContent.trim
68-
val anchor = document.createElement("a").asInstanceOf[html.Anchor]
69-
anchor.id = s"snippet-$name"
62+
val anchor = a(id := s"snippet-$name")
7063
snippet.insertBefore(anchor, snippet.firstChild)
7164
case _ =>
7265
}
@@ -75,54 +68,42 @@ class CodeSnippets:
7568
val included = snippet.querySelectorAll("code span.include")
7669
val pre = snippet.querySelector("pre")
7770
if included != null && included.nonEmpty && pre != null then {
78-
val includesDiv = document.createElement("div")
79-
includesDiv.classList.add("included-section")
80-
includesDiv.classList.add("hideable")
81-
included
71+
val includes = included
8272
.collect { case e: html.Element => e }
8373
.toList
8474
.filter(_.hasAttribute("name"))
8575
.map(_.getAttribute("name"))
8676
.distinct
8777
.map { name =>
88-
val a = document.createElement("a").asInstanceOf[html.Anchor]
89-
a.classList.add("unselectable")
90-
a.href = s"#snippet-$name"
91-
a.innerHTML = s"included <b>$name</b>"
92-
a
78+
a(cls := "unselectable", href := s"#snippet-$name")(
79+
"included",
80+
b(name)
81+
)
9382
}
94-
.foreach(a => includesDiv.appendChild(a))
83+
84+
val includesDiv = div(cls := "included-section hideable")(includes)
9585

9686
snippet.insertBefore(includesDiv, pre)
9787
}
9888
}
9989

10090
private def copyRunButtons(snippet: html.Element) = {
10191
def copyButton = {
102-
val div = document.createElement("div")
103-
val button = document.createElement("button")
104-
val icon = document.createElement("i")
105-
icon.classList.add("far")
106-
icon.classList.add("fa-clone")
107-
button.appendChild(icon)
108-
button.classList.add("copy-button")
109-
button.addEventListener("click", _ => {
110-
val code = snippet.querySelectorAll("code>span:not(.hidden)")
111-
.map(_.textContent)
112-
.mkString
113-
window.navigator.clipboard.writeText(code)
114-
})
115-
div.appendChild(button)
116-
div
92+
div(
93+
button(cls := "copy-button")(
94+
i(cls := "far fa-clone")
95+
).tap(_.addEventListener("click", _ => {
96+
val code = snippet.querySelectorAll("code>span:not(.hidden)")
97+
.map(_.textContent)
98+
.mkString
99+
window.navigator.clipboard.writeText(code)
100+
}))
101+
)
117102
}
118103
def runButton = {
119-
val div = document.createElement("div").asInstanceOf[html.Div]
120-
val runButton = document.createElement("button").asInstanceOf[html.Button]
121-
val runIcon = document.createElement("i")
122-
runIcon.classList.add("fas")
123-
runIcon.classList.add("fa-play")
124-
runButton.classList.add("run-button")
125-
runButton.appendChild(runIcon)
104+
val runButton = button(cls := "run-button")(
105+
i(cls := "fas fa-play")
106+
)
126107

127108
runButton.addEventListener("click", _ =>
128109
if !runButton.hasAttribute("opened") then {
@@ -148,18 +129,14 @@ class CodeSnippets:
148129
}
149130
)
150131

151-
div.appendChild(runButton)
152-
div
132+
div(runButton)
153133
}
154134
def exitButton = {
155-
val div = document.createElement("div").asInstanceOf[html.Div]
156-
val exitButton = document.createElement("button").asInstanceOf[html.Element]
157-
val exitIcon = document.createElement("i")
158-
exitIcon.classList.toggle("fas")
159-
exitIcon.classList.toggle("fa-times")
160-
exitButton.classList.add("exit-button")
161-
div.style = "display:none;"
162-
exitButton.appendChild(exitIcon)
135+
val exitButton = button(cls := "exit-button")(
136+
i(cls := "fas fa-times")
137+
)
138+
139+
val bdiv = div(style := "display:none;")(exitButton)
163140

164141
exitButton.addEventListener("click", _ =>
165142
snippet.querySelector("pre") match {
@@ -178,22 +155,16 @@ class CodeSnippets:
178155
case btn: html.Element => btn.parentElement.style = "display:none;"
179156
case _ =>
180157
}
181-
div.style = "display:none;"
158+
bdiv.style = "display:none;"
182159
)
183160

184-
div.appendChild(exitButton)
185-
div
161+
bdiv
186162
}
187-
def toScastieButton = {
188-
val div = document.createElement("div").asInstanceOf[html.Div]
189-
val toScastieButton = document.createElement("button").asInstanceOf[html.Element]
190-
val toScastieIcon = document.createElement("i").asInstanceOf[html.Image]
191163

192-
toScastieIcon.classList.add("fas")
193-
toScastieIcon.classList.add("fa-external-link-alt")
194-
toScastieButton.classList.add("to-scastie-button")
195-
div.style = "display:none;"
196-
toScastieButton.appendChild(toScastieIcon)
164+
def toScastieButton = {
165+
val toScastieButton = button(cls := "to-scastie-button")(
166+
i(cls := "fas fa-external-link-alt")
167+
)
197168

198169
toScastieButton.addEventListener("click", _ =>
199170
snippet.querySelector(".embedded-menu li.logo") match {
@@ -202,9 +173,9 @@ class CodeSnippets:
202173
}
203174
)
204175

205-
div.appendChild(toScastieButton)
206-
div
176+
div("style" := "display:none;")(toScastieButton)
207177
}
178+
208179
val buttonsSection = getButtonsSection(snippet)
209180
buttonsSection.foreach(s =>
210181
s.appendChild(copyButton)

scaladoc-js/common/src/utils/html.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object HTML {
1818
val elem: T = elemFactory()
1919
def unpackTags(tags: TagArg*): Unit = tags.foreach {
2020
case e: domhtml.Element => elem.appendChild(e)
21-
case s: String => elem.appendChild(textNode(s.escapeReservedTokens))
21+
case s: String => elem.appendChild(textNode(s))
2222
case elemSeq: (Seq[domhtml.Element | String] @unchecked) => unpackTags(elemSeq*)
2323
}
2424

@@ -45,7 +45,10 @@ object HTML {
4545
.replace("'", "&apos;")
4646

4747
case class Attr(name: String):
48-
def :=(value: String): AppliedAttr = new AppliedAttr(name, value)
48+
def :=(value: String): AppliedAttr = (name, value)
49+
50+
extension (key: String) def :=(value: String): AppliedAttr =
51+
(key, value)
4952

5053
opaque type AppliedAttr = (String, String)
5154

@@ -88,6 +91,8 @@ object HTML {
8891
val th = Tag[domhtml.TableCell]("th")
8992
val tr = Tag[domhtml.TableRow]("tr")
9093
val td = Tag[domhtml.TableCell]("td")
94+
val b = Tag[domhtml.Element]("b")
95+
val i = Tag[domhtml.Element]("i")
9196

9297
val cls = Attr("class")
9398
val href = Attr("href")

scaladoc-js/contributors/src/content-contributors/ContentContributors.scala

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import scala.concurrent.ExecutionContext.Implicits.global
1313
import scala.concurrent.Future
1414
import scala.util.{Success,Failure}
1515

16+
import utils.HTML._
17+
1618
// Contributors widget
1719
// see https://stackoverflow.com/a/19200303/4496364
1820
// Copied from https://github.com/scala/docs.scala-lang/blob/main/resources/js/functions.js and rewritten to Scala.js
@@ -90,21 +92,17 @@ class ContentContributors:
9092
getAuthorsForFilename(Globals.githubContributorsFilename.stripPrefix("/")).onComplete {
9193
case Success(authors) =>
9294
val maybeDiv = Option(document.getElementById("documentation-contributors"))
93-
maybeDiv.foreach { div =>
94-
authors.foreach { case FullAuthor(name, url, img) =>
95-
val divN = document.createElement("div")
96-
val imgN = document.createElement("img").asInstanceOf[html.Image]
97-
imgN.src = img
98-
val autN = document.createElement("a").asInstanceOf[html.Anchor]
99-
autN.href = url
100-
autN.text = name
101-
divN.appendChild(imgN)
102-
divN.appendChild(autN)
103-
div.appendChild(divN)
95+
maybeDiv.foreach { mdiv =>
96+
authors.foreach { case FullAuthor(name, url, imgUrl) =>
97+
val inner = div(
98+
img(src := imgUrl)(),
99+
a(href := url)(name)
100+
)
101+
mdiv.appendChild(inner)
104102
}
105103

106104
if authors.nonEmpty then
107-
div.asInstanceOf[html.Div].parentElement.classList.toggle("hidden")
105+
mdiv.asInstanceOf[html.Div].parentElement.classList.toggle("hidden")
108106
}
109107
case Failure(err) =>
110108
println(s"Couldn't fetch contributors. $err")

0 commit comments

Comments
 (0)