Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/features/workspace/data_grid_value_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class _DataGridValuePanelState extends material.State<DataGridValuePanel> {
bool _wordWrap = true;
bool _preserveCompact = false;
Timer? _debounceTimer;
bool _isProgrammaticUpdate = false;

static bool _isCompact(String input) {
final trimmed = input.trim();
Expand Down Expand Up @@ -83,7 +84,19 @@ class _DataGridValuePanelState extends material.State<DataGridValuePanel> {
_validateContent();
}

void _setControllerTextProgrammatically(String text) {
if (_controller.text == text) return;
_isProgrammaticUpdate = true;
try {
_controller.text = text;
} finally {
_isProgrammaticUpdate = false;
_debounceTimer?.cancel();
}
}

void _onTextChanged() {
if (_isProgrammaticUpdate) return;
_debounceTimer?.cancel();
_debounceTimer = Timer(const Duration(milliseconds: 150), () {
if (mounted) {
Expand All @@ -96,10 +109,9 @@ class _DataGridValuePanelState extends material.State<DataGridValuePanel> {
void didUpdateWidget(covariant DataGridValuePanel oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.cellValue != widget.cellValue) {
_debounceTimer?.cancel();
_initialDetectedLanguage = _detectLanguage(widget.cellValue);
_preserveCompact = _isCompact(widget.cellValue);
_controller.text = _formatInitialValue(widget.cellValue);
_setControllerTextProgrammatically(_formatInitialValue(widget.cellValue));
_validateContent();
}
}
Expand Down Expand Up @@ -187,11 +199,13 @@ class _DataGridValuePanelState extends material.State<DataGridValuePanel> {
try {
final parsed = jsonDecode(_controller.text);
final pretty = const JsonEncoder.withIndent(' ').convert(parsed);
setState(() => _controller.text = pretty);
setState(() => _setControllerTextProgrammatically(pretty));
_validateContent();
} catch (_) {}
} else if (lang == ValuePanelLanguage.xml) {
final pretty = XmlHtmlFormatter.format(_controller.text);
setState(() => _controller.text = pretty);
setState(() => _setControllerTextProgrammatically(pretty));
_validateContent();
}
}

Expand All @@ -202,16 +216,18 @@ class _DataGridValuePanelState extends material.State<DataGridValuePanel> {
final parsed = jsonDecode(_controller.text);
final compact = jsonEncode(parsed);
setState(() {
_controller.text = compact;
_setControllerTextProgrammatically(compact);
_preserveCompact = true;
});
_validateContent();
} catch (_) {}
} else if (lang == ValuePanelLanguage.xml) {
final compact = XmlHtmlFormatter.minify(_controller.text);
setState(() {
_controller.text = compact;
_setControllerTextProgrammatically(compact);
_preserveCompact = true;
});
_validateContent();
}
}

Expand Down
37 changes: 37 additions & 0 deletions test/features/workspace/data_grid_value_panel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,42 @@ void main() {
final button = tester.widget<material.ElevatedButton>(find.byType(material.ElevatedButton));
expect(button.onPressed, isNull);
});

testWidgets('didUpdateWidget validates immediately without scheduling redundant debounce', (tester) async {
await tester.pumpWidget(
queryaThemeTestShell(
child: material.Material(
child: DataGridValuePanel(
columnName: 'json_col',
cellValue: '{"valid":true}',
rowIndex: 0,
onClose: () {},
),
),
),
);
await tester.pumpAndSettle();
expect(find.textContaining('Invalid JSON'), findsNothing);

// Change cellValue to invalid JSON via didUpdateWidget
await tester.pumpWidget(
queryaThemeTestShell(
child: material.Material(
child: DataGridValuePanel(
columnName: 'json_col',
cellValue: '{"invalid":,}',
rowIndex: 1,
onClose: () {},
),
),
),
);
// Synchronous validation occurred immediately
await tester.pump();
expect(find.textContaining('Invalid JSON'), findsOneWidget);

// Verify no pending timer callback after pump
expect(tester.binding.hasScheduledFrame, isFalse);
});
});
}
Loading