From 3c07bc1966c85de3351602c4e8798fa507c40e49 Mon Sep 17 00:00:00 2001 From: Marcin Aman Date: Thu, 30 Jul 2020 15:05:06 +0200 Subject: Create navigation search component --- .../src/main/kotlin/renderers/html/HtmlRenderer.kt | 3 ++ .../main/kotlin/renderers/html/NavigationPage.kt | 2 +- .../kotlin/renderers/html/htmlPreprocessors.kt | 59 ++++++++++++++++------ 3 files changed, 48 insertions(+), 16 deletions(-) (limited to 'plugins/base/src/main/kotlin/renderers/html') diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 5747c6da..22fb3e63 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -674,6 +674,9 @@ open class HtmlRenderer( div { id = "logo" } + div { + id = "paneSearch" + } div { id = "sideMenu" } diff --git a/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt b/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt index a6a16412..f569ed12 100644 --- a/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt +++ b/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt @@ -38,7 +38,7 @@ class NavigationPage(val root: NavigationNode) : RendererSpecificPage { } } -class NavigationNode( +data class NavigationNode( val name: String, val dri: DRI, val sourceSets: Set, diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt index 407e5899..6e779931 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -1,20 +1,18 @@ package org.jetbrains.dokka.base.renderers.html +import com.fasterxml.jackson.databind.annotation.JsonSerialize +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import kotlinx.html.h1 import kotlinx.html.id import kotlinx.html.table import kotlinx.html.tbody -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.Platform -import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.renderers.sourceSets -import org.jetbrains.dokka.base.resolvers.local.LocationProvider +import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.DEnum import org.jetbrains.dokka.model.DEnumEntry import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.plugin -import org.jetbrains.dokka.plugability.querySingle import org.jetbrains.dokka.transformers.pages.PageTransformer @@ -40,19 +38,45 @@ object SearchPageInstaller : PageTransformer { } object NavigationPageInstaller : PageTransformer { - override fun invoke(input: RootPageNode) = input.modified( - children = input.children + NavigationPage( - input.children.filterIsInstance().single() - .let(NavigationPageInstaller::visit) + private val mapper = jacksonObjectMapper() + + private data class NavigationNodeView( + val name: String, + val label: String = name, + val searchKey: String = name, + @get:JsonSerialize(using = ToStringSerializer::class) val dri: DRI, + val location: String + ) { + companion object { + fun from(node: NavigationNode, location: String): NavigationNodeView = + NavigationNodeView(name = node.name, dri = node.dri, location = location) + } + } + + override fun invoke(input: RootPageNode): RootPageNode { + val nodes = input.children.filterIsInstance().single() + .let(NavigationPageInstaller::visit) + + val page = RendererSpecificResourcePage( + name = "scripts/navigation-pane.json", + children = emptyList(), + strategy = RenderingStrategy.LocationResolvableWrite { resolver -> + val flattened = flattenNavigationNodes(listOf(nodes)) + val view = flattened.map { NavigationNodeView.from(it, resolver(it.dri, it.sourceSets)) } + mapper.writeValueAsString(view) + }) + + return input.modified( + children = input.children + page + NavigationPage(nodes) ) - ) + } private fun visit(page: ContentPage): NavigationNode = NavigationNode( - page.name, - page.dri.first(), - page.sourceSets(), - page.navigableChildren() + name = page.name, + dri = page.dri.first(), + sourceSets = page.sourceSets(), + children = page.navigableChildren() ) private fun ContentPage.navigableChildren(): List = @@ -63,6 +87,11 @@ object NavigationPageInstaller : PageTransformer { children.filter { it is ContentPage && it.documentable is DEnumEntry }.map { visit(it as ContentPage) } else -> emptyList() }.sortedBy { it.name.toLowerCase() } + + private tailrec fun flattenNavigationNodes(nodes: List, acc: List = emptyList()): List { + if(nodes.isEmpty()) return acc + return flattenNavigationNodes(nodes.flatMap { it.children }, nodes.map { it.copy(children = emptyList()) } + acc) + } } object ResourceInstaller : PageTransformer { -- cgit