-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #571 from geonetwork/GSGCT-55-hyperlinks-clickable
Datahub / make hyperlinks clickable in "lineage" and "usage conditions"
- Loading branch information
Showing
5 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
libs/ui/elements/src/lib/metadata-info/linkify.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { TestBed, ComponentFixture, waitForAsync } from '@angular/core/testing' | ||
import { Component, DebugElement } from '@angular/core' | ||
import { By } from '@angular/platform-browser' | ||
import { GnUiLinkifyDirective } from './linkify.directive' | ||
|
||
@Component({ | ||
template: `<div [gnUiLinkify]>Click this link https://www.example.com</div>`, | ||
}) | ||
class TestComponent {} | ||
|
||
describe('GnUiLinkifyDirective', () => { | ||
let fixture: ComponentFixture<TestComponent> | ||
let component: TestComponent | ||
let debugElement: DebugElement | ||
|
||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [GnUiLinkifyDirective, TestComponent], | ||
}) | ||
|
||
fixture = TestBed.createComponent(TestComponent) | ||
component = fixture.componentInstance | ||
debugElement = fixture.debugElement.query( | ||
By.directive(GnUiLinkifyDirective) | ||
) | ||
|
||
fixture.detectChanges() | ||
})) | ||
|
||
it('should create an anchor element with the correct href', () => { | ||
fixture.detectChanges() | ||
const anchorElement = debugElement.query(By.css('a')) | ||
|
||
const href = anchorElement.nativeElement.getAttribute('href') | ||
expect(href).toBe('https://www.example.com') | ||
}) | ||
|
||
it('should have the target attribute set to "_blank"', () => { | ||
fixture.detectChanges() | ||
const anchorElement = debugElement.query(By.css('a')) | ||
|
||
const target = anchorElement.nativeElement.getAttribute('target') | ||
expect(target).toBe('_blank') | ||
}) | ||
}) |
41 changes: 41 additions & 0 deletions
41
libs/ui/elements/src/lib/metadata-info/linkify.directive.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
componentWrapperDecorator, | ||
Meta, | ||
moduleMetadata, | ||
StoryObj, | ||
} from '@storybook/angular' | ||
import { GnUiLinkifyDirective } from './linkify.directive' | ||
|
||
export default { | ||
title: 'Elements/GnUiLinkifyDirective', | ||
decorators: [ | ||
moduleMetadata({ | ||
declarations: [GnUiLinkifyDirective], | ||
}), | ||
], | ||
} as Meta<GnUiLinkifyDirective> | ||
|
||
export const Primary: StoryObj<any> = { | ||
args: { | ||
htmlContent: `Région Hauts-de-France, Dreal, IGN BD Topo<br> | ||
Les données produites s'appuient sur le modèle CNIG de juin 2018 relatif aux SCoT : http://cnig.gouv.fr/wp-content/uploads/2019/04/190315_Standard_CNIG_SCOT.pdf<br> | ||
La structure a été modifiée au 03/2023 pour prendre en compte les évolutions du modèle CNIG du 10/06/2021 :<br> | ||
http://cnig.gouv.fr/IMG/pdf/210615_standard_cnig_nouveauscot.pdf<br> | ||
(il coexiste donc dans le modèle des champs liés aux deux modèles, par exemple sur les PADD pour les "anciens" SCoT, ou encore sur les PAS ou les DAAC pour les "nouveaux" SCoT)`, | ||
}, | ||
argTypes: { | ||
htmlContent: { | ||
control: 'text', | ||
}, | ||
}, | ||
render: (args) => ({ | ||
props: args, | ||
template: ` | ||
<div | ||
gnUiLinkify> | ||
${args.htmlContent} | ||
</div>`, | ||
}), | ||
} |
38 changes: 38 additions & 0 deletions
38
libs/ui/elements/src/lib/metadata-info/linkify.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint-disable @angular-eslint/directive-selector */ | ||
import { Directive, ElementRef, Renderer2, OnInit } from '@angular/core' | ||
|
||
@Directive({ | ||
selector: '[gnUiLinkify]', | ||
}) | ||
export class GnUiLinkifyDirective implements OnInit { | ||
constructor(private el: ElementRef, private renderer: Renderer2) {} | ||
|
||
ngOnInit() { | ||
setTimeout(() => { | ||
this.processLinks() | ||
}, 0) | ||
} | ||
|
||
private processLinks() { | ||
const container = this.el.nativeElement | ||
|
||
const nodes = Array.from(container.childNodes) | ||
nodes.forEach((node) => { | ||
if (node instanceof Text) { | ||
const textNode = node as Text | ||
const linkified = this.linkifyText(textNode.nodeValue) | ||
const span = this.renderer.createElement('span') | ||
span.innerHTML = linkified | ||
container.insertBefore(span, textNode) | ||
container.removeChild(textNode) | ||
} | ||
}) | ||
} | ||
|
||
private linkifyText(text: string): string { | ||
return text.replace(/(\bhttps?:\/\/\S+\b)/g, (match) => { | ||
return `<a href="${match}" target="_blank" | ||
class="text-primary cursor-pointer hover:underline">${match} <mat-icon class="mat-icon !w-[12px] !h-[14px] !text-[14px] opacity-75 material-icons">open_in_new</mat-icon></a>` | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters