11package dotty .tools .scaladoc
22
3+ import scala .scalajs .js
34import org .scalajs .dom ._
45import org .scalajs .dom .ext ._
56
7+ import CodeSnippetsGlobals ._
8+
69class CodeSnippets :
10+ lazy val scastieConfig = getScastieConfiguration
11+
12+ private def getScastieConfiguration : js.Dynamic =
13+ js.Dynamic .literal(
14+ sbtConfig = scastieConfiguration,
15+ targetType = " scala3"
16+ )
717
818 private def getButtonsSection (snippet : html.Element ): Option [html.Div ] = snippet.querySelector(" div.buttons" ) match {
919 case div : html.Div => Some (div)
@@ -12,7 +22,7 @@ class CodeSnippets:
1222
1323 def enrichSnippets () = document.querySelectorAll(" div.snippet" ).foreach {
1424 case snippet : html.Element =>
15- snippet.addEventListener(" click" , e => e.stopPropagation() )
25+ snippet.addEventListener(" click" , ( e : MouseEvent ) => e.asInstanceOf [js. Dynamic ].fromSnippet = true )
1626 snippetAnchor(snippet)
1727 handleHideableCode(snippet)
1828 handleImportedCode(snippet)
@@ -106,23 +116,103 @@ class CodeSnippets:
106116 div
107117 }
108118 def runButton = {
109- val div = document.createElement(" div" )
110- val button = document.createElement(" button" ).asInstanceOf [html.Button ]
111- val icon = document.createElement(" i" )
112- icon.classList.add(" fas" )
113- icon.classList.add(" fa-play" )
114- button.appendChild(icon)
115- button.classList.add(" run-button" )
116- button.addEventListener(" click" , _ => {}) // TODO: Run button #13065
117- button.disabled = true
118- div.appendChild(button)
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" ) match {
133+ case btn : html.Element =>
134+ btn.style = " display:none;"
135+ case _ =>
136+ }
137+ snippet.querySelector(" .scastie .embedded-menu .run-button" ) match {
138+ case btn : html.Element => btn.click()
139+ case _ =>
140+ }
141+ snippet.querySelector(" .buttons .exit-button" ) match {
142+ case btn : html.Element => btn.parentElement.style = " "
143+ case _ =>
144+ }
145+ snippet.querySelector(" .buttons .to-scastie-button" ) match {
146+ case btn : html.Element => btn.parentElement.style = " "
147+ case _ =>
148+ }
149+ )
150+
151+ div.appendChild(runButton)
152+ div
153+ }
154+ 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)
163+
164+ exitButton.addEventListener(" click" , _ =>
165+ snippet.querySelector(" pre" ) match {
166+ case p : html.Element => p.style = " "
167+ case _ =>
168+ }
169+ snippet.querySelector(" .scastie.embedded" ) match {
170+ case s : html.Element => snippet.removeChild(s)
171+ case _ =>
172+ }
173+ snippet.querySelector(" .buttons .run-button" ) match {
174+ case btn : html.Element => btn.removeAttribute(" opened" )
175+ case _ =>
176+ }
177+ snippet.querySelector(" .buttons .to-scastie-button" ) match {
178+ case btn : html.Element => btn.parentElement.style = " display:none;"
179+ case _ =>
180+ }
181+ div.style = " display:none;"
182+ )
183+
184+ div.appendChild(exitButton)
185+ div
186+ }
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 ]
191+
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)
197+
198+ toScastieButton.addEventListener(" click" , _ =>
199+ snippet.querySelector(" .embedded-menu li.logo" ) match {
200+ case toScastie : html.Element => toScastie.click()
201+ case _ =>
202+ }
203+ )
204+
205+ div.appendChild(toScastieButton)
119206 div
120207 }
121208 val buttonsSection = getButtonsSection(snippet)
122209 buttonsSection.foreach(s =>
123210 s.appendChild(copyButton)
124- // Temporarily disabled
125- // s.appendChild(runButton)
211+ if ! snippet.hasAttribute(" hasContext" ) then {
212+ s.appendChild(toScastieButton)
213+ s.appendChild(runButton)
214+ s.appendChild(exitButton)
215+ }
126216 )
127217 }
128218
0 commit comments