diff --git a/Moonlight/Moonlight.csproj b/Moonlight/Moonlight.csproj index b0120d81..1c684c61 100644 --- a/Moonlight/Moonlight.csproj +++ b/Moonlight/Moonlight.csproj @@ -62,6 +62,8 @@ <_ContentIncludedByDefault Remove="Shared\Components\Tables\Table.razor" /> <_ContentIncludedByDefault Remove="Shared\Views\Admin\Servers\Cleanup\Exceptions\Add.razor" /> <_ContentIncludedByDefault Remove="Shared\Views\Admin\Servers\Cleanup\Exceptions\Edit.razor" /> + <_ContentIncludedByDefault Remove="Shared\Components\News\NewsEditor.razor" /> + <_ContentIncludedByDefault Remove="Shared\News\Edit.razor" /> diff --git a/Moonlight/Shared/Components/Navigations/AdminSystemNavigation.razor b/Moonlight/Shared/Components/Navigations/AdminSystemNavigation.razor index 18385f6c..bb235e50 100644 --- a/Moonlight/Shared/Components/Navigations/AdminSystemNavigation.razor +++ b/Moonlight/Shared/Components/Navigations/AdminSystemNavigation.razor @@ -39,6 +39,11 @@ Discord bot + diff --git a/Moonlight/Shared/Components/News/NewsEditor.razor b/Moonlight/Shared/Components/News/NewsEditor.razor deleted file mode 100644 index a58d1b10..00000000 --- a/Moonlight/Shared/Components/News/NewsEditor.razor +++ /dev/null @@ -1,98 +0,0 @@ -@using Moonlight.App.Services -@using Moonlight.App.Database.Entities -@using BlazorMonaco - -@inject SmartTranslateService SmartTranslateService -@inject IJSRuntime JsRuntime - -
-
-

- -

