Skip to content

Commit 6f80915

Browse files
committed
Add a directive uiImageLoader, fixed github avatar problem.
1 parent 398c5a1 commit 6f80915

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

electron/src/book.backend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class BookBackend {
6565
});
6666

6767
const webContents = bookWindow.webContents;
68-
//webContents.openDevTools();
68+
webContents.openDevTools();
6969

7070
bookWindow.loadURL(this.bookUrl);
7171

src/app/book/book.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { ContentSearchDirective } from './directives/content-search.directive';
4040

4141
import { environment } from '../../environments/environment';
4242
import { config } from '../gbr.config';
43+
import { UiImageLoaderDirective } from './directives/ui-image-loader.directive';
4344

4445
@NgModule({
4546
imports: [
@@ -59,6 +60,7 @@ import { config } from '../gbr.config';
5960
ElementInputDirective,
6061
ContentSearchDirective,
6162
SafeHtmlPipe,
63+
UiImageLoaderDirective,
6264
],
6365
providers: [
6466
SettingsService,
@@ -86,6 +88,7 @@ import { config } from '../gbr.config';
8688
ElementInputDirective,
8789
SafeHtmlPipe,
8890
ContentSearchDirective,
91+
UiImageLoaderDirective,
8992
]
9093
})
9194
export class BookPageModule {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
Directive,
3+
Attribute,
4+
Renderer2,
5+
ElementRef,
6+
HostListener } from '@angular/core';
7+
8+
@Directive({
9+
selector: '[uiImageLoader]'
10+
})
11+
export class UiImageLoaderDirective {
12+
constructor(
13+
@Attribute('loader') public loader: string,
14+
@Attribute('onErrorSrc') public onErrorSrc: string,
15+
private renderer: Renderer2,
16+
private el: ElementRef) {
17+
this.renderer.setAttribute(this.el.nativeElement, 'src', this.loader);
18+
}
19+
20+
@HostListener('load') onLoad() {
21+
this.renderer.setAttribute(this.el.nativeElement, 'src', this.el.nativeElement.src);
22+
}
23+
@HostListener('error') onError() {
24+
this.renderer.setAttribute(this.el.nativeElement, 'src', this.onErrorSrc);
25+
}
26+
}

src/app/home/components/book-list.component.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
<!-- 作者和来源 -->
1414
<mat-card-header>
15-
<img mat-card-avatar [src]="book.isFromMainstreamPlatform ? book.writer.avatarUrl : 'assets/images/avatar.png'" />
15+
<img mat-card-avatar
16+
uiImageLoader
17+
onErrorSrc="assets/images/avatar.png"
18+
loader="assets/images/loader.gif"
19+
[src]="book.isFromMainstreamPlatform ? book.writer.avatarUrl : 'assets/images/avatar.png'"
20+
/>
1621
<mat-card-title>{{book.writer.fullName}}</mat-card-title>
1722
<mat-card-subtitle>
1823
<ion-icon *ngIf="book.writer.location.length > 0" name="location-outline"></ion-icon>{{book.writer.location}}

src/assets/images/loader.gif

46.9 KB
Loading

src/assets/images/loader.svg

Lines changed: 37 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)