Skip to content

Commit

Permalink
feat: improve image loading and displaying in gn-ui thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
f-necas committed Jul 25, 2023
1 parent 116e47c commit 10e9abb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export class ElasticsearchFieldMapper {
selectFallback(
selectFallbackFields(
getFirstValue(selectField(source, 'overview')),
'data',
'url'
'url',
'data'
),
''
)
Expand Down
2 changes: 2 additions & 0 deletions libs/ui/elements/src/lib/thumbnail/thumbnail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
[ngClass]="objectFit === 'contain' ? 'h-4/5' : 'h-full'"
[ngStyle]="{ objectFit: objectFit }"
alt="thumbnail"
loading="lazy"
(load)="setObjectFit($event)"
[src]="imgUrl | safe: 'url'"
/>
</div>
13 changes: 12 additions & 1 deletion libs/ui/elements/src/lib/thumbnail/thumbnail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ThumbnailComponent implements AfterViewInit, OnDestroy {
this.imgUrl = url || this.placeholderUrl
this.isPlaceholder = !url
}
@Input() fit: 'cover' | 'contain' = 'cover'
@Input() fit: 'cover' | 'contain' = 'contain'
@ViewChild('imageElement') imgElement: ElementRef<HTMLImageElement>
imgUrl: string
placeholderUrl = this.optionalPlaceholderUrl || DEFAULT_PLACEHOLDER
Expand Down Expand Up @@ -65,4 +65,15 @@ export class ThumbnailComponent implements AfterViewInit, OnDestroy {
this.changeDetector.detectChanges()
}
}

setObjectFit(e) {
if (e && e.target) {
if (
e.target.naturalWidth > e.target.parentElement.clientWidth ||
e.target.naturalHeight > e.target.parentElement.clientHeight
) {
this.fit = 'cover'
}
}
}
}

0 comments on commit 10e9abb

Please sign in to comment.