Skip to content

Commit 0014c5b

Browse files
committed
feat(package): added matLinkPreview directive to find and render links
1 parent 98a2c53 commit 0014c5b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { MatLinkPreviewDirective } from './mat-link-preview.directive';
2+
3+
describe('MatLinkPreviewDirective', () => {
4+
it('should create an instance', () => {
5+
const directive = new MatLinkPreviewDirective();
6+
expect(directive).toBeTruthy();
7+
});
8+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {Directive, OnInit} from '@angular/core';
2+
import {fromEvent} from 'rxjs';
3+
import {debounceTime, distinctUntilChanged, map} from 'rxjs/operators';
4+
import {Link, NgxLinkifyjsService} from 'ngx-linkifyjs';
5+
import {LinkPreviewService} from '../..';
6+
7+
@Directive({
8+
selector: '[matLinkPreview]'
9+
})
10+
export class MatLinkPreviewDirective implements OnInit {
11+
12+
constructor(public linkifyService: NgxLinkifyjsService,
13+
public linkPreviewService: LinkPreviewService) {
14+
}
15+
16+
ngOnInit(): void {
17+
this._init();
18+
}
19+
20+
private _init() {
21+
fromEvent(document, 'input')
22+
.pipe(
23+
debounceTime(2000),
24+
distinctUntilChanged(),
25+
map(event => {
26+
const data = event.target['value'];
27+
const links: Link[] = this.linkifyService.find(data);
28+
console.log('data: ', data);
29+
console.log('links: ', links);
30+
return links;
31+
})).subscribe((links) => {
32+
this.linkPreviewService.onLinkFound.emit(links);
33+
});
34+
}
35+
36+
}

0 commit comments

Comments
 (0)