|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +namespace BootstrapBlazor.Server.Components.Samples.Tutorials; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Online sheet sample code |
| 10 | +/// </summary> |
| 11 | +public partial class OnlineSheet : ComponentBase, IDisposable |
| 12 | +{ |
| 13 | + [Inject, NotNull] |
| 14 | + private IWebHostEnvironment? WebHost { get; set; } |
| 15 | + |
| 16 | + [Inject, NotNull] |
| 17 | + private ToastService? ToastService { get; set; } |
| 18 | + |
| 19 | + [Inject, NotNull] |
| 20 | + private IStringLocalizer<OnlineSheet>? Localizer { get; set; } |
| 21 | + |
| 22 | + [Inject] |
| 23 | + [NotNull] |
| 24 | + private IDispatchService<Contributor>? DispatchService { get; set; } |
| 25 | + |
| 26 | + [NotNull] |
| 27 | + private UniverSheet? _sheetExcel = null; |
| 28 | + |
| 29 | + private UniverSheetData? _data = null; |
| 30 | + |
| 31 | + private bool _inited = false; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// <inheritdoc/> |
| 35 | + /// </summary> |
| 36 | + protected override void OnInitialized() |
| 37 | + { |
| 38 | + base.OnInitialized(); |
| 39 | + |
| 40 | + var reportFile = Path.Combine(WebHost.WebRootPath, "univer-sheet", "report.json"); |
| 41 | + if (File.Exists(reportFile)) |
| 42 | + { |
| 43 | + var sheetData = File.ReadAllText(reportFile); |
| 44 | + |
| 45 | + _data = new UniverSheetData() |
| 46 | + { |
| 47 | + Data = sheetData |
| 48 | + }; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private async Task OnReadyAsync() |
| 53 | + { |
| 54 | + _inited = true; |
| 55 | + await ToastService.Information(Localizer["ToastOnReadyTitle"], Localizer["ToastOnReadyContent"]); |
| 56 | + DispatchService.Subscribe(Dispatch); |
| 57 | + } |
| 58 | + |
| 59 | + private async Task Dispatch(DispatchEntry<Contributor> entry) |
| 60 | + { |
| 61 | + if (!_inited) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (entry.Entry != null) |
| 67 | + { |
| 68 | + await ToastService.Show(new ToastOption() |
| 69 | + { |
| 70 | + Title = "Dispatch 服务测试", |
| 71 | + ChildContent = BootstrapDynamicComponent.CreateComponent<OnlineContributor>(new Dictionary<string, object?>() |
| 72 | + { |
| 73 | + { "Contributor", entry.Entry } |
| 74 | + }).Render(), |
| 75 | + Category = ToastCategory.Information, |
| 76 | + Delay = 3000, |
| 77 | + ForceDelay = true |
| 78 | + }); |
| 79 | + |
| 80 | + DispatchService.UnSubscribe(Dispatch); |
| 81 | + |
| 82 | + await _sheetExcel.PushDataAsync(entry.Entry.Data); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void Dispose(bool disposing) |
| 87 | + { |
| 88 | + if (disposing) |
| 89 | + { |
| 90 | + DispatchService.UnSubscribe(Dispatch); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// <inheritdoc/> |
| 96 | + /// </summary> |
| 97 | + public void Dispose() |
| 98 | + { |
| 99 | + Dispose(true); |
| 100 | + GC.SuppressFinalize(this); |
| 101 | + } |
| 102 | +} |
| 103 | + |
0 commit comments