Fix menu link click error and charts failing after the JSON editor #14

Merged
clayschaad merged 1 commit from fix-navlink-and-chart-loading into main 2026-10-30 21:21:11 +00:00
3 changed files with 34 additions and 19 deletions
Showing only changes of commit c07fd2893e - Show all commits

View file

@ -77,7 +77,7 @@ Pages live in `Schaad.Accounting.UI/Components/Pages/`. Each page typically has
- `Microsoft.FluentUI.AspNetCore.Components` — UI components (FluentDataGrid, FluentDialog, etc.) - `Microsoft.FluentUI.AspNetCore.Components` — UI components (FluentDataGrid, FluentDialog, etc.)
- `Plotly.Blazor` — charts on the older chart pages - `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`. Pages are interactive one by one, so `NavigationManager.NavigateTo` and menu links run enhanced navigation, which syncs `<head>` (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`) - `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 `<head>` (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` - 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`; `charts.js` hides Monaco's global AMD `define` while loading them, otherwise the UMD bundles register as AMD modules and `window.Chart` stays undefined after visiting the JSON editor
- `FreeSpire.PDF` — PDF parsing for bank statement imports - `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`) - `Schaad.Finance.dll` / `Schaad.Finance.Api.dll` — proprietary DLLs for MT940/CAMT053 parsing and FX rate lookups (FixerIo API key in `appsettings.Development.json`)

View file

@ -5,7 +5,7 @@
<div class="navmenu hidePrint"> <div class="navmenu hidePrint">
<nav class="sitenav" aria-labelledby="main-menu"> <nav class="sitenav" aria-labelledby="main-menu">
<FluentNavMenu Id="main-menu" Collapsible="true" Width="250" Title="Navigation menu" @bind-Expanded="expanded" CustomToggle="true"> <FluentNavMenu Id="main-menu" Collapsible="true" Width="250" Title="Navigation menu" @bind-Expanded="expanded">
<FluentNavLink Href="/" Match="NavLinkMatch.All" Icon="@(new Size20.Home())" IconColor="Color.Accent">Home</FluentNavLink> <FluentNavLink Href="/" Match="NavLinkMatch.All" Icon="@(new Size20.Home())" IconColor="Color.Accent">Home</FluentNavLink>
<FluentNavLink Href="transactions" Icon="@(new Size20.NumberSymbolSquare())" IconColor="Color.Accent">Transaktionen</FluentNavLink> <FluentNavLink Href="transactions" Icon="@(new Size20.NumberSymbolSquare())" IconColor="Color.Accent">Transaktionen</FluentNavLink>
<FluentNavLink Href="banktransactions" Icon="@(new Size20.BuildingBank())" IconColor="Color.Accent">Bank Transaktionen</FluentNavLink> <FluentNavLink Href="banktransactions" Icon="@(new Size20.BuildingBank())" IconColor="Color.Accent">Bank Transaktionen</FluentNavLink>

View file

@ -1,28 +1,43 @@
// Thin Chart.js wrapper for Blazor (see ChartJsChart.razor). Chart.js and the treemap plugin are // Thin Chart.js wrapper for Blazor (see ChartJsChart.razor). Chart.js and the treemap plugin are
// loaded on first use from wwwroot/lib, so pages without charts don't pay for them. // loaded on first use from wwwroot/lib, so pages without charts don't pay for them.
const scripts = new Map(); let chartJsLoading;
let treemapLoading;
function loadScript(src) { function loadScript(src) {
if (!scripts.has(src)) { return new Promise((resolve, reject) => {
scripts.set(src, new Promise((resolve, reject) => {
const script = document.createElement('script'); const script = document.createElement('script');
script.src = src; script.src = src;
script.onload = resolve; script.onload = resolve;
script.onerror = () => reject(new Error(`${src} could not be loaded`)); script.onerror = () => reject(new Error(`${src} could not be loaded`));
document.head.appendChild(script); document.head.appendChild(script);
})); });
}
return scripts.get(src);
} }
async function loadChartJs(kind) { // The JSON editor's Monaco loader (js/monaco.js) leaves a global AMD define() in the window, and
await loadScript('lib/chartjs/chart.umd.min.js'); // pages switch without a full reload. Chart.js and the treemap plugin are UMD bundles that prefer
if (kind === 'treemap') { // define() when it exists, registering as AMD modules instead of setting window.Chart, so hide it
// Registers itself with the global Chart. // while they load. Each script loads only once (see loadChartJs), so this never overlaps with
await loadScript('lib/chartjs-chart-treemap/chartjs-chart-treemap.min.js'); // itself and always restores Monaco's define.
async function loadUmdScript(src) {
const define = window.define;
window.define = undefined;
try {
await loadScript(src);
} finally {
window.define = define;
} }
window.Chart.defaults.font.family = theme.font; }
function loadChartJs(kind) {
chartJsLoading ??= loadUmdScript('lib/chartjs/chart.umd.min.js')
.then(() => { window.Chart.defaults.font.family = theme.font; });
if (kind !== 'treemap') {
return chartJsLoading;
}
// Registers itself with the global Chart.
treemapLoading ??= chartJsLoading.then(() => loadUmdScript('lib/chartjs-chart-treemap/chartjs-chart-treemap.min.js'));
return treemapLoading;
} }
// Same look as ChartTheme.cs. // Same look as ChartTheme.cs.