-
- @{ - string dateInt(int i) => i.ToString().Length < 2 ? "0" + i : i.ToString(); - var date = Model.Date == default ? DateTime.Now : Model.Date; - } - @dateInt(date.Day).@dateInt(date.Month).@date.Year -
-
-
- -
- -
- -@code { - // Monaco Editor - private MonacoEditor Editor; - private StandaloneEditorConstructionOptions EditorOptions; - - [Parameter] - public NewsEntry Model { get; set; } - - [Parameter] - public Func Save { get; set; } - - protected override void OnInitialized() - { - EditorOptions = new() - { - AutomaticLayout = true, - Language = "plaintext", - Value = "Loading content", - Theme = "moonlight-theme", - Contextmenu = false, - Minimap = new() - { - Enabled = false - }, - AutoIndent = true - }; - } - - protected override async Task OnAfterRenderAsync(bool firstRender) - { - if (firstRender) - { - await JsRuntime.InvokeVoidAsync("initMonacoTheme"); - - Editor.OnDidInit = new EventCallback(this, async () => - { - EditorOptions.Language = "markdown"; - - var model = await Editor.GetModel(); - await MonacoEditorBase.SetModelLanguage(model, EditorOptions.Language); - await Editor.SetPosition(new Position() - { - Column = 0, - LineNumber = 1 - }); - - await Editor.SetValue(string.IsNullOrWhiteSpace(Model.Markdown) ? "*enter your markdown here*" : Model.Markdown); - - await Editor.Layout(new Dimension() - { - Height = 500, - Width = 1000 - }); - }); - } - } - - private async Task DoSave() - { - Model.Date = Model.Date == default ? DateTime.Now : Model.Date; - Model.Markdown = await Editor.GetValue(); - - Save?.Invoke(Model); - } - - public async Task UpdateMonacoText() - { - await Editor.SetValue(Model.Markdown); - } -} \ No newline at end of file diff --git a/Moonlight/Shared/Components/Partials/SidebarMenu.razor b/Moonlight/Shared/Components/Partials/SidebarMenu.razor index 777667c6..c7e6c692 100644 --- a/Moonlight/Shared/Components/Partials/SidebarMenu.razor +++ b/Moonlight/Shared/Components/Partials/SidebarMenu.razor @@ -68,14 +68,6 @@ else Changelog - if (User.Admin) { diff --git a/Moonlight/Shared/News/Edit.razor b/Moonlight/Shared/News/Edit.razor deleted file mode 100644 index aed04bf9..00000000 --- a/Moonlight/Shared/News/Edit.razor +++ /dev/null @@ -1,32 +0,0 @@ -@page "/news/edit/{Id:int}" - -@using Moonlight.App.Database.Entities -@using Moonlight.App.Repositories -@using Moonlight.Shared.Components.News - -@inject NewsEntryRepository NewsEntryRepository -@inject NavigationManager NavigationManager - - - - - - - -@code { - [Parameter] - public int Id { get; set; } - - private NewsEntry Entry; - - private async Task Load(LazyLoader loader) - { - Entry = NewsEntryRepository.Get().First(x => x.Id == Id); - } - - private async Task DoSave(NewsEntry entry) - { - NewsEntryRepository.Update(entry); - NavigationManager.NavigateTo("/news"); - } -} \ No newline at end of file diff --git a/Moonlight/Shared/Views/Admin/Sys/News/Edit.razor b/Moonlight/Shared/Views/Admin/Sys/News/Edit.razor new file mode 100644 index 00000000..e26a0e6d --- /dev/null +++ b/Moonlight/Shared/Views/Admin/Sys/News/Edit.razor @@ -0,0 +1,78 @@ +@page "/admin/system/news/edit/{Id:int}" + +@using Moonlight.App.Database.Entities +@using Moonlight.App.Helpers +@using Moonlight.App.Repositories +@using Moonlight.App.Services +@using Moonlight.Shared.Components.FileManagerPartials + +@inject SmartTranslateService SmartTranslateService +@inject NavigationManager NavigationManager +@inject NewsEntryRepository NewsEntryRepository + + + + @if (Entry == null) + { +
+
+

+ No entry found +

+ + We were not able to find the news entry with this id + +
+
+ } + else + { +
+
+

+ +

+
+ @(Formatter.FormatDateOnly(Entry.Date)) +
+
+
+ +
+ +
+ } +
+
+ +@code +{ + [Parameter] + public int Id { get; set; } + + private NewsEntry? Entry; + + private FileEditor FileEditor; + + private async Task Save() + { + Entry!.Markdown = await FileEditor.GetData(); + + NewsEntryRepository.Update(Entry); + + NavigationManager.NavigateTo("/admin/system/news"); + } + + private Task Load(LazyLoader arg) + { + Entry = NewsEntryRepository.Get().FirstOrDefault(x => x.Id == Id); + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/Moonlight/Shared/Views/Admin/Sys/News/Index.razor b/Moonlight/Shared/Views/Admin/Sys/News/Index.razor new file mode 100644 index 00000000..337210d8 --- /dev/null +++ b/Moonlight/Shared/Views/Admin/Sys/News/Index.razor @@ -0,0 +1,84 @@ +@page "/admin/system/news" +@using Moonlight.App.Repositories +@using Moonlight.App.Database.Entities +@using Markdig +@using Moonlight.App.Helpers +@using Moonlight.App.Services +@using Moonlight.App.Services.Interop +@using Moonlight.Shared.Components.Navigations +@using Moonlight.Shared.Components.FileManagerPartials + +@inject NewsEntryRepository NewsEntryRepository +@inject SmartTranslateService SmartTranslateService +@inject AlertService AlertService + + + + + + + + @foreach (var entry in Entries) + { +
+
+

@entry.Title

+
+ + + + + + + + @(Formatter.FormatDateOnly(entry.Date)) +
+
+
+ @{ + var html = (MarkupString)Markdown.ToHtml(entry.Markdown); + } + + @(html) +
+
+ } +
+
+ +@code +{ + private NewsEntry[] Entries; + + private LazyLoader LazyLoader; + private FileEditor FileEditor; + + private Task Load(LazyLoader loader) + { + Entries = NewsEntryRepository.Get().OrderByDescending(x => x.Date).ToArray(); + + return Task.CompletedTask; + } + + private async Task Delete(NewsEntry entry) + { + var confirm = await AlertService.ConfirmMath(); + + if (!confirm) return; + + NewsEntryRepository.Delete(entry); + + await LazyLoader.Reload(); + } +} \ No newline at end of file diff --git a/Moonlight/Shared/Views/Admin/Sys/News/New.razor b/Moonlight/Shared/Views/Admin/Sys/News/New.razor new file mode 100644 index 00000000..64da8468 --- /dev/null +++ b/Moonlight/Shared/Views/Admin/Sys/News/New.razor @@ -0,0 +1,52 @@ +@page "/admin/system/news/new" +@using Moonlight.App.Database.Entities +@using Moonlight.App.Helpers +@using Moonlight.App.Repositories +@using Moonlight.App.Services +@using Moonlight.Shared.Components.FileManagerPartials + +@inject SmartTranslateService SmartTranslateService +@inject NavigationManager NavigationManager +@inject NewsEntryRepository NewsEntryRepository + + +
+
+

+ +

+
+ @(Formatter.FormatDateOnly(Model.Date)) +
+
+
+ +
+ +
+
+ +@code +{ + private NewsEntry Model = new() //TODO: Smart form model + { + Date = DateTime.UtcNow + }; + + private FileEditor FileEditor; + + private async Task Save() + { + Model.Markdown = await FileEditor.GetData(); + + NewsEntryRepository.Add(Model); + + NavigationManager.NavigateTo("/admin/system/news"); + } +} \ No newline at end of file diff --git a/Moonlight/Shared/Views/Index.razor b/Moonlight/Shared/Views/Index.razor index 3a8162d4..33385548 100644 --- a/Moonlight/Shared/Views/Index.razor +++ b/Moonlight/Shared/Views/Index.razor @@ -3,14 +3,62 @@ @using Moonlight.App.Repositories.Servers @using Microsoft.EntityFrameworkCore @using Moonlight.App.Database.Entities +@using Moonlight.App.Helpers @using Moonlight.App.Repositories @using Moonlight.App.Repositories.Domains +@using Markdig @inject ServerRepository ServerRepository @inject WebsiteRepository WebsiteRepository @inject DomainRepository DomainRepository +@inject NewsEntryRepository NewsEntryRepository +@if (NewsEntries.Any()) +{ + if (CurrentNewsIndex > NewsEntries.Count - 1) + CurrentNewsIndex = 0; + + if (CurrentNewsIndex < 0) + CurrentNewsIndex = NewsEntries.Count - 1; + + var currentEntry = NewsEntries[CurrentNewsIndex]; + +
+
+
+
+ + + +

@(Formatter.FormatDateOnly(currentEntry.Date))

+
+
+ +
+
+
+ @{ + var html = (MarkupString)Markdown.ToHtml(currentEntry.Markdown); + } + + @(html) +
+
+
+} +
@@ -193,11 +241,14 @@ { [CascadingParameter] public User User { get; set; } - + private int ServerCount = 0; private int DomainCount = 0; private int WebsiteCount = 0; + private List NewsEntries; + private int CurrentNewsIndex = 0; + private Task Load(LazyLoader lazyLoader) { ServerCount = ServerRepository @@ -214,7 +265,15 @@ .Get() .Include(x => x.Owner) .Count(x => x.Owner.Id == User.Id); - + + NewsEntries = NewsEntryRepository.Get().ToList(); + return Task.CompletedTask; } + + private async Task ChangeNewsIndex(int i) + { + CurrentNewsIndex += i; + await InvokeAsync(StateHasChanged); + } } \ No newline at end of file diff --git a/Moonlight/Shared/Views/News.razor b/Moonlight/Shared/Views/News.razor deleted file mode 100644 index d11f0779..00000000 --- a/Moonlight/Shared/Views/News.razor +++ /dev/null @@ -1,89 +0,0 @@ -@page "/news" -@using Moonlight.App.Repositories -@using Moonlight.App.Database.Entities -@using Markdig -@using Moonlight.App.Services -@using Moonlight.App.Services.Interop -@using Moonlight.Shared.Components.News - -@inject NewsEntryRepository NewsEntryRepository -@inject SmartTranslateService SmartTranslateService -@inject NavigationManager NavigationManager -@inject AlertService AlertService - - - - - - - @foreach (var entry in Entries) - { -
-
-

@entry.Title

-
- - - - - - - - - @{ - string dateInt(int i) => i.ToString().Length < 2 ? "0" + i : i.ToString(); - } - @dateInt(entry.Date.Day).@dateInt(entry.Date.Month).@entry.Date.Year -
-
-
- @{ - var html = (MarkupString)Markdown.ToHtml(entry.Markdown); - } - - @html -
-
- - } - - -@code { - private NewsEntry NewPost = new(); - private NewsEditor NewPostEditor; - - private NewsEntry[] Entries; - - private async Task Load(LazyLoader loader) - { - Entries = NewsEntryRepository.Get().OrderByDescending(x => x.Date).ToArray(); - } - - private async Task DoSaveNewPost(NewsEntry post) - { - NewsEntryRepository.Add(post); - - NavigationManager.NavigateTo(NavigationManager.Uri, true); - } - - private async Task Delete(NewsEntry entry) - { - var confirm = await AlertService.YesNo( - SmartTranslateService.Translate("Delete post"), - SmartTranslateService.Translate("Do you really want to delete the post \"") + entry.Title + "\"?", - SmartTranslateService.Translate("Yes"), - SmartTranslateService.Translate("No") - ); - - if(!confirm) return; - - NewsEntryRepository.Delete(entry); - - NavigationManager.NavigateTo(NavigationManager.Uri, true); - } -} \ No newline at end of file diff --git a/Moonlight/resources/lang/de_de.lang b/Moonlight/resources/lang/de_de.lang index 6bb869e6..eb4bb032 100644 --- a/Moonlight/resources/lang/de_de.lang +++ b/Moonlight/resources/lang/de_de.lang @@ -563,3 +563,5 @@ You have no websites;You have no websites We were not able to find any websites associated with your account;We were not able to find any websites associated with your account Guest;Guest You need a domain;You need a domain +New post;New post +New entry;New entry