Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
User must manually enable the text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberboss committed Jun 28, 2020
1 parent d990cdf commit 290cf29
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
54 changes: 25 additions & 29 deletions src/Tgstation.Server.ControlPanel/ViewModels/StaticFileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ public enum StaticFileCommand
BrowseUpload,
BrowseDownload,
Delete,
Download
}
static class Chars
{
public static char NUL = (char)0; // Null char
public static char BS = (char)8; // Back Space
public static char CR = (char)13; // Carriage Return
public static char SUB = (char)26; // Substitute
Download,
EnableEditor
}

public bool Refreshing
Expand All @@ -43,13 +37,15 @@ public bool Refreshing
{
this.RaiseAndSetIfChanged(ref refreshing, value);
this.RaisePropertyChanged(nameof(Icon));
EditorEnabled = false;
Refresh.Recheck();
Write.Recheck();
Delete.Recheck();
Upload.Recheck();
Download.Recheck();
BrowseDownload.Recheck();
BrowseUpload.Recheck();
EnableEditor.Recheck();
}
}

Expand Down Expand Up @@ -77,6 +73,15 @@ public bool Denied
}
}

public bool EditorEnabled
{
get => editorEnabled;
set
{
this.RaiseAndSetIfChanged(ref editorEnabled, value);
}
}

public string TextBlob
{
get => textBlob;
Expand Down Expand Up @@ -119,15 +124,17 @@ public ConfigurationFile ConfigurationFile
set
{
this.RaiseAndSetIfChanged(ref configurationFile, value);
if (configurationFile.Content != null && !IsBinary(configurationFile.Content))
try
{
TextBlob = Encoding.UTF8.GetString(configurationFile.Content);
else
TextBlob = null;
}
catch { }
textChanged = false;
Write.Recheck();
Delete.Recheck();
Download.Recheck();
Upload.Recheck();
EnableEditor.Recheck();
}
}

Expand All @@ -139,6 +146,7 @@ public ConfigurationFile ConfigurationFile
public EnumCommand<StaticFileCommand> Download { get; }
public EnumCommand<StaticFileCommand> BrowseUpload { get; }
public EnumCommand<StaticFileCommand> BrowseDownload { get; }
public EnumCommand<StaticFileCommand> EnableEditor { get; }

public string Title => System.IO.Path.GetFileName(Path);

Expand All @@ -162,27 +170,10 @@ public ConfigurationFile ConfigurationFile
bool denied;
bool firstLoad;
bool textChanged;
bool editorEnabled;

bool confirmingDelete;

static bool IsBinary(byte[] data)
{
var length = data.Length;
if (length == 0)
return false;

using (var stream = new StreamReader(new MemoryStream(data)))
{
int ch;
while ((ch = stream.Read()) != -1)
{
if ((ch > Chars.NUL && ch < Chars.BS) || (ch > Chars.CR && ch < Chars.SUB))
return true;
}
}
return false;
}

public StaticFileViewModel(PageContextViewModel pageContext, IConfigurationClient configurationClient, IInstanceUserRightsProvider rightsProvider, IStaticNode parent, string path)
{
this.pageContext = pageContext ?? throw new ArgumentNullException(nameof(pageContext));
Expand All @@ -199,6 +190,7 @@ public StaticFileViewModel(PageContextViewModel pageContext, IConfigurationClien
Download = new EnumCommand<StaticFileCommand>(StaticFileCommand.Download, this);
BrowseDownload = new EnumCommand<StaticFileCommand>(StaticFileCommand.BrowseDownload, this);
BrowseUpload = new EnumCommand<StaticFileCommand>(StaticFileCommand.BrowseUpload, this);
EnableEditor = new EnumCommand<StaticFileCommand>(StaticFileCommand.EnableEditor, this);

rightsProvider.OnUpdated += (a, b) =>
{
Expand Down Expand Up @@ -278,6 +270,7 @@ public bool CanRunCommand(StaticFileCommand command)
}
case StaticFileCommand.BrowseDownload:
case StaticFileCommand.BrowseUpload:
case StaticFileCommand.EnableEditor:
return !Refreshing;
default:
throw new ArgumentOutOfRangeException(nameof(command), command, "Invalid command!");
Expand Down Expand Up @@ -372,6 +365,9 @@ async void ResetDelete()
};
UploadPath = (await ofd.ShowAsync(Application.Current.MainWindow).ConfigureAwait(true))[0] ?? UploadPath;
break;
case StaticFileCommand.EnableEditor:
EditorEnabled = true;
break;
default:
throw new ArgumentOutOfRangeException(nameof(command), command, "Invalid command!");
}
Expand Down
5 changes: 3 additions & 2 deletions src/Tgstation.Server.ControlPanel/Views/Pages/StaticFile.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@
<Rectangle HorizontalAlignment="Stretch" Fill="#A0A0A0" Height="1" Margin="0,5,0,0"/>
<Rectangle HorizontalAlignment="Stretch" Fill="#FFFFFF" Height="1" Margin="0,0,0,5"/>
<TextBlock Text="Edit Text" FontSize="20" />
<TextBox TextWrapping="Wrap" AcceptsReturn="True" Text="{Binding PageContext.ActiveObject.TextBlob}" MinHeight="500"/>
<Button Content="Enable Text Editor" Command="{Binding PageContext.ActiveObject.EnableEditor}" IsVisible="{Binding !PageContext.ActiveObject.EditorEnabled}"/>
<TextBox TextWrapping="Wrap" AcceptsReturn="True" Text="{Binding PageContext.ActiveObject.TextBlob}" MinHeight="500" IsVisible="{Binding PageContext.ActiveObject.EditorEnabled}"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>
<Button Content="Save" Command="{Binding PageContext.ActiveObject.Write}" Grid.Column="1" Margin="0,5,0,0"/>
<Button Content="Save" Command="{Binding PageContext.ActiveObject.Write}" Grid.Column="1" Margin="0,5,0,0" IsVisible="{Binding PageContext.ActiveObject.EditorEnabled}"/>
</Grid>
</StackPanel>
<Rectangle HorizontalAlignment="Stretch" Fill="#A0A0A0" Height="1" Margin="0,5,0,0"/>
Expand Down

0 comments on commit 290cf29

Please sign in to comment.