Skip to content

Commit

Permalink
Merge pull request #152 from Blazor-Diagrams/develop
Browse files Browse the repository at this point in the history
Version 2.1.6
  • Loading branch information
zHaytam authored Nov 1, 2021
2 parents 1c719cf + 4afbbcf commit ef9309c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Diagrams (2.1.6) - 2021-10-31

### Fixed

- `ZoomBehavior` using new zoom before Clamp to set Pan. (fixes #141)
- `PanChanged` not triggering when zooming with the mouse wheel.
- Zoom value decreasing when the mouse wheel delta is zero.
- Ports aren't refreshed when links are added in `OnInitializedAsync`. (fixes #111)

## Diagrams (2.1.5) - 2021-08-30

### Fixed
Expand Down
20 changes: 12 additions & 8 deletions src/Blazor.Diagrams.Core/Behaviors/ZoomBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Blazor.Diagrams.Core.Geometry;

using Microsoft.AspNetCore.Components.Web;

using System;

namespace Blazor.Diagrams.Core.Behaviors
Expand All @@ -13,16 +15,19 @@ public ZoomBehavior(Diagram diagram) : base(diagram)

private void Diagram_Wheel(WheelEventArgs e)
{
if (Diagram.Container == null || e.DeltaY == 0)
return;

if (!Diagram.Options.Zoom.Enabled)
return;

var scale = Math.Clamp(Diagram.Options.Zoom.ScaleFactor, 1.01, 2);

var oldZoom = Diagram.Zoom;
var deltaY = Diagram.Options.Zoom.Inverse ? e.DeltaY * -1 : e.DeltaY;
var newZoom = deltaY > 0 ? oldZoom * scale : oldZoom / scale;
newZoom = Math.Clamp(newZoom, Diagram.Options.Zoom.Minimum, Diagram.Options.Zoom.Maximum);

if (newZoom < 0)
if (newZoom < 0 || newZoom == Diagram.Zoom)
return;

// Other algorithms (based only on the changes in the zoom) don't work for our case
Expand All @@ -38,12 +43,11 @@ private void Diagram_Wheel(WheelEventArgs e)
var newPanX = Diagram.Pan.X - widthDiff * xFactor;
var newPanY = Diagram.Pan.Y - heightDiff * yFactor;

newZoom = Math.Clamp(newZoom, Diagram.Options.Zoom.Minimum, Diagram.Options.Zoom.Maximum);
if (newZoom == Diagram.Zoom)
return;

Diagram.Pan = new Point(newPanX, newPanY);
Diagram.SetZoom(newZoom);
Diagram.Batch(() =>
{
Diagram.SetPan(newPanX, newPanY);
Diagram.SetZoom(newZoom);
});
}

public override void Dispose()
Expand Down
6 changes: 3 additions & 3 deletions src/Blazor.Diagrams.Core/Blazor.Diagrams.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Authors>zHaytam</Authors>
<Description>A fully customizable and extensible all-purpose diagrams library for Blazor</Description>
<AssemblyVersion>2.1.5</AssemblyVersion>
<FileVersion>2.1.5</FileVersion>
<AssemblyVersion>2.1.6</AssemblyVersion>
<FileVersion>2.1.6</FileVersion>
<RepositoryUrl>https://github.com/zHaytam/Blazor.Diagrams</RepositoryUrl>
<Version>2.1.5</Version>
<Version>2.1.6</Version>
<PackageId>Z.Blazor.Diagrams.Core</PackageId>
<PackageTags>blazor diagrams diagramming svg drag</PackageTags>
<Product>Z.Blazor.Diagrams.Core</Product>
Expand Down
9 changes: 8 additions & 1 deletion src/Blazor.Diagrams.Core/Diagram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Diagram(DiagramOptions? options = null)
public LinkLayer Links { get; }
public IReadOnlyList<GroupModel> Groups => _groups;
public Rectangle? Container { get; internal set; }
public Point Pan { get; internal set; } = Point.Zero;
public Point Pan { get; private set; } = Point.Zero;
public double Zoom { get; private set; } = 1;
public DiagramOptions Options { get; }
public bool SuspendRefresh { get; set; }
Expand Down Expand Up @@ -307,6 +307,13 @@ public void ZoomToFit(double margin = 10)
Refresh();
}

public void SetPan(double x, double y)
{
Pan = new Point(x, y);
PanChanged?.Invoke();
Refresh();
}

public void UpdatePan(double deltaX, double deltaY)
{
Pan = Pan.Add(deltaX, deltaY);
Expand Down
6 changes: 3 additions & 3 deletions src/Blazor.Diagrams/Blazor.Diagrams.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<RazorLangVersion>3.0</RazorLangVersion>
<Authors>zHaytam</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AssemblyVersion>2.1.5</AssemblyVersion>
<FileVersion>2.1.5</FileVersion>
<AssemblyVersion>2.1.6</AssemblyVersion>
<FileVersion>2.1.6</FileVersion>
<RepositoryUrl>https://github.com/zHaytam/Blazor.Diagrams</RepositoryUrl>
<Description>A fully customizable and extensible all-purpose diagrams library for Blazor</Description>
<Version>2.1.5</Version>
<Version>2.1.6</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>blazor diagrams diagramming svg drag</PackageTags>
<PackageId>Z.Blazor.Diagrams</PackageId>
Expand Down
23 changes: 20 additions & 3 deletions src/Blazor.Diagrams/Components/Renderers/PortRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class PortRenderer : ComponentBase, IDisposable
{
private bool _shouldRender = true;
private ElementReference _element;
private bool _updatingDimensions;
private bool _shouldRefreshPort;

[CascadingParameter]
public Diagram Diagram { get; set; }
Expand Down Expand Up @@ -105,6 +107,7 @@ private PortModel FindPortOn(double clientX, double clientY)

private async Task UpdateDimensions()
{
_updatingDimensions = true;
var zoom = Diagram.Zoom;
var pan = Diagram.Pan;
var rect = await JSRuntime.GetBoundingClientRect(_element);
Expand All @@ -114,14 +117,28 @@ private async Task UpdateDimensions()
(rect.Top - Diagram.Container.Top - pan.Y) / zoom);

Port.Initialized = true;
_updatingDimensions = false;

// We don't really need to refresh the port again,
// let's just refresh the links so that they use the new port's position
Port.RefreshLinks();
if (_shouldRefreshPort)
{
_shouldRefreshPort = false;
Port.RefreshAll();
}
else
{
Port.RefreshLinks();
}
}

private async void OnPortChanged()
{
// If an update is ongoing and the port is refreshed again,
// it's highly likely the port needs to be refreshed (e.g. link added)
if (_updatingDimensions)
{
_shouldRefreshPort = true;
}

if (Port.Initialized)
{
_shouldRender = true;
Expand Down

0 comments on commit ef9309c

Please sign in to comment.