Skip to content

Commit f8d5bdf

Browse files
committed
feat(package): optimized the UI
1 parent 368014b commit f8d5bdf

10 files changed

+88
-32
lines changed
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
<ng-container *ngFor="let link of linkPreviewService.links; trackBy: trackLinks">
2-
<mat-link-preview [link]="link"></mat-link-preview>
3-
</ng-container>
1+
<ng-container *ngIf="!multiple && linkPreviewService.links.length > 0; then first else list"></ng-container>
2+
3+
<ng-template #first>
4+
<mat-link-preview [link]="linkPreviewService?.links[0]"
5+
[showLoadingsProgress]="showLoadingsProgress">
6+
</mat-link-preview>
7+
</ng-template>
8+
<ng-template #list>
9+
<div *ngFor="let link of linkPreviewService.links; trackBy: trackLinks">
10+
<mat-link-preview [link]="link"
11+
[showLoadingsProgress]="showLoadingsProgress">
12+
</mat-link-preview>
13+
</div>
14+
</ng-template>

src/module/components/mat-link-preview-container/mat-link-preview-container.component.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import {async, ComponentFixture, TestBed} from '@angular/core/testing';
22
import {EventEmitter} from '@angular/core';
33
import {Link} from 'ngx-linkifyjs';
44
import {MatLinkPreviewContainerComponent} from './mat-link-preview-container.component';
5-
import {MatLinkPreviewComponent, MatLinkPreviewModule, MatLinkPreviewService} from '../../..';
5+
import {MatLinkPreviewComponent, MatLinkPreviewService} from '../../..';
6+
import {MatButtonModule, MatCardModule, MatProgressSpinnerModule} from '@angular/material';
7+
import {FormsModule} from '@angular/forms';
68

