Skip to content

Commit

Permalink
fix(template): find link use id for scrollToAnchor and init scroll co…
Browse files Browse the repository at this point in the history
…ntainer before set links #OSP-197
  • Loading branch information
why520crazy committed Sep 29, 2021
1 parent 3ee26e2 commit 96fef31
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/template/src/services/toc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ export class TocService {
});
this.highestLevel = this.highestLevel && headerLevel > this.highestLevel ? this.highestLevel : headerLevel;
});
this.linksSubject$.next(links);
this.initializeScrollContainer(scrollContainer);
this.linksSubject$.next(links);
}

initializeScrollContainer(scrollContainerSelector: string) {
Promise.resolve().then(() => {
this.scrollContainer = scrollContainerSelector ? this.document.querySelectorAll(scrollContainerSelector)[0] : window;
this.scrollContainer = scrollContainerSelector ? this.document.querySelectorAll(scrollContainerSelector)[0] : window;

Promise.resolve().then(() => {
if (this.scrollContainer) {
fromEvent(this.scrollContainer, 'scroll')
.pipe(takeUntil(this.destroyed$), debounceTime(10))
Expand Down Expand Up @@ -127,7 +127,7 @@ export class TocService {
this.viewportScroller.scrollToAnchor(urlFragment);
} else {
const link = this.links.find(link => {
return link.name === urlFragment;
return link.id === urlFragment;
});
if (link) {
this.scrollContainer.scrollTop = link.element.offsetTop - 10;
Expand Down

0 comments on commit 96fef31

Please sign in to comment.