Skip to content

Commit 30a4e07

Browse files
committed
add format operation
1 parent e6f87f0 commit 30a4e07

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/XamlViewer/ViewModels/DataViewModel.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
using XamlTheme.Controls;
1515
using XamlViewer.Models;
1616
using XamlViewer.Utils;
17+
using Newtonsoft.Json;
18+
using Newtonsoft.Json.Linq;
1719

1820
namespace XamlViewer.ViewModels
1921
{
@@ -29,6 +31,7 @@ public class DataViewModel : BindableBase
2931
public DelegateCommand DelayArrivedCommand { get; private set; }
3032

3133
public DelegateCommand ClearCommand { get; private set; }
34+
public DelegateCommand FormatCommand { get; private set; }
3235
public DelegateCommand RequestCommand { get; private set; }
3336

3437
public DataViewModel(IContainerExtension container, IEventAggregator eventAggregator)
@@ -55,6 +58,7 @@ private void InitCommand()
5558
DelayArrivedCommand = new DelegateCommand(DelayArrived);
5659

5760
ClearCommand = new DelegateCommand(Clear);
61+
FormatCommand = new DelegateCommand(Format);
5862
RequestCommand = new DelegateCommand(Request);
5963
}
6064

@@ -99,6 +103,16 @@ private void Clear()
99103
_textEditor.Text = JsonString = string.Empty;
100104
}
101105

106+
private void Format()
107+
{
108+
var text = _textEditor.Text;
109+
if(string.IsNullOrWhiteSpace(text))
110+
return;
111+
112+
JsonString = JToken.Parse(text).ToString(Formatting.Indented);
113+
_textEditor.Text = JsonString;
114+
}
115+
102116
private async void Request()
103117
{
104118
try
@@ -111,7 +125,11 @@ private async void Request()
111125

112126
CanFetch = false;
113127

114-
JsonString = await HttpUtil.GetString(RestApi);
128+
var json = await HttpUtil.GetString(RestApi);
129+
if(!string.IsNullOrWhiteSpace(json))
130+
json = JToken.Parse(json).ToString(Formatting.Indented);
131+
132+
JsonString = json;
115133
_textEditor.Text = JsonString;
116134

117135
CanFetch = true;
@@ -120,6 +138,10 @@ private async void Request()
120138
{
121139
System.Diagnostics.Trace.TraceError("[ Http GetString ] " + XamlUtil.Common.Common.GetExceptionStringFormat(ex));
122140
}
141+
finally
142+
{
143+
CanFetch = true;
144+
}
123145
}
124146

125147
#endregion

src/XamlViewer/Views/DataControl.xaml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
<Grid Grid.Row="1" Margin="1,2">
3030
<Grid.ColumnDefinitions>
3131
<ColumnDefinition Width="Auto"/>
32-
<ColumnDefinition Width="Auto"/>
3332
<ColumnDefinition Width="*"/>
33+
<ColumnDefinition Width="Auto"/>
34+
<ColumnDefinition Width="Auto"/>
3435
<ColumnDefinition Width="Auto"/>
3536
</Grid.ColumnDefinitions>
3637
<ToggleButton Grid.Column="0" Width="30" ToolTip="Bind"
@@ -51,16 +52,9 @@
5152
</controls:DrawingIcon.HDPIDrawing>
5253
</controls:DrawingIcon>
5354
</ToggleButton>
54-
<Button Grid.Column="1" Width="30" ToolTip="Clear"
55-
IsEnabled="{Binding CanClear}"
56-
Command="{Binding ClearCommand}"
57-
Style="{StaticResource {x:Static themes:ResourceKeys.ToolbarButtonStyleKey}}">
58-
<Path Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}"
59-
Data="M1,6 L1,10 3,10 3,7 4,7 4,10 10,10 10,6 z M0,3 L4,3 4,4 1,4 1,5 10,5 10,4 7,4 7,3 11,3 11,5 11,6 11,11 0,11 0,6 0,5 z M3,0 L4,0 7,0 8,0 8,3 7,3 7,1 4,1 4,3 3,3 z"/>
60-
</Button>
61-
<Button Grid.Column="3" Width="30" ToolTip="Fetch"
62-
Command="{Binding RequestCommand}"
55+
<Button Grid.Column="2" Width="30" ToolTip="Fetch"
6356
IsEnabled="{Binding CanFetch}"
57+
Command="{Binding RequestCommand}"
6458
Style="{StaticResource {x:Static themes:ResourceKeys.ToolbarButtonStyleKey}}">
6559
<controls:DrawingIcon>
6660
<controls:DrawingIcon.Drawing>
@@ -72,6 +66,28 @@
7266
Geometry="M0,9 L0,11 11,11 11,9 10,9 10,10 1,10 1,9 z M4,0 L7,0 7,4 10,4 5.5,9 1,4 4,4 z" />
7367
</controls:DrawingIcon.HDPIDrawing>
7468
</controls:DrawingIcon>
69+
</Button>
70+
<Button Grid.Column="3" Width="30" ToolTip="Format"
71+
IsEnabled="{Binding CanClear}"
72+
Command="{Binding FormatCommand}"
73+
Style="{StaticResource {x:Static themes:ResourceKeys.ToolbarButtonStyleKey}}">
74+
<controls:DrawingIcon>
75+
<controls:DrawingIcon.Drawing>
76+
<GeometryDrawing Brush="{Binding Foreground,RelativeSource={RelativeSource AncestorType=Button},FallbackValue={x:Static swm:Brushes.Transparent}}"
77+
Geometry="M0,10 L11,10 11,11 0,11 z M5,8 L11,8 11,9 5,9 z M7,6 L11,6 11,7 7,7 z M7,4 L11,4 11,5 7,5 z M5,2 L11,2 11,3 5,3 z M0,2 L1,2 1,3 2,3 2,4 3,4 3,5 4,5 4,6 3,6 3,7 2,7 2,8 1,8 1,9 0,9 z M0,0 L11,0 11,1 0,1 z" />
78+
</controls:DrawingIcon.Drawing>
79+
<controls:DrawingIcon.HDPIDrawing>
80+
<GeometryDrawing Brush="{Binding Foreground,RelativeSource={RelativeSource AncestorType=Button},FallbackValue={x:Static swm:Brushes.Transparent}}"
81+
Geometry="M0,10 L11,10 11,11 0,11 z M5,8 L11,8 11,9 5,9 z M7,6 L11,6 11,7 7,7 z M7,4 L11,4 11,5 7,5 z M5,2 L11,2 11,3 5,3 z M0,2 L4,5.5 0,9 z M0,0 L11,0 11,1 0,1 z" />
82+
</controls:DrawingIcon.HDPIDrawing>
83+
</controls:DrawingIcon>
84+
</Button>
85+
<Button Grid.Column="4" Width="30" ToolTip="Clear"
86+
IsEnabled="{Binding CanClear}"
87+
Command="{Binding ClearCommand}"
88+
Style="{StaticResource {x:Static themes:ResourceKeys.ToolbarButtonStyleKey}}">
89+
<Path Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}"
90+
Data="M1,6 L1,10 3,10 3,7 4,7 4,10 10,10 10,6 z M0,3 L4,3 4,4 1,4 1,5 10,5 10,4 7,4 7,3 11,3 11,5 11,6 11,11 0,11 0,6 0,5 z M3,0 L4,0 7,0 8,0 8,3 7,3 7,1 4,1 4,3 3,3 z"/>
7591
</Button>
7692
</Grid>
7793

0 commit comments

Comments
 (0)