From f60fc4b7b58ee2eb83e725838b6abdaaae7c4ae1 Mon Sep 17 00:00:00 2001 From: Gary Chan Date: Wed, 29 May 2024 06:45:32 -0700 Subject: [PATCH] [MarkdownSection] Optimize to minimize number of calls to OnContentConverted (#2092) * reduce number of renders * optimize to a single call to OnContentConverted * clean up code for optimization * fixes page to show table after a single call to OnFreshTableOfContents * rerender contents when markdown source is changed * reduce number of renders * optimize to a single call to OnContentConverted * clean up code for optimization * fixes page to show table after a single call to OnFreshTableOfContents * rerender contents when markdown source is changed * resolve pr comments --------- Co-authored-by: Vincent Baaij Co-authored-by: Denis Voituron --- .../Components/MarkdownSection.razor.cs | 81 ++++++++++++------- .../Pages/Design/DesignTokensPage.razor | 4 +- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/examples/Demo/Shared/Components/MarkdownSection.razor.cs b/examples/Demo/Shared/Components/MarkdownSection.razor.cs index 35759335c0..fe8b9d64f1 100644 --- a/examples/Demo/Shared/Components/MarkdownSection.razor.cs +++ b/examples/Demo/Shared/Components/MarkdownSection.razor.cs @@ -7,9 +7,10 @@ namespace FluentUI.Demo.Shared.Components; public partial class MarkdownSection : FluentComponentBase { - private string? _content; - private bool _raiseContentConverted; private IJSObjectReference _jsModule = default!; + private bool _markdownChanged = false; + private string? _content; + private string? _fromAsset; [Inject] protected IJSRuntime JSRuntime { get; set; } = default!; @@ -21,34 +22,39 @@ public partial class MarkdownSection : FluentComponentBase /// Gets or sets the Markdown content /// [Parameter] - public string? Content { get; set; } + public string? Content + { + get => _content; + set + { + if (_content is not null && !_content.Equals(value)) + { + _markdownChanged = true; + } + _content = value; + } + } /// /// Gets or sets asset to read the Markdown from /// [Parameter] - public string? FromAsset { get; set; } - - [Parameter] - public EventCallback OnContentConverted { get; set; } - - public string? InternalContent + public string? FromAsset { - get => _content; + get => _fromAsset; set { - _content = value; - HtmlContent = ConvertToMarkupString(_content); - - if (OnContentConverted.HasDelegate) + if (_fromAsset is not null && !_fromAsset.Equals(value)) { - OnContentConverted.InvokeAsync(); + _markdownChanged = true; } - _raiseContentConverted = true; - StateHasChanged(); + _fromAsset = value; } } + [Parameter] + public EventCallback OnContentConverted { get; set; } + public MarkupString HtmlContent { get; private set; } protected override void OnInitialized() @@ -57,36 +63,53 @@ protected override void OnInitialized() { throw new ArgumentException("You need to provide either Content or FromAsset parameter"); } - - InternalContent = Content; } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { - if (!string.IsNullOrEmpty(FromAsset)) - { - InternalContent = await StaticAssetService.GetAsync(FromAsset); - } - // add highlight for any code blocks + // import code for highlighting code blocks _jsModule = await JSRuntime.InvokeAsync("import", "./_content/FluentUI.Demo.Shared/Components/MarkdownSection.razor.js"); - await _jsModule.InvokeVoidAsync("highlight"); - await _jsModule.InvokeVoidAsync("addCopyButton"); } - if (_raiseContentConverted) + if (firstRender || _markdownChanged) { - _raiseContentConverted = false; + _markdownChanged = false; + + // create markup from markdown source + HtmlContent = await MarkdownToMarkupStringAsync(); + StateHasChanged(); + + // notify that content converted from markdown if (OnContentConverted.HasDelegate) { await OnContentConverted.InvokeAsync(); } - + await _jsModule.InvokeVoidAsync("highlight"); + await _jsModule.InvokeVoidAsync("addCopyButton"); } } + /// + /// Converts markdown, provided in Content or from markdown file stored as a static asset, to MarkupString for rendering. + /// + /// MarkupString + private async Task MarkdownToMarkupStringAsync() + { + string? markdown; + if (string.IsNullOrEmpty(FromAsset)) + { + markdown = Content; + } + else + { + markdown = await StaticAssetService.GetAsync(FromAsset); + } + + return ConvertToMarkupString(markdown); + } private static MarkupString ConvertToMarkupString(string? value) { if (!string.IsNullOrWhiteSpace(value)) diff --git a/examples/Demo/Shared/Pages/Design/DesignTokensPage.razor b/examples/Demo/Shared/Pages/Design/DesignTokensPage.razor index 8297ae8151..fe17128f88 100644 --- a/examples/Demo/Shared/Pages/Design/DesignTokensPage.razor +++ b/examples/Demo/Shared/Pages/Design/DesignTokensPage.razor @@ -6,7 +6,7 @@ -@if (refreshCount > 3) +@if (refreshCount > 0) { } @@ -20,6 +20,6 @@ private async Task RefreshTableOfContents() { await OnRefreshTableOfContents.InvokeAsync(); - refreshCount++; ; + refreshCount++; } }