79
describe('MatLinkPreviewContainerComponent', () => {
810
let component: MatLinkPreviewContainerComponent;
911
let fixture: ComponentFixture<MatLinkPreviewContainerComponent>;
1012
const linkPreviewServicePartial: Partial<MatLinkPreviewService> = {
11-
onLinkFound: new EventEmitter<Array<Link>>()
13+
onLinkFound: new EventEmitter<Array<Link>>(),
14+
links: []
1215
};
1316

1417
beforeEach(async(() => {
1518
TestBed.configureTestingModule({
16-
imports: [MatLinkPreviewModule],
19+
imports: [MatCardModule, MatProgressSpinnerModule, MatButtonModule, FormsModule],
1720
declarations: [MatLinkPreviewContainerComponent, MatLinkPreviewComponent],
1821
providers: [{provide: MatLinkPreviewService, useValue: linkPreviewServicePartial}]
1922
})

src/module/components/mat-link-preview-container/mat-link-preview-container.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, OnInit} from '@angular/core';
1+
import {Component, Input} from '@angular/core';
22
import {Link} from 'ngx-linkifyjs';
33
import {MatLinkPreviewService} from '../../service/mat-link-preview.service';
44

@@ -7,13 +7,14 @@ import {MatLinkPreviewService} from '../../service/mat-link-preview.service';
77
templateUrl: './mat-link-preview-container.component.html',
88
styleUrls: ['./mat-link-preview-container.component.scss']
99
})
10-
export class MatLinkPreviewContainerComponent implements OnInit {
10+
export class MatLinkPreviewContainerComponent {
1111

12-
constructor(public linkPreviewService: MatLinkPreviewService) {
13-
}
12+
// to forward
13+
@Input() color = 'primary'; // accent | warn
14+
@Input() multiple: boolean;
15+
@Input() showLoadingsProgress = true;
1416

15-
16-
ngOnInit() {
17+
constructor(public linkPreviewService: MatLinkPreviewService) {
1718
}
1819

1920
trackLinks(index: number, link: Link) {
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
<div *ngIf="this.linkPreview; then render else loading"></div>
2-
3-
<ng-template #render>
4-
<mat-card>
5-
<img mat-card-image-xs [src]="linkPreview.image">
6-
<mat-card-title>{{linkPreview.title}}</mat-card-title>
7-
<mat-card-subtitle>{{linkPreview.description}}</mat-card-subtitle>
8-
</mat-card>
9-
</ng-template>
10-
<ng-template #loading>
11-
<mat-progress-bar *ngIf="false" mode="indeterminate"></mat-progress-bar>
12-
</ng-template>
1+
<div class="center-auto" *ngIf="!this.linkPreview && !this.loaded && this.showLoadingsProgress">
2+
<mat-spinner></mat-spinner>
3+
</div>
4+
<mat-card>
5+
<mat-card-content>
6+
<div class="img-container">
7+
<img mat-card-image [src]="linkPreview?.image">
8+
</div>
9+
<div>
10+
<mat-card-title>{{linkPreview?.title}}</mat-card-title>
11+
<mat-card-subtitle>{{linkPreview?.description}}</mat-card-subtitle>
12+
<a [href]="linkPreview?.url" mat-button [color]="color">{{linkPreview?.url}}</a>
13+
</div>
14+
</mat-card-content>
15+
</mat-card>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
:host {
22
display: inline-block;
33
}
4+
5+
mat-card-content {
6+
flex-direction: row;
7+
box-sizing: border-box;
8+
display: flex;
9+
10+
a {
11+
padding-left: 0;
12+
padding-right: 0;
13+
}
14+
}
15+
16+
.img-container {
17+
margin-right: 1rem;
18+
place-content: center;
19+
align-items: center;
20+
flex-direction: row;
21+
box-sizing: border-box;
22+
display: flex;
23+
flex: 1 1 100%;
24+
max-width: 20%;
25+
padding: 24px 16px;
26+
}
27+
28+
.center-auto {
29+
margin-left: auto !important;
30+
margin-right: auto !important;
31+
}

src/module/components/mat-link-preview/mat-link-preview.component.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
2-
import {By} from '@angular/platform-browser';
32
import {DebugElement, EventEmitter} from '@angular/core';
43

54
import {MatLinkPreviewComponent} from './mat-link-preview.component';
6-
import {MatCardModule, MatProgressBarModule} from '@angular/material';
5+
import {MatButtonModule, MatCardModule, MatProgressSpinnerModule} from '@angular/material';
76
import {FormsModule} from '@angular/forms';
87
import {MatLinkPreviewService} from '../../../index';
98
import {Link} from 'ngx-linkifyjs';
@@ -13,12 +12,13 @@ describe('LinkPreviewComponent', function () {
1312
let comp: MatLinkPreviewComponent;
1413
let fixture: ComponentFixture<MatLinkPreviewComponent>;
1514
const linkPreviewServicePartial: Partial<MatLinkPreviewService> = {
16-
onLinkFound: new EventEmitter<Array<Link>>()
15+
onLinkFound: new EventEmitter<Array<Link>>(),
16+
links: []
1717
};
1818

1919
beforeEach(async(() => {
2020
TestBed.configureTestingModule({
21-
imports: [MatCardModule, MatProgressBarModule, FormsModule],
21+
imports: [MatCardModule, MatProgressSpinnerModule, MatButtonModule, FormsModule],
2222
declarations: [MatLinkPreviewComponent],
2323
providers: [{provide: MatLinkPreviewService, useValue: linkPreviewServicePartial}]
2424
})

src/module/components/mat-link-preview/mat-link-preview.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export class MatLinkPreviewComponent implements OnInit, OnDestroy {
1313

1414
@Input() link: Link;
1515
@Input() linkPreview: LinkPreview;
16+
17+
// forwarded from the container
18+
@Input() color = 'primary'; // accent | warn
19+
@Input() showLoadingsProgress = true;
20+
1621
loaded: boolean;
1722
private _subscription: Subscription;
1823

src/module/directives/mat-link-preview.directive.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class MatLinkPreviewDirective implements OnInit {
2727
const links: Link[] = this.linkifyService.find(data);
2828
console.log('data: ', data);
2929
console.log('links: ', links);
30+
// event.target['value'] = this.linkifyService.linkify(data);
3031
return links;
3132
})).subscribe((links) => {
3233
this.linkPreviewService.onLinkFound.emit(links);

src/module/mat-link-preview.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {ModuleWithProviders, NgModule} from '@angular/core';
44
import {MatLinkPreviewService} from './service/mat-link-preview.service';
55
import {NgxLinkifyjsModule, NgxLinkifyjsService} from 'ngx-linkifyjs';
66
import {HttpClientModule} from '@angular/common/http';
7-
import {MatCardModule, MatProgressBarModule} from '@angular/material';
7+
import {MatButtonModule, MatCardModule, MatProgressSpinnerModule} from '@angular/material';
88
import {MatLinkPreviewDirective} from './directives/mat-link-preview.directive';
99
import {MatLinkPreviewComponent} from './components/mat-link-preview/mat-link-preview.component';
1010
import {MatLinkPreviewContainerComponent} from './components/mat-link-preview-container/mat-link-preview-container.component';
@@ -22,7 +22,8 @@ export {MatLinkPreviewService} from './service/mat-link-preview.service';
2222
HttpClientModule,
2323
NgxLinkifyjsModule,
2424
MatCardModule,
25-
MatProgressBarModule
25+
MatButtonModule,
26+
MatProgressSpinnerModule,
2627
],
2728
exports: [MatLinkPreviewComponent, MatLinkPreviewContainerComponent, MatLinkPreviewDirective],
2829
declarations: [MatLinkPreviewComponent, MatLinkPreviewContainerComponent, MatLinkPreviewDirective]

src/module/service/mat-link-preview.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ export class MatLinkPreviewService implements OnDestroy {
1111

1212
private _accessKey = '5b54e80a65c77848ceaa4630331e8384950e09d392365';
1313
private _apiURL = 'http://api.linkpreview.net/';
14+
private _subscription: Subscription;
1415

15-
subscription: Subscription;
1616
onLinkFound: EventEmitter<Array<Link>> = new EventEmitter<Array<Link>>();
1717

1818
links: Link[] = [];
1919

2020
constructor(private http: HttpClient) {
21-
this.onLinkFound.subscribe((links: Array<Link>) => this.links = links);
21+
this._subscription = this.onLinkFound.subscribe((links: Array<Link>) => this.links = links);
2222
}
2323

2424
ngOnDestroy(): void {
25+
if (this._subscription) {
26+
this._subscription.unsubscribe();
27+
}
2528
}
2629

2730
fetchLink(url: string): Observable<LinkPreview> {

0 commit comments

Comments
 (0)