Skip to content

Commit a680f6a

Browse files
committed
修复node组件事件触发问题,添加更多组件类型
1 parent 16c633c commit a680f6a

8 files changed

Lines changed: 226 additions & 49 deletions

File tree

src/components/BootstrapBlazor.NodeGraph/Components/NodeGraphCanvas.razor

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</div>
1212

1313
@code {
14+
public Graph Graph { get; private set; } = null!;
1415

1516
private string? ClassString => CssBuilder.Default("graph-main-container")
1617
.AddClassFromAttributes(AdditionalAttributes)
@@ -20,16 +21,21 @@
2021
private ElementReference _graphCanvas;
2122

2223
/// <inheritdoc />
23-
protected override async Task InvokeInitAsync()
24+
protected override async Task OnAfterRenderAsync(bool firstRender)
2425
{
25-
// 初始化并添加引用
26-
await InvokeVoidAsync("init", Id, DotNetObjectReference.Create(NodeGraphService));
27-
// 创建图表配置
28-
var graphRef = await InvokeAsync<IJSObjectReference>("createLGraph");
29-
// var graph = new Graph(graphRef!);
30-
// 创建图表画布
31-
_graphCanvasRef = await InvokeAsync<IJSObjectReference>("createLGraphCanvas", _graphCanvas, graphRef)
32-
?? throw new InvalidOperationException("Create GraphCanvas failed!");
26+
await base.OnAfterRenderAsync(firstRender);
27+
if (firstRender)
28+
{
29+
// 初始化并添加引用
30+
await InvokeVoidAsync("init", Id, DotNetObjectReference.Create(NodeGraphService));
31+
// 创建图表配置
32+
var graphRef = await InvokeAsync<IJSObjectReference>("createLGraph");
33+
Graph = new Graph(graphRef!);
34+
// var graph = new Graph(graphRef!);
35+
// 创建图表画布
36+
_graphCanvasRef = await InvokeAsync<IJSObjectReference>("createLGraphCanvas", _graphCanvas, graphRef)
37+
?? throw new InvalidOperationException("Create GraphCanvas failed!");
38+
}
3339
}
3440

3541
}

src/components/BootstrapBlazor.NodeGraph/Data/Interop/NodeRegister.cs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace BootstrapBlazor.Components.Data.Interop;
77
/// <summary>
88
/// 节点插槽
99
/// </summary>
10-
public class NodeSlot
10+
public class NodeSlotDto
1111
{
1212
/// <summary>
1313
/// 插槽名称
@@ -20,10 +20,46 @@ public class NodeSlot
2020
public string Type { get; set; } = string.Empty;
2121
}
2222

23+
/// <summary>
24+
/// 节点组件
25+
/// </summary>
26+
public class WidgetDto
27+
{
28+
/// <summary>
29+
/// 组件ID,需要在当前节点下唯一
30+
/// </summary>
31+
public string WidgetId { get; set; } = string.Empty;
32+
33+
/// <summary>
34+
/// 节点组件类型
35+
/// </summary>
36+
public string WidgetType { get; set; } = string.Empty;
37+
38+
/// <summary>
39+
/// 节点组件名称
40+
/// </summary>
41+
public string DisplayName { get; set; } = string.Empty;
42+
43+
/// <summary>
44+
/// 默认值
45+
/// </summary>
46+
public object? Value { get; set; }
47+
48+
/// <summary>
49+
/// 节点组件配置
50+
/// </summary>
51+
public WidgetOptions? WidgetOptions { get; set; }
52+
53+
/// <summary>
54+
/// 是否有回调函数
55+
/// </summary>
56+
public bool HasCallback { get; set; }
57+
}
58+
2359
/// <summary>
2460
/// 节点配置
2561
/// </summary>
26-
public class GraphNodeConfig
62+
public class GraphNodeConfigDto
2763
{
2864
/// <summary>
2965
/// 节点唯一类型路径,形如 groupA/groupB/NodeName
@@ -38,17 +74,17 @@ public class GraphNodeConfig
3874
/// <summary>
3975
/// 输入插槽
4076
/// </summary>
41-
public List<NodeSlot> Inputs { get; set; } = new();
77+
public List<NodeSlotDto> Inputs { get; set; } = new();
4278

4379
/// <summary>
4480
/// 输出插槽
4581
/// </summary>
46-
public List<NodeSlot> Outputs { get; set; } = new();
82+
public List<NodeSlotDto> Outputs { get; set; } = new();
4783

4884
/// <summary>
4985
/// 节点组件
5086
/// </summary>
51-
public List<NodeWidget> Widgets { get; set; } = new();
87+
public List<WidgetDto> Widgets { get; set; } = new();
5288

5389
/// <summary>
5490
/// 是否有执行方法

src/components/BootstrapBlazor.NodeGraph/Graph.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ internal Graph(IJSObjectReference lGraphRef)
1212
{
1313
GraphRef = lGraphRef;
1414
}
15+
16+
public async Task RunStep(int step, bool ignoreErrors = true, int? limits = null)
17+
{
18+
await GraphRef.InvokeVoidAsync("runStep", step, ignoreErrors, limits);
19+
}
1520
}

