1
1
package dotty .tools .scaladoc
2
2
3
+ import scala .scalajs .js
3
4
import org .scalajs .dom ._
4
5
import org .scalajs .dom .ext ._
5
6
7
+ import CodeSnippetsGlobals ._
8
+
6
9
class CodeSnippets :
7
10
8
11
private def getButtonsSection (snippet : html.Element ): Option [html.Div ] = snippet.querySelector(" div.buttons" ) match {
@@ -12,7 +15,7 @@ class CodeSnippets:
12
15
13
16
def enrichSnippets () = document.querySelectorAll(" div.snippet" ).foreach {
14
17
case snippet : html.Element =>
15
- snippet.addEventListener(" click" , e => e.stopPropagation() )
18
+ snippet.addEventListener(" click" , ( e : MouseEvent ) => e.asInstanceOf [js. Dynamic ].fromSnippet = true )
16
19
snippetAnchor(snippet)
17
20
handleHideableCode(snippet)
18
21
handleImportedCode(snippet)
@@ -109,20 +112,41 @@ class CodeSnippets:
109
112
val div = document.createElement(" div" )
110
113
val button = document.createElement(" button" ).asInstanceOf [html.Button ]
111
114
val icon = document.createElement(" i" )
112
- icon.classList.add(" fas" )
113
- icon.classList.add(" fa-play" )
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()
114
127
button.appendChild(icon)
115
128
button.classList.add(" run-button" )
116
- button.addEventListener(" click" , _ => {}) // TODO: Run button #13065
117
- button.disabled = true
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()
142
+ )
118
143
div.appendChild(button)
119
144
div
120
145
}
121
146
val buttonsSection = getButtonsSection(snippet)
122
147
buttonsSection.foreach(s =>
123
148
s.appendChild(copyButton)
124
- // Temporarily disabled
125
- // s.appendChild(runButton)
149
+ if ! snippet.hasAttribute(" hasContext" ) then s.appendChild(runButton)
126
150
)
127
151
}
128
152
0 commit comments