153 lines
4.8 KiB
Text
153 lines
4.8 KiB
Text
@using LANCommander.Server.Models
|
|
@using System.ComponentModel.DataAnnotations
|
|
@using WikiClientLibrary.Pages
|
|
@inject GameService GameService
|
|
@inject RedistributableService RedistributableService
|
|
@inject WikiService WikiService
|
|
@inject IMessageService MessageService
|
|
@inject IJSRuntime JS
|
|
@inject ILogger<WikiEditor> Logger
|
|
|
|
<Spin Spinning="@Loading">
|
|
<Space Direction="DirectionVHType.Vertical" Size="@("large")" Style="width: 100%">
|
|
<SpaceItem>
|
|
<StandaloneDiffEditor @ref="DiffEditor" Id="wiki-diff" ConstructionOptions="DiffEditorConstructionOptions" OnDidInit="LoadDiff" />
|
|
</SpaceItem>
|
|
|
|
<SpaceItem>
|
|
<Form Model="Changes">
|
|
<FormItem Label="Minor">
|
|
<Checkbox @bind-Value="context.Minor" />
|
|
</FormItem>
|
|
<FormItem Label="Summary">
|
|
<TextArea @bind-Value="context.Summary" />
|
|
</FormItem>
|
|
</Form>
|
|
</SpaceItem>
|
|
|
|
<SpaceItem>
|
|
<GridRow Justify="end">
|
|
<GridCol>
|
|
<Space Direction="@DirectionVHType.Horizontal">
|
|
<SpaceItem>
|
|
<a href="@WikiService.GetUrl(Page)" target="_blank" class="ant-btn ant-btn-default">View</a>
|
|
</SpaceItem>
|
|
<SpaceItem>
|
|
<Button OnClick="Publish" Type="@ButtonType.Primary" Disabled="@(OriginalContents == ModifiedContents)">Publish</Button>
|
|
</SpaceItem>
|
|
</Space>
|
|
</GridCol>
|
|
</GridRow>
|
|
</SpaceItem>
|
|
</Space>
|
|
</Spin>
|
|
|
|
@code {
|
|
[Parameter] public Guid GameId { get; set; }
|
|
[Parameter] public Guid RedistributableId { get; set; }
|
|
|
|
LANCommanderSettings Settings = SettingService.GetSettings();
|
|
|
|
bool Loading = true;
|
|
bool Authenticated = false;
|
|
|
|
bool Minor = false;
|
|
string Summary = "";
|
|
|
|
string OriginalContents = "";
|
|
string ModifiedContents = "";
|
|
|
|
StandaloneDiffEditor DiffEditor;
|
|
Game Game;
|
|
Redistributable Redistributable;
|
|
WikiPage Page;
|
|
WikiPageChanges Changes = new WikiPageChanges();
|
|
|
|
StandaloneDiffEditorConstructionOptions DiffEditorConstructionOptions(StandaloneDiffEditor editor)
|
|
{
|
|
return new StandaloneDiffEditorConstructionOptions
|
|
{
|
|
OriginalEditable = true,
|
|
AutomaticLayout = true,
|
|
Theme = "vs-dark",
|
|
};
|
|
}
|
|
|
|
class WikiPageChanges
|
|
{
|
|
public bool Minor { get; set; } = false;
|
|
[Required]
|
|
public string Summary { get; set; } = "";
|
|
}
|
|
|
|
async Task LoadDiff()
|
|
{
|
|
try
|
|
{
|
|
await WikiService.AuthenticateAsync(Settings.Wiki.Username, Settings.Wiki.Password);
|
|
|
|
if (GameId != Guid.Empty)
|
|
{
|
|
Game = await GameService.Get(GameId);
|
|
Page = await WikiService.GetPage(Game);
|
|
|
|
ModifiedContents = await WikiService.GenerateGamePage(Game.Id);
|
|
}
|
|
else if (RedistributableId != Guid.Empty)
|
|
{
|
|
Redistributable = await RedistributableService.Get(RedistributableId);
|
|
Page = await WikiService.GetPage(Redistributable);
|
|
|
|
ModifiedContents = await WikiService.GenerateRedistributablePage(Redistributable.Id);
|
|
}
|
|
|
|
OriginalContents = Page.Content;
|
|
|
|
if (!Page.Exists)
|
|
Changes.Summary = $"Created new page for {Game.Title}";
|
|
|
|
Authenticated = true;
|
|
|
|
TextModel originalModel = await BlazorMonaco.Editor.Global.CreateModel(JS, OriginalContents, "plaintext", "sample-diff-editor-originalModel");
|
|
TextModel modifiedModel = await BlazorMonaco.Editor.Global.CreateModel(JS, ModifiedContents, "plaintext", "sample-diff-editor-modifiedModel");
|
|
|
|
await DiffEditor.SetModel(new DiffEditorModel
|
|
{
|
|
Original = originalModel,
|
|
Modified = modifiedModel
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageService.Error("Wiki initialization failed!");
|
|
Logger.LogError(ex, "Wiki initialization failed!");
|
|
}
|
|
finally
|
|
{
|
|
Loading = false;
|
|
}
|
|
}
|
|
|
|
async Task Publish()
|
|
{
|
|
try
|
|
{
|
|
Loading = true;
|
|
|
|
Page.Content = ModifiedContents;
|
|
|
|
await WikiService.PublishPage(Page, Changes.Summary, Changes.Minor);
|
|
|
|
MessageService.Success("Changes published!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageService.Error("Could not publish changes!");
|
|
Logger.LogError(ex, "Could not publish changes!");
|
|
}
|
|
finally
|
|
{
|
|
Loading = false;
|
|
}
|
|
}
|
|
}
|