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>
{% endblock %}
{% 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"] %}
<script src="{{ path | url }}"></script>
{% endfor %}

View File

@ -29,7 +29,8 @@ import {
fromEvent,
map,
of,
switchMap
switchMap,
withLatestFrom
} from "rxjs"
import { configuration } from "~/_"
@ -90,24 +91,39 @@ export function setupVersionSelector(
)
/* Intercept inter-version navigation */
combineLatest([versions$, current$])
versions$
.pipe(
map(([versions, current]) => new Map(versions
.filter(version => version !== current)
.map(version => [
`${new URL(`../${version.version}/`, config.base)}`,
version
])
)),
map(versions => new Map(versions.map(version => [
`${new URL(`../${version.version}/`, config.base)}`,
version
]))),
switchMap(urls => fromEvent<MouseEvent>(document.body, "click")
.pipe(
filter(ev => !ev.metaKey && !ev.ctrlKey),
switchMap(ev => {
withLatestFrom(current$),
switchMap(([ev, current]) => {
ev.preventDefault()
if (ev.target instanceof Element) {
const el = ev.target.closest("a")
if (el && !el.target && urls.has(el.href)) {
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