add: 在LocalStorage里面存储文法

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

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,19 +12,34 @@
<div class="control-zone">
<Space Size="@("0")">
<SpaceItem>
<Button Type="@ButtonType.Text" OnClick="@QuitButtontClicked">
退出
</Button>
<Tooltip Placement="@Placement.Bottom"
Title="退出语法编辑器">
<Button Type="@ButtonType.Text" OnClick="@QuitButtonClicked">
退出
</Button>
</Tooltip>
</SpaceItem>
<SpaceItem>
<Button Type="@ButtonType.Text" @onclick="@CompileGrammarClicked">
编译
</Button>
<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>
<StandaloneCodeEditor Id="code-editor" @ref="@_editor"
ConstructionOptions="GetEditorConstructionOptions"/>
@@ -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();
}
}
}

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;
}
}