add: 在LocalStorage里面存储文法
This commit is contained in:
		@@ -4,10 +4,12 @@
 | 
				
			|||||||
        <TargetFramework>net7.0</TargetFramework>
 | 
					        <TargetFramework>net7.0</TargetFramework>
 | 
				
			||||||
        <Nullable>enable</Nullable>
 | 
					        <Nullable>enable</Nullable>
 | 
				
			||||||
        <ImplicitUsings>enable</ImplicitUsings>
 | 
					        <ImplicitUsings>enable</ImplicitUsings>
 | 
				
			||||||
 | 
					        <RunAOTCompilation>true</RunAOTCompilation>
 | 
				
			||||||
    </PropertyGroup>
 | 
					    </PropertyGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <ItemGroup>
 | 
					    <ItemGroup>
 | 
				
			||||||
      <PackageReference Include="AntDesign" Version="0.15.5" />
 | 
					      <PackageReference Include="AntDesign" Version="0.15.5" />
 | 
				
			||||||
 | 
					      <PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
 | 
				
			||||||
      <PackageReference Include="BlazorMonaco" Version="3.1.0" />
 | 
					      <PackageReference Include="BlazorMonaco" Version="3.1.0" />
 | 
				
			||||||
      <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.11" />
 | 
					      <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.11" />
 | 
				
			||||||
      <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.11" PrivateAssets="all" />
 | 
					      <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.11" PrivateAssets="all" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,10 @@
 | 
				
			|||||||
@page "/editor"
 | 
					@page "/editor"
 | 
				
			||||||
@using Katheryne.Services
 | 
					 | 
				
			||||||
@using Katheryne.Exceptions
 | 
					@using Katheryne.Exceptions
 | 
				
			||||||
 | 
					@using Katheryne.Abstractions
 | 
				
			||||||
 | 
					@using Frontend.Services
 | 
				
			||||||
@inject NavigationManager Navigation
 | 
					@inject NavigationManager Navigation
 | 
				
			||||||
@inject KatheryneChatRobotFactory RobotFactory
 | 
					@inject IChatRobotFactory RobotFactory
 | 
				
			||||||
 | 
					@inject GrammarStorageService GrammarStorage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<Layout>
 | 
					<Layout>
 | 
				
			||||||
    <Content>
 | 
					    <Content>
 | 
				
			||||||
@@ -10,15 +12,30 @@
 | 
				
			|||||||
            <div class="control-zone">
 | 
					            <div class="control-zone">
 | 
				
			||||||
                <Space Size="@("0")">
 | 
					                <Space Size="@("0")">
 | 
				
			||||||
                    <SpaceItem>
 | 
					                    <SpaceItem>
 | 
				
			||||||
                        <Button Type="@ButtonType.Text" OnClick="@QuitButtontClicked">
 | 
					                        <Tooltip Placement="@Placement.Bottom"
 | 
				
			||||||
 | 
					                                 Title="退出语法编辑器">
 | 
				
			||||||
 | 
					                            <Button Type="@ButtonType.Text" OnClick="@QuitButtonClicked">
 | 
				
			||||||
                                退出
 | 
					                                退出
 | 
				
			||||||
                            </Button>
 | 
					                            </Button>
 | 
				
			||||||
 | 
					                        </Tooltip>
 | 
				
			||||||
                    </SpaceItem>
 | 
					                    </SpaceItem>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    <SpaceItem>
 | 
					                    <SpaceItem>
 | 
				
			||||||
 | 
					                        <Tooltip Placement="@Placement.Bottom"
 | 
				
			||||||
 | 
					                                 Title="编译文法并保存在浏览器缓存中">
 | 
				
			||||||
                            <Button Type="@ButtonType.Text" @onclick="@CompileGrammarClicked">
 | 
					                            <Button Type="@ButtonType.Text" @onclick="@CompileGrammarClicked">
 | 
				
			||||||
                                编译
 | 
					                                编译
 | 
				
			||||||
                            </Button>
 | 
					                            </Button>
 | 
				
			||||||
 | 
					                        </Tooltip>
 | 
				
			||||||
 | 
					                    </SpaceItem>
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
 | 
					                    <SpaceItem>
 | 
				
			||||||
 | 
					                        <Tooltip Placement="@Placement.Bottom"
 | 
				
			||||||
 | 
					                                 Title="清除浏览器缓存中的文法">
 | 
				
			||||||
 | 
					                            <Button Type="@ButtonType.Text" @onclick="@ClearGrammarClicked">
 | 
				
			||||||
 | 
					                                清除
 | 
				
			||||||
 | 
					                            </Button>
 | 
				
			||||||
 | 
					                        </Tooltip>
 | 
				
			||||||
                    </SpaceItem>
 | 
					                    </SpaceItem>
 | 
				
			||||||
                </Space>
 | 
					                </Space>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
