@@ -10,19 +12,34 @@
-
+
+
+
-
+
+
+
+
+
+
+
+
+
-
+
@@ -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,14 +100,20 @@
}
}
- 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}");
StateHasChanged();
- }
+ }
}
\ No newline at end of file
diff --git a/Frontend/Pages/Index.razor b/Frontend/Pages/Index.razor
index 2a17ff5..ec0b3fc 100644
--- a/Frontend/Pages/Index.razor
+++ b/Frontend/Pages/Index.razor
@@ -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
@@ -40,7 +44,7 @@
-
+
@@ -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 GetChatMessages()
+ {
+ return _chatDictionary.TryGetValue(_currentGuid, out Chat? chat) ? chat.Messages : new List();
+ }
+
+ private IChatRobot GetChatRobot()
+ {
+ return _chatDictionary.TryGetValue(_currentGuid, out Chat? chat) ? chat.Robot : DefaultRobot;
+ }
}
\ No newline at end of file
diff --git a/Frontend/Program.cs b/Frontend/Program.cs
index 62ff349..203b4ad 100644
--- a/Frontend/Program.cs
+++ b/Frontend/Program.cs
@@ -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");
builder.RootComponents.Add("head::after");
builder.Services.AddAntDesign();
+builder.Services.AddBlazoredLocalStorage();
+
+builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Services.AddKatheryne();
+builder.Services.AddScoped();
WebAssemblyHost app = builder.Build();
diff --git a/Frontend/Services/GrammarStorageService.cs b/Frontend/Services/GrammarStorageService.cs
new file mode 100644
index 0000000..a457522
--- /dev/null
+++ b/Frontend/Services/GrammarStorageService.cs
@@ -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 _logger;
+ private readonly IChatRobotFactory _robotFactory;
+
+ public GrammarStorageService(ILocalStorageService localStorage,
+ ILogger logger,
+ IChatRobotFactory robotFactory)
+ {
+ _localStorage = localStorage;
+ _logger = logger;
+ _robotFactory = robotFactory;
+ }
+
+ ///
+ /// 尝试从LocalStorage中恢复之前设置的文法
+ ///
+ /// 恢复文法是否成功
+ public async Task RestoreGrammar()
+ {
+ _logger.LogDebug("Try to restore grammar text.");
+
+ string result = await _localStorage.GetItemAsync(GrammarTextKey);
+
+ if (result == default)
+ {
+ return false;
+ }
+
+ _logger.LogDebug("Restore grammar text successfully.");
+ _robotFactory.SetGrammar(result);
+ return true;
+ }
+
+ ///
+ /// 保存当前设置使用的文法
+ ///
+ public async Task StoreGrammar()
+ {
+ if (!string.IsNullOrEmpty(_robotFactory.GrammarText))
+ {
+ _logger.LogDebug("Store current grammar text.");
+ await _localStorage.SetItemAsync(GrammarTextKey, _robotFactory.GrammarText);
+ }
+ }
+
+ ///
+ /// 清除当前设置的文法
+ ///
+ public async Task RemoveGrammar()
+ {
+ await _localStorage.RemoveItemAsync(GrammarTextKey);
+ }
+}
\ No newline at end of file