-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaComponents.cs
More file actions
70 lines (61 loc) · 2.47 KB
/
MediaComponents.cs
File metadata and controls
70 lines (61 loc) · 2.47 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Code311.Tabler.Core.Mapping;
using Code311.Ui.Abstractions.Semantics;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Code311.Tabler.Components.Media;
[HtmlTargetElement("cd311-avatar")]
public sealed class Cd311AvatarTagHelper(ITablerSemanticClassMapper mapper) : TagHelper
{
public string Name { get; set; } = string.Empty;
public string? ImageUrl { get; set; }
public UiTone Tone { get; set; } = UiTone.Neutral;
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "span";
output.Attributes.SetAttribute("class", $"avatar bg-{mapper.MapTone(Tone)}");
if (!string.IsNullOrWhiteSpace(ImageUrl))
{
output.Attributes.SetAttribute("style", $"background-image:url('{ImageUrl}')");
}
else
{
output.Content.SetContent(new string(Name.Where(char.IsLetter).Take(2).ToArray()).ToUpperInvariant());
}
}
}
[HtmlTargetElement("cd311-icon")]
public sealed class Cd311IconTagHelper : TagHelper
{
public string Name { get; set; } = "circle";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "i";
output.Attributes.SetAttribute("class", $"ti ti-{Name}");
}
}
[HtmlTargetElement("cd311-image")]
public sealed class Cd311ImageTagHelper : TagHelper
{
public string Src { get; set; } = string.Empty;
public string Alt { get; set; } = string.Empty;
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "img";
output.TagMode = TagMode.SelfClosing;
output.Attributes.SetAttribute("src", Src);
output.Attributes.SetAttribute("alt", Alt);
output.Attributes.SetAttribute("class", "img-fluid");
}
}
public sealed class Cd311FilePreviewViewComponent : ViewComponent
{
public IViewComponentResult Invoke(string fileName, string mediaType)
=> new HtmlContentViewComponentResult(new HtmlString($"<div class=\"file-preview\"><strong>{fileName}</strong><small>{mediaType}</small></div>"));
}
public sealed class Cd311ProfileBlockViewComponent : ViewComponent
{
public IViewComponentResult Invoke(string title, string subtitle)
=> new HtmlContentViewComponentResult(new HtmlString($"<div class=\"profile-block\"><h4>{title}</h4><p>{subtitle}</p></div>"));
}