using System; using System.Runtime.CompilerServices; using Microsoft.AspNetCore.Components.Rendering; using Microsoft.AspNetCore.Components; using static FastReport.Web.Blazor.Export.BlazorConstants; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace FastReport.Web.Blazor.Export { interface IRenderContext : IDisposable { public void OpenElement(string elementName); public void CloseElement(); public void AddMarkupContent(string? markupContent); public void AddContent(string? textContent); public void AddContent(RenderFragment? fragment); public void AddContent(RenderFragment? fragment, TValue value); public void AddContent(MarkupString markupContent); //public void AddContent(object? textContent); //public void AddAttribute(string name); public void AddAttribute(string name, bool value); public void AddAttribute(string name, string? value); public void AddAttribute(string name, MulticastDelegate? value); //public void AddAttribute(string name, EventCallback value); //public void AddAttribute(string name, EventCallback value); public void AddAttribute(string name, object? value); //public void AddMultipleAttributes(IEnumerable>? attributes); //public void SetUpdatesAttributeName(string updatesAttributeName); public void OpenComponent<[DynamicallyAccessedMembers(LinkerFlags.All)] TComponent>() where TComponent : notnull, IComponent; //public void OpenComponent(Type componentType); public void CloseComponent(); public void AddOnClick(OnClickEventArgs click); } internal sealed class RenderContext : IRenderContext { private int _seq; private readonly RenderTreeBuilder _builder; private readonly EventHandler _onClick; public RenderContext(int seq, RenderTreeBuilder builder) : this(builder) { _seq = seq; } public RenderContext(RenderTreeBuilder builder, EventHandler eventHandler) : this(builder) { _onClick = eventHandler; } public RenderContext(RenderTreeBuilder builder) { _builder = builder; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private int GetSequence() { return _seq++; } public void AddMarkupContent(string? markupContent) { _builder.AddMarkupContent(GetSequence(), markupContent); } public void AddContent(RenderFragment? fragment) { _builder.AddContent(GetSequence(), fragment); } public void AddContent(MarkupString markupContent) { _builder.AddContent(GetSequence(), markupContent); } public void AddContent(string? textContent) { _builder.AddContent(GetSequence(), textContent); } public void AddContent(RenderFragment? fragment, TValue value) { _builder.AddContent(GetSequence(), fragment, value); } public void AddAttribute(string name, bool value) { _builder.AddAttribute(GetSequence(), name, value); } public void AddAttribute(string name, string? value) { _builder.AddAttribute(GetSequence(), name, value); } public void AddAttribute(string name, object? value) { _builder.AddAttribute(GetSequence(), name, value); } public void AddAttribute(string name, MulticastDelegate? value) { _builder.AddAttribute(GetSequence(), name, value); } public void AddOnClick(OnClickEventArgs click) { _builder.AddAttribute(GetSequence(), ONCLICK, (Action)(() => { _onClick?.Invoke(null, click); })); } public void AddAction(string actionName, Action action) { _builder.AddAttribute(GetSequence(), actionName, action); } public void AddOnChange(OnClickEventArgs click) { _builder.AddAttribute(GetSequence(), ONCHANGE, (Action)(() => { _onClick?.Invoke(null, click); })); } public void AddAttribute(string name, EventCallback value) { _builder.AddAttribute(GetSequence(), name, value); } public void OpenElement(string elementName) { _builder.OpenElement(GetSequence(), elementName); } public void OpenComponent<[DynamicallyAccessedMembers(LinkerFlags.All)] TComponent>() where TComponent : IComponent { _builder.OpenComponent(GetSequence()); } public void CloseElement() { _builder.CloseElement(); } public void CloseComponent() { _builder.CloseComponent(); } public void Dispose() { // Does it dispose the global context?? //throw new NotImplementedException(); //_builder.Dispose(); } } }