Skip to content

Commit 14a89bc

Browse files
committed
Fixed eager anchor tracking through debouncing
1 parent a644f57 commit 14a89bc

File tree

6 files changed

+68
-34
lines changed

6 files changed

+68
-34
lines changed

CHANGELOG

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
mkdocs-material-8.0.0 (2021-11-28)
2+
3+
* Added support for code annotations
4+
* Added support for anchor tracking
5+
* Added support for version warning
6+
* Added copyright partial for easier override
7+
* Removed deprecated content tabs legacy implementation
8+
* Removed deprecated seealso admonition type
9+
* Removed deprecated site_keywords setting (unsupported by MkDocs)
10+
* Removed deprecated prebuilt search index support
11+
* Removed deprecated web app manifest – use customization
12+
* Removed extracopyright variable – use new copyright partial
13+
* Removed Disqus integation – use customization
14+
* Switched to :is() selectors for simple selector lists
15+
* Switched autoprefixer from last 4 years to last 2 years
16+
* Improved CSS overall to match modern standards
17+
* Improved CSS variable semantics for fonts
18+
* Improved extensibility by restructuring partials
19+
* Improved handling of details when printing
20+
* Improved keyboard navigation for footnotes
21+
* Fixed #3214: Search highlighting breaks site when empty
22+
123
mkdocs-material-7.3.6+insiders-3.2.3 (2021-11-20)
224

325
* Updated Swedish and French translations

material/assets/javascripts/bundle.c4fbc467.min.js renamed to material/assets/javascripts/bundle.e8a254df.min.js

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/assets/javascripts/bundle.c4fbc467.min.js.map renamed to material/assets/javascripts/bundle.e8a254df.min.js.map

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/base.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@
213213
</script>
214214
{% endblock %}
215215
{% block scripts %}
216-
<script src="{{ 'assets/javascripts/bundle.c4fbc467.min.js' | url }}"></script>
216+
<script src="{{ 'assets/javascripts/bundle.e8a254df.min.js' | url }}"></script>
217217
{% for path in config["extra_javascript"] %}
218218
<script src="{{ path | url }}"></script>
219219
{% endfor %}

mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ theme:
6060
- navigation.tabs
6161
# - navigation.tabs.sticky
6262
- navigation.top
63-
# - navigation.tracking
63+
- navigation.tracking
6464
- search.highlight
6565
- search.share
6666
- search.suggest

src/assets/javascripts/components/toc/index.ts

+36-24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
Subject,
2626
bufferCount,
2727
combineLatest,
28+
debounceTime,
2829
defer,
2930
distinctUntilChanged,
3031
distinctUntilKeyChanged,
@@ -34,7 +35,10 @@ import {
3435
scan,
3536
startWith,
3637
switchMap,
37-
tap
38+
takeLast,
39+
takeUntil,
40+
tap,
41+
withLatestFrom
3842
} from "rxjs"
3943

4044
import { feature } from "~/_"
@@ -243,7 +247,7 @@ export function watchTableOfContents(
243247
* @returns Table of contents component observable
244248
*/
245249
export function mountTableOfContents(
246-
el: HTMLElement, options: MountOptions
250+
el: HTMLElement, { viewport$, header$ }: MountOptions
247251
): Observable<Component<TableOfContents>> {
248252
return defer(() => {
249253
const push$ = new Subject<TableOfContents>()
@@ -265,31 +269,39 @@ export function mountTableOfContents(
265269
index === prev.length - 1
266270
)
267271
}
268-
269-
/* Set up anchor tracking, if enabled */
270-
if (feature("navigation.tracking")) {
271-
const url = getLocation()
272-
273-
/* Set hash fragment to active anchor */
274-
const anchor = prev[prev.length - 1]
275-
if (anchor && anchor.length) {
276-
const [active] = anchor
277-
const { hash } = new URL(active.href)
278-
if (url.hash !== hash) {
279-
url.hash = hash
280-
history.replaceState({}, "", `${url}`)
281-
}
282-
283-
/* Reset anchor when at the top */
284-
} else {
285-
url.hash = ""
286-
history.replaceState({}, "", `${url}`)
287-
}
288-
}
289272
})
290273

274+
/* Set up anchor tracking, if enabled */
275+
if (feature("navigation.tracking"))
276+
viewport$
277+
.pipe(
278+
takeUntil(push$.pipe(takeLast(1))),
279+
debounceTime(250),
280+
distinctUntilKeyChanged("offset"),
281+
withLatestFrom(push$)
282+
)
283+
.subscribe(([, { prev }]) => {
284+
const url = getLocation()
285+
286+
/* Set hash fragment to active anchor */
287+
const anchor = prev[prev.length - 1]
288+
if (anchor && anchor.length) {
289+
const [active] = anchor
290+
const { hash } = new URL(active.href)
291+
if (url.hash !== hash) {
292+
url.hash = hash
293+
history.replaceState({}, "", `${url}`)
294+
}
295+
296+
/* Reset anchor when at the top */
297+
} else {
298+
url.hash = ""
299+
history.replaceState({}, "", `${url}`)
300+
}
301+
})
302+
291303
/* Create and return component */
292-
return watchTableOfContents(el, options)
304+
return watchTableOfContents(el, { viewport$, header$ })
293305
.pipe(
294306
tap(state => push$.next(state)),
295307
finalize(() => push$.complete()),

0 commit comments

Comments
 (0)