Katheryne/Frontend/Components/Pages/Editor.razor

138 lines
4.3 KiB
Plaintext
Raw Permalink Normal View History

2023-10-14 22:12:19 +08:00
@page "/editor"
@using Katheryne.Exceptions
2023-10-17 23:32:39 +08:00
@using Katheryne.Abstractions
@using Frontend.Services
@inject NavigationManager Navigation
2023-10-17 23:32:39 +08:00
@inject IChatRobotFactory RobotFactory
@inject GrammarStorageService GrammarStorage
2023-10-14 22:12:19 +08:00
<Layout>
<Content>
<div class="editor-zone">
<div class="control-zone">
<Space Size="@("0")">
<SpaceItem>
2023-10-17 23:32:39 +08:00
<Tooltip Placement="@Placement.Bottom"
Title="退出语法编辑器">
<Button Type="@ButtonType.Text" OnClick="@QuitButtonClicked">
退出
</Button>
</Tooltip>
</SpaceItem>
<SpaceItem>
2023-10-17 23:32:39 +08:00
<Tooltip Placement="@Placement.Bottom"
Title="编译文法并保存在浏览器缓存中">
<Button Type="@ButtonType.Text" @onclick="@CompileGrammarClicked">
编译
</Button>
</Tooltip>
</SpaceItem>
2023-10-18 13:43:13 +08:00
2023-10-17 23:32:39 +08:00
<SpaceItem>
<Tooltip Placement="@Placement.Bottom"
Title="清除浏览器缓存中的文法">
<Button Type="@ButtonType.Text" @onclick="@ClearGrammarClicked">
清除
</Button>
</Tooltip>
</SpaceItem>
2023-10-18 13:43:13 +08:00
<SpaceItem>
<Button Type="@ButtonType.Text" @onclick="HelpButtonClicked">
帮助
</Button>
</SpaceItem>
</Space>
</div>
2023-10-17 23:32:39 +08:00
<StandaloneCodeEditor Id="code-editor" @ref="@_editor"
ConstructionOptions="GetEditorConstructionOptions"/>
<div class="logging-zone">
<AntList TItem="@string" DataSource="@_logs" Split="@false" @ref="@_logList">
2023-10-17 22:05:21 +08:00
<ListItem Style="padding: 0 0 0">
<p class="logging-item">@context</p>
</ListItem>
</AntList>
</div>
2023-10-14 22:12:19 +08:00
</div>
</Content>
</Layout>
2023-10-18 13:43:13 +08:00
<GrammarHelp @ref="@_grammarHelp"/>
2023-10-14 22:12:19 +08:00
@code {
private StandaloneCodeEditor _editor = null!;
private AntList<string> _logList = null!;
2023-10-18 13:43:13 +08:00
private GrammarHelp _grammarHelp = null!;
private readonly List<string> _logs = new();
2023-11-01 16:49:52 +08:00
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
Log("编辑器加载完成");
if (await GrammarStorage.RestoreGrammar())
{
await _editor.SetValue(RobotFactory.GrammarText);
Log("从浏览器中恢复成功");
}
else
{
Log("尚未设置语法");
}
}
await base.OnAfterRenderAsync(firstRender);
}
2023-10-17 23:32:39 +08:00
private StandaloneEditorConstructionOptions GetEditorConstructionOptions(StandaloneCodeEditor editor)
{
2023-10-14 22:12:19 +08:00
return new StandaloneEditorConstructionOptions
{
Language = "yaml",
2023-10-17 23:32:39 +08:00
Value = !string.IsNullOrEmpty(RobotFactory.GrammarText) ? RobotFactory.GrammarText : string.Empty
2023-10-14 22:12:19 +08:00
};
}
private async Task CompileGrammarClicked()
{
string grammarText = await _editor.GetValue();
try
{
Log("编译文法...");
RobotFactory.SetGrammar(grammarText);
Log("编译成功!");
2023-10-17 23:32:39 +08:00
await GrammarStorage.StoreGrammar();
}
catch (GrammarException e)
{
Log($"编译文法遇到错误:{e.Message}");
}
}
2023-10-17 23:32:39 +08:00
private void QuitButtonClicked()
{
Navigation.NavigateTo("/", replace: true);
}
2023-10-17 23:32:39 +08:00
private async Task ClearGrammarClicked()
{
await GrammarStorage.RemoveGrammar();
Log("清除浏览器中的语法成功");
}
2023-10-18 13:43:13 +08:00
private void HelpButtonClicked()
{
_grammarHelp.Show();
}
private void Log(string message)
{
_logs.Add($"{DateTime.Now:HH:mm:ss} {message}");
StateHasChanged();
2023-10-17 23:32:39 +08:00
}
2023-10-14 22:12:19 +08:00
}