src/components/BootstrapBlazor.NodeGraph/GraphNode.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,16 @@ public ValueTask DisposeAsync()
2121

2222
public ValueTask<T?> GetInputData<T>(int slotIndex)
2323
{
24-
return _graphNodeReference.InvokeAsync<T?>("getInputData", slotIndex);
24+
return _graphNodeReference.InvokeAsync<T?>( "getInputData", slotIndex);
2525
}
2626

2727
public ValueTask<T?> GetOutputData<T>(int slotIndex)
2828
{
29-
return _graphNodeReference.InvokeAsync<T?>("getOutputData", slotIndex);
30-
}
31-
32-
public ValueTask SetInputData<T>(int slotIndex, T inputData)
33-
{
34-
return _graphNodeReference.InvokeVoidAsync("setInputData", inputData);
29+
return _graphNodeReference.InvokeAsync<T?>( "getOutputData", slotIndex);
3530
}
3631

3732
public ValueTask SetOutputData<T>(int slotIndex, T outputData)
3833
{
39-
return _graphNodeReference.InvokeVoidAsync("setOutputData", outputData);
34+
return _graphNodeReference.InvokeVoidAsync( "setOutputData", slotIndex, outputData);
4035
}
4136
}

src/components/BootstrapBlazor.NodeGraph/Interfaces/INodeWidget.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ namespace BootstrapBlazor.Components.Interfaces;
1111
/// </summary>
1212
public interface INodeWidget
1313
{
14+
/// <summary>
15+
/// 组件ID,需要在当前节点下唯一
16+
/// </summary>
17+
public string WidgetId { get; set; }
1418
/// <summary>
1519
/// 节点组件类型
1620
/// </summary>
@@ -34,7 +38,7 @@ public interface INodeWidget
3438
/// <summary>
3539
/// 回调函数
3640
/// </summary>
37-
public Func<object?, NodeWidget, GraphNode, Task>? Callback { get; set; }
41+
public Func<object?, GraphNode, Task>? Callback { get; set; }
3842
}
3943

4044
/// <inheritdoc />
@@ -53,5 +57,5 @@ public interface INodeWidget<TValue, TOption> : INodeWidget where TOption : Widg
5357
/// <summary>
5458
/// 回调函数
5559
/// </summary>
56-
public new Func<TValue?, NodeWidget, GraphNode, Task>? Callback { get; set; }
60+
public new Func<TValue?, GraphNode, Task>? Callback { get; set; }
5761
}

src/components/BootstrapBlazor.NodeGraph/NodeWidgets.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public enum NodeWidgetType
3838
/// </summary>
3939
public abstract class NodeWidget : INodeWidget
4040
{
41+
/// <inheritdoc />
42+
public string WidgetId { get; set; } = string.Empty;
43+
4144
/// <inheritdoc />
4245
public abstract NodeWidgetType WidgetType { get; }
4346

@@ -51,7 +54,7 @@ public abstract class NodeWidget : INodeWidget
5154
public WidgetOptions? WidgetOptions { get; set; }
5255

5356
/// <inheritdoc />
54-
public Func<object?, NodeWidget, GraphNode, Task>? Callback { get; set; }
57+
public Func<object?, GraphNode, Task>? Callback { get; set; }
5558
}
5659

5760
/// <inheritdoc cref="NodeWidget" />
@@ -69,7 +72,7 @@ public abstract class NodeWidget<TValue, TOption> : NodeWidget, INodeWidget<TVal
6972
}
7073

7174
/// <inheritdoc />
72-
public new Func<TValue?, NodeWidget, GraphNode, Task>? Callback
75+
public new Func<TValue?, GraphNode, Task>? Callback
7376
{
7477
get
7578
{
@@ -78,7 +81,7 @@ public abstract class NodeWidget<TValue, TOption> : NodeWidget, INodeWidget<TVal
7881
return null;
7982
}
8083

81-
return (v, widget, node) => base.Callback.Invoke(v, widget, node);
84+
return (v, node) => base.Callback.Invoke(v, node);
8285
}
8386
set
8487
{
@@ -88,7 +91,7 @@ public abstract class NodeWidget<TValue, TOption> : NodeWidget, INodeWidget<TVal
8891
}
8992
else
9093
{
91-
base.Callback = (v, widget, node) => value((TValue?)v, widget, node);
94+
base.Callback = (v, node) => value((TValue?)v, node);
9295
}
9396
}
9497
}

0 commit comments

Comments
 (0)