Skip to content

Commit cf3315c

Browse files
committed
Fix Scastie CSS. Add option to set scastie sbt configuration
1 parent f849b91 commit cf3315c

File tree

6 files changed

+118
-57
lines changed

6 files changed

+118
-57
lines changed

scaladoc-js/resources/scaladoc-searchbar.css

+36-20
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ input:checked + .slider:before {
254254
}
255255

256256

257-
.snippet .buttons>:not(:last-child) {
258-
border-left: 2px solid var(--inactive-bg);
257+
.snippet .buttons>:not(:first-child) {
258+
border-right: 2px solid var(--inactive-bg);
259259
}
260260

261261
.snippet .buttons>* {
@@ -313,35 +313,36 @@ input:checked + .slider:before {
313313
height:unset;
314314
}
315315

316-
.snippet .scastie.embedded .editor-container .code .CodeMirror-scroll {
317-
height:unset !important;
318-
min-height: 50px !important;
316+
.snippet .scastie.embedded .app.light .editor-container .code .CodeMirror-scroll {
317+
height:unset;
318+
min-height: 50px;
319319
}
320320

321-
.snippet .scastie .editor-container .console-container .console {
322-
height: unset !important;
321+
.snippet .scastie .app.light .editor-container .console-container .console {
322+
height: unset;
323323
}
324324

325-
.snippet .scastie .CodeMirror-gutters {
325+
.snippet .scastie .app.light .CodeMirror-gutters {
326326
background-color: var(--code-bg) !important;
327+
border-color: var(--code-bg) !important;
327328
}
328329

329-
.snippet .scastie .CodeMirror {
330-
color: var(--code-fg) !important;
331-
background-color: var(--code-bg) !important;
330+
.snippet .scastie .app.light .CodeMirror {
331+
color: var(--code-fg);
332+
background-color: var(--code-bg);
332333
}
333334

334-
.snippet .scastie .embedded-menu > * {
335-
background-color: transparent !important;
336-
color: var(--active-fg) !important;
335+
.snippet .scastie .app.light .embedded-menu > * {
336+
background-color: transparent;
337+
color: var(--active-fg);
337338
width: 64px;
338339
font-size: 1em;
339340
padding: 8px;
340341
position: unset;
341342
}
342343

343-
.snippet .scastie .embedded-menu li:hover {
344-
background-color: var(--active-bg) !important;
344+
.snippet .scastie .app.light .embedded-menu li:hover {
345+
background-color: var(--active-bg);
345346
}
346347

347348
.snippet .scastie .embedded-menu {
@@ -363,23 +364,38 @@ input:checked + .slider:before {
363364
}
364365

365366

366-
.snippet .scastie .output-console pre {
367+
.snippet .scastie .app.light .output-console pre {
367368
color: white;
368369
background-color: rgb(0, 43, 54);
369370
}
370371

371372
.snippet .scastie .app.light .editor-container .handler {
372-
background-color: var(--code-bg) !important;
373+
background-color: var(--code-bg);
373374
}
374375

375376
.snippet .scastie .console-container {
376377
margin-left: 30px;
377378
}
378379

379-
.snippet .scastie .main-panel {
380-
background-color: unset !important;
380+
.snippet .scastie .app.light .main-panel {
381+
background-color: unset;
381382
}
382383

384+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-widget .fold,
385+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-linewidget .compilation-info,
386+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-linewidget .runtime-error,
387+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-linewidget .line,
388+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-linewidget .inline {
389+
background-color: var(--code-bg);
390+
}
391+
392+
.snippet .scastie .ansi-color-yellow {
393+
color: #b58900;
394+
}
395+
396+
.snippet .fa-warning:before, .fa-exclamation-triangle:before {
397+
color: #b58900;
398+
}
383399

384400
@media(max-width: 576px) {
385401
.snippet-showhide {

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

+65-32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import org.scalajs.dom.ext._
77
import CodeSnippetsGlobals._
88

99
class CodeSnippets:
10+
lazy val scastieConfig = getScastieConfiguration
11+
12+
private def getScastieConfiguration: js.Dynamic =
13+
js.Dynamic.literal(
14+
sbtConfig = scastieConfiguration,
15+
targetType = "scala3"
16+
)
1017

1118
private def getButtonsSection(snippet: html.Element): Option[html.Div] = snippet.querySelector("div.buttons") match {
1219
case div: html.Div => Some(div)
@@ -109,44 +116,70 @@ class CodeSnippets:
109116
div
110117
}
111118
def runButton = {
112-
val div = document.createElement("div")
113-
val button = document.createElement("button").asInstanceOf[html.Button]
114-
val icon = document.createElement("i")
115-
def initialState() = {
116-
icon.classList.add("fas")
117-
icon.classList.add("fa-play")
118-
button.setAttribute("state", "run")
119-
}
120-
def toggleState() = {
121-
icon.classList.toggle("fa-play")
122-
icon.classList.toggle("fa-times")
123-
if button.getAttribute("state") == "run" then button.setAttribute("state", "exit")
124-
else button.setAttribute("state", "run")
125-
}
126-
initialState()
127-
button.appendChild(icon)
128-
button.classList.add("run-button")
129-
button.addEventListener("click", _ =>
130-
if button.getAttribute("state") == "run" then
131-
scastie.Embedded(snippet.querySelector("pre"))
132-
else
133-
snippet.querySelector("pre") match {
134-
case p: html.Element => p.style = ""
135-
case _ =>
136-
}
137-
snippet.querySelector(".scastie.embedded") match {
138-
case s: html.Element => snippet.removeChild(s)
139-
case _ =>
140-
}
141-
toggleState()
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)
126+
127+
runButton.addEventListener("click", _ =>
128+
if !runButton.hasAttribute("opened") then {
129+
scastie.Embedded(snippet.querySelector("pre"), scastieConfig)
130+
runButton.setAttribute("opened", "opened")
131+
}
132+
snippet.querySelector(".scastie .embedded-menu .run-button") match {
133+
case btn: html.Element =>
134+
btn.style = "display:none;"
135+
btn.click()
136+
case _ =>
137+
}
138+
snippet.querySelector(".buttons .exit-button") match {
139+
case btn: html.Element => btn.parentElement.style = ""
140+
case _ =>
141+
}
142142
)
143-
div.appendChild(button)
143+
144+
div.appendChild(runButton)
145+
div
146+
}
147+
def exitButton = {
148+
val div = document.createElement("div").asInstanceOf[html.Div]
149+
val exitButton = document.createElement("button").asInstanceOf[html.Element]
150+
val exitIcon = document.createElement("i")
151+
exitIcon.classList.toggle("fas")
152+
exitIcon.classList.toggle("fa-times")
153+
exitButton.classList.add("exit-button")
154+
div.style = "display:none;"
155+
exitButton.appendChild(exitIcon)
156+
157+
exitButton.addEventListener("click", _ =>
158+
snippet.querySelector("pre") match {
159+
case p: html.Element => p.style = ""
160+
case _ =>
161+
}
162+
snippet.querySelector(".scastie.embedded") match {
163+
case s: html.Element => snippet.removeChild(s)
164+
case _ =>
165+
}
166+
snippet.querySelector(".buttons .run-button") match {
167+
case btn: html.Element => btn.removeAttribute("opened")
168+
case _ =>
169+
}
170+
div.style = "display:none;"
171+
)
172+
173+
div.appendChild(exitButton)
144174
div
145175
}
146176
val buttonsSection = getButtonsSection(snippet)
147177
buttonsSection.foreach(s =>
148178
s.appendChild(copyButton)
149-
if !snippet.hasAttribute("hasContext") then s.appendChild(runButton)
179+
if !snippet.hasAttribute("hasContext") then {
180+
s.appendChild(runButton)
181+
s.appendChild(exitButton)
182+
}
150183
)
151184
}
152185

scaladoc-js/src/code-snippets/CodeSnippetsGlobals.scala

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ import scala.scalajs.js.annotation.JSGlobalScope
77
@JSGlobalScope
88
object CodeSnippetsGlobals extends js.Object {
99
val scastie: Scastie = js.native
10+
val scastieConfiguration: String = js.native
1011
}

scaladoc/src/dotty/tools/scaladoc/Scaladoc.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ object Scaladoc:
5858
noLinkWarnings: Boolean = false,
5959
versionsDictionaryUrl: Option[String] = None,
6060
generateInkuire : Boolean = false,
61-
apiSubdirectory : Boolean = false
61+
apiSubdirectory : Boolean = false,
62+
scastieConfiguration: String = ""
6263
)
6364

6465
def run(args: Array[String], rootContext: CompilerContext): Reporter =
@@ -224,6 +225,7 @@ object Scaladoc:
224225
versionsDictionaryUrl.nonDefault,
225226
generateInkuire.get,
226227
apiSubdirectory.get,
228+
scastieConfiguration.get
227229
)
228230
(Some(docArgs), newContext)
229231
}

scaladoc/src/dotty/tools/scaladoc/ScaladocSettings.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,8 @@ class ScaladocSettings extends SettingGroup with AllScalaSettings:
120120
val apiSubdirectory: Setting[Boolean] =
121121
BooleanSetting("-Yapi-subdirectory", "Put the API documentation pages inside a directory `api/`", false)
122122

123+
val scastieConfiguration: Setting[String] =
124+
StringSetting("-scastie-configuration", "Scastie configuration", "Additional configuration passed to Scastie in code snippets", "")
125+
123126
def scaladocSpecificSettings: Set[Setting[_]] =
124-
Set(sourceLinks, syntax, revision, externalDocumentationMappings, socialLinks, skipById, skipByRegex, deprecatedSkipPackages, docRootContent, snippetCompiler, generateInkuire)
127+
Set(sourceLinks, syntax, revision, externalDocumentationMappings, socialLinks, skipById, skipByRegex, deprecatedSkipPackages, docRootContent, snippetCompiler, generateInkuire, scastieConfiguration)

scaladoc/src/dotty/tools/scaladoc/renderers/Resources.scala

+9-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
4545
val end = if param < 0 then url.length else param
4646
val point = url.lastIndexOf('.', end)
4747
url.substring(point+1, end)
48-
4948
for res <- resources yield
5049
fileExtension(res) match
5150
case "css" => link(rel := "stylesheet", href := resolveLink(dri, res))
@@ -120,7 +119,8 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
120119
fromResources ++ urls ++ projectLogo ++ Seq(scaladocVersionFile, dynamicJsData)
121120

122121
val searchDataPath = "scripts/searchData.js"
123-
val memberResourcesPaths = Seq(searchDataPath) ++ memberResources.map(_.path)
122+
val scastieConfigurationPath = "scripts/scastieConfiguration.js"
123+
val memberResourcesPaths = Seq(searchDataPath) ++ Seq(scastieConfigurationPath) ++ memberResources.map(_.path)
124124
val earlyMemberResourcePaths = earlyMemberResources.map(_.path)
125125

126126
def searchData(pages: Seq[Page]) =
@@ -160,6 +160,11 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
160160
val entries = pages.flatMap(processPage)
161161
Resource.Text(searchDataPath, s"pages = ${jsonList(entries)};")
162162

163+
def scastieConfiguration() =
164+
Resource.Text(scastieConfigurationPath, s"scastieConfiguration = \"${
165+
ctx.args.scastieConfiguration.replace('"'.toString, """\"""")
166+
}\"")
167+
163168

164169
def allResources(pages: Seq[Page]): Seq[Resource] = earlyMemberResources ++ memberResources ++ Seq(
165170
dottyRes("favicon.ico"),
@@ -189,7 +194,8 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
189194
dottyRes("images/twitter-icon-white.png"),
190195
dottyRes("images/gitter-icon-black.png"),
191196
dottyRes("images/gitter-icon-white.png"),
192-
searchData(pages)
197+
searchData(pages),
198+
scastieConfiguration(),
193199
)
194200

195201
def renderResource(resource: Resource): Seq[String] =

0 commit comments

Comments
 (0)