docs: #319 section editor

This commit is contained in:
Hamid Reza Mohammadi 2022-05-11 09:22:38 +04:30
parent bcae003d66
commit 3af5aea88e
3 changed files with 216 additions and 1 deletions

View File

@ -624,7 +624,7 @@ else
{
<tr>
<td>
<a href="/User/Section?id=@poemSetion.Id">
<a href="/User/Section?poemId=@poemSetion.PoemId&index=@poemSetion.Index">
قطعهٔ @(poemSetion.Number.ToPersianNumbers()):
</a>
</td>

View File

@ -0,0 +1,110 @@
@page
@model GanjooRazor.Areas.User.Pages.SectionModel
@using DNTPersianUtils.Core
@{
Layout = "_UserPanelLayout";
ViewData["Title"] = "ویرایشگر قطعه";
await GanjooRazor.Utils.GanjoorSessionChecker.ApplyPermissionsToViewData(Request, Response, ViewData);
}
<h1>@ViewData["Title"]</h1>
@if (!string.IsNullOrEmpty(Model.FatalError))
{
@Html.Raw(Model.FatalError)
}
else
{
<table>
<tr>
<td>
@Html.Raw(Model.PoemSection.HtmlText)
</td>
</tr>
<tr>
<td>
وزن فعلی:
</td>
</tr>
<tr>
<td>
<p id="rhythm-old">@(Model.PoemSection.GanjoorMetre == null ? "" : Model.PoemSection.GanjoorMetre.Rhythm)</p>
</td>
</tr>
<tr>
<td>
وزن ویراستهٔ من:
</td>
</tr>
<tr>
<td>
<label for="select-search">جستجوی وزن:</label><br />
<input type="text" id="select-search" value="" oninput="doSearchInRhythmsCombo('select-search', 'rhythm-new')" list="inputRhythms" autocomplete="off" size="50" />
<datalist id="inputRhythms">
@if (Model.RhythmsByVerseCount != null)
@foreach (var rhythm in Model.RhythmsByVerseCount)
{
<option value="@rhythm.Rhythm">@rhythm.Rhythm</option>
}
</datalist>
</td>
</tr>
<tr>
<td>
<label for="rhythm-new">وزن انتخاب شده:</label>
<br />
<select id="rhythm-new" name="rhythm-new">
@foreach (var rhythm in Model.RhythmsAlphabetically)
{
if (Model.MyLastEdit != null && Model.MyLastEdit.Rhythm == rhythm.Rhythm)
{
<option value="@rhythm.Rhythm" selected>@rhythm.Rhythm</option>
}
else
{
if (Model.PoemSection.GanjoorMetre != null && Model.PoemSection.GanjoorMetre.Rhythm == rhythm.Rhythm)
{
<option value="@rhythm.Rhythm" selected>@rhythm.Rhythm</option>
}
else
{
<option value="@rhythm.Rhythm">@rhythm.Rhythm</option>
}
}
}
</select>
<a role="button" onclick="resetRhythm('rhythm-new')" class="pagebutton">پاک کردن وزن</a>
<br />
<script>
function newRhythm(){
var rhythm = prompt("پیشنهاد وزن جدید", "");
if(rhythm != null){
var select = document.getElementById('rhythm-new');
var options = select.options;
for (let i = 0; i < options. length; i++){
if(options[i].value == rhythm){
alert('وزن پیشنهادی تکراری است.');
return;
}
}
var opt = document.createElement('option');
opt.value = rhythm;
opt.innerHTML = rhythm;
select.appendChild(opt);
var opt2 = document.createElement('option');
opt2.value = rhythm;
opt2.innerHTML = rhythm;
document.getElementById('rhythm-new2').appendChild(opt2);
alert('وزن پیشنهادی به انتهای لیست وزن‌ها اضافه شده. لطفاً از لیست آن را انتخاب کنید.');
}
}
</script>
<a role="button" onclick="newRhythm()" class="pagebutton">پیشنهاد وزن جدید</a>
</td>
</tr>
</table>
}

View File

@ -0,0 +1,105 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using GanjooRazor.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMuseum.Models.Ganjoor;
using RMuseum.Models.Ganjoor.ViewModels;
namespace GanjooRazor.Areas.User.Pages
{
[IgnoreAntiforgeryToken(Order = 1001)]
public class SectionModel : PageModel
{
/// <summary>
/// fatal error
/// </summary>
public string FatalError { get; set; }
/// <summary>
/// page
/// </summary>
public GanjoorPageCompleteViewModel PageInformation { get; set; }
/// <summary>
/// section
/// </summary>
public GanjoorPoemSection PoemSection { get; set; }
/// <summary>
/// rhythms alphabetically
/// </summary>
public GanjoorMetre[] RhythmsAlphabetically { get; set; }
/// <summary>
/// rhythms by frequency
/// </summary>
public GanjoorMetre[] RhythmsByVerseCount { get; set; }
/// <summary>
/// my last edit
/// </summary>
public GanjoorPoemCorrectionViewModel MyLastEdit { get; set; }
public async Task<IActionResult> OnGetAsync()
{
if (string.IsNullOrEmpty(Request.Cookies["Token"]))
return Redirect("/");
FatalError = "";
using (HttpClient secureClient = new HttpClient())
{
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
{
var pageUrlResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/ganjoor/pageurl?id={Request.Query["poemId"]}");
if (!pageUrlResponse.IsSuccessStatusCode)
{
FatalError = JsonConvert.DeserializeObject<string>(await pageUrlResponse.Content.ReadAsStringAsync());
return Page();
}
var pageUrl = JsonConvert.DeserializeObject<string>(await pageUrlResponse.Content.ReadAsStringAsync());
var pageQuery = await secureClient.GetAsync($"{APIRoot.Url}/api/ganjoor/page?url={pageUrl}");
if (!pageQuery.IsSuccessStatusCode)
{
FatalError = JsonConvert.DeserializeObject<string>(await pageQuery.Content.ReadAsStringAsync());
return Page();
}
PageInformation = JObject.Parse(await pageQuery.Content.ReadAsStringAsync()).ToObject<GanjoorPageCompleteViewModel>();
var rhythmResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/ganjoor/rhythms?sortOnVerseCount=true");
if (!rhythmResponse.IsSuccessStatusCode)
{
FatalError = JsonConvert.DeserializeObject<string>(await rhythmResponse.Content.ReadAsStringAsync());
return Page();
}
RhythmsByVerseCount = JsonConvert.DeserializeObject<GanjoorMetre[]>(await rhythmResponse.Content.ReadAsStringAsync());
List<GanjoorMetre> rhythmsByVerseCount = new List<GanjoorMetre>(RhythmsByVerseCount);
rhythmsByVerseCount.Sort((a, b) => a.Rhythm.CompareTo(b.Rhythm));
rhythmsByVerseCount.Insert(0, new GanjoorMetre()
{
Rhythm = ""
}
);
RhythmsAlphabetically = rhythmsByVerseCount.ToArray();
PoemSection = PageInformation.Poem.Sections.Where(s => s.Index == int.Parse(Request.Query["index"])).Single();
}
else
{
FatalError = "لطفا از گنجور خارج و مجددا به آن وارد شوید.";
}
}
return Page();
}
}
}