add: 语法编辑器界面

语法热重载功能
This commit is contained in:
jackfiled 2023-10-16 00:10:46 +08:00
parent d8edc6c28b
commit 8dfbdb02b6
7 changed files with 100 additions and 23 deletions

View File

@ -1,19 +1,93 @@
@page "/editor"
@using Katheryne.Services
@using Katheryne.Exceptions
@inject NavigationManager Navigation
@inject KatheryneChatRobotFactory RobotFactory
<Layout>
<Content>
<div class="editor-zone">
<StandaloneCodeEditor ConstructionOptions="GetEditorConstructionOptions" Id="my-editor-instance"/>
<div class="control-zone">
<Space Size="@("0")">
<SpaceItem>
<Button Type="@ButtonType.Text">
退出
</Button>
</SpaceItem>
<SpaceItem>
<Button Type="@ButtonType.Text" @onclick="@CompileGrammarClicked">
编译
</Button>
</SpaceItem>
</Space>
</div>
<StandaloneCodeEditor Id="code-editor" @ref="@_editor"
ConstructionOptions="GetEditorConstructionOptions"/>
<div class="logging-zone">
<AntList TItem="@string" DataSource="@_logs" Split="@false" @ref="@_logList">
<p style="margin-bottom: 0">@context</p>
</AntList>
</div>
</div>
</Content>
</Layout>
@code {
private StandaloneCodeEditor _editor = null!;
private AntList<string> _logList = null!;
private readonly List<string> _logs = new();
protected override async Task OnInitializedAsync()
{
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"
Language = "yaml",
Value = grammarText
};
}
private async Task CompileGrammarClicked()
{
string grammarText = await _editor.GetValue();
try
{
Log("编译文法...");
RobotFactory.SetGrammar(grammarText);
Log("编译成功!");
}
catch (GrammarException e)
{
Log($"编译文法遇到错误:{e.Message}");
}
}
private void Log(string message)
{
_logs.Add($"{DateTime.Now:HH:mm:ss} {message}");
StateHasChanged();
}
}

View File

@ -1,3 +1,13 @@
.editor-zone {
min-height: calc(100vh - 64px);
height: calc(100vh - 64px);
}
.control-zone {
height: 5%;
padding: 2px;
}
.logging-zone {
height: 15%;
overflow-y: auto;
}

View File

@ -51,23 +51,6 @@
protected override void OnInitialized()
{
ChatRobotFactory.SetGrammar(@"
robotName: 凯瑟琳
stages:
- name: start
answer: 向着星辰和深渊!欢迎来到冒险家协会。
transformers:
- pattern: .*?
nextStageName: running
- name: running
answer: 对不起,做不到。
transformers:
- pattern: .*?
nextStageName: running
beginStageName: start
");
Chat chat = GetInitChat();
_chatDictionary.Add(chat.Guid, chat);
_currentGuid = chat.Guid;

View File

@ -9,12 +9,18 @@
试作自律型可重编程客服机器人
</p>
</SpaceItem>
<SpaceItem>
<p class="subtitle-text">
a.k.a 凯瑟琳
</p>
</SpaceItem>
<SpaceItem>
<div>
<a href="editor/">维护界面</a>
</div>
</SpaceItem>
</Space>
</Header>

View File

@ -32,5 +32,6 @@ body {
}
.monaco-editor-container {
min-height: 100%;
min-height: 80%;
margin: 5px;
}

View File

@ -9,6 +9,6 @@ public static class ServiceCollectionExtensions
{
collection.AddSingleton<YamlDeserializerFactory>();
collection.AddSingleton<DefaultChatRobot>();
collection.AddScoped<KatheryneChatRobotFactory>();
collection.AddSingleton<KatheryneChatRobotFactory>();
}
}

View File

@ -14,6 +14,8 @@ public class KatheryneChatRobotFactory
private Grammar? _grammar;
public string GrammarText { get; private set; } = string.Empty;
public KatheryneChatRobotFactory(YamlDeserializerFactory deserializerFactory,
ILogger<KatheryneChatRobotFactory> factoryLogger,
ILogger<KatheryneChatRobot> robotLogger,
@ -28,6 +30,7 @@ public class KatheryneChatRobotFactory
public void SetGrammar(string grammarText)
{
_factoryLogger.LogInformation("Receive new grammar: {}.", grammarText);
GrammarText = grammarText;
IDeserializer deserializer = _deserializerFactory.GetDeserializer();
LexicalModel model = deserializer.Deserialize<LexicalModel>(grammarText);