Skip to content

Commit 16c633c

Browse files
committed
添加一些widget的支持
1 parent cbe6a17 commit 16c633c

15 files changed

Lines changed: 624 additions & 72 deletions

File tree

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.NodeGraph/js/Graph.js", AutoInvokeDispose = false, JSObjectReference = true)]
44
@inherits BootstrapModuleComponentBase
55

6+
@inject NodeGraphService NodeGraphService
67
@inject ILogger<NodeGraphCanvas> Logger
78

89
<div class="@ClassString" @attributes="@AdditionalAttributes" id="@Id">
@@ -15,22 +16,20 @@
1516
.AddClassFromAttributes(AdditionalAttributes)
1617
.Build();
1718

18-
[Parameter]
19-
public Graph? Graph { get; set; }
20-
2119
private IJSObjectReference _graphCanvasRef = null!;
2220
private ElementReference _graphCanvas;
2321

24-
2522
/// <inheritdoc />
2623
protected override async Task InvokeInitAsync()
2724
{
28-
await InvokeVoidAsync("init", Id);
25+
// 初始化并添加引用
26+
await InvokeVoidAsync("init", Id, DotNetObjectReference.Create(NodeGraphService));
27+
// 创建图表配置
2928
var graphRef = await InvokeAsync<IJSObjectReference>("createLGraph");
30-
Graph = new Graph(graphRef!);
29+
// var graph = new Graph(graphRef!);
30+
// 创建图表画布
3131
_graphCanvasRef = await InvokeAsync<IJSObjectReference>("createLGraphCanvas", _graphCanvas, graphRef)
32-
?? throw new InvalidOperationException("Create GraphCanvas failed!");
33-
// await InvokeVoidAsync("constructGraphNode", "测试节点");
32+
?? throw new InvalidOperationException("Create GraphCanvas failed!");
3433
}
3534

3635
}

src/components/BootstrapBlazor.NodeGraph/_Imports.razor renamed to src/components/BootstrapBlazor.NodeGraph/Components/_Imports.razor

