-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathPreviewImage.razor
39 lines (32 loc) · 1.14 KB
/
PreviewImage.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@using Microsoft.AspNetCore.StaticFiles
@if (string.IsNullOrEmpty(PreviewImageUrlFallback))
{
<img class="preview-img" src="@PreviewImageUrl" alt="Preview image blogpost" loading="@LazyLoadTag" decoding="@DecodingTag" />
}
else
{
<picture>
<source srcset="@PreviewImageUrl" type="@GetMimeType()"/>
<img class="preview-img" src="@PreviewImageUrlFallback" alt="Preview image blogpost" loading="@LazyLoadTag" decoding="@DecodingTag" />
</picture>
}
@code {
[Parameter, EditorRequired]
public required string PreviewImageUrl { get; set; }
[Parameter]
public string? PreviewImageUrlFallback { get; set; }
[Parameter]
public bool LazyLoadImage { get; set; }
private string LazyLoadTag => LazyLoadImage ? "lazy" : "eager";
private string DecodingTag => LazyLoadImage ? "async" : "auto";
private static readonly FileExtensionContentTypeProvider Provider = new();
static PreviewImage()
{
Provider.Mappings.TryAdd(".avif", "image/avif");
}
private string? GetMimeType()
{
Provider.TryGetContentType(PreviewImageUrl, out var contentType);
return contentType;
}
}