@@ -4,6 +4,9 @@ import scala.scalajs.js
4
4
import org .scalajs .dom ._
5
5
import org .scalajs .dom .ext ._
6
6
7
+ import utils .HTML ._
8
+ import scala .util .chaining ._
9
+
7
10
import CodeSnippetsGlobals ._
8
11
9
12
class CodeSnippets :
@@ -35,22 +38,13 @@ class CodeSnippets:
35
38
case _ =>
36
39
}
37
40
def createShowHideButton (toggleRoot : html.Element ) = {
38
- val div = document.createElement(" div" )
39
- div.classList.add(" snippet-showhide" )
40
- val p = document.createElement(" p" )
41
- p.textContent = " Show collapsed lines"
42
- val showHideButton = document.createElement(" label" )
43
- showHideButton.classList.add(" snippet-showhide-button" )
44
- val checkbox = document.createElement(" input" ).asInstanceOf [html.Input ]
45
- checkbox.`type` = " checkbox"
46
- val slider = document.createElement(" span" )
47
- slider.classList.add(" slider" )
48
- showHideButton.appendChild(checkbox)
49
- showHideButton.appendChild(slider)
50
- checkbox.addEventListener(" change" , _ => toggleHide(toggleRoot))
51
- div.appendChild(showHideButton)
52
- div.appendChild(p)
53
- div
41
+ div(cls := " snippet-showhide" )(
42
+ label(cls := " snippet-showhide-button" )(
43
+ input(" type" := " checkbox" ).tap(_.addEventListener(" change" , _ => toggleHide(toggleRoot))),
44
+ span(cls := " slider" )
45
+ ),
46
+ p(" Show collapsed lines" )
47
+ )
54
48
}
55
49
56
50
toggleHide(snippet)
@@ -65,8 +59,7 @@ class CodeSnippets:
65
59
private def snippetAnchor (snippet : html.Element ): Unit = snippet.querySelector(" .snippet-meta .snippet-label" ) match {
66
60
case e : html.Element =>
67
61
val name = e.textContent.trim
68
- val anchor = document.createElement(" a" ).asInstanceOf [html.Anchor ]
69
- anchor.id = s " snippet- $name"
62
+ val anchor = a(id := s " snippet- $name" )
70
63
snippet.insertBefore(anchor, snippet.firstChild)
71
64
case _ =>
72
65
}
@@ -75,54 +68,42 @@ class CodeSnippets:
75
68
val included = snippet.querySelectorAll(" code span.include" )
76
69
val pre = snippet.querySelector(" pre" )
77
70
if included != null && included.nonEmpty && pre != null then {
78
- val includesDiv = document.createElement(" div" )
79
- includesDiv.classList.add(" included-section" )
80
- includesDiv.classList.add(" hideable" )
81
- included
71
+ val includes = included
82
72
.collect { case e : html.Element => e }
83
73
.toList
84
74
.filter(_.hasAttribute(" name" ))
85
75
.map(_.getAttribute(" name" ))
86
76
.distinct
87
77
.map { name =>
88
- val a = document.createElement(" a" ).asInstanceOf [html.Anchor ]
89
- a.classList.add(" unselectable" )
90
- a.href = s " #snippet- $name"
91
- a.innerHTML = s " included <b> $name</b> "
92
- a
78
+ a(cls := " unselectable" , href := s " #snippet- $name" )(
79
+ " included" ,
80
+ b(name)
81
+ )
93
82
}
94
- .foreach(a => includesDiv.appendChild(a))
83
+
84
+ val includesDiv = div(cls := " included-section hideable" )(includes)
95
85
96
86
snippet.insertBefore(includesDiv, pre)
97
87
}
98
88
}
99
89
100
90
private def copyRunButtons (snippet : html.Element ) = {
101
91
def copyButton = {
102
- val div = document.createElement(" div" )
103
- val button = document.createElement(" button" )
104
- val icon = document.createElement(" i" )
105
- icon.classList.add(" far" )
106
- icon.classList.add(" fa-clone" )
107
- button.appendChild(icon)
108
- button.classList.add(" copy-button" )
109
- button.addEventListener(" click" , _ => {
110
- val code = snippet.querySelectorAll(" code>span:not(.hidden)" )
111
- .map(_.textContent)
112
- .mkString
113
- window.navigator.clipboard.writeText(code)
114
- })
115
- div.appendChild(button)
116
- div
92
+ div(
93
+ button(cls := " copy-button" )(
94
+ i(cls := " far fa-clone" )
95
+ ).tap(_.addEventListener(" click" , _ => {
96
+ val code = snippet.querySelectorAll(" code>span:not(.hidden)" )
97
+ .map(_.textContent)
98
+ .mkString
99
+ window.navigator.clipboard.writeText(code)
100
+ }))
101
+ )
117
102
}
118
103
def runButton = {
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)
104
+ val runButton = button(cls := " run-button" )(
105
+ i(cls := " fas fa-play" )
106
+ )
126
107
127
108
runButton.addEventListener(" click" , _ =>
128
109
if ! runButton.hasAttribute(" opened" ) then {
@@ -148,18 +129,14 @@ class CodeSnippets:
148
129
}
149
130
)
150
131
151
- div.appendChild(runButton)
152
- div
132
+ div(runButton)
153
133
}
154
134
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)
135
+ val exitButton = button(cls := " exit-button" )(
136
+ i(cls := " fas fa-times" )
137
+ )
138
+
139
+ val bdiv = div(style := " display:none;" )(exitButton)
163
140
164
141
exitButton.addEventListener(" click" , _ =>
165
142
snippet.querySelector(" pre" ) match {
@@ -178,22 +155,16 @@ class CodeSnippets:
178
155
case btn : html.Element => btn.parentElement.style = " display:none;"
179
156
case _ =>
180
157
}
181
- div .style = " display:none;"
158
+ bdiv .style = " display:none;"
182
159
)
183
160
184
- div.appendChild(exitButton)
185
- div
161
+ bdiv
186
162
}
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
163
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)
164
+ def toScastieButton = {
165
+ val toScastieButton = button(cls := " to-scastie-button" )(
166
+ i(cls := " fas fa-external-link-alt" )
167
+ )
197
168
198
169
toScastieButton.addEventListener(" click" , _ =>
199
170
snippet.querySelector(" .embedded-menu li.logo" ) match {
@@ -202,9 +173,9 @@ class CodeSnippets:
202
173
}
203
174
)
204
175
205
- div.appendChild(toScastieButton)
206
- div
176
+ div(" style" := " display:none;" )(toScastieButton)
207
177
}
178
+
208
179
val buttonsSection = getButtonsSection(snippet)
209
180
buttonsSection.foreach(s =>
210
181
s.appendChild(copyButton)
0 commit comments