Fixed same-page version navigation for current version

This commit is contained in:
squidfunk 2022-06-12 11:52:43 +02:00
parent c490983de2
commit 7512441511
4 changed files with 33 additions and 17 deletions

View File

@ -216,7 +216,7 @@
</script> </script>
{% endblock %} {% endblock %}
{% block scripts %} {% block scripts %}
<script src="{{ 'assets/javascripts/bundle.f758a944.min.js' | url }}"></script> <script src="{{ 'assets/javascripts/bundle.431f3d41.min.js' | url }}"></script>
{% for path in config["extra_javascript"] %} {% for path in config["extra_javascript"] %}
<script src="{{ path | url }}"></script> <script src="{{ path | url }}"></script>
{% endfor %} {% endfor %}

View File

@ -29,7 +29,8 @@ import {
fromEvent, fromEvent,
map, map,
of, of,
switchMap switchMap,
withLatestFrom
} from "rxjs" } from "rxjs"
import { configuration } from "~/_" import { configuration } from "~/_"
@ -90,24 +91,39 @@ export function setupVersionSelector(
) )
/* Intercept inter-version navigation */ /* Intercept inter-version navigation */
combineLatest([versions$, current$]) versions$
.pipe( .pipe(
map(([versions, current]) => new Map(versions map(versions => new Map(versions.map(version => [
.filter(version => version !== current) `${new URL(`../${version.version}/`, config.base)}`,
.map(version => [ version
`${new URL(`../${version.version}/`, config.base)}`, ]))),
version
])
)),
switchMap(urls => fromEvent<MouseEvent>(document.body, "click") switchMap(urls => fromEvent<MouseEvent>(document.body, "click")
.pipe( .pipe(
filter(ev => !ev.metaKey && !ev.ctrlKey), filter(ev => !ev.metaKey && !ev.ctrlKey),
switchMap(ev => { withLatestFrom(current$),
switchMap(([ev, current]) => {
ev.preventDefault()
if (ev.target instanceof Element) { if (ev.target instanceof Element) {
const el = ev.target.closest("a") const el = ev.target.closest("a")
if (el && !el.target && urls.has(el.href)) { if (el && !el.target && urls.has(el.href)) {
ev.preventDefault() ev.preventDefault()
return of(el.href) const url = el.href
// This is a temporary hack to detect if a version inside the
// version selector or on another part of the site was clicked.
// If we're inside the version selector, we definitely want to
// find the same page, as we might have different deployments
// due to aliases. However, if we're outside the version
// selector, we must abort here, because we might otherwise
// interfere with instant loading. We need to refactor this
// at some point together with instant loading.
//
// See https://github.com/squidfunk/mkdocs-material/issues/4012
if (!ev.target.closest(".md-version")) {
const version = urls.get(url)!
if (version === current)
return EMPTY
}
return of(url)
} }
} }
return EMPTY return EMPTY