From 9802fedcc37f966cc5306d3881474d8a31a2c09d Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Thu, 29 Oct 2020 12:33:51 +0000 Subject: [PATCH] Make default notebook get stored like other notebooks --- Client/App.razor | 19 ++++-------- Client/Data/NotebookService.cs | 46 +++++++++++++++++------------ Client/Pages/Index.razor | 2 +- Client/Shared/MainToolbar.razor | 25 +++++----------- Client/Shared/NotebookName.razor | 50 +++++++++++++++----------------- Shared/Notebook.cs | 1 - 6 files changed, 64 insertions(+), 79 deletions(-) diff --git a/Client/App.razor b/Client/App.razor index 5440bcf..33f7b7b 100644 --- a/Client/App.razor +++ b/Client/App.razor @@ -3,9 +3,7 @@ - - - + @@ -17,18 +15,11 @@ @code { private async Task OnNavigateAsync(NavigationContext args) { - try + if (args.Path.EndsWith("notebooks")) { - if (args.Path.EndsWith("notebooks")) - { - await assemblyLoader.LoadAssembliesAsync( - new List() { "BlazorTable.dll" } - ); - } - } - catch (Exception ex) - { - Console.WriteLine(ex); + await assemblyLoader.LoadAssembliesAsync( + new List() { "BlazorTable.dll" } + ); } } } diff --git a/Client/Data/NotebookService.cs b/Client/Data/NotebookService.cs index 382c3db..e4770d4 100644 --- a/Client/Data/NotebookService.cs +++ b/Client/Data/NotebookService.cs @@ -12,6 +12,8 @@ namespace blazoract.Client.Data { public class NotebookService { + public const string DefaultNotebookId = "_default_notebook"; + public event Action OnChange; private ILocalStorageService _storage; @@ -23,17 +25,6 @@ public NotebookService(ILocalStorageService storage, HttpClient http) { _storage = storage; _http = http; - - var id = Guid.NewGuid().ToString("N"); - var title = "Default Notebook"; - var notebook = new Notebook(id, title); - var initialContent = new List(); - for (var i = 0; i < 100; i++) - { - initialContent.Add(new Cell(id, $"{i} * {i}")); - } - notebook.Cells = initialContent; - _storage.SetItemAsync("_default_notebook", notebook); } public async Task GetInitialContent() @@ -46,26 +37,43 @@ public async Task GetById(string id) if (!_inMemoryNotebooks.TryGetValue(id, out var result)) { result = await _storage.GetItemAsync(id); + + if (result == null && id == DefaultNotebookId) + { + result = await CreateNewNotebook(defaultNotebook: true); + } + _inMemoryNotebooks[id] = result; } return result; } - public async Task CreateNewNotebook() + public async Task CreateNewNotebook(bool defaultNotebook = false) { - var id = Guid.NewGuid().ToString("N"); - var title = "New notebook"; - var notebook = new Notebook(title, id); - notebook.Cells = new List() { new Cell(id, "// Type your code here", 0) }; + var id = defaultNotebook ? DefaultNotebookId : Guid.NewGuid().ToString("N"); + var notebook = new Notebook("New notebook", id); + + if (!defaultNotebook) + { + notebook.Cells = new List() { new Cell(id, "// Type your code here", 0) }; + } + else + { + notebook.Cells = new List(); + for (var i = 0; i < 100; i++) + { + notebook.Cells.Add(new Cell(id, $"{i} * {i}")); + } + } + await _storage.SetItemAsync(id, notebook); var notebooks = await _storage.GetItemAsync>("blazoract-notebooks") ?? new List(); notebooks.Add(id); await _storage.SetItemAsync("blazoract-notebooks", notebooks); - _inMemoryNotebooks[id] = notebook; - return id; + return notebook; } public async Task AddCell(string id, string content, CellType type, int position) @@ -93,4 +101,4 @@ public async Task> GetNotebooks() return new List(); } } -} \ No newline at end of file +} diff --git a/Client/Pages/Index.razor b/Client/Pages/Index.razor index b9527c7..c157b2d 100644 --- a/Client/Pages/Index.razor +++ b/Client/Pages/Index.razor @@ -2,7 +2,7 @@ @page "/notebook/{Id}"
- +
diff --git a/Client/Shared/MainToolbar.razor b/Client/Shared/MainToolbar.razor index 54e665f..2b921cf 100644 --- a/Client/Shared/MainToolbar.razor +++ b/Client/Shared/MainToolbar.razor @@ -3,9 +3,9 @@

