@@ -7,6 +7,13 @@ import org.scalajs.dom.ext._
77import CodeSnippetsGlobals ._
88
99class 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
0 commit comments