mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
2d8635d29d
To increase the ease of development we are moving @angular/docs into the adev directory within this repo. While we are doing this to improve our development experience in the short term, efforts are also in place to maintain a division between this @angular/docs (shared) code and adev itself, so that it can be extracted back out in the future when components is ready to leverage it as well. PR Close #57132
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
/*!
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.dev/license
|
|
*/
|
|
|
|
import {isPlatformBrowser} from '@angular/common';
|
|
import {Directive, ElementRef, OnInit, PLATFORM_ID, inject} from '@angular/core';
|
|
import {isExternalLink} from '../../utils/index';
|
|
import {WINDOW} from '../../providers/index';
|
|
|
|
/**
|
|
* The directive will set target of anchor elements to '_blank' for the external links.
|
|
* We can opt-out this behavior by adding `noBlankForExternalLink` attribute to anchor element.
|
|
*/
|
|
@Directive({
|
|
selector: 'a[href]:not([noBlankForExternalLink])',
|
|
host: {
|
|
'[attr.target]': 'target',
|
|
},
|
|
standalone: true,
|
|
})
|
|
export class ExternalLink implements OnInit {
|
|
private readonly anchor: ElementRef<HTMLAnchorElement> = inject(ElementRef);
|
|
private readonly platformId = inject(PLATFORM_ID);
|
|
private readonly window = inject(WINDOW);
|
|
|
|
target?: '_blank' | '_self' | '_parent' | '_top' | '';
|
|
|
|
ngOnInit(): void {
|
|
this.setAnchorTarget();
|
|
}
|
|
|
|
private setAnchorTarget(): void {
|
|
if (!isPlatformBrowser(this.platformId)) {
|
|
return;
|
|
}
|
|
|
|
if (isExternalLink(this.anchor.nativeElement.href, this.window.location.origin)) {
|
|
this.target = '_blank';
|
|
}
|
|
}
|
|
}
|