@@ -26,7 +26,7 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
26
26
27
27
wrapper.appendChild(icon)
28
28
wrapper.appendChild(resultA)
29
- wrapper .appendChild(location)
29
+ resultA .appendChild(location)
30
30
wrapper.addEventListener(" mouseover" , {
31
31
case e : MouseEvent => handleHover(wrapper)
32
32
})
@@ -64,6 +64,8 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
64
64
document.body.appendChild(rootDiv)
65
65
input.focus()
66
66
}
67
+ // open the search if the user hits the `s` key when not focused on a text input
68
+ document.body.addEventListener(" keydown" , (e : KeyboardEvent ) => handleGlobalKeyDown(e))
67
69
68
70
val element = createNestingDiv(" search-content" )(
69
71
createNestingDiv(" search-container" )(
@@ -75,7 +77,6 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
75
77
document.getElementById(" scaladoc-searchBar" ).appendChild(element)
76
78
element
77
79
78
-
79
80
private val input : html.Input =
80
81
val element = document.createElement(" input" ).asInstanceOf [html.Input ]
81
82
element.id = " scaladoc-searchbar-input"
@@ -111,6 +112,7 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
111
112
if e.keyCode == 40 then handleArrowDown()
112
113
else if e.keyCode == 38 then handleArrowUp()
113
114
else if e.keyCode == 13 then handleEnter()
115
+ else if e.keyCode == 27 then handleEscape()
114
116
})
115
117
element.id = " scaladoc-searchbar"
116
118
element.appendChild(input)
@@ -151,6 +153,12 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
151
153
selectedElement.click()
152
154
}
153
155
}
156
+ private def handleEscape () = {
157
+ // clear the search input and close the search
158
+ input.value = " "
159
+ handleNewQuery(" " )
160
+ document.body.removeChild(rootDiv)
161
+ }
154
162
155
163
private def handleHover (elem : html.Element ) = {
156
164
val selectedElement = resultsDiv.querySelector(" [selected]" )
@@ -160,4 +168,21 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
160
168
elem.setAttribute(" selected" ," " )
161
169
}
162
170
171
+ private def handleGlobalKeyDown (e : KeyboardEvent ) = {
172
+ // if the user presses the "S" key while not focused on an input, open the search
173
+ if (e.key == " s" || e.key == " /" ) {
174
+ val tag = e.target.asInstanceOf [html.Element ].tagName
175
+ if (tag != " INPUT" && tag != " TEXTAREA" ) {
176
+ if (! document.body.contains(rootDiv)) {
177
+ // Firefox's "quick find" uses "/" as a trigger; prevent that.
178
+ e.preventDefault()
179
+
180
+ document.body.appendChild(rootDiv)
181
+ // if we focus during the event handler, the `s` gets typed into the input
182
+ window.setTimeout(() => input.focus(), 1.0 )
183
+ }
184
+ }
185
+ }
186
+ }
187
+
163
188
handleNewQuery(" " )
0 commit comments