@@ -26,7 +26,7 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
2626
2727 wrapper.appendChild(icon)
2828 wrapper.appendChild(resultA)
29- wrapper .appendChild(location)
29+ resultA .appendChild(location)
3030 wrapper.addEventListener(" mouseover" , {
3131 case e : MouseEvent => handleHover(wrapper)
3232 })
@@ -64,6 +64,8 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
6464 document.body.appendChild(rootDiv)
6565 input.focus()
6666 }
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))
6769
6870 val element = createNestingDiv(" search-content" )(
6971 createNestingDiv(" search-container" )(
@@ -75,7 +77,6 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
7577 document.getElementById(" scaladoc-searchBar" ).appendChild(element)
7678 element
7779
78-
7980 private val input : html.Input =
8081 val element = document.createElement(" input" ).asInstanceOf [html.Input ]
8182 element.id = " scaladoc-searchbar-input"
@@ -111,6 +112,7 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
111112 if e.keyCode == 40 then handleArrowDown()
112113 else if e.keyCode == 38 then handleArrowUp()
113114 else if e.keyCode == 13 then handleEnter()
115+ else if e.keyCode == 27 then handleEscape()
114116 })
115117 element.id = " scaladoc-searchbar"
116118 element.appendChild(input)
@@ -151,6 +153,12 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
151153 selectedElement.click()
152154 }
153155 }
156+ private def handleEscape () = {
157+ // clear the search input and close the search
158+ input.value = " "
159+ handleNewQuery(" " )
160+ document.body.removeChild(rootDiv)
161+ }
154162
155163 private def handleHover (elem : html.Element ) = {
156164 val selectedElement = resultsDiv.querySelector(" [selected]" )
@@ -160,4 +168,21 @@ class SearchbarComponent(val callback: (String) => List[PageEntry]):
160168 elem.setAttribute(" selected" ," " )
161169 }
162170
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+
163188 handleNewQuery(" " )
0 commit comments