Skip to content

Commit c52b4ec

Browse files
committed
Fix Scastie CSS. Add option to set scastie sbt configuration
1 parent 47f11b2 commit c52b4ec

File tree

6 files changed

+119
-57
lines changed

6 files changed

+119
-57
lines changed

scaladoc-js/resources/code-snippets.css

+37-20
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ input:checked + .slider:before {
160160
}
161161

162162

163-
.snippet .buttons>:not(:last-child) {
164-
border-left: 2px solid var(--inactive-bg);
163+
.snippet .buttons>:not(:first-child) {
164+
border-right: 2px solid var(--inactive-bg);
165165
}
166166

167167
.snippet .buttons>* {
@@ -219,35 +219,36 @@ input:checked + .slider:before {
219219
height:unset;
220220
}
221221

222-
.snippet .scastie.embedded .editor-container .code .CodeMirror-scroll {
223-
height:unset !important;
224-
min-height: 50px !important;
222+
.snippet .scastie.embedded .app.light .editor-container .code .CodeMirror-scroll {
223+
height:unset;
224+
min-height: 50px;
225225
}
226226

227-
.snippet .scastie .editor-container .console-container .console {
228-
height: unset !important;
227+
.snippet .scastie .app.light .editor-container .console-container .console {
228+
height: unset;
229229
}
230230

231-
.snippet .scastie .CodeMirror-gutters {
231+
.snippet .scastie .app.light .CodeMirror-gutters {
232232
background-color: var(--code-bg) !important;
233+
border-color: var(--code-bg) !important;
233234
}
234235

235-
.snippet .scastie .CodeMirror {
236-
color: var(--code-fg) !important;
237-
background-color: var(--code-bg) !important;
236+
.snippet .scastie .app.light .CodeMirror {
237+
color: var(--code-fg);
238+
background-color: var(--code-bg);
238239
}
239240

240-
.snippet .scastie .embedded-menu > * {
241-
background-color: transparent !important;
242-
color: var(--active-fg) !important;
241+
.snippet .scastie .app.light .embedded-menu > * {
242+
background-color: transparent;
243+
color: var(--active-fg);
243244
width: 64px;
244245
font-size: 1em;
245246
padding: 8px;
246247
position: unset;
247248
}
248249

249-
.snippet .scastie .embedded-menu li:hover {
250-
background-color: var(--active-bg) !important;
250+
.snippet .scastie .app.light .embedded-menu li:hover {
251+
background-color: var(--active-bg);
251252
}
252253

253254
.snippet .scastie .embedded-menu {
@@ -269,21 +270,37 @@ input:checked + .slider:before {
269270
}
270271

271272

272-
.snippet .scastie .output-console pre {
273+
.snippet .scastie .app.light .output-console pre {
273274
color: white;
274275
background-color: rgb(0, 43, 54);
275276
}
276277

277278
.snippet .scastie .app.light .editor-container .handler {
278-
background-color: var(--code-bg) !important;
279+
background-color: var(--code-bg);
279280
}
280281

281282
.snippet .scastie .console-container {
282283
margin-left: 30px;
283284
}
284285

285-
.snippet .scastie .main-panel {
286-
background-color: unset !important;
286+
.snippet .scastie .app.light .main-panel {
287+
background-color: unset;
288+
}
289+
290+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-widget .fold,
291+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-linewidget .compilation-info,
292+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-linewidget .runtime-error,
293+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-linewidget .line,
294+
.snippet .scastie .cm-s-solarized.cm-s-light .CodeMirror-linewidget .inline {
295+
background-color: var(--code-bg);
296+
}
297+
298+
.snippet .scastie .ansi-color-yellow {
299+
color: #b58900;
300+
}
301+
302+
.snippet .fa-warning:before, .fa-exclamation-triangle:before {
303+
color: #b58900;
287304
}
288305

289306
@media(max-width: 576px) {

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))
@@ -121,7 +120,8 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
121120
fromResources ++ urls ++ projectLogo ++ Seq(scaladocVersionFile, dynamicJsData)
122121

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

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

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

165170
def allResources(pages: Seq[Page]): Seq[Resource] = earlyMemberResources ++ memberResources ++ Seq(
166171
dottyRes("favicon.ico"),
@@ -190,7 +195,8 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
190195
dottyRes("images/twitter-icon-white.png"),
191196
dottyRes("images/gitter-icon-black.png"),
192197
dottyRes("images/gitter-icon-white.png"),
193-
searchData(pages)
198+
searchData(pages),
199+
scastieConfiguration(),
194200
)
195201

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

0 commit comments

Comments
 (0)