2020
2121namespace NodeDev . Blazor . Components ;
2222
23- public partial class GraphCanvas : ComponentBase , IDisposable , IGraphCanvas
23+ public partial class GraphCanvas : ComponentBase , IDisposable
2424{
2525 [ Parameter , EditorRequired ]
2626 public Graph Graph { get ; set ; } = null ! ;
@@ -46,8 +46,6 @@ protected override void OnInitialized()
4646 base . OnInitialized ( ) ;
4747 _ = NodeProvider . WarmExtensionMethodCatalogAsync ( ) ;
4848
49- Graph . GraphCanvas = this ;
50-
5149 var options = new BlazorDiagramOptions
5250 {
5351 GridSize = 30 ,
@@ -79,6 +77,7 @@ protected override void OnInitialized()
7977 DiagramSynchronizer = new GraphDiagramSynchronizer ( Graph , action => InvokeAsync ( action ) ) ;
8078 DiagramProjection = new GraphDiagramProjection ( Graph , Diagram , DiagramSynchronizer , OnConnectionAdded ) ;
8179 DebugVisualizer = new GraphDebugVisualizer ( Graph , Diagram , action => InvokeAsync ( action ) ) ;
80+ IndexPage . RegisterGraphCanvas ( this ) ;
8281 }
8382
8483 #endregion
@@ -93,30 +92,13 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
9392 {
9493 await Task . Delay ( 100 ) ;
9594 Diagram . Batch ( DiagramProjection . Initialize ) ;
96- DiagramSynchronizer . Start ( DiagramProjection . Rebuild , StateHasChanged ) ;
95+ DiagramSynchronizer . Start ( DiagramProjection , StateHasChanged ) ;
9796 DebugVisualizer . Start ( ) ;
9897 }
9998 }
10099
101100 #endregion
102101
103- #region UpdateConnectionType
104-
105- public void UpdatePortColor ( Connection connection )
106- {
107- var port = FindPort ( connection ) ;
108- if ( port == null )
109- return ;
110-
111- var color = GetTypeShapeColor ( connection . Type , connection . Parent . TypeFactory ) ;
112- foreach ( var link in port . Links . Cast < LinkModel > ( ) )
113- link . Color = color ;
114-
115- Diagram . Refresh ( ) ;
116- }
117-
118- #endregion
119-
120102 #region UpdateNodes
121103
122104 private NodeModel ? FindNodeModel ( Node node )
@@ -426,10 +408,6 @@ private void OnNewOverloadSelected(Node.AlternateOverload overload)
426408
427409 GraphManagerService . SelectNodeOverload ( PopupState . Node , overload ) ;
428410
429- // Refresh the node visually after overload selection
430- // The node's ports have changed, so we need to update the UI
431- Refresh ( PopupState . Node ) ;
432-
433411 CancelPopup ( ) ;
434412 }
435413
@@ -639,92 +617,6 @@ public void ToggleBreakpointOnSelectedNode()
639617
640618 #endregion
641619
642- #region RemoveNode
643-
644- public void RemoveNode ( Node node )
645- {
646- if ( node is LambdaReturnNode { IsImplicit : true } boundaryReturn )
647- {
648- var group = FindLambdaGroup ( boundaryReturn . CallableScopeId ) ;
649- if ( group != null )
650- {
651- foreach ( var port in group . Ports
652- . OfType < GraphPortModel > ( )
653- . Where ( x => x . Connection . Parent == boundaryReturn )
654- . ToList ( ) )
655- {
656- group . RemovePort ( port ) ;
657- }
658- group . Refresh ( ) ;
659- }
660- return ;
661- }
662-
663- var nodeModel = FindNodeModel ( node ) ;
664- if ( nodeModel == null )
665- return ;
666-
667- using var suppression = DiagramSynchronizer . SuppressCanvasMutations ( ) ;
668- if ( nodeModel is LambdaGroupModel modelGroup )
669- Diagram . Groups . Remove ( modelGroup ) ;
670- else
671- Diagram . Nodes . Remove ( nodeModel ) ;
672- }
673-
674- #endregion
675-
676- #region AddLink / RemoveLink
677-
678- public void RemoveLinkFromGraphCanvas ( Connection source , Connection destination )
679- {
680- using ( DiagramSynchronizer . SuppressConnectionUpdates ( ) )
681- {
682- var link = Diagram . Links . FirstOrDefault ( x => ( x . Source . Model as GraphPortModel ) ? . Connection == source && ( x . Target . Model as GraphPortModel ) ? . Connection == destination ) ;
683- if ( link != null )
684- Diagram . Links . Remove ( link ) ;
685- }
686- }
687-
688- public void AddLinkToGraphCanvas ( Connection source , Connection destination )
689- {
690- using ( DiagramSynchronizer . SuppressConnectionUpdates ( ) )
691- {
692- if ( source . IsInput )
693- ( destination , source ) = ( source , destination ) ;
694-
695- var sourcePort = FindPort ( source ) ?? throw new InvalidOperationException ( $ "No canvas port exists for { source . Parent . Name } .{ source . Name } .") ;
696- var destinationPort = FindPort ( destination ) ?? throw new InvalidOperationException ( $ "No canvas port exists for { destination . Parent . Name } .{ destination . Name } .") ;
697-
698- // Make sure there isn't already an existing identical link
699- if ( Diagram . Links . OfType < LinkModel > ( ) . Any ( x => ( x . Source as SinglePortAnchor ) ? . Port == sourcePort && ( x . Target as SinglePortAnchor ) ? . Port == destinationPort ) )
700- return ;
701-
702- var link = Diagram . Links . Add ( new LinkModel ( sourcePort , destinationPort ) ) ;
703-
704- OnConnectionAdded ( link , true ) ;
705- }
706- }
707-
708- #endregion
709-
710- #region AddNode
711-
712- public void AddNode ( Node node )
713- {
714- DiagramProjection . AddNode ( node ) ;
715- }
716-
717- #endregion
718-
719- #region Refresh
720-
721- public void Refresh ( Node node )
722- {
723- DiagramProjection . Refresh ( node ) ;
724- }
725-
726- #endregion
727-
728620 #region Initialize
729621
730622 public static string GetTypeShapeColor ( TypeBase type , TypeFactory typeFactory )
@@ -750,9 +642,7 @@ public static string GetTypeShapeColor(TypeBase type, TypeFactory typeFactory)
750642 public void Dispose ( )
751643 {
752644 GC . SuppressFinalize ( this ) ;
753-
754- if ( Graph . GraphCanvas == this )
755- Graph . GraphCanvas = null ;
645+ IndexPage . UnregisterGraphCanvas ( this ) ;
756646
757647 DebugVisualizer . Dispose ( ) ;
758648 DiagramSynchronizer . Dispose ( ) ;
0 commit comments