Skip to content

Commit

Permalink
Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Mar 23, 2024
1 parent f0c9528 commit 3e9614d
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 119 deletions.
2 changes: 1 addition & 1 deletion Libs/ScriptContainer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.8</Version>
<Version>1.3.0</Version>
<Authors>artemiusgreat</Authors>
<Copyright>indemos.com</Copyright>
<PackageProjectUrl>http://indemos.com</PackageProjectUrl>
Expand Down
28 changes: 21 additions & 7 deletions Libs/ScriptService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,16 @@ public async Task<ScriptService> CreateModule(IDictionary<string, dynamic> optio
{
try
{
await DisposeAsync();

_serviceInstance = DotNetObjectReference.Create(this);
_scriptModule = await _runtime.InvokeAsync<IJSObjectReference>("import", $"./_content/ScriptContainer/ScriptControl.razor.js?{Guid.NewGuid()}");
_scriptInstance = await _scriptModule.InvokeAsync<IJSObjectReference>("getScriptModule", _serviceInstance, options ?? new Dictionary<string, object>());
_scriptModule = await _runtime.InvokeAsync<IJSObjectReference>("import", "./_content/ScriptContainer/ScriptControl.razor.js");
_scriptInstance = await _scriptModule.InvokeAsync<IJSObjectReference>("getScriptModule", _serviceInstance, options ?? new Dictionary<string, dynamic>());
}
catch (Exception)
{
Console.WriteLine($"{e}");

Check failure on line 123 in Libs/ScriptService.cs

View workflow job for this annotation

GitHub Actions / build

The name 'e' does not exist in the current context

Check failure on line 123 in Libs/ScriptService.cs

View workflow job for this annotation

GitHub Actions / build

The name 'e' does not exist in the current context
}
catch (Exception) { }

return this;
}
Expand All @@ -140,17 +145,26 @@ public void OnChange(dynamic message, string actionName)
/// Dispose
/// </summary>
/// <returns></returns>
public virtual void Dispose()
public virtual void Dispose() => DisposeAsync();

/// <summary>
/// Dispose
/// </summary>
/// <returns></returns>
public virtual Task DisposeAsync()
{
Actions?.Clear();

_serviceInstance?.Dispose();
_scriptModule?.DisposeAsync();
_scriptInstance?.DisposeAsync();

_serviceInstance = null;
_scriptModule = null;
_scriptInstance = null;
_serviceInstance = null;

return Task.WhenAll(
_scriptModule is null ? Task.CompletedTask : _scriptModule.DisposeAsync().AsTask(),
_scriptInstance is null ? Task.CompletedTask : _scriptInstance.DisposeAsync().AsTask()
);
}
}
}
15 changes: 2 additions & 13 deletions Samples/SampleServer/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
private int currentCount = 0;
private void IncrementCount() => currentCount++;
}
111 changes: 13 additions & 98 deletions Samples/SampleServer/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,102 +1,17 @@
@page "/"
@using ScriptContainer
@inject IJSRuntime runtime

<div class="element-container">
<button @onclick="Expand">
Setup
</button>
<div class="doc">
<h2>Document Bounds</h2>
<ul>
<li>@DocW</li>
<li>@DocH</li>
</ul>
</div>
<div class="element" @ref="ElementA">
<h2>Element A Bounds</h2>
<ul>
<li>@ItemAW</li>
<li>@ItemAH</li>
</ul>
</div>
<div class="element element-sm" @ref="ElementB">
<h2>Element B Bounds</h2>
<ul>
<li>@ItemBW</li>
<li>@ItemBH</li>
</ul>
</div>
@if (Rows.Any())
{
<ul>
@foreach (var row in Rows)
{
<li>@row</li>
}
</ul>
}
</div>