@@ -46,27 +63,23 @@
 | 
				
			|||||||
    protected override async Task OnInitializedAsync()
 | 
					    protected override async Task OnInitializedAsync()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        Log("编辑器加载完成");
 | 
					        Log("编辑器加载完成");
 | 
				
			||||||
 | 
					        if (await GrammarStorage.RestoreGrammar())
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            Log("从浏览器中恢复成功");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            Log("尚未设置语法");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        await base.OnInitializedAsync();
 | 
					        await base.OnInitializedAsync();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private StandaloneEditorConstructionOptions GetEditorConstructionOptions(StandaloneCodeEditor editor)
 | 
					    private StandaloneEditorConstructionOptions GetEditorConstructionOptions(StandaloneCodeEditor editor)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        string grammarText;
 | 
					 | 
				
			||||||
        if (!string.IsNullOrEmpty(RobotFactory.GrammarText))
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            Log("加载文法...");
 | 
					 | 
				
			||||||
            grammarText = RobotFactory.GrammarText;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        else
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            Log("未设置文法");
 | 
					 | 
				
			||||||
            grammarText = string.Empty;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return new StandaloneEditorConstructionOptions
 | 
					        return new StandaloneEditorConstructionOptions
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            Language = "yaml",
 | 
					            Language = "yaml",
 | 
				
			||||||
            Value = grammarText
 | 
					            Value = !string.IsNullOrEmpty(RobotFactory.GrammarText) ? RobotFactory.GrammarText : string.Empty
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -79,6 +92,7 @@
 | 
				
			|||||||
            Log("编译文法...");
 | 
					            Log("编译文法...");
 | 
				
			||||||
            RobotFactory.SetGrammar(grammarText);
 | 
					            RobotFactory.SetGrammar(grammarText);
 | 
				
			||||||
            Log("编译成功!");
 | 
					            Log("编译成功!");
 | 
				
			||||||
 | 
					            await GrammarStorage.StoreGrammar();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        catch (GrammarException e)
 | 
					        catch (GrammarException e)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -86,11 +100,17 @@
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void QuitButtontClicked()
 | 
					    private void QuitButtonClicked()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        Navigation.NavigateTo("/", replace: true);
 | 
					        Navigation.NavigateTo("/", replace: true);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private async Task ClearGrammarClicked()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        await GrammarStorage.RemoveGrammar();
 | 
				
			||||||
 | 
					        Log("清除浏览器中的语法成功");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void Log(string message)
 | 
					    private void Log(string message)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        _logs.Add($"{DateTime.Now:HH:mm:ss} {message}");
 | 
					        _logs.Add($"{DateTime.Now:HH:mm:ss} {message}");
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,11 @@
 | 
				
			|||||||
@page "/"
 | 
					@page "/"
 | 
				
			||||||
@using Frontend.Models
 | 
					@using Frontend.Models
 | 
				
			||||||
 | 
					@using Frontend.Services
 | 
				
			||||||
 | 
					@using Katheryne
 | 
				
			||||||
@using Katheryne.Abstractions
 | 
					@using Katheryne.Abstractions
 | 
				
			||||||
@inject IChatRobotFactory ChatRobotFactory
 | 
					@inject IChatRobotFactory ChatRobotFactory
 | 
				
			||||||
 | 
					@inject GrammarStorageService GrammarStorage
 | 
				
			||||||
 | 
					@inject DefaultChatRobot DefaultRobot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<Layout>
 | 
					<Layout>
 | 
				
			||||||
@@ -40,7 +44,7 @@
 | 
				
			|||||||
    </Sider>
 | 
					    </Sider>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <Content>
 | 
					    <Content>
 | 
				
			||||||
        <ChatZone Messages="@_chatDictionary[_currentGuid].Messages" Robot="@_chatDictionary[_currentGuid].Robot"/>
 | 
					        <ChatZone Messages="@GetChatMessages()" Robot="@GetChatRobot()"/>
 | 
				
			||||||
    </Content>
 | 
					    </Content>
 | 
				
			||||||
