Skip to content

fix(docs): enable fragment navigation and anchor linking (#3291) #3303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { Meta, Title } from '@angular/platform-browser';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { ViewportScroller } from '@angular/common';
import { filter } from 'rxjs/operators';
import { HOMEPAGE_TITLE, TITLE_SUFFIX } from './constants';

Expand All @@ -17,6 +18,7 @@ export class AppComponent implements OnInit {
private readonly metaService: Meta,
private readonly router: Router,
private readonly activatedRoute: ActivatedRoute,
private readonly viewportScroller: ViewportScroller,
) {}

async ngOnInit() {
Expand All @@ -25,6 +27,7 @@ export class AppComponent implements OnInit {
.subscribe((ev: NavigationEnd) => {
this.updateTitle();
this.updateMeta(ev);
this.handleFragmentScroll(ev);
});
}

Expand Down Expand Up @@ -58,4 +61,20 @@ export class AppComponent implements OnInit {
this.robotsElement = undefined;
}
}

private handleFragmentScroll(event: NavigationEnd) {
const fragment = event.url.split('#')[1];
if (fragment) {
setTimeout(() => {
const element = document.getElementById(fragment);
if (element) {
const offsetTop = element.offsetTop - 100;
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
}
}, 100);
}
}
}
6 changes: 6 additions & 0 deletions src/app/shared/components/toc/toc.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ export class TocComponent implements OnInit, OnDestroy, OnChanges {
}

navigateToAnchor($event: MouseEvent, elementRef: HTMLElement) {
$event.preventDefault();
if (elementRef) {
const offsetTop = elementRef.offsetTop - this.scrollTopOffset;
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
this.findCurrentHeading();
}
}
Expand Down