@code
{
public double DocW { get; set; }
public double DocH { get; set; }
public double ItemAW { get; set; }
public double ItemAH { get; set; }
public double ItemBW { get; set; }
public double ItemBH { get; set; }
public ScriptService ScaleService { get; set; }
public ElementReference ElementA { get; set; }
public ElementReference ElementB { get; set; }
public ElementReference CanvasA { get; set; }
public ElementReference CanvasB { get; set; }
public IList<string> Rows { get; set; } = new string[0];

protected override async Task OnAfterRenderAsync(bool setup)
{
if (setup)
{
await Setup();
}

await base.OnAfterRenderAsync(setup);
}

protected void Expand()
{
Rows = Rows.Any() ?
new string[0] :
Enumerable.Range(0, 50).Select(o => $"{o}").ToList();
<style type="text/css">
.container {
display: flex;
flex-grow: 1;
width: 100%;
height: 100%;
}
</style>

protected async Task Setup()
{
ScaleService?.Dispose();
ScaleService = new ScriptService(runtime);
ScaleService.Actions[nameof(MessageEnum.Size)] = async message => await GetBounds();
ScaleService.Actions["Demo"] = async message => await GetBounds();

await ScaleService.CreateModule();
await ScaleService.SubscribeToSize(ElementA, "Demo");
await GetBounds();
}

protected async Task GetBounds()
{
var docBounds = await ScaleService.GetDocBounds();

DocW = docBounds.Value.X;
DocH = docBounds.Value.Y;

var itemBoundsA = await ScaleService.GetElementBounds(ElementA);
var itemBoundsB = await ScaleService.GetElementBounds(ElementB);

ItemAW = itemBoundsA.Value.X;
ItemAH = itemBoundsA.Value.Y;
ItemBW = itemBoundsB.Value.X;
ItemBH = itemBoundsB.Value.Y;

await InvokeAsync(StateHasChanged);
}
}
<div class="container">
<Measures></Measures>
<div class="container">
<Measures></Measures>
</div>
</div>
104 changes: 104 additions & 0 deletions Samples/SampleServer/Pages/Measures.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
@using ScriptContainer
@inject IJSRuntime runtime

<div class="element-container">
<button @onclick="Expand">
Setup
</button>
<div class="doc">
<h2>Document Bounds</h2>
<ul>
<li>@DocW</li>
<li>@DocH</li>
</ul>
</div>
<div class="element" @ref="ElementA">
<h2>Element A Bounds</h2>
<ul>
<li>@ItemAW</li>
<li>@ItemAH</li>
</ul>
</div>
<div class="element element-sm" @ref="ElementB">
<h2>Element B Bounds</h2>
<ul>
<li>@ItemBW</li>
<li>@ItemBH</li>
</ul>
</div>
@if (Rows.Any())
{
<ul>
@foreach (var row in Rows)
{
<li>@row</li>
}
</ul>
}
</div>

@code
{
public double DocW { get; set; }
public double DocH { get; set; }
public double ItemAW { get; set; }
public double ItemAH { get; set; }
public double ItemBW { get; set; }
public double ItemBH { get; set; }
public ScriptService ScaleService { get; set; }
public ElementReference ElementA { get; set; }
public ElementReference ElementB { get; set; }
public ElementReference CanvasA { get; set; }
public ElementReference CanvasB { get; set; }
public IList<string> Rows { get; set; } = new string[0];

protected override async Task OnAfterRenderAsync(bool setup)
{
if (setup)
{
await Setup();
}

await base.OnAfterRenderAsync(setup);
}

protected async Task Expand()
{
Rows = Rows.Any() ?
new string[0] :
Enumerable.Range(0, 50).Select(o => $"{o}").ToList();

await Setup();
}

protected async Task Setup()
{
ScaleService?.Dispose();
ScaleService = new ScriptService(runtime);

await ScaleService.CreateModule();
await ScaleService.SubscribeToSize(ElementA, "Demo");

ScaleService.Actions["Demo"] = async message => await GetBounds();

await GetBounds();
}

protected async Task GetBounds()
{
var docBounds = await ScaleService.GetDocBounds();

DocW = docBounds.Value.X;
DocH = docBounds.Value.Y;

var itemBoundsA = await ScaleService.GetElementBounds(ElementA);
var itemBoundsB = await ScaleService.GetElementBounds(ElementB);

ItemAW = itemBoundsA.Value.X;
ItemAH = itemBoundsA.Value.Y;
ItemBW = itemBoundsB.Value.X;
ItemBH = itemBoundsB.Value.Y;

await InvokeAsync(StateHasChanged);
}
}

0 comments on commit 3e9614d

Please sign in to comment.