Skip to content

Commit

Permalink
Make default notebook get stored like other notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Oct 29, 2020
1 parent fd15831 commit 9802fed
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 79 deletions.
19 changes: 5 additions & 14 deletions Client/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

<Router AppAssembly="@typeof(Program).Assembly" OnNavigateAsync="@OnNavigateAsync">
<Found Context="routeData">
<CascadingValue Value="@routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</CascadingValue>
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
Expand All @@ -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<string>() { "BlazorTable.dll" }
);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
await assemblyLoader.LoadAssembliesAsync(
new List<string>() { "BlazorTable.dll" }
);
}
}
}
46 changes: 27 additions & 19 deletions Client/Data/NotebookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace blazoract.Client.Data
{
public class NotebookService
{
public const string DefaultNotebookId = "_default_notebook";

public event Action OnChange;

private ILocalStorageService _storage;
Expand All @@ -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<Cell>();
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<Notebook> GetInitialContent()
Expand All @@ -46,26 +37,43 @@ public async Task<Notebook> GetById(string id)
if (!_inMemoryNotebooks.TryGetValue(id, out var result))
{
result = await _storage.GetItemAsync<Notebook>(id);

if (result == null && id == DefaultNotebookId)
{
result = await CreateNewNotebook(defaultNotebook: true);
}

_inMemoryNotebooks[id] = result;
}

return result;
}

public async Task<string> CreateNewNotebook()
public async Task<Notebook> CreateNewNotebook(bool defaultNotebook = false)
{
var id = Guid.NewGuid().ToString("N");
var title = "New notebook";
var notebook = new Notebook(title, id);
notebook.Cells = new List<Cell>() { 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<Cell>() { new Cell(id, "// Type your code here", 0) };
}
else
{
notebook.Cells = new List<Cell>();
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<List<string>>("blazoract-notebooks") ?? new List<string>();
notebooks.Add(id);
await _storage.SetItemAsync("blazoract-notebooks", notebooks);

_inMemoryNotebooks[id] = notebook;
return id;
return notebook;
}

public async Task<Notebook> AddCell(string id, string content, CellType type, int position)
Expand Down Expand Up @@ -93,4 +101,4 @@ public async Task<IEnumerable<Notebook>> GetNotebooks()
return new List<Notebook>();
}
}
}
}
2 changes: 1 addition & 1 deletion Client/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@page "/notebook/{Id}"

<div class="page">
<MainToolbar />
<MainToolbar NotebookId="@NotebookId" />
<Cells @key="@NotebookId" NotebookId="@NotebookId" />
</div>

Expand Down
25 changes: 7 additions & 18 deletions Client/Shared/MainToolbar.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

<div class="main-toolbar">
<h2>blazoract</h2>
@if (!string.IsNullOrEmpty(notebookId))
@if (!string.IsNullOrEmpty(NotebookId))
{
<NotebookName NotebookId="@notebookId" />
<NotebookName NotebookId="@NotebookId" />
}
<div class="right">
<button @onclick="NewNotebook">New</button>
Expand All @@ -15,30 +15,19 @@
</div>

@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);
}
}
Expand Down
50 changes: 24 additions & 26 deletions Client/Shared/NotebookName.razor
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
@inject IJSRuntime JSRuntime
@inject NotebookService content
@implements IAsyncDisposable

<div class="notebook-name" contenteditable="true" @ref=nameDiv>
@if (notebook != null)
{
@notebook.Title
} else {
@name
}
<div class="notebook-name" contenteditable="true" @ref="nameDiv">
@notebook?.Title
</div>

@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<NotebookName> thisReference;

IJSObjectReference module;
protected override async Task OnInitializedAsync()
protected override async Task OnParametersSetAsync()
{
module = await JSRuntime.InvokeAsync<IJSObjectReference>("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<string>("registerListener", nameDiv, component);
module = await JSRuntime.InvokeAsync<IJSObjectReference>(
"import", "./js/notebook-name.js");

thisReference = DotNetObjectReference.Create(this);
await module.InvokeAsync<string>("registerListener", nameDiv, thisReference);
}
}

[JSInvokable("RenameNotebook")]
public async Task<bool> 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);
}

}
async ValueTask IAsyncDisposable.DisposeAsync()
{
thisReference.Dispose();
await module.DisposeAsync();
}
}
1 change: 0 additions & 1 deletion Shared/Notebook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ public Notebook(string title, string notebookId)
public DateTime Created { get; set; }

public DateTime Updated { get; set; }

}
}

0 comments on commit 9802fed

Please sign in to comment.