@using LANCommander.Server.Extensions; @if (FileInfo != null) { @foreach (var part in FileInfo.FullName.Split(Path.DirectorySeparatorChar)) { @part } } @code { [Parameter] public string FilePath { get; set; } TabPane Pane; FileInfo FileInfo; StandaloneCodeEditor Editor; string Contents; bool HasUnsavedChanges = false; private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor) { return new StandaloneEditorConstructionOptions { AutomaticLayout = true, Theme = "vs-dark" }; } protected override async Task OnParametersSetAsync() { FileInfo = new FileInfo(FilePath); } protected override async Task OnAfterRenderAsync(bool firstRender) { if (Editor != null && firstRender) { if (File.Exists(FileInfo.FullName)) { if (!FileInfo.IsBinaryFile()) { Contents = await File.ReadAllTextAsync(FileInfo.FullName); await Editor.SetValue(Contents); } else { await Editor.SetValue("This file cannot be edited because it is a binary file."); } } await Editor.AddCommand((int)BlazorMonaco.KeyMod.CtrlCmd | (int)BlazorMonaco.KeyCode.KeyS, async (editor) => { await Save(); }, null); } } private void OnChange(ModelContentChangedEvent eventArgs) { if (!HasUnsavedChanges) { HasUnsavedChanges = true; StateHasChanged(); } } private async Task Save() { await File.WriteAllTextAsync(FileInfo.FullName, await Editor.GetValue()); HasUnsavedChanges = false; StateHasChanged(); } }