Skip to content

Replace default dokka searchbar with new implemented in Scala.js #11021

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

Merged
merged 8 commits into from
Jan 19, 2021

Conversation

pikinier20
Copy link
Contributor

No description provided.

@pikinier20
Copy link
Contributor Author

@TheElectronWill Could you look at the frontend part? I mean CSS'es and DOM manipulation part (SearchbarComponent.scala file) Maybe there are some obvious optimizations or easy ways to make it look more smooth

Copy link
Contributor

@TheElectronWill TheElectronWill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have much time to review in detail, but I think that a DocumentFragment should be worth it in at least one place.

Copy link
Contributor

@romanowski romanowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scala-js part does not have any tests

package dotty.dokka

object Main extends App {
def initializeSearchbar(): Unit = Searchbar()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we simply call Searchbar() instead of defining method and calling it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

resultsDiv.scrollTop = 0
while (resultsDiv.hasChildNodes()) resultsDiv.removeChild(resultsDiv.lastChild)
val fragment = document.createDocumentFragment()
result.take(100).foreach(fragment.appendChild)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic number

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored

}
loadMoreResults(result.drop(100))

private val logoClick: html.Div =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logoClick seems a bit misleading, maybe searchIcon is better name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

@@ -0,0 +1,19 @@
package dotty.dokka

enum Matchers(func: (PageEntry) => Int) extends Function1[PageEntry, Int]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be more readable if func is implemented in this way:

 enum Matchers extends Function1[PageEntry, Int]:
   case ByName(query: String)
   case ByKind(kind: String)
   
  def apply(p: PageEntry): Int = this match
    case ByName(query) =>
       val nameOption = Option(p.shortName)
       val acronym = p.acronym
       //Edge case for empty query string
       if query == "" then 1 else
         val results = List(
           nameOption.filter(_.contains(query.toLowerCase)).fold(-1)(_.size - query.size),
           acronym.filter(_.contains(query)).fold(-1)(_.size - query.size + 1)
         )
          if results.forall(_ == -1) then -1 else results.filter(_ != -1).min
   case ByKind(kind) =>
      p.fullName.split(" ").headOption match
        case world if world.equalsIgnoreCase(kind) => 1 // kind matches
        case _ => -1



Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to try here some Scala3 tricks, but apparently it didn't make things clearer


class SearchbarEngine(pages: List[PageEntry]) {
def query(query: List[Matchers]): List[PageEntry] = {
pages.map(p => p -> query.map(_(p))).filterNot(_(1).exists(_ < 0)).sortBy(_(1)).map(_(0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is rather hard to understand, maybe we can split it into methods or multiple lines?

Also using code like .sortBy{ case (_, measure) => measure} helps a lot when working with traits

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, little monster here. I've refactored this code to make it more readable.

@@ -53,8 +60,11 @@ class ScalaSearchbarDataInstaller(val ctx: DokkaContext) extends SearchbarDataIn
pages.addOne(p.getName + link, PageEntry(p.getName, p.getName, link, ""))
}

private def createAcronym(s: String): Option[String] =
s.headOption.filter(_.isUpper).map(_ => s.filter(_.isUpper))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. can we move this part to scalaJS?
  2. fuzzt search works with names starting with lowerCase like lowerCaseName. lowerCaseName will be matched if user provides e.g. loCN or "CN" or lCasNam

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved creating acronym to JS

@pikinier20 pikinier20 requested a review from romanowski January 15, 2021 09:31
@romanowski
Copy link
Contributor

@pikinier20 please create a followup PR with tests

@romanowski romanowski merged commit 23ba6c3 into scala:master Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants