From c11ff632d2c140e99597dfaef75680d3afb2db88 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Fri, 22 Dec 2023 20:47:00 +0100 Subject: [PATCH] Implemented theme exporting and importing --- .../App/Models/Json/Theme/ThemeExport.cs | 10 ++ .../App/Models/Json/Theme/ThemeImport.cs | 10 ++ .../Services/Interop/FileDownloadService.cs | 34 +++++ Moonlight/Pages/_Host.cshtml | 2 +- Moonlight/Program.cs | 1 + .../Shared/Components/Forms/AutoCrud.razor | 12 +- .../Forms/SmartCustomFileSelect.razor | 56 ++++++++ .../Shared/Views/Admin/Community/Filter.razor | 6 +- .../Shared/Views/Admin/Store/Coupons.razor | 12 +- .../Shared/Views/Admin/Store/Gifts.razor | 12 +- Moonlight/Shared/Views/Admin/Sys/Themes.razor | 120 +++++++++++++----- Moonlight/wwwroot/js/moonlight.js | 13 ++ 12 files changed, 239 insertions(+), 49 deletions(-) create mode 100644 Moonlight/App/Models/Json/Theme/ThemeExport.cs create mode 100644 Moonlight/App/Models/Json/Theme/ThemeImport.cs create mode 100644 Moonlight/App/Services/Interop/FileDownloadService.cs create mode 100644 Moonlight/Shared/Components/Forms/SmartCustomFileSelect.razor diff --git a/Moonlight/App/Models/Json/Theme/ThemeExport.cs b/Moonlight/App/Models/Json/Theme/ThemeExport.cs new file mode 100644 index 0000000..fee81d6 --- /dev/null +++ b/Moonlight/App/Models/Json/Theme/ThemeExport.cs @@ -0,0 +1,10 @@ +namespace Moonlight.App.Models.Json.Theme; + +public class ThemeExport +{ + public string Name { get; set; } = ""; + public string Author { get; set; } = ""; + public string? DonateUrl { get; set; } = ""; + public string CssUrl { get; set; } = ""; + public string? JsUrl { get; set; } = ""; +} \ No newline at end of file diff --git a/Moonlight/App/Models/Json/Theme/ThemeImport.cs b/Moonlight/App/Models/Json/Theme/ThemeImport.cs new file mode 100644 index 0000000..370dbfc --- /dev/null +++ b/Moonlight/App/Models/Json/Theme/ThemeImport.cs @@ -0,0 +1,10 @@ +namespace Moonlight.App.Models.Json.Theme; + +public class ThemeImport +{ + public string Name { get; set; } = ""; + public string Author { get; set; } = ""; + public string? DonateUrl { get; set; } = ""; + public string CssUrl { get; set; } = ""; + public string? JsUrl { get; set; } = ""; +} \ No newline at end of file diff --git a/Moonlight/App/Services/Interop/FileDownloadService.cs b/Moonlight/App/Services/Interop/FileDownloadService.cs new file mode 100644 index 0000000..572417e --- /dev/null +++ b/Moonlight/App/Services/Interop/FileDownloadService.cs @@ -0,0 +1,34 @@ +using System.Text; +using Microsoft.JSInterop; + +namespace Moonlight.App.Services.Interop; + +public class FileDownloadService +{ + private readonly IJSRuntime JsRuntime; + + public FileDownloadService(IJSRuntime jsRuntime) + { + JsRuntime = jsRuntime; + } + + public async Task DownloadStream(string fileName, Stream stream) + { + using var streamRef = new DotNetStreamReference(stream); + + await JsRuntime.InvokeVoidAsync("moonlight.utils.download", fileName, streamRef); + } + + public async Task DownloadBytes(string fileName, byte[] bytes) + { + var ms = new MemoryStream(bytes); + + await DownloadStream(fileName, ms); + + ms.Close(); + await ms.DisposeAsync(); + } + + public async Task DownloadString(string fileName, string content) => + await DownloadBytes(fileName, Encoding.UTF8.GetBytes(content)); +} \ No newline at end of file diff --git a/Moonlight/Pages/_Host.cshtml b/Moonlight/Pages/_Host.cshtml index 801ac5e..381ac91 100644 --- a/Moonlight/Pages/_Host.cshtml +++ b/Moonlight/Pages/_Host.cshtml @@ -55,7 +55,7 @@ @foreach (var theme in themes) { - if (theme.JsUrl != null) + if (!string.IsNullOrEmpty(theme.JsUrl)) { diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index c167441..f5c1482 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -56,6 +56,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Services / Store builder.Services.AddScoped(); diff --git a/Moonlight/Shared/Components/Forms/AutoCrud.razor b/Moonlight/Shared/Components/Forms/AutoCrud.razor index e6b5bcc..dfad589 100644 --- a/Moonlight/Shared/Components/Forms/AutoCrud.razor +++ b/Moonlight/Shared/Components/Forms/AutoCrud.razor @@ -14,7 +14,8 @@

@(Title)

-
@@ -26,7 +27,7 @@ PageSize="50" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3 fs-6" TableHeadClass="fw-bold text-muted"> - @ChildContent + @View