Skip to content

Commit 6a45202

Browse files
committed
Add hints to searchbar
1 parent 542ee05 commit 6a45202

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
@@ -234,7 +234,11 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
234234
private val input: html.Input =
235235
val element = document.createElement("input").asInstanceOf[html.Input]
236236
element.id = "scaladoc-searchbar-input"
237-
element.addEventListener("input", (e) => handleNewQuery(e.target.asInstanceOf[html.Input].value))
237+
element.addEventListener("input", { e =>
238+
val inputValue = e.target.asInstanceOf[html.Input].value
239+
if inputValue.isEmpty then showHints()
240+
else handleNewQuery(inputValue)
241+
})
238242
element.autocomplete = "off"
239243
element
240244

@@ -333,7 +337,7 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
333337
private def handleEscape() = {
334338
// clear the search input and close the search
335339
input.value = ""
336-
handleNewQuery("")
340+
showHints()
337341
document.body.removeChild(rootDiv)
338342
}
339343

@@ -362,4 +366,59 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
362366
}
363367
}
364368

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

0 commit comments

Comments
 (0)