11import 'dart:async' show unawaited;
22import 'dart:convert' ;
3+ import 'package:flutter/foundation.dart' show compute, visibleForTesting;
34import 'package:flutter/material.dart' as material;
45import 'package:flutter/services.dart' ;
56import 'package:querya_desktop/core/editor/querya_code_editor.dart' ;
@@ -8,6 +9,42 @@ import 'package:querya_desktop/features/workspace/xml_html_formatter.dart';
89import 'package:querya_desktop/shared/widgets/widgets.dart' ;
910import 'package:querya_desktop/core/ui/querya_control_tokens.dart' ;
1011
12+ /// Text of at least this many UTF-16 units (about 256 KB) is parsed, formatted
13+ /// and minified on a worker isolate: `jsonDecode` and the XML formatter on a
14+ /// multi-megabyte cell would hold the UI thread for whole frames.
15+ @visibleForTesting
16+ int inspectorIsolateThreshold = 256 * 1024 ;
17+
18+ String _prettyJson (String text) =>
19+ const JsonEncoder .withIndent (' ' ).convert (jsonDecode (text));
20+
21+ String _compactJson (String text) => jsonEncode (jsonDecode (text));
22+
23+ String _prettyXml (String text) => XmlHtmlFormatter .format (text);
24+
25+ bool _looksLikeJson (String trimmed) =>
26+ (trimmed.startsWith ('{' ) && trimmed.endsWith ('}' )) ||
27+ (trimmed.startsWith ('[' ) && trimmed.endsWith (']' ));
28+
29+ bool _looksLikeXml (String trimmed) =>
30+ trimmed.startsWith ('<' ) && trimmed.endsWith ('>' );
31+
32+ /// Whether [trimmed] is valid JSON or valid XML (top-level so [compute] can run it).
33+ ({bool json, bool xml}) _detectKind (String trimmed) {
34+ if (_looksLikeJson (trimmed)) {
35+ try {
36+ jsonDecode (trimmed);
37+ return (json: true , xml: false );
38+ } catch (_) {
39+ return (json: false , xml: false );
40+ }
41+ }
42+ if (_looksLikeXml (trimmed)) {
43+ return (json: false , xml: XmlHtmlFormatter .validate (trimmed) == null );
44+ }
45+ return (json: false , xml: false );
46+ }
47+
1148/// Opens a rich modal inspector for viewing and editing large text, JSON, XML, or BLOB values.
1249///
1350/// **Apply** and **Ctrl/Cmd+Enter** return the edited string for local staging
@@ -100,41 +137,48 @@ class _GridCellInspectorDialogState
100137 super .dispose ();
101138 }
102139
103- void _formatJson () {
104- try {
105- final parsed = jsonDecode (_controller.text);
106- final pretty = const JsonEncoder .withIndent (' ' ).convert (parsed);
107- setState (() {
108- _isNull = false ;
109- _controller.text = pretty;
110- });
111- } catch (_) {
112- // Not valid JSON, keep as is
113- }
114- }
140+ bool _busy = false ;
115141
116- void _minifyJson () {
117- try {
118- final parsed = jsonDecode (_controller.text);
119- final compact = jsonEncode (parsed);
120- setState (() {
121- _isNull = false ;
122- _controller.text = compact;
123- });
124- } catch (_) {
125- // Not valid JSON, keep as is
142+ /// Replaces the text with `transform(text)` ; a text that does not parse is
143+ /// left as it is. Past [inspectorIsolateThreshold] the work runs on a worker
144+ /// isolate, with the buttons held back meanwhile; an edit made while it ran
145+ /// wins over the result.
146+ Future <void > _rewrite (String Function (String ) transform) async {
147+ if (_busy) return ;
148+ final text = _controller.text;
149+ String result;
150+ if (text.length < inspectorIsolateThreshold) {
151+ try {
152+ result = transform (text);
153+ } catch (_) {
154+ return ;
155+ }
156+ } else {
157+ setState (() => _busy = true );
158+ try {
159+ result = await compute (transform, text);
160+ } catch (_) {
161+ if (mounted) setState (() => _busy = false );
162+ return ;
163+ }
164+ if (! mounted) return ;
165+ _busy = false ;
166+ if (! identical (text, _controller.text) && text != _controller.text) {
167+ setState (() {});
168+ return ;
169+ }
126170 }
171+ setState (() {
172+ _isNull = false ;
173+ _controller.text = result;
174+ });
127175 }
128176
129- void _formatXml () {
130- try {
131- final pretty = XmlHtmlFormatter .format (_controller.text);
132- setState (() {
133- _isNull = false ;
134- _controller.text = pretty;
135- });
136- } catch (_) {}
137- }
177+ void _formatJson () => unawaited (_rewrite (_prettyJson));
178+
179+ void _minifyJson () => unawaited (_rewrite (_compactJson));
180+
181+ void _formatXml () => unawaited (_rewrite (_prettyXml));
138182
139183 void _formatHex () {
140184 var raw = _controller.text.trim ();
@@ -171,27 +215,53 @@ class _GridCellInspectorDialogState
171215
172216 // build() asks for both on every rebuild. Parsing a large cell each time
173217 // janks, so the answer is kept for the text it was computed for (the
174- // controller hands out the same String until the text changes).
218+ // controller hands out the same String until the text changes). Text past
219+ // [inspectorIsolateThreshold] is checked on a worker isolate: the buttons
220+ // appear when it answers.
175221 String ? _kindText;
176222 var _kindIsJson = false ;
177223 var _kindIsXml = false ;
224+ var _detecting = false ;
178225
179226 void _refreshKind () {
180227 final raw = _controller.text;
181228 if (identical (raw, _kindText)) return ;
182229 _kindText = raw;
183- final text = raw.trim ();
184230 _kindIsJson = false ;
185231 _kindIsXml = false ;
186- if ((text.startsWith ('{' ) && text.endsWith ('}' )) ||
187- (text.startsWith ('[' ) && text.endsWith (']' ))) {
188- try {
189- jsonDecode (text);
190- _kindIsJson = true ;
191- } catch (_) {}
192- } else if (text.startsWith ('<' ) && text.endsWith ('>' )) {
193- _kindIsXml = XmlHtmlFormatter .validate (text) == null ;
232+ final text = raw.trim ();
233+ if (! _looksLikeJson (text) && ! _looksLikeXml (text)) return ;
234+ if (raw.length >= inspectorIsolateThreshold) {
235+ _detectInBackground ();
236+ return ;
194237 }
238+ final kind = _detectKind (text);
239+ _kindIsJson = kind.json;
240+ _kindIsXml = kind.xml;
241+ }
242+
243+ void _detectInBackground () {
244+ if (_detecting) return ; // the running check re-reads the latest text
245+ final raw = _kindText;
246+ if (raw == null ) return ;
247+ _detecting = true ;
248+ unawaited (compute (_detectKind, raw.trim ()).then ((kind) {
249+ _detecting = false ;
250+ if (! mounted) return ;
251+ if (identical (raw, _kindText)) {
252+ if (kind.json != _kindIsJson || kind.xml != _kindIsXml) {
253+ setState (() {
254+ _kindIsJson = kind.json;
255+ _kindIsXml = kind.xml;
256+ });
257+ }
258+ } else if (_kindText != null &&
259+ _kindText! .length >= inspectorIsolateThreshold) {
260+ _detectInBackground (); // edited meanwhile: check the new text
261+ }
262+ }, onError: (Object _) {
263+ _detecting = false ;
264+ }));
195265 }
196266
197267 bool _isJson () {
@@ -312,14 +382,15 @@ class _GridCellInspectorDialogState
312382 QueryaButton .ghost (
313383 label: 'Format JSON' ,
314384 icon: material.Icons .format_align_left_rounded,
315- onPressed: _formatJson,
385+ loading: _busy,
386+ onPressed: _busy ? null : _formatJson,
316387 size: QueryaControlSize .sm,
317388 ),
318389 const Gap (6 ),
319390 QueryaButton .ghost (
320391 label: 'Minify' ,
321392 icon: material.Icons .compress_rounded,
322- onPressed: _minifyJson,
393+ onPressed: _busy ? null : _minifyJson,
323394 size: QueryaControlSize .sm,
324395 ),
325396 const Gap (6 ),
@@ -328,7 +399,8 @@ class _GridCellInspectorDialogState
328399 QueryaButton .ghost (
329400 label: 'Format XML' ,
330401 icon: material.Icons .code_rounded,
331- onPressed: _formatXml,
402+ loading: _busy,
403+ onPressed: _busy ? null : _formatXml,
332404 size: QueryaControlSize .sm,
333405 ),
334406 const Gap (6 ),
0 commit comments