258 lines
7.5 KiB
Plaintext
258 lines
7.5 KiB
Plaintext
@page "/edit-template/{TemplateId:int}"
|
|
@using BLAIzor.Components.Layout
|
|
@using BLAIzor.Models
|
|
@using BLAIzor.Services
|
|
@using Newtonsoft.Json
|
|
@using BLAIzor.Components.Partials
|
|
@attribute [Authorize]
|
|
@layout AdminLayout
|
|
@inject DesignTemplateService DesignTemplateService
|
|
@inject CssTemplateService CssTemplateService
|
|
@inject NavigationManager NavigationManager
|
|
@inject QDrantService QDrantService
|
|
@inject HtmlSnippetProcessor HtmlSnippetProcessor
|
|
@inject IJSRuntime JSRuntime
|
|
|
|
<h3>Edit Design Template</h3>
|
|
|
|
<HeadContent>
|
|
@* <link rel="stylesheet" href="@(selectedBrandName+".css")" /> *@
|
|
@if (!string.IsNullOrEmpty(currentCssTemplate.CssContent))
|
|
{
|
|
<style>@currentCssTemplate.CssContent</style>
|
|
}
|
|
</HeadContent>
|
|
|
|
@if (isLoading)
|
|
{
|
|
<p>Loading...</p>
|
|
}
|
|
else
|
|
{
|
|
<EditForm Model="currentTemplate" OnValidSubmit="SaveTemplate">
|
|
<DataAnnotationsValidator />
|
|
<ValidationSummary />
|
|
|
|
<div class="form-group">
|
|
<label>Template Name</label>
|
|
<InputText class="form-control" @bind-Value="currentTemplate.TemplateName" />
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Template photo url</label>
|
|
<InputText class="form-control" @bind-Value="currentTemplate.TemplatePhotoUrl" />
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Description</label>
|
|
<InputTextArea class="form-control" @bind-Value="currentTemplate.Description" />
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Tags (comma-separated)</label>
|
|
<InputText class="form-control" @bind-Value="currentTemplate.Tags" />
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>CSS Content</label>
|
|
<RadzenHtmlEditor
|
|
@bind-Value=@currentCssTemplate.CssContent
|
|
style="height: 450px; color:#000; background-color: rgba(255,255,255,0.4)"
|
|
Input=@OnInput
|
|
Change=@OnChange
|
|
Paste=@OnPaste
|
|
UploadComplete=@OnUploadComplete
|
|
Execute=@OnExecute
|
|
UploadUrl="upload/image" >
|
|
<RadzenHtmlEditorUndo />
|
|
<RadzenHtmlEditorRedo />
|
|
<RadzenHtmlEditorSource />
|
|
</RadzenHtmlEditor>
|
|
@* <InputTextArea class="form-control" @bind-Value="currentCssTemplate.CssContent" /> *@
|
|
</div>
|
|
|
|
<button class="btn btn-success" type="submit">Save Changes</button>
|
|
</EditForm>
|
|
|
|
<hr />
|
|
|
|
<button class="btn btn-danger" @onclick="DeleteTemplate">Delete Template</button>
|
|
|
|
foreach(var snippet in SnippetList)
|
|
{
|
|
<TemplateSnippetEditor Snippet="snippet" OnContentUpdated="UpdateSnippet"></TemplateSnippetEditor>
|
|
}
|
|
|
|
<button class="btn btn-primary mt-3" @onclick="AddSnippet">Add new html snippets</button>
|
|
<button class="btn btn-success mt-3" @onclick="SaveSnippets">Save html snippets</button>
|
|
|
|
|
|
|
|
}
|
|
|
|
@code {
|
|
[Parameter]
|
|
public int TemplateId { get; set; }
|
|
|
|
private bool isLoading = true;
|
|
private DesignTemplate currentTemplate = new();
|
|
private CssTemplate currentCssTemplate = new();
|
|
private bool hasCss = false;
|
|
private bool hasCollection = false;
|
|
private List<HtmlSnippet> SnippetList = [];
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await LoadTemplate();
|
|
}
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
{
|
|
await JSRuntime.InvokeVoidAsync("setHtmlEditorSourceMode");
|
|
}
|
|
}
|
|
|
|
private async Task LoadTemplate()
|
|
{
|
|
isLoading = true;
|
|
|
|
// Load the design template and its associated CSS template from the database
|
|
currentTemplate = await DesignTemplateService.GetByIdAsync(TemplateId);
|
|
currentCssTemplate = await CssTemplateService.GetByDesignTemplateIdAsync(TemplateId);
|
|
var currentCollection = await QDrantService.GetCollectionByNameAsync(currentTemplate.QDrandCollectionName);
|
|
Console.Write(currentCollection);
|
|
if(currentCssTemplate == null)
|
|
{
|
|
hasCss = false;
|
|
currentCssTemplate = new();
|
|
currentCssTemplate.DesignTemplateId = TemplateId;
|
|
}
|
|
else
|
|
{
|
|
hasCss = true;
|
|
}
|
|
if(!string.IsNullOrEmpty(currentCollection))
|
|
{
|
|
hasCollection = true;
|
|
var collectionCount = await QDrantService.GetCollectionCount(currentTemplate.QDrandCollectionName);
|
|
for(int i=1; i <= collectionCount; i++)
|
|
{
|
|
var snippet = await QDrantService.GetSnippetAsync(i, currentTemplate.QDrandCollectionName);
|
|
var selectedPoint = JsonConvert.DeserializeObject<QDrantGetPointResult>(snippet)!;
|
|
|
|
|
|
Console.Write($"Id: {selectedPoint.result.id}, html: {selectedPoint.result.payload.Html}");
|
|
SnippetList.Add(selectedPoint.result.payload);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//let's create a collection
|
|
var result = QDrantService.CreateQdrantCollectionAsync(currentTemplate.QDrandCollectionName);
|
|
}
|
|
|
|
isLoading = false;
|
|
}
|
|
|
|
private async Task SaveTemplate()
|
|
{
|
|
isLoading = true;
|
|
currentCssTemplate.CssContent = currentCssTemplate.CssContent.Trim();
|
|
|
|
if(hasCss)
|
|
{
|
|
// Update the CSS template content
|
|
currentCssTemplate.LastUpdated = DateTime.UtcNow;
|
|
await CssTemplateService.UpdateAsync(currentCssTemplate);
|
|
|
|
}
|
|
else
|
|
{
|
|
// Create the CSS template content
|
|
currentCssTemplate.LastUpdated = DateTime.UtcNow;
|
|
await CssTemplateService.CreateAsync(currentCssTemplate);
|
|
}
|
|
|
|
// Save the CSS template
|
|
|
|
// Save the design template
|
|
currentTemplate.UpdatedAt = DateTime.Now;
|
|
await DesignTemplateService.UpdateAsync(currentTemplate);
|
|
|
|
isLoading = false;
|
|
|
|
// Redirect to the template manager page after saving
|
|
NavigationManager.NavigateTo("/design-templates");
|
|
}
|
|
|
|
private async Task DeleteTemplate()
|
|
{
|
|
isLoading = true;
|
|
|
|
// Delete the CSS template first
|
|
await CssTemplateService.DeleteAsync(currentCssTemplate.Id);
|
|
|
|
// Delete the design template
|
|
await DesignTemplateService.DeleteAsync(currentTemplate.Id);
|
|
|
|
isLoading = false;
|
|
|
|
// Redirect to the template manager page after deletion
|
|
NavigationManager.NavigateTo("/design-templates");
|
|
}
|
|
private void UpdateSnippet(HtmlSnippet updatedItem)
|
|
{
|
|
var item = SnippetList.FirstOrDefault(i => i.Name == updatedItem.Name);
|
|
if (item != null)
|
|
{
|
|
item.Html = updatedItem.Html;
|
|
}
|
|
}
|
|
|
|
private async Task SaveSnippets()
|
|
{
|
|
//I want to update all items without gaps in the ids always 1 to x
|
|
for (int i=1; i<=SnippetList.Count(); i++)
|
|
{
|
|
SnippetList[i-1].Id = i;
|
|
|
|
}
|
|
await HtmlSnippetProcessor.ProcessAndStoreTemplateSnippetAsync(currentTemplate.QDrandCollectionName, SnippetList);
|
|
}
|
|
|
|
async Task OnPaste(HtmlEditorPasteEventArgs args)
|
|
{
|
|
|
|
// await OnContentUpdated.InvokeAsync(MenuItem);
|
|
}
|
|
|
|
async Task OnChange(string html)
|
|
{
|
|
|
|
|
|
}
|
|
|
|
async Task OnInput(string html)
|
|
{
|
|
|
|
|
|
}
|
|
|
|
void OnExecute(HtmlEditorExecuteEventArgs args)
|
|
{
|
|
|
|
}
|
|
|
|
void OnUploadComplete(UploadCompleteEventArgs args)
|
|
{
|
|
|
|
}
|
|
|
|
public async Task AddSnippet()
|
|
{
|
|
SnippetList.Add(new HtmlSnippet());
|
|
}
|
|
}
|