@page "/editor"
@using Katheryne.Exceptions
@using Katheryne.Abstractions
@using Frontend.Services
@inject NavigationManager Navigation
@inject IChatRobotFactory RobotFactory
@inject GrammarStorageService GrammarStorage
@code {
private StandaloneCodeEditor _editor = null!;
private AntList _logList = null!;
private GrammarHelp _grammarHelp = null!;
private readonly List _logs = new();
protected override async Task OnInitializedAsync()
{
Log("编辑器加载完成");
if (await GrammarStorage.RestoreGrammar())
{
await _editor.SetValue(RobotFactory.GrammarText);
Log("从浏览器中恢复成功");
}
else
{
Log("尚未设置语法");
}
await base.OnInitializedAsync();
}
private StandaloneEditorConstructionOptions GetEditorConstructionOptions(StandaloneCodeEditor editor)
{
return new StandaloneEditorConstructionOptions
{
Language = "yaml",
Value = !string.IsNullOrEmpty(RobotFactory.GrammarText) ? RobotFactory.GrammarText : string.Empty
};
}
private async Task CompileGrammarClicked()
{
string grammarText = await _editor.GetValue();
try
{
Log("编译文法...");
RobotFactory.SetGrammar(grammarText);
Log("编译成功!");
await GrammarStorage.StoreGrammar();
}
catch (GrammarException e)
{
Log($"编译文法遇到错误:{e.Message}");
}
}
private void QuitButtonClicked()
{
Navigation.NavigateTo("/", replace: true);
}
private async Task ClearGrammarClicked()
{
await GrammarStorage.RemoveGrammar();
Log("清除浏览器中的语法成功");
}
private void HelpButtonClicked()
{
_grammarHelp.Show();
}
private void Log(string message)
{
_logs.Add($"{DateTime.Now:HH:mm:ss} {message}");
StateHasChanged();
}
}