add: 在LocalStorage里面存储文法

This commit is contained in:
jackfiled 2023-10-17 23:32:39 +08:00
parent 091a07460a
commit 28c7098251
5 changed files with 134 additions and 26 deletions

View File

@ -4,10 +4,12 @@
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RunAOTCompilation>true</RunAOTCompilation>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AntDesign" Version="0.15.5" />
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="BlazorMonaco" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.11" PrivateAssets="all" />

View File

@ -1,8 +1,10 @@
@page "/editor"
@using Katheryne.Services
@using Katheryne.Exceptions
@using Katheryne.Abstractions
@using Frontend.Services
@inject NavigationManager Navigation
@inject KatheryneChatRobotFactory RobotFactory
@inject IChatRobotFactory RobotFactory
@inject GrammarStorageService GrammarStorage
<Layout>
<Content>
@ -10,15 +12,30 @@
<div class="control-zone">
<Space Size="@("0")">
<SpaceItem>
<Button Type="@ButtonType.Text" OnClick="@QuitButtontClicked">
<Tooltip Placement="@Placement.Bottom"
Title="退出语法编辑器">
<Button Type="@ButtonType.Text" OnClick="@QuitButtonClicked">
退出
</Button>
</Tooltip>
</SpaceItem>
<SpaceItem>
<Tooltip Placement="@Placement.Bottom"
Title="编译文法并保存在浏览器缓存中">
<Button Type="@ButtonType.Text" @onclick="@CompileGrammarClicked">
编译
</Button>
</Tooltip>
</SpaceItem>
<SpaceItem>
<Tooltip Placement="@Placement.Bottom"
Title="清除浏览器缓存中的文法">
<Button Type="@ButtonType.Text" @onclick="@ClearGrammarClicked">
清除
</Button>
</Tooltip>
</SpaceItem>
</Space>
</div>
@ -46,27 +63,23 @@
protected override async Task OnInitializedAsync()
{
Log("编辑器加载完成");
if (await GrammarStorage.RestoreGrammar())
{
Log("从浏览器中恢复成功");
}
else
{
Log("尚未设置语法");
}
await base.OnInitializedAsync();
}
private StandaloneEditorConstructionOptions GetEditorConstructionOptions(StandaloneCodeEditor editor)
{
string grammarText;
if (!string.IsNullOrEmpty(RobotFactory.GrammarText))
{
Log("加载文法...");
grammarText = RobotFactory.GrammarText;
}
else
{
Log("未设置文法");
grammarText = string.Empty;
}
return new StandaloneEditorConstructionOptions
{
Language = "yaml",
Value = grammarText
Value = !string.IsNullOrEmpty(RobotFactory.GrammarText) ? RobotFactory.GrammarText : string.Empty
};
}
@ -79,6 +92,7 @@
Log("编译文法...");
RobotFactory.SetGrammar(grammarText);
Log("编译成功!");
await GrammarStorage.StoreGrammar();
}
catch (GrammarException e)
{
@ -86,11 +100,17 @@
}
}
private void QuitButtontClicked()
private void QuitButtonClicked()
{
Navigation.NavigateTo("/", replace: true);
}
private async Task ClearGrammarClicked()
{
await GrammarStorage.RemoveGrammar();
Log("清除浏览器中的语法成功");
}
private void Log(string message)
{
_logs.Add($"{DateTime.Now:HH:mm:ss} {message}");

View File

@ -1,7 +1,11 @@
@page "/"
@using Frontend.Models
@using Frontend.Services
@using Katheryne
@using Katheryne.Abstractions
@inject IChatRobotFactory ChatRobotFactory
@inject GrammarStorageService GrammarStorage
@inject DefaultChatRobot DefaultRobot
<Layout>
@ -40,7 +44,7 @@
</Sider>
<Content>
<ChatZone Messages="@_chatDictionary[_currentGuid].Messages" Robot="@_chatDictionary[_currentGuid].Robot"/>
<ChatZone Messages="@GetChatMessages()" Robot="@GetChatRobot()"/>
</Content>
</Layout>
@ -49,11 +53,15 @@
private Guid _currentGuid;
protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
await GrammarStorage.RestoreGrammar();
Chat chat = GetInitChat();
_chatDictionary.Add(chat.Guid, chat);
_currentGuid = chat.Guid;
await base.OnInitializedAsync();
}
private void CreateChatClicked()
@ -94,4 +102,14 @@
return chat;
}
private List<ChatMessage> GetChatMessages()
{
return _chatDictionary.TryGetValue(_currentGuid, out Chat? chat) ? chat.Messages : new List<ChatMessage>();
}
private IChatRobot GetChatRobot()
{
return _chatDictionary.TryGetValue(_currentGuid, out Chat? chat) ? chat.Robot : DefaultRobot;
}
}

View File

@ -1,14 +1,20 @@
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Katheryne;
using Frontend;
using Frontend.Services;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddAntDesign();
builder.Services.AddBlazoredLocalStorage();
builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Services.AddKatheryne();
builder.Services.AddScoped<GrammarStorageService>();
WebAssemblyHost app = builder.Build();

View File

@ -0,0 +1,62 @@
using Blazored.LocalStorage;
using Katheryne.Abstractions;
namespace Frontend.Services;
public class GrammarStorageService
{
private const string GrammarTextKey = "GrammarText";
private readonly ILocalStorageService _localStorage;
private readonly ILogger<GrammarStorageService> _logger;
private readonly IChatRobotFactory _robotFactory;
public GrammarStorageService(ILocalStorageService localStorage,
ILogger<GrammarStorageService> logger,
IChatRobotFactory robotFactory)
{
_localStorage = localStorage;
_logger = logger;
_robotFactory = robotFactory;
}
/// <summary>
/// 尝试从LocalStorage中恢复之前设置的文法
/// </summary>
/// <returns>恢复文法是否成功</returns>
public async Task<bool> RestoreGrammar()
{
_logger.LogDebug("Try to restore grammar text.");
string result = await _localStorage.GetItemAsync<string>(GrammarTextKey);
if (result == default)
{
return false;
}
_logger.LogDebug("Restore grammar text successfully.");
_robotFactory.SetGrammar(result);
return true;
}
/// <summary>
/// 保存当前设置使用的文法
/// </summary>
public async Task StoreGrammar()
{
if (!string.IsNullOrEmpty(_robotFactory.GrammarText))
{
_logger.LogDebug("Store current grammar text.");
await _localStorage.SetItemAsync(GrammarTextKey, _robotFactory.GrammarText);
}
}
/// <summary>
/// 清除当前设置的文法
/// </summary>
public async Task RemoveGrammar()
{
await _localStorage.RemoveItemAsync(GrammarTextKey);
}
}