From 04bf48feb61494aeacef4e968054e08831f00502 Mon Sep 17 00:00:00 2001 From: Claudio Schaad Date: Fri, 30 Oct 2026 22:09:23 +0100 Subject: [PATCH] JSON editor: fix switching files and returning to the page Pages are interactive one by one, so NavigateTo and menu links run an enhanced navigation that re-renders the page on the server and syncs . That broke the editor in three ways: - Switching files navigated to ?file=...: the editor kept the old file, and with the large Transactions.json its DOM got mangled. The file is now loaded in place and only the URL is replaced (js/url.js). - Leaving the page re-ran OnParametersSetAsync on the disposing editor, which threw and terminated the circuit. The file parameter is now only read in OnInitializedAsync. - Coming back without a full reload showed Monaco unstyled, because its stylesheets had been removed from . monaco.js now remembers them and puts them back. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 2 +- .../Components/Pages/JsonEditor.razor.cs | 51 +++++++++++-------- Schaad.Accounting.UI/wwwroot/js/monaco.js | 31 ++++++++++- Schaad.Accounting.UI/wwwroot/js/url.js | 8 +++ 4 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 Schaad.Accounting.UI/wwwroot/js/url.js diff --git a/CLAUDE.md b/CLAUDE.md index 2057124..19e00f5 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -76,7 +76,7 @@ Pages live in `Schaad.Accounting.UI/Components/Pages/`. Each page typically has - `Microsoft.FluentUI.AspNetCore.Components` — UI components (FluentDataGrid, FluentDialog, etc.) - `Plotly.Blazor` — charts on the older chart pages -- `BlazorMonaco` — Monaco editor for the JSON editor page; loaded on demand by `wwwroot/js/monaco.js` instead of script tags in `App.razor` +- `BlazorMonaco` — Monaco editor for the JSON editor page; loaded on demand by `wwwroot/js/monaco.js` instead of script tags in `App.razor`. Pages are interactive one by one, so `NavigationManager.NavigateTo` and menu links run enhanced navigation, which syncs `` (removing Monaco's styles; `monaco.js` puts them back) and re-renders the page on the server. The editor therefore switches files in place and only rewrites the URL (`wwwroot/js/url.js`) - Chart.js 4 + `chartjs-chart-treemap` — charts on the Analyse page; vendored in `wwwroot/lib/` (no CDN), wrapped by `wwwroot/js/charts.js` and `ChartJsChart.razor` - `FreeSpire.PDF` — PDF parsing for bank statement imports - `Schaad.Finance.dll` / `Schaad.Finance.Api.dll` — proprietary DLLs for MT940/CAMT053 parsing and FX rate lookups (FixerIo API key in `appsettings.Development.json`) diff --git a/Schaad.Accounting.UI/Components/Pages/JsonEditor.razor.cs b/Schaad.Accounting.UI/Components/Pages/JsonEditor.razor.cs index 4831b8b..babb2cc 100644 --- a/Schaad.Accounting.UI/Components/Pages/JsonEditor.razor.cs +++ b/Schaad.Accounting.UI/Components/Pages/JsonEditor.razor.cs @@ -39,6 +39,7 @@ public partial class JsonEditor : ComponentBase, IAsyncDisposable private StandaloneCodeEditor? editor; private IJSObjectReference? unsavedGuard; + private IJSObjectReference? url; private bool monacoLoaded; private string? monacoError; private RawDataFile? file; @@ -48,20 +49,12 @@ public partial class JsonEditor : ComponentBase, IAsyncDisposable private bool saving; private IReadOnlyList errors = []; - protected override async Task OnParametersSetAsync() + // The file parameter is only read here: switching files doesn't navigate (see OnFileChanged), + // and while leaving the page the query changes again for a component that is about to go. + protected override async Task OnInitializedAsync() { - var requested = repository.FileNames.Contains(FileParameter ?? "") ? FileParameter! : repository.FileNames[0]; - if (requested == fileName) - { - return; - } - fileName = requested; + fileName = repository.FileNames.Contains(FileParameter ?? "") ? FileParameter! : repository.FileNames[0]; file = await repository.LoadAsync(fileName); - errors = []; - if (editor is not null) - { - await ShowFileAsync(); - } } protected override async Task OnAfterRenderAsync(bool firstRender) @@ -71,6 +64,7 @@ public partial class JsonEditor : ComponentBase, IAsyncDisposable return; } unsavedGuard = await js.InvokeAsync("import", "./js/unsaved-guard.js"); + url = await js.InvokeAsync("import", "./js/url.js"); try { await using var module = await js.InvokeAsync("import", "./js/monaco.js"); @@ -166,24 +160,33 @@ public partial class JsonEditor : ComponentBase, IAsyncDisposable await ShowFileAsync(); } - // Switching goes through the URL, so the file is part of bookmarks and back/forward. + // Loads the file in place and only puts it into the address bar (for bookmarks and reloads). + // Navigating instead would run an enhanced navigation, which re-renders the page on the server + // and merges it into the DOM; the editor doesn't survive that. private async Task OnFileChanged(string name) { if (name == fileName) { return; } - if (dirty) + if (dirty && await ConfirmDiscardAsync() == false) { - if (await ConfirmDiscardAsync() == false) - { - // Re-render puts the select back on the current file. - StateHasChanged(); - return; - } - await SetDirtyAsync(false); + // Re-render puts the select back on the current file. + StateHasChanged(); + return; + } + fileName = name; + file = await repository.LoadAsync(fileName); + errors = []; + // Before Monaco is up, the editor picks up the file when it is created (EditorOptions). + if (editor is not null) + { + await ShowFileAsync(); + } + if (url is not null) + { + await url.InvokeVoidAsync("replace", navigation.GetUriWithQueryParameter("file", name)); } - navigation.NavigateTo(navigation.GetUriWithQueryParameter("file", name)); } private async Task ConfirmDiscardAsync() @@ -226,6 +229,10 @@ public partial class JsonEditor : ComponentBase, IAsyncDisposable await unsavedGuard.InvokeVoidAsync("set", (object?)null); await unsavedGuard.DisposeAsync(); } + if (url is not null) + { + await url.DisposeAsync(); + } } catch (JSDisconnectedException) { diff --git a/Schaad.Accounting.UI/wwwroot/js/monaco.js b/Schaad.Accounting.UI/wwwroot/js/monaco.js index cc18080..d612d0f 100644 --- a/Schaad.Accounting.UI/wwwroot/js/monaco.js +++ b/Schaad.Accounting.UI/wwwroot/js/monaco.js @@ -4,6 +4,25 @@ let loading; +// Enhanced navigation (moving between pages without a full reload) syncs with the +// server-rendered page and so removes the stylesheets Monaco added at runtime. Monaco itself stays +// loaded in the window, so remember those elements and put them back before the next editor is +// created. Monaco keeps references to its