Skip to content

Commit f215c2c

Browse files
committed
Fix navigation (including browser back button)
1 parent a2476f0 commit f215c2c

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

docs/js/ra-doc-exec.js

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import * as babel from 'https://esm.sh/prettier@3.5.1/plugins/babel';
55
import * as estree from 'https://esm.sh/prettier@3.5.1/plugins/estree';
66
import { marked } from 'https://esm.sh/marked@15.0.7';
77

8-
var tipElement, tipContainer, allMenus, navLinks, versionsLinks;
8+
var allMenus, navLinks, versionsLinks;
99

1010
const showTip = async () => {
11+
const tipElement = document.getElementById('tip');
12+
if (!tipElement) return;
13+
1114
const tips = await getContents('/assets/tips.md');
1215
const features = await getContents('/assets/features.md');
1316
const all = tips.concat(features);
@@ -261,19 +264,21 @@ function buildPageToC() {
261264

262265
function replaceContent(text) {
263266
var tocContainer = document.querySelector('.toc-container');
264-
tocContainer.className =
265-
text.trim() !== ''
266-
? 'toc-container col hide-on-small-only m3'
267-
: 'toc-container';
268-
269-
var tmpElement = document.createElement('div');
270-
tmpElement.innerHTML = text;
271-
272-
toggleDockBlocks(false);
267+
if (tocContainer) {
268+
tocContainer.className =
269+
text.trim() !== ''
270+
? 'toc-container col hide-on-small-only m3'
271+
: 'toc-container';
272+
273+
var tmpElement = document.createElement('div');
274+
tmpElement.innerHTML = text;
275+
}
273276

274-
var content = document.querySelector('.DocSearch-content');
275-
content.innerHTML =
276-
tmpElement.querySelector('.DocSearch-content').innerHTML;
277+
var content = document.querySelector('.container');
278+
var tmpContent = tmpElement.querySelector('.container');
279+
if (content && tmpContent) {
280+
content.innerHTML = tmpContent.innerHTML;
281+
}
277282

278283
window.scrollTo(0, 0);
279284

@@ -285,9 +290,10 @@ function replaceContent(text) {
285290
function changeSelectedMenu() {
286291
var activeMenu = document.querySelector(`.sidenav li.active`);
287292
activeMenu && activeMenu.classList.remove('active');
288-
allMenus
289-
.find(menuEl => menuEl.href === window.location.href)
290-
.parentNode.classList.add('active');
293+
const newActiveMenu = allMenus.find(
294+
menuEl => menuEl.href === window.location.href
295+
);
296+
newActiveMenu && newActiveMenu.parentNode.classList.add('active');
291297
}
292298

293299
function toggleDockBlocks(status) {
@@ -343,11 +349,26 @@ function showNonBeginnerDoc() {
343349
});
344350
}
345351

352+
function hideTips() {
353+
const tipElement = document.getElementById('tip');
354+
const tipContainer = document.getElementById('tip-container');
355+
356+
if (tipElement) {
357+
tipElement.remove();
358+
}
359+
if (tipContainer) {
360+
tipContainer.remove();
361+
}
362+
}
363+
346364
document.addEventListener('DOMContentLoaded', () => {
347365
const beginnerModeTrigger = document.getElementById(
348366
'beginner-mode-trigger'
349367
);
350368

369+
if (window.location.pathname === '/documentation.html') {
370+
}
371+
351372
if (beginnerModeTrigger) {
352373
beginnerModeTrigger.addEventListener('click', () => {
353374
beginnerMode = !beginnerMode;
@@ -383,12 +404,6 @@ document.addEventListener('click', event => {
383404
if (!navLinks.includes(href)) {
384405
return; // not a navigation link
385406
}
386-
if (tipElement) {
387-
tipElement.remove();
388-
}
389-
if (tipContainer) {
390-
tipContainer.remove();
391-
}
392407
window.sessionStorage.setItem(
393408
'scrollIntoView',
394409
link.closest('.sidenav') ? 'false' : 'true'
@@ -406,6 +421,13 @@ document.addEventListener('click', event => {
406421
fetch(href)
407422
.then(res => res.text())
408423
.then(replaceContent)
424+
.then(() => {
425+
if (href.includes('documentation.html')) {
426+
showTip();
427+
} else {
428+
hideTips();
429+
}
430+
})
409431
.then(buildJSCodeBlocksFromTS)
410432
.then(loadNewsletterScript);
411433
// change the URL
@@ -420,29 +442,39 @@ window.addEventListener('popstate', () => {
420442
return;
421443
}
422444
if (window.location.pathname === '/documentation.html') {
423-
document.querySelector('.DocSearch-content').innerHTML = '';
424-
toggleDockBlocks(true);
445+
fetch(window.location.pathname)
446+
.then(res => res.text())
447+
.then(replaceContent)
448+
.then(() => {
449+
document.querySelector('.DocSearch-content').innerHTML = '';
450+
toggleDockBlocks(true);
451+
showTip();
452+
});
425453
} else {
426454
// fetch the new content
427455
fetch(window.location.pathname)
428456
.then(res => res.text())
429457
.then(replaceContent)
458+
.then(() => {
459+
toggleDockBlocks(false);
460+
})
461+
.then(hideTips)
430462
.then(buildJSCodeBlocksFromTS)
431463
.then(loadNewsletterScript);
432464
}
433465
changeSelectedMenu();
434466
});
435467

436468
window.addEventListener('DOMContentLoaded', () => {
437-
tipElement = document.getElementById('tip');
438-
tipContainer = document.getElementById('tip-container');
439469
allMenus = Array.from(document.querySelectorAll(`.sidenav a.nav-link`));
440470
navLinks = allMenus
441471
.filter(link => !link.classList.contains('external'))
442472
.map(link => link.href);
443473
versionsLinks = Array.from(document.querySelectorAll('#versions > li > a'));
444474

445-
if (tipElement) showTip();
475+
if (window.location.pathname === '/documentation.html') {
476+
showTip();
477+
}
446478
buildPageToC();
447479

448480
navigationFitScroll();

0 commit comments

Comments
 (0)