</Layout>
 | 
					</Layout>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -49,11 +53,15 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private Guid _currentGuid;
 | 
					    private Guid _currentGuid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected override void OnInitialized()
 | 
					    protected override async Task OnInitializedAsync()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        await GrammarStorage.RestoreGrammar();
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        Chat chat = GetInitChat();
 | 
					        Chat chat = GetInitChat();
 | 
				
			||||||
        _chatDictionary.Add(chat.Guid, chat);
 | 
					        _chatDictionary.Add(chat.Guid, chat);
 | 
				
			||||||
        _currentGuid = chat.Guid;
 | 
					        _currentGuid = chat.Guid;
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        await base.OnInitializedAsync();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void CreateChatClicked()
 | 
					    private void CreateChatClicked()
 | 
				
			||||||
@@ -94,4 +102,14 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return chat;
 | 
					        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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,14 +1,20 @@
 | 
				
			|||||||
 | 
					using Blazored.LocalStorage;
 | 
				
			||||||
using Microsoft.AspNetCore.Components.Web;
 | 
					using Microsoft.AspNetCore.Components.Web;
 | 
				
			||||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
 | 
					using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
 | 
				
			||||||
using Katheryne;
 | 
					using Katheryne;
 | 
				
			||||||
using Frontend;
 | 
					using Frontend;
 | 
				
			||||||
 | 
					using Frontend.Services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
 | 
					var builder = WebAssemblyHostBuilder.CreateDefault(args);
 | 
				
			||||||
builder.RootComponents.Add<App>("#app");
 | 
					builder.RootComponents.Add<App>("#app");
 | 
				
			||||||
builder.RootComponents.Add<HeadOutlet>("head::after");
 | 
					builder.RootComponents.Add<HeadOutlet>("head::after");
 | 
				
			||||||
builder.Services.AddAntDesign();
 | 
					builder.Services.AddAntDesign();
 | 
				
			||||||
 | 
					builder.Services.AddBlazoredLocalStorage();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					builder.Logging.SetMinimumLevel(LogLevel.Debug);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
builder.Services.AddKatheryne();
 | 
					builder.Services.AddKatheryne();
 | 
				
			||||||
 | 
					builder.Services.AddScoped<GrammarStorageService>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
WebAssemblyHost app = builder.Build();
 | 
					WebAssemblyHost app = builder.Build();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										62
									
								
								Frontend/Services/GrammarStorageService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								Frontend/Services/GrammarStorageService.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
				
			|||||||
 | 
					using Blazored.LocalStorage;
 | 
				
			||||||
 | 
					using Katheryne.Abstractions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Frontend.Services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class GrammarStorageService
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    private const string GrammarTextKey = "GrammarText";
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    private readonly ILocalStorageService _localStorage;
 | 
				
			||||||
 | 
					    private readonly ILogger<GrammarStorageService> _logger;
 | 
				
			||||||
 | 
					    private readonly IChatRobotFactory _robotFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public GrammarStorageService(ILocalStorageService localStorage,
 | 
				
			||||||
 | 
					        ILogger<GrammarStorageService> logger,
 | 
				
			||||||
 | 
					        IChatRobotFactory robotFactory)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        _localStorage = localStorage;
 | 
				
			||||||
 | 
					        _logger = logger;
 | 
				
			||||||
 | 
					        _robotFactory = robotFactory;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// 尝试从LocalStorage中恢复之前设置的文法
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    /// <returns>恢复文法是否成功</returns>
 | 
				
			||||||
 | 
					    public async Task<bool> RestoreGrammar()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        _logger.LogDebug("Try to restore grammar text.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        string result = await _localStorage.GetItemAsync<string>(GrammarTextKey);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (result == default)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        _logger.LogDebug("Restore grammar text successfully.");
 | 
				
			||||||
 | 
					        _robotFactory.SetGrammar(result);
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// 保存当前设置使用的文法
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    public async Task StoreGrammar()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if (!string.IsNullOrEmpty(_robotFactory.GrammarText))
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _logger.LogDebug("Store current grammar text.");
 | 
				
			||||||
 | 
					            await _localStorage.SetItemAsync(GrammarTextKey, _robotFactory.GrammarText);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// 清除当前设置的文法
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    public async Task RemoveGrammar()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        await _localStorage.RemoveItemAsync(GrammarTextKey);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user