1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-11-23 23:21:00 +01:00

Fixed invalid anchor offsets when using instant loading

This commit is contained in:
squidfunk 2021-08-07 11:47:50 +02:00
parent 51d4920a2e
commit adafd844f9
4 changed files with 35 additions and 22 deletions

View File

@ -223,7 +223,7 @@
</script>
{% endblock %}
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.806c4996.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.8fb3741f.min.js' | url }}"></script>
{% for path in config["extra_javascript"] %}
<script src="{{ path | url }}"></script>
{% endfor %}

View File

@ -186,11 +186,23 @@ export function setupInstantLoading(
/* Handle HTML and SVG elements */
if (ev.target instanceof Element) {
const el = ev.target.closest("a")
if (el && !el.target && urls.includes(el.href)) {
ev.preventDefault()
return of({
url: new URL(el.href)
})
if (el && !el.target) {
const url = new URL(el.href)
/* Canonicalize URL */
url.search = ""
url.hash = ""
/* Check if URL should be intercepted */
if (
url.pathname !== location.pathname &&
urls.includes(url.toString())
) {
ev.preventDefault()
return of({
url: new URL(el.href)
})
}
}
}
return NEVER
@ -252,18 +264,6 @@ export function setupInstantLoading(
)
.subscribe(document$)
/* Emit history state change */
merge(push$, pop$)
.pipe(
sample(document$)
)
.subscribe(({ url, offset }) => {
if (url.hash && !offset)
setLocationHash(url.hash)
else
setViewportOffset(offset || { y: 0 })
})
/* Replace meta tags and components */
document$
.pipe(
@ -324,6 +324,19 @@ export function setupInstantLoading(
)
.subscribe()
/* Emit history state change */
merge(push$, pop$)
.pipe(
sample(document$),
)
.subscribe(({ url, offset }) => {
if (url.hash && !offset) {
setLocationHash(url.hash)
} else {
setViewportOffset(offset || { y: 0 })
}
})
/* Debounce update of viewport offset */
viewport$
.pipe(