Skip to content

Commit 9645e73

Browse files
committed
Improve JSON output wrapping and selection behavior
Wrap the formatted JSON output in a ScrollViewer and enable TextWrapping on read-only TextBoxes to support long lines and scrolling. Fix selection logic by using the current TextBox.Text (not the original text variable), handle null JSON string values safely, and trim leading spaces when selecting value portions. Update the context menu copy action to prefer the selected text over the full TextBox content. These changes address selection bugs and improve usability for large or wrapped JSON values.
1 parent f910a0f commit 9645e73

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

Pages/JsonFormatPage.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
<!-- Collapsible formatted JSON display -->
4242
<Border Grid.Row="4" BorderBrush="LightGray" BorderThickness="1" Padding="4">
4343
<Grid>
44-
<StackPanel x:Name="JsonOutputPanel" />
44+
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
45+
<StackPanel x:Name="JsonOutputPanel" />
46+
</ScrollViewer>
4547
<Border x:Name="LoadingOverlay" Background="#80000000" Visibility="Collapsed">
4648
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
4749
<TextBlock Text="处理中..." FontSize="16" Foreground="White" HorizontalAlignment="Center" />

Pages/JsonFormatPage.xaml.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ private UIElement CreateVisualForElement(string name, JsonElement el, ref int el
334334
case JsonValueKind.String:
335335
{
336336
elementCount++;
337-
var text = $"{name}: \"{el.GetString()}\"";
337+
var stringValue = el.GetString() ?? string.Empty;
338+
var text = $"{name}: \"{stringValue}\"";
338339
var tb = new TextBox
339340
{
340341
Text = text,
@@ -354,13 +355,14 @@ private UIElement CreateVisualForElement(string name, JsonElement el, ref int el
354355
{
355356
try
356357
{
357-
var colonIndex = text.IndexOf(':');
358+
var currentText = textBox.Text;
359+
var colonIndex = currentText.IndexOf(':');
358360
if (colonIndex >= 0)
359361
{
360-
var firstQuote = text.IndexOf('"', colonIndex);
362+
var firstQuote = currentText.IndexOf('"', colonIndex);
361363
if (firstQuote >= 0)
362364
{
363-
var lastQuote = text.LastIndexOf('"');
365+
var lastQuote = currentText.LastIndexOf('"');
364366
if (lastQuote > firstQuote)
365367
{
366368
textBox.Select(firstQuote + 1, lastQuote - firstQuote - 1);
@@ -389,6 +391,7 @@ private UIElement CreateVisualForElement(string name, JsonElement el, ref int el
389391
IsReadOnly = true,
390392
BorderThickness = new Thickness(0),
391393
Background = System.Windows.Media.Brushes.Transparent,
394+
TextWrapping = TextWrapping.Wrap,
392395
Margin = new Thickness(4,2,0,2),
393396
FontFamily = new System.Windows.Media.FontFamily("Consolas"),
394397
CaretBrush = System.Windows.Media.Brushes.Transparent,
@@ -401,11 +404,17 @@ private UIElement CreateVisualForElement(string name, JsonElement el, ref int el
401404
{
402405
try
403406
{
404-
var colonIndex = text.IndexOf(':');
405-
if (colonIndex >= 0 && colonIndex < text.Length - 1)
407+
var currentText = textBox.Text;
408+
var colonIndex = currentText.IndexOf(':');
409+
if (colonIndex >= 0 && colonIndex < currentText.Length - 1)
406410
{
407411
var valueStart = colonIndex + 1;
408-
textBox.Select(valueStart, text.Length - valueStart);
412+
// Trim leading spaces
413+
while (valueStart < currentText.Length && currentText[valueStart] == ' ')
414+
{
415+
valueStart++;
416+
}
417+
textBox.Select(valueStart, currentText.Length - valueStart);
409418
}
410419
}
411420
catch
@@ -426,6 +435,7 @@ private UIElement CreateVisualForElement(string name, JsonElement el, ref int el
426435
IsReadOnly = true,
427436
BorderThickness = new Thickness(0),
428437
Background = System.Windows.Media.Brushes.Transparent,
438+
TextWrapping = TextWrapping.Wrap,
429439
Margin = new Thickness(4,2,0,2),
430440
FontFamily = new System.Windows.Media.FontFamily("Consolas"),
431441
CaretBrush = System.Windows.Media.Brushes.Transparent,
@@ -438,11 +448,17 @@ private UIElement CreateVisualForElement(string name, JsonElement el, ref int el
438448
{
439449
try
440450
{
441-
var colonIndex = text.IndexOf(':');
442-
if (colonIndex >= 0 && colonIndex < text.Length - 1)
451+
var currentText = textBox2.Text;
452+
var colonIndex = currentText.IndexOf(':');
453+
if (colonIndex >= 0 && colonIndex < currentText.Length - 1)
443454
{
444455
var valueStart = colonIndex + 1;
445-
textBox2.Select(valueStart, text.Length - valueStart);
456+
// Trim leading spaces
457+
while (valueStart < currentText.Length && currentText[valueStart] == ' ')
458+
{
459+
valueStart++;
460+
}
461+
textBox2.Select(valueStart, currentText.Length - valueStart);
446462
}
447463
}
448464
catch
@@ -474,7 +490,11 @@ private void AddContextMenu(TextBox tb)
474490
{
475491
var contextMenu = new ContextMenu();
476492
var copyItem = new MenuItem { Header = Strings.Copy };
477-
copyItem.Click += (s, e) => ClipboardHelper.CopyWithFeedback(tb.Text, null);
493+
copyItem.Click += (s, e) =>
494+
{
495+
var textToCopy = !string.IsNullOrEmpty(tb.SelectedText) ? tb.SelectedText : tb.Text;
496+
ClipboardHelper.CopyWithFeedback(textToCopy, null);
497+
};
478498
contextMenu.Items.Add(copyItem);
479499
tb.ContextMenu = contextMenu;
480500
}

0 commit comments

Comments
 (0)