remember selected schema when editing a file

This commit is contained in:
Jesse Plamondon-Willard 2020-06-20 11:36:22 -04:00
parent d02a40de99
commit ed3309e7bb
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
4 changed files with 27 additions and 15 deletions

View File

@ -22,6 +22,7 @@
* Added GitHub licenses to mod compatibility list.
* Improved JSON validator:
* added SMAPI `i18n` schema;
* editing an uploaded file now remembers the selected schema;
* changed default schema to plain JSON.
* Updated ModDrop URLs.
* Internal changes to improve performance and reliability.

View File

@ -58,16 +58,22 @@ namespace StardewModdingAPI.Web.Controllers
/// <summary>Render the schema validator UI.</summary>
/// <param name="schemaName">The schema name with which to validate the JSON, or 'edit' to return to the edit screen.</param>
/// <param name="id">The stored file ID.</param>
/// <param name="operation">The operation to perform for the selected log ID. This can be 'edit', or any other value to view.</param>
[HttpGet]
[Route("json")]
[Route("json/{schemaName}")]
[Route("json/{schemaName}/{id}")]
public async Task<ViewResult> Index(string schemaName = null, string id = null)
[Route("json/{schemaName}/{id}/{operation}")]
public async Task<ViewResult> Index(string schemaName = null, string id = null, string operation = null)
{
// parse arguments
schemaName = this.NormalizeSchemaName(schemaName);
bool hasId = !string.IsNullOrWhiteSpace(id);
bool isEditView = !hasId || operation?.Trim().ToLower() == "edit";
var result = new JsonValidatorModel(id, schemaName, this.SchemaFormats);
if (string.IsNullOrWhiteSpace(id))
// build result model
var result = this.GetModel(id, schemaName, isEditView);
if (!hasId)
return this.View("Index", result);
// fetch raw JSON
@ -77,7 +83,7 @@ namespace StardewModdingAPI.Web.Controllers
result.SetContent(file.Content, expiry: file.Expiry, uploadWarning: file.Warning);
// skip parsing if we're going to the edit screen
if (schemaName?.ToLower() == "edit")
if (isEditView)
return this.View("Index", result);
// parse JSON
@ -131,7 +137,7 @@ namespace StardewModdingAPI.Web.Controllers
public async Task<ActionResult> PostAsync(JsonValidatorRequestModel request)
{
if (request == null)
return this.View("Index", this.GetModel(null, null).SetUploadError("The request seems to be invalid."));
return this.View("Index", this.GetModel(null, null, isEditView: true).SetUploadError("The request seems to be invalid."));
// normalize schema name
string schemaName = this.NormalizeSchemaName(request.SchemaName);
@ -139,12 +145,12 @@ namespace StardewModdingAPI.Web.Controllers
// get raw text
string input = request.Content;
if (string.IsNullOrWhiteSpace(input))
return this.View("Index", this.GetModel(null, schemaName).SetUploadError("The JSON file seems to be empty."));
return this.View("Index", this.GetModel(null, schemaName, isEditView: true).SetUploadError("The JSON file seems to be empty."));
// upload file
UploadResult result = await this.Storage.SaveAsync(input);
if (!result.Succeeded)
return this.View("Index", this.GetModel(result.ID, schemaName).SetUploadError(result.UploadError));
return this.View("Index", this.GetModel(result.ID, schemaName, isEditView: true).SetContent(input, null).SetUploadError(result.UploadError));
// redirect to view
return this.Redirect(this.Url.PlainAction("Index", "JsonValidator", new { schemaName = schemaName, id = result.ID }));
@ -157,9 +163,10 @@ namespace StardewModdingAPI.Web.Controllers
/// <summary>Build a JSON validator model.</summary>
/// <param name="pasteID">The stored file ID.</param>
/// <param name="schemaName">The schema name with which the JSON was validated.</param>
private JsonValidatorModel GetModel(string pasteID, string schemaName)
/// <param name="isEditView">Whether to show the edit view.</param>
private JsonValidatorModel GetModel(string pasteID, string schemaName, bool isEditView)
{
return new JsonValidatorModel(pasteID, schemaName, this.SchemaFormats);
return new JsonValidatorModel(pasteID, schemaName, this.SchemaFormats, isEditView);
}
/// <summary>Get a normalized schema name, or the <see cref="DefaultSchemaID"/> if blank.</summary>

View File

@ -10,6 +10,9 @@ namespace StardewModdingAPI.Web.ViewModels.JsonValidator
/*********
** Accessors
*********/
/// <summary>Whether to show the edit view.</summary>
public bool IsEditView { get; set; }
/// <summary>The paste ID.</summary>
public string PasteID { get; set; }
@ -51,11 +54,13 @@ namespace StardewModdingAPI.Web.ViewModels.JsonValidator
/// <param name="pasteID">The stored file ID.</param>
/// <param name="schemaName">The schema name with which the JSON was validated.</param>
/// <param name="schemaFormats">The supported JSON schemas (names indexed by ID).</param>
public JsonValidatorModel(string pasteID, string schemaName, IDictionary<string, string> schemaFormats)
/// <param name="isEditView">Whether to show the edit view.</param>
public JsonValidatorModel(string pasteID, string schemaName, IDictionary<string, string> schemaFormats, bool isEditView)
{
this.PasteID = pasteID;
this.SchemaName = schemaName;
this.SchemaFormats = schemaFormats;
this.IsEditView = isEditView;
}
/// <summary>Set the validated content.</summary>

View File

@ -9,7 +9,6 @@
string newUploadUrl = this.Url.PlainAction("Index", "JsonValidator", new { schemaName = Model.SchemaName });
string schemaDisplayName = null;
bool isValidSchema = Model.SchemaName != null && Model.SchemaFormats.TryGetValue(Model.SchemaName, out schemaDisplayName) && schemaDisplayName?.ToLower() != "none";
bool isEditView = Model.Content == null || Model.SchemaName?.ToLower() == "edit";
// build title
ViewData["Title"] = "JSON validator";
@ -63,7 +62,7 @@ else if (Model.ParseError != null)
<small v-pre>Error details: @Model.ParseError</small>
</div>
}
else if (!isEditView && Model.PasteID != null)
else if (!Model.IsEditView && Model.PasteID != null)
{
<div class="banner success">
<strong>Share this link to let someone else see this page:</strong> <code>@curPageUrl</code><br />
@ -84,7 +83,7 @@ else if (!isEditView && Model.PasteID != null)
}
@* upload new file *@
@if (isEditView)
@if (Model.IsEditView)
{
<h2>Upload a JSON file</h2>
<form action="@this.Url.PlainAction("PostAsync", "JsonValidator")" method="post">
@ -112,7 +111,7 @@ else if (!isEditView && Model.PasteID != null)
}
@* validation results *@
@if (!isEditView)
@if (!Model.IsEditView)
{
<div id="output">
@if (Model.UploadError == null)
@ -158,7 +157,7 @@ else if (!isEditView && Model.PasteID != null)
{
<option value="@pair.Key" selected="@(Model.SchemaName == pair.Key)">@pair.Value</option>
}
</select>) or <a href="@(this.Url.PlainAction("Index", "JsonValidator", new { id = this.Model.PasteID, schemaName = "edit" }))">edit this file</a>.
</select>) or <a href="@(this.Url.PlainAction("Index", "JsonValidator", new { id = this.Model.PasteID, schemaName = this.Model.SchemaName, operation = "edit" }))">edit this file</a>.
</div>
<pre id="raw-content" class="sunlight-highlight-javascript">@Model.Content</pre>