File renamed without changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components.Data.Interop;
6+
7+
/// <summary>
8+
/// 节点插槽
9+
/// </summary>
10+
public class NodeSlot
11+
{
12+
/// <summary>
13+
/// 插槽名称
14+
/// </summary>
15+
public string Name { get; set; } = string.Empty;
16+
17+
/// <summary>
18+
/// 插槽类型 TypeName
19+
/// </summary>
20+
public string Type { get; set; } = string.Empty;
21+
}
22+
23+
/// <summary>
24+
/// 节点配置
25+
/// </summary>
26+
public class GraphNodeConfig
27+
{
28+
/// <summary>
29+
/// 节点唯一类型路径,形如 groupA/groupB/NodeName
30+
/// </summary>
31+
public string TypePath { get; set; } = null!;
32+
33+
/// <summary>
34+
/// 节点名称
35+
/// </summary>
36+
public string DisplayName { get; set; } = string.Empty;
37+
38+
/// <summary>
39+
/// 输入插槽
40+
/// </summary>
41+
public List<NodeSlot> Inputs { get; set; } = new();
42+
43+
/// <summary>
44+
/// 输出插槽
45+
/// </summary>
46+
public List<NodeSlot> Outputs { get; set; } = new();
47+
48+
/// <summary>
49+
/// 节点组件
50+
/// </summary>
51+
public List<NodeWidget> Widgets { get; set; } = new();
52+
53+
/// <summary>
54+
/// 是否有执行方法
55+
/// </summary>
56+
public bool HasAction { get; set; }
57+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace BootstrapBlazor.Components.Data;
8+
9+
/// <summary>
10+
/// 节点组件配置
11+
/// </summary>
12+
public abstract record WidgetOptions
13+
{
14+
// public string? On { get; set; }
15+
// public string? Off { get; set; }
16+
17+
/// <summary>
18+
/// 是否只读
19+
/// </summary>
20+
public bool? ReadOnly { get; set; }
21+
22+
// public int? Y { get; set; }
23+
// public bool? Multiline { get; set; }
24+
25+
// TODO: 待测试
26+
public string? Property { get; set; }
27+
28+
/// <summary>
29+
/// 不显示可编辑组件
30+
/// </summary>
31+
public bool? Socketless { get; set; }
32+
33+
// TODO: Combo中使用
34+
// public TValue[]? Values { get; set; }
35+
}
36+
37+
/// <summary>
38+
/// 布尔组件配置
39+
/// </summary>
40+
public record BooleanWidgetOptions : WidgetOptions
41+
{
42+
}
43+
44+
/// <summary>
45+
/// 数字组件配置
46+
/// </summary>
47+
public record NumberWidgetOptions : WidgetOptions
48+
{
49+
/// <summary>
50+
/// 最小值
51+
/// </summary>
52+
public double? Min { get; set; }
53+
54+
/// <summary>
55+
/// 最大值
56+
/// </summary>
57+
public double? Max { get; set; }
58+
59+
// step 已经废弃
60+
/// <summary>
61+
/// 步长
62+
/// </summary>
63+
[JsonPropertyName("step2")]
64+
public double? Step { get; set; }
65+
66+
/// <summary>
67+
/// 精度
68+
/// </summary>
69+
public int? Precision { get; set; }
70+
}
71+
72+
/// <summary>
73+
/// 滑动条组件配置
74+
/// </summary>
75+
public record SliderWidgetOptions : WidgetOptions
76+
{
77+
/// <summary>
78+
/// 最小值
79+
/// </summary>
80+
public double Min { get; set; }
81+
82+
/// <summary>
83+
/// 最大值
84+
/// </summary>
85+
public double Max { get; set; }
86+
87+
// step 已经废弃
88+
/// <summary>
89+
/// 步长
90+
/// </summary>
91+
[JsonPropertyName("step2")]
92+
public double? Step { get; set; }
93+
94+
/// <summary>
95+
/// 精度
96+
/// </summary>
97+
public int? Precision { get; set; }
98+
99+
// TODO: CanvasColour
100+
// public object? SliderColor { get; set; } // TODO: Replace with actual type for CanvasColour
101+
// public object? MarkerColor { get; set; } // TODO: Replace with actual type for CanvasColour
102+
}
103+
104+
/// <summary>
105+
/// 旋钮组件配置
106+
/// </summary>
107+
public record KnobWidgetOptions : WidgetOptions
108+
{
109+
/// <summary>
110+
/// 最小值
111+
/// </summary>
112+
public double Min { get; set; }
113+
114+
/// <summary>
115+
/// 最大值
116+
/// </summary>
117+
public double Max { get; set; }
118+
119+
// step 已经废弃
120+
/// <summary>
121+
/// 步长
122+
/// </summary>
123+
[JsonPropertyName("step2")]
124+
public double? Step { get; set; }
125+
126+
/// <summary>
127+
/// 精度
128+
/// </summary>
129+
public int? Precision { get; set; }
130+
131+
// TODO: CanvasColour
132+
// public object? SliderColor { get; set; } // TODO: Replace with actual type for CanvasColour
133+
// public object? MarkerColor { get; set; } // TODO: Replace with actual type for CanvasColour
134+
135+
// public string? GradientStops { get; set; }
136+
}
137+
138+
/// <summary>
139+
/// 文本组件配置
140+
/// </summary>
141+
public record StringWidgetOptions : WidgetOptions
142+
{
143+
}
144+
145+
/// <summary>
146+
/// 按钮组件配置
147+
/// </summary>
148+
public record ButtonWidgetOptions : WidgetOptions
149+
{
150+
}

src/components/BootstrapBlazor.NodeGraph/GraphNode.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,38 @@
44

55
namespace BootstrapBlazor.Components;
66

7-
public class GraphNode
7+
public class GraphNode : IAsyncDisposable
88
{
9-
9+
private IJSObjectReference _graphNodeReference;
10+
11+
internal GraphNode(IJSObjectReference graphNodeReference)
12+
{
13+
_graphNodeReference = graphNodeReference;
14+
}
15+
16+
/// <inheritdoc />
17+
public ValueTask DisposeAsync()
18+
{
19+
return _graphNodeReference.DisposeAsync();
20+
}
21+
22+
public ValueTask<T?> GetInputData<T>(int slotIndex)
23+
{
24+
return _graphNodeReference.InvokeAsync<T?>("getInputData", slotIndex);
25+
}
26+
27+
public ValueTask<T?> GetOutputData<T>(int slotIndex)
28+
{
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);
35+
}
36+
37+
public ValueTask SetOutputData<T>(int slotIndex, T outputData)
38+
{
39+
return _graphNodeReference.InvokeVoidAsync("setOutputData", outputData);
40+
}
1041
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components.Interfaces;
6+
7+
public interface INodeSlot
8+
{
9+
/// <summary>
10+
/// slot name
11+
/// </summary>
12+
public string Name { get; set; }
13+
/// <summary>
14+
/// node下的唯一id
15+
/// </summary>
16+
public string Id { get; set; }
17+
/// <summary>
18+
/// 数据类型
19+
/// </summary>
20+
public Type ValueType { get; }
21+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using BootstrapBlazor.Components.Data;
6+
7+
namespace BootstrapBlazor.Components.Interfaces;
8+
9+
/// <summary>
10+
/// 节点组件接口
11+
/// </summary>
12+
public interface INodeWidget
13+
{
14+
/// <summary>
15+
/// 节点组件类型
16+
/// </summary>
17+
public NodeWidgetType WidgetType { get; }
18+
19+
/// <summary>
20+
/// 节点组件名称
21+
/// </summary>
22+
public string DisplayName { get; set; }
23+
24+
/// <summary>
25+
/// 默认值
26+
/// </summary>
27+
public object? Value { get; set; }
28+
29+
/// <summary>
30+
/// 节点组件配置
31+
/// </summary>
32+
public WidgetOptions? WidgetOptions { get; set; }
33+
34+
/// <summary>
35+
/// 回调函数
36+
/// </summary>
37+
public Func<object?, NodeWidget, GraphNode, Task>? Callback { get; set; }
38+
}
39+
40+
/// <inheritdoc />
41+
public interface INodeWidget<TValue, TOption> : INodeWidget where TOption : WidgetOptions
42+
{
43+
/// <summary>
44+
/// 默认值
45+
/// </summary>
46+
public new TValue? Value { get; set; }
47+
48+
/// <summary>
49+
/// 节点组件配置
50+
/// </summary>
51+
public new TOption? WidgetOptions { get; set; }
52+
53+
/// <summary>
54+
/// 回调函数
55+
/// </summary>
56+
public new Func<TValue?, NodeWidget, GraphNode, Task>? Callback { get; set; }
57+
}

0 commit comments

Comments
 (0)