2023-10-14 22:12:19 +08:00
|
|
|
@page "/editor"
|
2023-10-16 00:10:46 +08:00
|
|
|
@using Katheryne.Exceptions
|
2023-10-17 23:32:39 +08:00
|
|
|
@using Katheryne.Abstractions
|
|
|
|
@using Frontend.Services
|
2023-10-16 00:10:46 +08:00
|
|
|
@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">
|
2023-10-16 00:10:46 +08:00
|
|
|
<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>
|
2023-10-16 00:10:46 +08:00
|
|
|
</SpaceItem>
|
|
|
|
|
|
|
|
<SpaceItem>
|
2023-10-17 23:32:39 +08:00
|
|
|
<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>
|
2023-10-16 00:10:46 +08:00
|
|
|
</SpaceItem>
|
|
|
|
</Space>
|
|
|
|
</div>
|
2023-10-17 23:32:39 +08:00
|
|
|
|
2023-10-16 00:10:46 +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>
|
2023-10-17 18:04:17 +08:00
|
|
|
</ListItem>
|
2023-10-16 00:10:46 +08:00
|
|
|
</AntList>
|
|
|
|
</div>
|
2023-10-14 22:12:19 +08:00
|
|
|
</div>
|
|
|
|
</Content>
|
|
|
|
</Layout>
|
|
|
|
|
|
|
|
@code {
|
2023-10-16 00:10:46 +08:00
|
|
|
private StandaloneCodeEditor _editor = null!;
|
|
|
|
private AntList<string> _logList = null!;
|
|
|
|
|
|
|
|
private readonly List<string> _logs = new();
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
{
|
|
|
|
Log("编辑器加载完成");
|
2023-10-17 23:32:39 +08:00
|
|
|
if (await GrammarStorage.RestoreGrammar())
|
2023-10-16 00:10:46 +08:00
|
|
|
{
|
2023-10-17 23:32:39 +08:00
|
|
|
Log("从浏览器中恢复成功");
|
2023-10-16 00:10:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-17 23:32:39 +08:00
|
|
|
Log("尚未设置语法");
|
2023-10-16 00:10:46 +08:00
|
|
|
}
|
2023-10-17 23:32:39 +08:00
|
|
|
await base.OnInitializedAsync();
|
|
|
|
}
|
2023-10-17 18:04:17 +08:00
|
|
|
|
2023-10-17 23:32:39 +08:00
|
|
|
private StandaloneEditorConstructionOptions GetEditorConstructionOptions(StandaloneCodeEditor editor)
|
|
|
|
{
|
2023-10-14 22:12:19 +08:00
|
|
|
return new StandaloneEditorConstructionOptions
|
|
|
|
{
|
2023-10-16 00:10:46 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|
2023-10-16 00:10:46 +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();
|
2023-10-16 00:10:46 +08:00
|
|
|
}
|
|
|
|
catch (GrammarException e)
|
|
|
|
{
|
|
|
|
Log($"编译文法遇到错误:{e.Message}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:32:39 +08:00
|
|
|
private void QuitButtonClicked()
|
2023-10-17 18:04:17 +08:00
|
|
|
{
|
|
|
|
Navigation.NavigateTo("/", replace: true);
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:32:39 +08:00
|
|
|
private async Task ClearGrammarClicked()
|
|
|
|
{
|
|
|
|
await GrammarStorage.RemoveGrammar();
|
|
|
|
Log("清除浏览器中的语法成功");
|
|
|
|
}
|
|
|
|
|
2023-10-16 00:10:46 +08:00
|
|
|
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
|
|
|
}
|