blazoract

- @if (!string.IsNullOrEmpty(notebookId)) + @if (!string.IsNullOrEmpty(NotebookId)) { - + }
@@ -15,30 +15,19 @@
@code { - [CascadingParameter] - RouteData RouteData { get; set; } - - private string notebookId; - - protected override void OnParametersSet() - { - if (RouteData.RouteValues.TryGetValue("NotebookId", out var _notebookId)) - { - notebookId = _notebookId.ToString(); - } - } + [Parameter] public string NotebookId { get; set; } private async Task NewNotebook() { - var id = await content.CreateNewNotebook(); - navigation.NavigateTo($"/notebook/{id}"); + var newNotebook = await content.CreateNewNotebook(); + navigation.NavigateTo($"/notebook/{newNotebook.NotebookId}"); } private async Task SaveNotebook() { - if (notebookId != null) + if (NotebookId != null) { - var notebook = await content.GetById(notebookId); + var notebook = await content.GetById(NotebookId); await content.Save(notebook); } } diff --git a/Client/Shared/NotebookName.razor b/Client/Shared/NotebookName.razor index 5775bcf..1f2fc0b 100644 --- a/Client/Shared/NotebookName.razor +++ b/Client/Shared/NotebookName.razor @@ -1,50 +1,48 @@ @inject IJSRuntime JSRuntime @inject NotebookService content +@implements IAsyncDisposable -
- @if (notebook != null) - { - @notebook.Title - } else { - @name - } +
+ @notebook?.Title
@code { [Parameter] public string NotebookId { get; set; } - private string name = "New notebook"; private Notebook notebook; - ElementReference nameDiv; + private ElementReference nameDiv; + private IJSObjectReference module; + private DotNetObjectReference thisReference; - IJSObjectReference module; - protected override async Task OnInitializedAsync() + protected override async Task OnParametersSetAsync() { - module = await JSRuntime.InvokeAsync("import", - "./js/notebook-name.js"); + notebook = null; notebook = await content.GetById(NotebookId); } protected override async Task OnAfterRenderAsync(bool firstRender) { - if (module != null) + if (firstRender) { - var component = DotNetObjectReference.Create(this); - await module.InvokeAsync("registerListener", nameDiv, component); + module = await JSRuntime.InvokeAsync( + "import", "./js/notebook-name.js"); + + thisReference = DotNetObjectReference.Create(this); + await module.InvokeAsync("registerListener", nameDiv, thisReference); } } - [JSInvokable("RenameNotebook")] - public async Task RenameNotebook(string newName) + [JSInvokable] + public async Task RenameNotebook(string newName) { - if (notebook != null) - { - notebook.Title = newName; - await content.Save(notebook); - return true; - } - return false; + notebook.Title = newName; + await content.Save(notebook); } -} \ No newline at end of file + async ValueTask IAsyncDisposable.DisposeAsync() + { + thisReference.Dispose(); + await module.DisposeAsync(); + } +} diff --git a/Shared/Notebook.cs b/Shared/Notebook.cs index c55a688..4b4e99d 100644 --- a/Shared/Notebook.cs +++ b/Shared/Notebook.cs @@ -21,6 +21,5 @@ public Notebook(string title, string notebookId) public DateTime Created { get; set; } public DateTime Updated { get; set; } - } }