Skip to content

Commit 05683d2

Browse files
committed
refactor: replace static fromJsonMap methods with unified factory constructors and improve CommentNode color observability
1 parent df173e5 commit 05683d2

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

packages/vyuh_node_flow/lib/graph/graph.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Node<T> defaultNodeFromJson<T>(
2525
) {
2626
final type = json['type'] as String;
2727
return switch (type) {
28-
'group' => GroupNode.fromJsonMap<T>(json, fromJsonT),
29-
'comment' => CommentNode.fromJsonMap<T>(json, fromJsonT),
28+
'group' => GroupNode<T>.fromJson(json, dataFromJson: fromJsonT),
29+
'comment' => CommentNode<T>.fromJson(json, dataFromJson: fromJsonT),
3030
_ => Node<T>.fromJson(json, fromJsonT),
3131
};
3232
}

packages/vyuh_node_flow/lib/nodes/comment_node.dart

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ class CommentNode<T> extends Node<T> with ResizableMixin<T> {
4242
required super.data,
4343
double width = 200.0,
4444
double height = 100.0,
45-
this.color = Colors.yellow,
45+
Color color = Colors.yellow,
4646
int zIndex = 0,
4747
bool isVisible = true,
4848
super.locked,
4949
}) : _text = Observable(text),
50+
_color = Observable(color),
5051
super(
5152
type: 'comment',
5253
size: Size(width, height),
@@ -65,15 +66,19 @@ class CommentNode<T> extends Node<T> with ResizableMixin<T> {
6566
String get text => _text.value;
6667
set text(String value) => runInAction(() => _text.value = value);
6768

69+
/// Observable background color of the comment.
70+
final Observable<Color> _color;
71+
72+
/// The background color of the comment.
73+
Color get color => _color.value;
74+
set color(Color value) => runInAction(() => _color.value = value);
75+
6876
/// The width of the comment in pixels.
6977
double get width => size.value.width;
7078

7179
/// The height of the comment in pixels.
7280
double get height => size.value.height;
7381

74-
/// The background color of the comment.
75-
final Color color;
76-
7782
/// Minimum and maximum size constraints for comments.
7883
static const double minWidth = 100.0;
7984
static const double minHeight = 60.0;
@@ -129,24 +134,24 @@ class CommentNode<T> extends Node<T> with ResizableMixin<T> {
129134

130135
/// Creates a [CommentNode] from a JSON map.
131136
///
132-
/// This factory method is used during workflow deserialization to recreate
137+
/// This factory constructor is used during workflow deserialization to recreate
133138
/// comment nodes from saved data.
134139
///
135140
/// Parameters:
136141
/// * [json] - The JSON map containing node data
137-
/// * [fromJsonT] - Function to deserialize the custom data of type [R]
138-
static CommentNode<R> fromJsonMap<R>(
139-
Map<String, dynamic> json,
140-
R Function(Object? json) fromJsonT,
141-
) {
142-
return CommentNode<R>(
142+
/// * [dataFromJson] - Function to deserialize the custom data of type [T]
143+
factory CommentNode.fromJson(
144+
Map<String, dynamic> json, {
145+
required T Function(Object? json) dataFromJson,
146+
}) {
147+
return CommentNode<T>(
143148
id: json['id'] as String,
144149
position: Offset(
145150
(json['x'] as num).toDouble(),
146151
(json['y'] as num).toDouble(),
147152
),
148153
text: json['text'] as String? ?? '',
149-
data: fromJsonT(json['data']),
154+
data: dataFromJson(json['data']),
150155
width: (json['width'] as num?)?.toDouble() ?? 200.0,
151156
height: (json['height'] as num?)?.toDouble() ?? 100.0,
152157
color: Color(json['color'] as int? ?? Colors.yellow.toARGB32()),

packages/vyuh_node_flow/lib/nodes/group_node.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -514,16 +514,16 @@ class GroupNode<T> extends Node<T> with ResizableMixin<T>, GroupableMixin<T> {
514514

515515
/// Creates a [GroupNode] from a JSON map.
516516
///
517-
/// This factory method is used during workflow deserialization to recreate
517+
/// This factory constructor is used during workflow deserialization to recreate
518518
/// group nodes from saved data.
519519
///
520520
/// Parameters:
521521
/// * [json] - The JSON map containing node data
522-
/// * [fromJsonT] - Function to deserialize the custom data of type [R]
523-
static GroupNode<R> fromJsonMap<R>(
524-
Map<String, dynamic> json,
525-
R Function(Object? json) fromJsonT,
526-
) {
522+
/// * [dataFromJson] - Function to deserialize the custom data of type [T]
523+
factory GroupNode.fromJson(
524+
Map<String, dynamic> json, {
525+
required T Function(Object? json) dataFromJson,
526+
}) {
527527
// Parse padding from JSON (stored as LTRB array or object)
528528
EdgeInsets padding = kGroupNodeDefaultPadding;
529529
final paddingJson = json['padding'];
@@ -557,7 +557,7 @@ class GroupNode<T> extends Node<T> with ResizableMixin<T>, GroupableMixin<T> {
557557
.toList() ??
558558
const [];
559559

560-
final node = GroupNode<R>(
560+
return GroupNode<T>(
561561
id: json['id'] as String,
562562
position: Offset(
563563
(json['x'] as num).toDouble(),
@@ -568,24 +568,21 @@ class GroupNode<T> extends Node<T> with ResizableMixin<T>, GroupableMixin<T> {
568568
(json['height'] as num?)?.toDouble() ?? 150.0,
569569
),
570570
title: json['title'] as String? ?? '',
571-
data: fromJsonT(json['data']),
571+
data: dataFromJson(json['data']),
572572
color: Color(json['color'] as int? ?? Colors.blue.toARGB32()),
573573
behavior: GroupBehavior.values.byName(
574574
json['behavior'] as String? ?? 'bounds',
575575
),
576-
nodeIds:
577-
(json['nodeIds'] as List<dynamic>?)
578-
?.map((e) => e as String)
579-
.toSet() ??
580-
{},
576+
nodeIds: (json['nodeIds'] as List<dynamic>?)
577+
?.map((e) => e as String)
578+
.toSet(),
581579
padding: padding,
582580
zIndex: json['zIndex'] as int? ?? -1,
583581
isVisible: json['isVisible'] as bool? ?? true,
584582
locked: json['locked'] as bool? ?? false,
585583
inputPorts: inputPorts,
586584
outputPorts: outputPorts,
587585
);
588-
return node;
589586
}
590587

591588
/// Converts this group node to a JSON map.

0 commit comments

Comments
 (0)