Skip to content

Commit 99a902b

Browse files
committed
Add hints to searchbar
1 parent 26e4f07 commit 99a902b

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

scaladoc-js/common/css/searchbar.css

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ div[selected] > .scaladoc-searchbar-inkuire-package {
228228
margin: 4px 8px 0px 0px;
229229
}
230230

231-
.scaladoc-searchbar-row:first-of-type {
232-
margin-top: 10px;
233-
}
234-
235231
.scaladoc-searchbar-row[selected] {
236232
background-color: var(--leftbar-hover-bg);
237233
color: var(--leftbar-hover-fg);
@@ -246,6 +242,19 @@ div[selected] > .scaladoc-searchbar-inkuire-package {
246242
padding-left: 20px;
247243
}
248244

245+
.searchbar-hints {
246+
height: 60vh;
247+
display: flex;
248+
flex-direction: column;
249+
align-items: center;
250+
justify-content: center;
251+
}
252+
253+
.searchbar-hints-list {
254+
font-size: medium;
255+
line-height: 2em;
256+
}
257+
249258
#searchBar {
250259
display: inline-flex;
251260
}

scaladoc-js/main/src/searchbar/SearchbarComponent.scala

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
226226
private val input: html.Input =
227227
val element = document.createElement("input").asInstanceOf[html.Input]
228228
element.id = "scaladoc-searchbar-input"
229-
element.addEventListener("input", (e) => handleNewQuery(e.target.asInstanceOf[html.Input].value))
229+
element.addEventListener("input", { e =>
230+
val inputValue = e.target.asInstanceOf[html.Input].value
231+
if inputValue.isEmpty then showHints()
232+
else handleNewQuery(inputValue)
233+
})
230234
element.autocomplete = "off"
231235
element
232236

@@ -325,7 +329,7 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
325329
private def handleEscape() = {
326330
// clear the search input and close the search
327331
input.value = ""
328-
handleNewQuery("")
332+
showHints()
329333
document.body.removeChild(rootDiv)
330334
}
331335

@@ -354,4 +358,59 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
354358
}
355359
}
356360

357-
handleNewQuery("")
361+
private case class ListRoot(elems: Seq[ListNode])
362+
private case class ListNode(value: String, nested: ListRoot)
363+
364+
private def ul(nodes: ListNode*) = ListRoot(nodes)
365+
private def li(s: String) = ListNode(s, ListRoot(Nil))
366+
private def li(s: String, nested: ListRoot) = ListNode(s, nested)
367+
368+
private def renderList: ListRoot => Option[html.UList] = {
369+
case ListRoot(Nil) => None
370+
case ListRoot(nodes) =>
371+
val list = document.createElement("ul").asInstanceOf[html.UList]
372+
nodes.foreach {
373+
case ListNode(txt, nested) =>
374+
val li = document.createElement("li").asInstanceOf[html.LI]
375+
li.innerHTML = txt
376+
renderList(nested).foreach(li.appendChild)
377+
list.appendChild(li)
378+
}
379+
Some(list)
380+
}
381+
382+
private def showHints() = {
383+
def clearResults() = while (resultsDiv.hasChildNodes()) resultsDiv.removeChild(resultsDiv.lastChild)
384+
val hintsDiv = document.createElement("div").asInstanceOf[html.Div]
385+
hintsDiv.classList.add("searchbar-hints")
386+
val icon = document.createElement("span").asInstanceOf[html.Span]
387+
icon.classList.add("fas")
388+
icon.classList.add("fa-lightbulb")
389+
icon.classList.add("fa-5x")
390+
val header = document.createElement("h1").asInstanceOf[html.Heading]
391+
header.textContent = "A bunch of hints to make your life easier"
392+
val listElements: ListRoot = ul(
393+
li("Type a phrase to search members <b>by name</b> and static sites <b>by title</b>"),
394+
li("Type abbreviations <b>cC, caCa, camCa</b> to search for <b>camelCase</b>"),
395+
li(
396+
"Type a function signature to search for members <b>by signature</b> using Inkuire",
397+
ul(
398+
li("Type <b>String => Int</b> to find <b>String.size</b>, <b>String.toInt</b>"),
399+
li("Type <b>String => String => String</b> to find <b>String.mkString</b>, <b>String.stripPrefix</b>"),
400+
li("Inkuire also finds field accessors. Type <b>Some[A] => A</b> to find <b>Some.value</b>"),
401+
li("For more information about Inkuire see <a href=https://docs.scala-lang.org/scala3/guides/scaladoc/search-engine.html>the documentation</a>"),
402+
li("The availability of this function depends on configuration used to generate Scaladoc")
403+
)
404+
)
405+
)
406+
407+
val list = renderList(listElements).get
408+
list.classList.add("searchbar-hints-list")
409+
hintsDiv.appendChild(icon)
410+
hintsDiv.appendChild(header)
411+
hintsDiv.appendChild(list)
412+
clearResults()
413+
resultsDiv.appendChild(hintsDiv)
414+
}
415+
416+
showHints()

0 commit comments

Comments
 (0)