diff --git a/src/DragonFly.Razor/DragonFlyApiExtensions.cs b/src/DragonFly.Razor/DragonFlyApiExtensions.cs index 2ac6877f..269c9091 100644 --- a/src/DragonFly.Razor/DragonFlyApiExtensions.cs +++ b/src/DragonFly.Razor/DragonFlyApiExtensions.cs @@ -2,7 +2,7 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Assets; +using DragonFly.Razor.Pages.Assets; using DragonFly.Razor.Pages.ContentItems.Fields; using DragonFly.Razor.Pages.ContentItems.Query; using DragonFly.Razor.Pages.ContentSchemas.Fields; diff --git a/src/DragonFly.Razor/DragonFlyClientExtensions.cs b/src/DragonFly.Razor/DragonFlyClientExtensions.cs index 8ddaf625..a7be10ac 100644 --- a/src/DragonFly.Razor/DragonFlyClientExtensions.cs +++ b/src/DragonFly.Razor/DragonFlyClientExtensions.cs @@ -7,6 +7,7 @@ using DragonFly.Builders; using DragonFly.Core.ContentStructures; using DragonFly.Razor; +using DragonFly.Razor.Assets; using DragonFly.Razor.Modules; using DragonFly.Razor.Services; using DragonFly.Storage; @@ -46,6 +47,7 @@ private static IDragonFlyBuilder AddDragonFlyClient(this WebAssemblyHostBuilder builder.Services.AddSingleton(ComponentManager.Default); builder.Services.AddSingleton(ContentFieldManager.Default); builder.Services.AddSingleton(AssetMetadataManager.Default); + builder.Services.AddSingleton(AssetPreviewManager.Default); builder.Services.AddTransient(sp => new HttpClient { BaseAddress = apiBaseUri }); builder.Services.AddTransient(); diff --git a/src/DragonFly.Razor/Extensions/NavigationManagerExtensions.cs b/src/DragonFly.Razor/Extensions/NavigationManagerExtensions.cs new file mode 100644 index 00000000..f841294d --- /dev/null +++ b/src/DragonFly.Razor/Extensions/NavigationManagerExtensions.cs @@ -0,0 +1,30 @@ +// Copyright (c) usercode +// https://github.com/usercode/DragonFly +// MIT License + +using Microsoft.AspNetCore.Components; + +namespace DragonFly.Razor.Extensions; + +public static class NavigationManagerExtensions +{ + public static void NavigateToAssets(this NavigationManager manager) + { + manager.NavigateTo("asset"); + } + + public static void NavigateToAsset(this NavigationManager manager, Asset asset) + { + manager.NavigateTo($"asset/{asset.Id}"); + } + + public static void NavigateToCreateAsset(this NavigationManager manager, long? folderId = null) + { + manager.NavigateTo($"asset/create/{folderId}"); + } + + public static void NavigateToContent(this NavigationManager manager, ContentItem content) + { + manager.NavigateTo($"content/{content.Schema.Name}/create/{content.Id}"); + } +} diff --git a/src/DragonFly.Razor/Modules/AssetModule.cs b/src/DragonFly.Razor/Modules/AssetModule.cs index 93654199..e3855888 100644 --- a/src/DragonFly.Razor/Modules/AssetModule.cs +++ b/src/DragonFly.Razor/Modules/AssetModule.cs @@ -3,6 +3,8 @@ // MIT License using DragonFly.Assets; +using DragonFly.Razor.Pages.Assets.Metadata; +using DragonFly.Razor.Pages.Assets.Preview; using DragonFly.Razor.Shared; namespace DragonFly.Razor.Modules; @@ -24,5 +26,8 @@ public override void Init(IDragonFlyApi api) api.RegisterMetadata(); api.RegisterMetadata(); + + api.AssetPreview().Register(MimeTypes.WebP, MimeTypes.Jpeg, MimeTypes.Png, MimeTypes.Gif, MimeTypes.Bmp); + api.AssetPreview().Register(MimeTypes.Pdf); } } diff --git a/src/DragonFly.Razor/Pages/Assets/AssetDetail.razor b/src/DragonFly.Razor/Pages/Assets/AssetDetail.razor index 446ee7dd..295a09d7 100644 --- a/src/DragonFly.Razor/Pages/Assets/AssetDetail.razor +++ b/src/DragonFly.Razor/Pages/Assets/AssetDetail.razor @@ -2,145 +2,134 @@ @page "/asset/create" @page "/asset/create/{FolderId:guid}" @inherits AssetDetailBase -@inject ComponentManager FieldComponentManager +@inject ComponentManager ComponentManager +@inject AssetPreviewManager AssetPreviewManager @if (Entity != null) { -
-

Asset

+
+

Asset

- -
-
- - -
- - -
- - -
- -
-
-
- - -
- - -
- -
-
-
- - -
- - -
- -
-
-
- - -
- - -
- -
-
-
+ +
+
+ + +
- -
- - -
- -
-
-
- - -
- - -
- -
-
-
+ +
+ + +
+ +
+
+
- -
- - -
- @if (Entity.Folder != null) - { - - } -
-
-
+ +
+ + +
+ +
+
+
- -
- - -
- -
-
-
+ +
+ + +
+ +
+
+
- @foreach (var m in Entity.Metaddata) - { -

@m.Key

- @FieldComponentManager.CreateComponent(m.Value) - } + +
+ + +
+ +
+
-
- @if (IsNewEntity == false) - { -
- + + +
+ + +
+
- - @if (string.IsNullOrEmpty(Entity.PreviewUrl) == false) - { - - - } - @*else if (Entity.IsPdf()) - { -
- -
- }*@ - } +
- - -
-
- -@code { - -} - diff --git a/src/DragonFly.Razor/Pages/Assets/Preview/Base/AssetPreviewComponent.cs b/src/DragonFly.Razor/Pages/Assets/Preview/Base/AssetPreviewComponent.cs new file mode 100644 index 00000000..8c4d53ab --- /dev/null +++ b/src/DragonFly.Razor/Pages/Assets/Preview/Base/AssetPreviewComponent.cs @@ -0,0 +1,13 @@ +// Copyright (c) usercode +// https://github.com/usercode/DragonFly +// MIT License + +using Microsoft.AspNetCore.Components; + +namespace DragonFly.Razor.Assets; + +public abstract class AssetPreviewComponent : ComponentBase, IAssetPreviewComponent +{ + [Parameter] + public Asset Asset { get; set; } +} diff --git a/src/DragonFly.Razor/Pages/Assets/Preview/Base/AssetPreviewManager.cs b/src/DragonFly.Razor/Pages/Assets/Preview/Base/AssetPreviewManager.cs new file mode 100644 index 00000000..d8a531f9 --- /dev/null +++ b/src/DragonFly.Razor/Pages/Assets/Preview/Base/AssetPreviewManager.cs @@ -0,0 +1,60 @@ +// Copyright (c) usercode +// https://github.com/usercode/DragonFly +// MIT License + +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Components; + +namespace DragonFly.Razor.Assets; + +public class AssetPreviewManager +{ + private static AssetPreviewManager _default; + + public static AssetPreviewManager Default + { + get + { + if (_default == null) + { + _default = new AssetPreviewManager(); + } + + return _default; + } + } + + private IDictionary _cache; + + private AssetPreviewManager() + { + _cache = new Dictionary(); + } + + public void Register(params string[] mimetypes) + where TAssetPreview : IAssetPreviewComponent + { + foreach (string mimetype in mimetypes) + { + _cache[mimetype] = typeof(TAssetPreview); + } + } + + public RenderFragment CreateComponent(Asset asset) + { + if (_cache.TryGetValue(asset.MimeType, out Type componentType)) + { + return builder => + { + builder.OpenComponent(0, componentType); + builder.AddAttribute(0, nameof(IAssetPreviewComponent.Asset), asset); + builder.CloseComponent(); + }; + } + else + { + return builder => { builder.OpenElement(0, "p"); builder.AddContent(0, $"Preview for asset isn't available."); builder.CloseElement(); }; + } + } +} diff --git a/src/DragonFly.Razor/Pages/Assets/Preview/Base/DragonFlyApiAssetPreviewManagerExtensions.cs b/src/DragonFly.Razor/Pages/Assets/Preview/Base/DragonFlyApiAssetPreviewManagerExtensions.cs new file mode 100644 index 00000000..0bddb3c7 --- /dev/null +++ b/src/DragonFly.Razor/Pages/Assets/Preview/Base/DragonFlyApiAssetPreviewManagerExtensions.cs @@ -0,0 +1,18 @@ +// Copyright (c) usercode +// https://github.com/usercode/DragonFly +// MIT License + +using DragonFly.Razor.Assets; + +namespace DragonFly; + +/// +/// DragonFlyApiAssetPreviewManagerExtensions +/// +public static class DragonFlyApiAssetPreviewManagerExtensions +{ + public static AssetPreviewManager AssetPreview(this IDragonFlyApi api) + { + return AssetPreviewManager.Default; + } +} diff --git a/src/DragonFly.Razor/Pages/Assets/Preview/Base/IAssetPreviewComponent.cs b/src/DragonFly.Razor/Pages/Assets/Preview/Base/IAssetPreviewComponent.cs new file mode 100644 index 00000000..30c97190 --- /dev/null +++ b/src/DragonFly.Razor/Pages/Assets/Preview/Base/IAssetPreviewComponent.cs @@ -0,0 +1,10 @@ +// Copyright (c) usercode +// https://github.com/usercode/DragonFly +// MIT License + +namespace DragonFly.Razor.Assets; + +public interface IAssetPreviewComponent +{ + Asset Asset { get; } +} diff --git a/src/DragonFly.Razor/Pages/Assets/Preview/ImagePreviewView.razor b/src/DragonFly.Razor/Pages/Assets/Preview/ImagePreviewView.razor new file mode 100644 index 00000000..08fa87bd --- /dev/null +++ b/src/DragonFly.Razor/Pages/Assets/Preview/ImagePreviewView.razor @@ -0,0 +1,7 @@ +@inherits AssetPreviewComponent + +@if (string.IsNullOrEmpty(Asset.PreviewUrl) == false) +{ + + +} \ No newline at end of file diff --git a/src/DragonFly.Razor/Pages/Assets/Preview/PdfPreviewView.razor b/src/DragonFly.Razor/Pages/Assets/Preview/PdfPreviewView.razor new file mode 100644 index 00000000..1c952b11 --- /dev/null +++ b/src/DragonFly.Razor/Pages/Assets/Preview/PdfPreviewView.razor @@ -0,0 +1,10 @@ +@inherits AssetPreviewComponent + +@if (string.IsNullOrEmpty(Asset.PreviewUrl) == false) +{ +
+ +
+} \ No newline at end of file diff --git a/src/DragonFly.Razor/Pages/ContentItems/ContentItemDetail.razor.cs b/src/DragonFly.Razor/Pages/ContentItems/ContentItemDetail.razor.cs index 8d1b1520..71b99a5b 100644 --- a/src/DragonFly.Razor/Pages/ContentItems/ContentItemDetail.razor.cs +++ b/src/DragonFly.Razor/Pages/ContentItems/ContentItemDetail.razor.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using DragonFly.Razor.Extensions; namespace DragonFly.Client.Pages.ContentItems; @@ -96,7 +97,7 @@ protected override async Task CreateActionAsync() { await ContentService.CreateAsync(Entity); - NavigationManager.NavigateTo($"content/{EntityType}/{Entity.Id}"); + NavigationManager.NavigateToContent(Entity); } protected override async Task UpdateActionAsync() diff --git a/src/DragonFly.Razor/Services/ComponentManagerExtensions.cs b/src/DragonFly.Razor/Services/ComponentManagerExtensions.cs index bbfaabc7..93186de5 100644 --- a/src/DragonFly.Razor/Services/ComponentManagerExtensions.cs +++ b/src/DragonFly.Razor/Services/ComponentManagerExtensions.cs @@ -2,8 +2,8 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Assets; using DragonFly.Query; +using DragonFly.Razor.Pages.Assets; using DragonFly.Razor.Pages.ContentItems.Fields; using DragonFly.Razor.Pages.ContentItems.Query; using DragonFly.Razor.Pages.ContentSchemas.Fields; @@ -66,26 +66,6 @@ public static RenderFragment CreateComponent(this ComponentManager componentMana } } - public static void RegisterAssetMetadata(this ComponentManager componentManager) - where TMetadataComponent : IAssetMetadataComponent - { - Type metadataType = typeof(TMetadataComponent).GetProperty(nameof(IAssetMetadataComponent.Metadata)).PropertyType; - - componentManager.Register(metadataType, typeof(TMetadataComponent)); - } - - public static RenderFragment CreateComponent(this ComponentManager componentManager, AssetMetadata metadata) - { - Type componentType = componentManager.GetComponentType(metadata.GetType()); - - return builder => - { - builder.OpenComponent(0, componentType); - builder.AddAttribute(0, nameof(IAssetMetadataComponent.Metadata), metadata); - builder.CloseComponent(); - }; - } - public static void RegisterQuery(this ComponentManager componentManager) where TQueryView : IFieldQueryComponent { diff --git a/src/DragonFly.Razor/Shared/AssetSelector.razor b/src/DragonFly.Razor/Shared/AssetSelector.razor index 6942a0b9..d999e054 100644 --- a/src/DragonFly.Razor/Shared/AssetSelector.razor +++ b/src/DragonFly.Razor/Shared/AssetSelector.razor @@ -13,8 +13,7 @@
- - +
@if (Asset != null) { @@ -81,7 +80,7 @@ protected void Navigate() { - NavigationManager.NavigateTo($"asset/{Asset.Id}"); + NavigationManager.NavigateToAsset(Asset); } protected void RemoveItem() diff --git a/src/DragonFly.Razor/Shared/ContentSelector.razor b/src/DragonFly.Razor/Shared/ContentSelector.razor index 53aa3f7d..6a65ef50 100644 --- a/src/DragonFly.Razor/Shared/ContentSelector.razor +++ b/src/DragonFly.Razor/Shared/ContentSelector.razor @@ -80,7 +80,7 @@ protected void Navigate() { - NavigationManager.NavigateTo($"content/{ContentItem.Schema.Name}/{ContentItem.Id}"); + NavigationManager.NavigateToContent(ContentItem); } protected void RemoveItem() diff --git a/src/DragonFly.Razor/_Imports.razor b/src/DragonFly.Razor/_Imports.razor index 005b75b2..90f1831e 100644 --- a/src/DragonFly.Razor/_Imports.razor +++ b/src/DragonFly.Razor/_Imports.razor @@ -39,4 +39,6 @@ @using DragonFly.Core.ContentStructures.Queries @using DragonFly.Storage @using DragonFly.Razor.Pages.ContentItems.Fields.Helpers +@using DragonFly.Razor.Extensions +@using DragonFly.Razor.Assets @attribute [Authorize] \ No newline at end of file