Skip to content

Commit d8786c1

Browse files
Merge branch 'scaladoc-fixes' into scaladoc-fixes-part3
1 parent f5521c8 commit d8786c1

File tree

5 files changed

+102
-118
lines changed

5 files changed

+102
-118
lines changed

scaladoc-js/main/src/searchbar/recent/recentQueries.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import scala.scalajs.js
55
class RecentQuery(val query: String, val timestamp: Double) extends js.Object
66

77
object RecentQueryStorage extends SafeLocalStorage[js.Array[RecentQuery]]("__RECENT__QUERIES__", js.Array()) {
8-
val maxEntries = 5
8+
val maxEntries = 3
99

1010
def addEntry(rq: RecentQuery): Unit = {
11-
val newData = getData :+ rq
12-
setData(newData.sortBy(_.timestamp).reverse.distinctBy(_.query).take(maxEntries))
11+
if !getData.exists(_.query.contains(rq.query)) then
12+
val newData = getData.filter(q => !rq.query.contains(q.query)) :+ rq
13+
setData(newData.sortBy(_.timestamp).reverse.distinctBy(_.query).take(maxEntries))
1314
}
1415
}

scaladoc/resources/dotty_res/scripts/ux.js

Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
window.addEventListener("DOMContentLoaded", () => {
1+
let observer = null;
22

3-
var toggler = document.getElementById("leftToggler");
4-
if (toggler) {
5-
toggler.onclick = function () {
6-
document.getElementById("leftColumn").classList.toggle("open");
7-
};
3+
function attachAllListeners() {
4+
if (observer) {
5+
observer.disconnect()
86
}
97

108
var scrollPosition = sessionStorage.getItem("scroll_value");
@@ -60,19 +58,53 @@ window.addEventListener("DOMContentLoaded", () => {
6058
$(this).parent().toggleClass("expanded");
6159
});
6260

61+
document.querySelectorAll('a').forEach(el => {
62+
const href = el.href
63+
if (href === "") { return }
64+
const url = new URL(href)
65+
el.addEventListener('click', e => {
66+
if (url.href.replace("#", "") === window.location.href.replace("#", "")) { return }
67+
if (url.origin !== window.location.origin) { return }
68+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) { return }
69+
e.preventDefault()
70+
e.stopPropagation()
71+
$.get(href, function (data) {
72+
const html = $.parseHTML(data)
73+
const title = html.find(node => node.nodeName === "TITLE").innerText
74+
const bodyDiv = html.find(node => node.nodeName === "DIV")
75+
const { children } = document.body.firstChild
76+
if (window.history.state === null) {
77+
window.history.replaceState({
78+
leftColumn: children[3].innerHTML,
79+
mainDiv: children[6].innerHTML,
80+
title: document.title,
81+
}, '')
82+
}
83+
document.title = title
84+
const leftColumn = bodyDiv.children[3].innerHTML
85+
const mainDiv = bodyDiv.children[6].innerHTML
86+
window.history.pushState({ leftColumn, mainDiv, title }, '', href)
87+
children[3].innerHTML = leftColumn
88+
children[6].innerHTML = mainDiv
89+
attachAllListeners()
90+
})
91+
})
92+
})
93+
6394
$(".ar").on("click", function (e) {
6495
$(this).parent().parent().toggleClass("expanded");
6596
$(this).toggleClass("expanded");
6697
e.stopPropagation();
6798
});
6899

69-
document.querySelectorAll(".nh").forEach((el) =>
70-
el.addEventListener("click", () => {
71-
el.lastChild.click();
72-
el.first.addClass("expanded");
73-
el.parent.addClass("expanded");
74-
}),
75-
);
100+
document.querySelectorAll(".nh").forEach(el => el.addEventListener('click', () => {
101+
if (el.lastChild.href.replace("#", "") === window.location.href.replace("#", "")) {
102+
el.parentElement.classList.toggle("expanded")
103+
el.firstChild.classList.toggle("expanded")
104+
} else {
105+
el.lastChild.click()
106+
}
107+
}))
76108

77109
document.querySelectorAll(".supertypes").forEach((el) =>
78110
el.firstChild.addEventListener("click", () => {
@@ -120,8 +152,6 @@ window.addEventListener("DOMContentLoaded", () => {
120152
observer.observe(section);
121153
});
122154

123-
document.querySelectorAll(".side-menu a").forEach(elem => elem.addEventListener('click', e => e.stopPropagation()))
124-
125155
if (location.hash) {
126156
var target = location.hash.substring(1);
127157
// setting the 'expand' class on the top-level container causes undesireable styles
@@ -134,28 +164,9 @@ window.addEventListener("DOMContentLoaded", () => {
134164
}
135165
}
136166

137-
var logo = document.getElementById("logo");
138-
if (logo) {
139-
logo.onclick = function () {
140-
window.location = pathToRoot; // global variable pathToRoot is created by the html renderer
141-
};
142-
}
143-
144-
document.querySelectorAll('.documentableAnchor').forEach(elem => {
145-
elem.addEventListener('click', event => {
146-
var $temp = $("<input>")
147-
$("body").append($temp)
148-
var a = document.createElement('a')
149-
a.href = $(elem).attr("link")
150-
$temp.val(a.href).select();
151-
document.execCommand("copy")
152-
$temp.remove();
153-
})
154-
})
155-
156-
hljs.registerLanguage("scala", highlightDotty);
157-
hljs.registerAliases(["dotty", "scala3"], "scala");
158-
hljs.initHighlighting();
167+
document.querySelectorAll('pre code').forEach(el => {
168+
hljs.highlightBlock(el);
169+
});
159170

160171
/* listen for the `F` key to be pressed, to focus on the member filter input (if it's present) */
161172
document.body.addEventListener('keydown', e => {
@@ -171,32 +182,44 @@ window.addEventListener("DOMContentLoaded", () => {
171182
}
172183
})
173184

174-
// show/hide side menu on mobile view
175-
const sideMenuToggler = document.getElementById("mobile-sidebar-toggle");
176-
sideMenuToggler.addEventListener('click', _e => {
177-
document.getElementById("leftColumn").classList.toggle("show")
178-
document.getElementById("content").classList.toggle("sidebar-shown")
179-
const toc = document.getElementById("toc");
180-
if(toc) {
181-
toc.classList.toggle("sidebar-shown")
182-
}
183-
sideMenuToggler.classList.toggle("menu-shown")
184-
})
185-
186-
// show/hide mobile menu on mobile view
187-
const mobileMenuOpenIcon = document.getElementById("mobile-menu-toggle");
188-
const mobileMenuCloseIcon = document.getElementById("mobile-menu-close");
189-
mobileMenuOpenIcon.addEventListener('click', _e => {
190-
document.getElementById("mobile-menu").classList.add("show")
191-
})
192-
mobileMenuCloseIcon.addEventListener('click', _e => {
193-
document.getElementById("mobile-menu").classList.remove("show")
194-
})
195-
196-
197185
// when document is loaded graph needs to be shown
186+
}
187+
188+
window.addEventListener("DOMContentLoaded", () => {
189+
hljs.registerLanguage("scala", highlightDotty);
190+
hljs.registerAliases(["dotty", "scala3"], "scala");
191+
attachAllListeners()
198192
});
199193

194+
// show/hide side menu on mobile view
195+
const sideMenuToggler = document.getElementById("mobile-sidebar-toggle")
196+
sideMenuToggler.addEventListener('click', _e => {
197+
document.getElementById("leftColumn").classList.toggle("show")
198+
document.getElementById("content").classList.toggle("sidebar-shown")
199+
const toc = document.getElementById("toc");
200+
if (toc && toc.childElementCount > 0) {
201+
toc.classList.toggle("sidebar-shown")
202+
}
203+
sideMenuToggler.classList.toggle("menu-shown")
204+
})
205+
206+
// show/hide mobile menu on mobile view
207+
document.getElementById("mobile-menu-toggle").addEventListener('click', _e => {
208+
document.getElementById("mobile-menu").classList.add("show")
209+
})
210+
document.getElementById("mobile-menu-close").addEventListener('click', _e => {
211+
document.getElementById("mobile-menu").classList.remove("show")
212+
})
213+
214+
window.addEventListener('popstate', e => {
215+
const { leftColumn, mainDiv, title } = e.state
216+
document.title = title
217+
const { children } = document.body.firstChild
218+
children[3].innerHTML = leftColumn
219+
children[6].innerHTML = mainDiv
220+
attachAllListeners()
221+
})
222+
200223
var zoom;
201224
var transform;
202225

scaladoc/resources/dotty_res/styles/scalastyle.css

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,6 @@ th {
136136
border-bottom: 2px solid var(--border-medium);
137137
}
138138

139-
/* Left bar toggler, only on small screens */
140-
#leftToggler {
141-
display: none;
142-
color: var(--icon-color);
143-
cursor: pointer;
144-
}
145-
146139
/* Left bar */
147140
#paneSearch {
148141
display: none;
@@ -732,30 +725,6 @@ footer .mode {
732725
display: flex;
733726
} */
734727

735-
.documentableAnchor:before {
736-
content: "\e901"; /* arrow down */
737-
font-family: "dotty-icons" !important;
738-
transform: rotate(-45deg);
739-
font-size: 20px;
740-
color: var(--link-fg);
741-
display: none;
742-
flex-direction: row;
743-
align-items: center;
744-
justify-content: center;
745-
position: absolute;
746-
top: 6px;
747-
left: -32px;
748-
}
749-
750-
.documentableAnchor:hover:before {
751-
color: var(--link-hover-fg);
752-
}
753-
754-
.documentableAnchor:active:before {
755-
color: var(--link-hover-fg);
756-
top: 8px;
757-
}
758-
759728
.memberDocumentation {
760729
font-size: 15px;
761730
line-height: 1.5;
@@ -1063,20 +1032,6 @@ footer .socials {
10631032
display: none;
10641033
}
10651034

1066-
#leftToggler {
1067-
display: unset;
1068-
position: absolute;
1069-
top: 5px;
1070-
left: 12px;
1071-
z-index: 5;
1072-
font-size: 30px;
1073-
}
1074-
#leftColumn.open ~ #main #leftToggler {
1075-
position: fixed;
1076-
left: unset;
1077-
right: 16vw;
1078-
color: var(--leftbar-fg);
1079-
}
10801035
.icon-toggler::before {
10811036
content: "\e90a"; /* menu icon */
10821037
}

scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class HtmlRenderer(rootPackage: Member, members: Map[DRI, Member])(using ctx: Do
137137
Seq(
138138
span(cls := s"nh " + attrs.mkString(" "))(
139139
if withArrow then Seq(button(cls := s"ar icon-button ${if isSelected || expanded then "expanded" else ""}")) else Nil,
140-
a(href := pathToPage(pageLink.dri, nav.link.dri))(icon, span(name))
140+
a(href := (if isSelected then "#" else pathToPage(pageLink.dri, nav.link.dri)))(icon, span(name))
141141
)
142142
)
143143

@@ -213,6 +213,9 @@ class HtmlRenderer(rootPackage: Member, members: Map[DRI, Member])(using ctx: Do
213213
div(cls := "breadcrumbs container")(innerTags:_*)
214214

215215
val (apiNavOpt, docsNavOpt): (Option[(Boolean, Seq[AppliedTag])], Option[(Boolean, Seq[AppliedTag])]) = buildNavigation(link)
216+
val isDocs = docsNavOpt match
217+
case Some(true, _) => true
218+
case _ => false
216219

217220
def textFooter: String | AppliedTag =
218221
args.projectFooter.fold("") { f =>
@@ -303,14 +306,16 @@ class HtmlRenderer(rootPackage: Member, members: Map[DRI, Member])(using ctx: Do
303306
parentsHtml,
304307
div(id := "content", cls := "body-medium")(
305308
div(content),
306-
renderTableOfContents(toc).fold(Nil) { toc =>
309+
if isDocs then
307310
div(id := "toc", cls:="body-small")(
308-
div(id := "toc-container") (
309-
span(cls := "toc-title h200")("In this article"),
310-
toc
311-
),
312-
)
313-
},
311+
renderTableOfContents(toc).fold(Nil) { toc =>
312+
div(id := "toc-container")(
313+
span(cls := "toc-title h200")("In this article"),
314+
toc,
315+
)
316+
},
317+
)
318+
else Nil
314319
),
315320
div(id := "footer", cls := "body-small mobile-footer")(
316321
div(cls := "left-container")(

scaladoc/test/dotty/tools/scaladoc/site/NavigationTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class NavigationTest extends BaseHtmlTest:
2525
NavMenuTestEntry("A directory", "dir/index.html", Seq(
2626
NavMenuTestEntry("Nested in a directory", "dir/nested.html", Nil)
2727
)),
28-
NavMenuTestEntry("Adoc", "Adoc.html", Seq())
28+
NavMenuTestEntry("Adoc", "#", Seq())
2929
)
3030

3131
val apiNav = Seq(
32-
NavMenuTestEntry("tests.site", "site.html", Seq(
32+
NavMenuTestEntry("tests.site", "#", Seq(
3333
NavMenuTestEntry("some.other", "site/some/other.html", Seq(
3434
NavMenuTestEntry("SomeOtherPackage", "site/some/other/SomeOtherPackage.html", Nil),
3535
)),

0 commit comments

Comments
 (0)