Skip to content

Commit

Permalink
Merge pull request #45 from WiseTechGlobal/M71/WI00761833/Updates-for…
Browse files Browse the repository at this point in the history
…-Blazor-Diagrams-mouse-configuration

WI00761833 - Updates for Blazor Diagrams mouse configuration
  • Loading branch information
shams-azam-wtg authored Jul 15, 2024
2 parents 5082e89 + 09f824d commit 1d5a782
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions site/Site/Pages/Documentation/Diagram/Behaviors.razor
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ Diagram.UnregisterBehavior<SelectionBehavior>();
Diagram.RegisterBehavior(new MySelectionBehavior(Diagram));
</code></pre>

<h2>Configure behaviors for different actions on input</h2>

You can configure a behavior to perform different actions for an input. Such as scrolling a diagram on mouse wheel instead of zooming in and out.
This can be done using the <code>BehaviorOptions</code>. See the below example.

<h3>Scrolling a diagram on mouse wheel</h3>

<p>To scroll a diagram using the mouse wheel set the <code>DiagramWheelBehavior</code> property of <code>BehaviorOptions</code> to use <code>ScrollBehavior</code>.</p>

<pre><code class="language-cs">
_diagram.BehaviorOptions.DiagramWheelBehavior = _diagram.GetBehavior&lt;ScrollBehavior&gt;();
</code></pre>


<NavigationButtons PreviousLink="/documentation/diagram"
PreviousTitle="Overview"
NextTitle="Ordering"
Expand Down
5 changes: 5 additions & 0 deletions src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Blazor.Diagrams.Core.Behaviors;
using Blazor.Diagrams.Core.Geometry;
using Blazor.Diagrams.Extensions;
using Microsoft.AspNetCore.Components;
Expand Down Expand Up @@ -64,6 +65,10 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
BlazorDiagram.SetContainer(await JSRuntime.GetBoundingClientRect(elementReference));
await JSRuntime.ObserveResizes(elementReference, _reference!);
if (BlazorDiagram.BehaviorOptions.DiagramWheelBehavior is ScrollBehavior)
{
await JSRuntime.AddDefaultPreventingForWheelHandler(elementReference);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public static async Task UnobserveResizes(this IJSRuntime jsRuntime, ElementRefe
{
await jsRuntime.InvokeVoidAsync("ZBlazorDiagrams.unobserve", element, element.Id);
}

public static async Task AddDefaultPreventingForWheelHandler(this IJSRuntime jsRuntime, ElementReference element)
{
await jsRuntime.InvokeVoidAsync("ZBlazorDiagrams.addDefaultPreventingHandler", element, "wheel");
}
}
3 changes: 3 additions & 0 deletions src/Blazor.Diagrams/wwwroot/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ var s = {
}
delete s.tracked[id];
delete s.canvases[id];
},
addDefaultPreventingHandler: (element, eventName) => {
element.addEventListener(eventName, e => e.preventDefault(), { passive: false });
}
};
window.ZBlazorDiagrams = s;
Expand Down
2 changes: 1 addition & 1 deletion src/Blazor.Diagrams/wwwroot/script.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1d5a782

Please sign in to comment.