Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse search query param in Scaladoc #19669

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,7 @@ object Build {
lazy val `scaladoc-js-common` = project.in(file("scaladoc-js/common")).
enablePlugins(DottyJSPlugin).
dependsOn(`scala3-library-bootstrappedJS`).
settings(libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "1.1.0").cross(CrossVersion.for3Use2_13))
settings(libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "2.8.0"))

lazy val `scaladoc-js-main` = project.in(file("scaladoc-js/main")).
enablePlugins(DottyJSPlugin).
Expand All @@ -1704,7 +1704,7 @@ object Build {
settings(
Test / fork := false,
scalaJSUseMainModuleInitializer := true,
libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "1.1.0").cross(CrossVersion.for3Use2_13)
libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "2.8.0")
)

def generateDocumentation(configTask: Def.Initialize[Task[GenerationConfig]]) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import org.scalajs.dom.ext._

import scala.util.matching.Regex._
import scala.util.matching._
import org.scalajs.dom.ext.Ajax
import scala.scalajs.js.JSON
import org.scalajs.dom._
import scala.scalajs.js
import scala.scalajs.js.JSON
import scala.scalajs.js.Thenable.Implicits.thenable2future

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
Expand Down Expand Up @@ -55,8 +56,8 @@ class ContentContributors:
def linkForFilename(filename: String) = githubContributorsUrl() + s"/commits?path=$filename"
def getAuthorsForFilename(filename: String): Future[List[FullAuthor]] = {
val link = linkForFilename(filename)
Ajax.get(link).map(_.responseText).flatMap { json =>
val res = JSON.parse(json).asInstanceOf[Commits]
fetch(link).flatMap(_.json()).flatMap { json =>
val res = json.asInstanceOf[Commits]
val authors = res.map { commit =>
commit.author match
case null =>
Expand All @@ -79,8 +80,8 @@ class ContentContributors:
}
}
def findRename(link: String, filename: String): Future[Option[String]] = {
Ajax.get(link).map(_.responseText).map { json =>
val res = JSON.parse(json).asInstanceOf[CommitDescription]
fetch(link).flatMap(_.json()).map { json =>
val res = json.asInstanceOf[CommitDescription]
val files = res.files
files
.find(_.filename == filename)
Expand Down
18 changes: 13 additions & 5 deletions scaladoc-js/main/src/searchbar/SearchbarComponent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import java.net.URI
class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearchEngine, parser: QueryParser):
val initialChunkSize = 5
val resultsChunkSize = 20

val querySearch = Option(URLSearchParams(window.location.search).get("search")).filter(_.nonEmpty)

def pathToRoot() = window.document.documentElement.getAttribute("data-pathToRoot")
extension (p: PageEntry)
def toHTML(boldChars: Set[Int]) =
Expand Down Expand Up @@ -148,7 +151,7 @@ class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearc
val htmlEntries = results.map(result => result.pageEntry.toHTML(result.indices))
val loadMoreElement = createLoadMoreElement

def loadMoreResults(entries: List[raw.HTMLElement]): Unit = {
def loadMoreResults(entries: List[HTMLElement]): Unit = {
loadMoreElement.onclick = (event: Event) => {
entries.take(resultsChunkSize).foreach(_.classList.remove("hidden"))
val nextElems = entries.drop(resultsChunkSize)
Expand Down Expand Up @@ -192,7 +195,7 @@ class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearc
}
}

def createLoadingAnimation: raw.HTMLElement =
def createLoadingAnimation: HTMLElement =
div(cls := "loading-wrapper")(
div(cls := "loading")
)
Expand Down Expand Up @@ -262,7 +265,8 @@ class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearc
document.body.addEventListener("keydown", (e: KeyboardEvent) => handleGlobalKeyDown(e))

private val inputElem: html.Input =
input(cls := "scaladoc-searchbar-input", `type` := "search", `placeholder`:= "Find anything").tap { element =>
val initialValue = querySearch.getOrElse("")
input(cls := "scaladoc-searchbar-input", `type` := "search", `placeholder`:= "Find anything", value := initialValue).tap { element =>
element.addEventListener("input", { e =>
clearTimeout(timeoutHandle)
val inputValue = e.target.asInstanceOf[html.Input].value
Expand Down Expand Up @@ -346,7 +350,7 @@ class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearc
val selectedElement = resultsDiv.querySelector("[selected]")
if selectedElement != null then {
selectedElement.removeAttribute("selected")
def recur(elem: raw.Element): raw.Element = {
def recur(elem: Element): Element = {
val prev = elem.previousElementSibling
if prev == null then null
else {
Expand All @@ -366,7 +370,7 @@ class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearc
}
private def handleArrowDown() = {
val selectedElement = resultsDiv.querySelector("[selected]")
def recur(elem: raw.Element): raw.Element = {
def recur(elem: Element): Element = {
val next = elem.nextElementSibling
if next == null then null
else {
Expand Down Expand Up @@ -453,3 +457,7 @@ class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearc
}

inputElem.dispatchEvent(new Event("input"))
if (querySearch.isDefined && !document.body.contains(rootDiv)) {
document.body.appendChild(rootDiv)
inputElem.focus()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dotty.tools.scaladoc

import scala.io.Source
import dotty.tools.scaladoc.PageEntry
import org.scalajs.dom.webworkers.Worker
import org.scalajs.dom._
import scala.scalajs.js.{ JSON, Dynamic }
import scala.collection.mutable.ListBuffer
Expand Down
7 changes: 4 additions & 3 deletions scaladoc-js/main/src/versions-dropdown/DropdownHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import scala.util.{Success,Failure}
import org.scalajs.dom._
import org.scalajs.dom.ext._
import scala.scalajs.js.annotation.JSExportTopLevel
import org.scalajs.dom.ext.Ajax
import org.scalajs.dom._
import scala.scalajs.js
import scala.scalajs.js.JSON
import scala.scalajs.js.Thenable.Implicits.thenable2future

import utils.HTML._

Expand All @@ -33,7 +34,7 @@ class DropdownHandler:
btn.classList.add("disabled")
btn.classList.add("hidden")

private def getURLContent(url: String): Future[String] = Ajax.get(url).map(_.responseText)
private def getURLContent(url: String): Future[String] = fetch(url).flatMap(_.text())

window.sessionStorage.getItem(KEY) match
case null => // If no key, returns null
Expand Down Expand Up @@ -68,7 +69,7 @@ end DropdownHandler
def dropdownHandler(e: Event) =
e.stopPropagation()
if document.getElementById("version-dropdown").getElementsByTagName("a").size > 0 &&
window.getSelection.toString.length == 0 then
window.getSelection().toString.length == 0 then
document.getElementById("version-dropdown").classList.toggle("expanded")
document.getElementById("dropdown-trigger").classList.toggle("selected")

Expand Down
Loading