Skip to content

Commit

Permalink
feat(scully): scully-content optional router link upgrade scoping, fi… (
Browse files Browse the repository at this point in the history
#1616)

* feat(scully): scully-content optional router link upgrade scoping, fixes #1615
* chore(docs): improve docs #1615
* chore(scully): improve readability #1615
  • Loading branch information
tomastrajan authored Sep 27, 2022
1 parent 602414f commit faf1588
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 12 additions & 0 deletions docs/Reference/ngLib/scully-content-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ The [`scully-content`](https://github.com/scullyio/scully/blob/main/libs/ng-lib/
```

**NOTE:** The [`scully-content`](https://github.com/scullyio/scully/blob/main/libs/ng-lib/src/lib/scully-content/scully-content.component.ts) component does not work inside an `*ngIf` directive.


## Configuration

`localLinksOnly: boolean | ''` - enable scoping of router link upgrade behavior to blog content instead of the whole document. Default is `false`.

```html
<!-- Enable router link upgrade scoping to the blog content instead of whole page -->
<scully-content localLinksOnly></scully-content>
<!-- or -->
<scully-content [localLinksOnly]="true"></scully-content>
```
18 changes: 15 additions & 3 deletions libs/ng-lib/src/lib/scully-content/scully-content.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
Component,
ElementRef,
Inject,
Input,
isDevMode,
OnDestroy,
OnInit,
ViewEncapsulation
ViewEncapsulation,
} from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter, firstValueFrom, tap } from 'rxjs';
Expand Down Expand Up @@ -70,6 +71,11 @@ export class ScullyContentComponent implements OnDestroy, OnInit {

routeSub = this.routeUpdates$.subscribe();

#localLinksOnly = false;
@Input() set localLinksOnly(value: boolean | '') {
this.#localLinksOnly = value === '' || value === true;
}

constructor(
private elmRef: ElementRef,
private srs: ScullyRoutesService,
Expand Down Expand Up @@ -130,7 +136,7 @@ export class ScullyContentComponent implements OnDestroy, OnInit {
*/
return;
}
await firstValueFrom( this.http.get(curPage + '/index.html', { responseType: 'text' }))
await firstValueFrom(this.http.get(curPage + '/index.html', { responseType: 'text' }))
.catch((e) => {
if (isDevMode()) {
/** in devmode (usually in `ng serve`) check the scully server for the content too */
Expand Down Expand Up @@ -169,7 +175,13 @@ export class ScullyContentComponent implements OnDestroy, OnInit {
parent.insertBefore(template.content, this.elm);
parent.insertBefore(end, this.elm);
/** upgrade all hrefs to simulated routelinks (in next microtask) */
setTimeout(() => this.document.querySelectorAll('[href]').forEach(this.upgradeToRoutelink.bind(this)), 10);
setTimeout(() => {
if (this.#localLinksOnly) {
parent.querySelectorAll('[href]').forEach(this.upgradeToRoutelink.bind(this));
} else {
this.document.querySelectorAll('[href]').forEach(this.upgradeToRoutelink.bind(this));
}
}, 10);
// document.querySelectorAll('[href]').forEach(this.upgradeToRoutelink.bind(this));
}

Expand Down

0 comments on commit faf1588

Please sign in to comment.