Detect internal link on plugin pages

This commit is contained in:
Chocobozzz 2024-06-20 09:32:07 +02:00
parent 1b9b904c18
commit 797c2f432f
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 20 additions and 1 deletions

View File

@ -1 +1 @@
<div #root></div> <div (click)="onRootClick($event)" #root></div>

View File

@ -32,6 +32,25 @@ export class PluginPagesComponent implements OnDestroy, AfterViewInit {
if (this.urlSub) this.urlSub.unsubscribe() if (this.urlSub) this.urlSub.unsubscribe()
} }
onRootClick (event: Event) {
const target = event.target as HTMLElement
if (!target) return
if (target.tagName !== 'A') return
const a = target as HTMLAnchorElement
// Get the href attribute set by the dev, not the one calculated by JS to detect if it's a relative/external link
const href = a.getAttribute('href')
if (a.target !== '_blank' && !href.match(/^https?:\/\//)) {
event.preventDefault()
event.stopPropagation()
this.router.navigateByUrl(href)
}
}
private async loadRoute () { private async loadRoute () {
await this.pluginService.ensurePluginsAreLoaded(this.route.snapshot.data.pluginScope || 'common') await this.pluginService.ensurePluginsAreLoaded(this.route.snapshot.data.pluginScope || 'common')