[chore:] better file- and folder creating
This commit is contained in:
+5
-1
@@ -327,8 +327,12 @@
|
||||
<Grid ColumnDefinitions="300,*">
|
||||
<Border Grid.Column="0" Background="#FFF" BorderBrush="#DDD" BorderThickness="0,0,1,0">
|
||||
<StackPanel>
|
||||
<Button Content="+ Hinzufügen" Margin="10" x:Name="BtnWikiAddFile"
|
||||
<StackPanel Spacing="10" Orientation="Horizontal" Margin="10">
|
||||
<Button Content="+ Datei" x:Name="BtnWikiAddFile"
|
||||
Click="BtnWikiAddFile_OnClick" />
|
||||
<Button Content="+ Ordner" x:Name="BtnWikiAddFolder"
|
||||
Click="BtnWikiAddFolder_OnClick" />
|
||||
</StackPanel>
|
||||
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
|
||||
<TreeView Name="NavTree" Margin="10" />
|
||||
</ScrollViewer>
|
||||
|
||||
+100
-5
@@ -260,12 +260,20 @@ public partial class MainWindow : Window
|
||||
}
|
||||
}
|
||||
|
||||
public void PopulateNavTree()
|
||||
public void PopulateNavTree(string? expandToPath = null, string? selectPath = null)
|
||||
{
|
||||
var roots = _wikiService.GetRootItems();
|
||||
var nodes = new List<TreeViewItem>();
|
||||
foreach (var r in roots) nodes.Add(BuildNode(r));
|
||||
NavTree.ItemsSource = nodes;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(expandToPath))
|
||||
ExpandAndFindNode(nodes, expandToPath, out _);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(selectPath) &&
|
||||
ExpandAndFindNode(nodes, selectPath, out var selectedNode) &&
|
||||
selectedNode != null)
|
||||
NavTree.SelectedItem = selectedNode;
|
||||
}
|
||||
|
||||
private TreeViewItem BuildNode(WikiItem item)
|
||||
@@ -278,6 +286,63 @@ public partial class MainWindow : Window
|
||||
return node;
|
||||
}
|
||||
|
||||
private string GetSelectedWikiTargetDirectory()
|
||||
{
|
||||
var wikiRoot = Global._instance.wiki_storage_path;
|
||||
if (NavTree.SelectedItem is TreeViewItem treeItem && treeItem.Tag is WikiItem selectedItem)
|
||||
{
|
||||
if (selectedItem.IsFolder) return selectedItem.Path;
|
||||
|
||||
var parentDir = Path.GetDirectoryName(selectedItem.Path);
|
||||
if (!string.IsNullOrWhiteSpace(parentDir)) return parentDir;
|
||||
}
|
||||
|
||||
return wikiRoot;
|
||||
}
|
||||
|
||||
private static bool PathsEqual(string left, string right)
|
||||
{
|
||||
var normalizedLeft = Path.GetFullPath(left)
|
||||
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
var normalizedRight = Path.GetFullPath(right)
|
||||
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
|
||||
var comparison = OperatingSystem.IsWindows()
|
||||
? StringComparison.OrdinalIgnoreCase
|
||||
: StringComparison.Ordinal;
|
||||
return string.Equals(normalizedLeft, normalizedRight, comparison);
|
||||
}
|
||||
|
||||
private bool ExpandAndFindNode(IEnumerable<TreeViewItem> nodes, string targetPath, out TreeViewItem? foundNode)
|
||||
{
|
||||
foreach (var node in nodes)
|
||||
if (TryExpandToPath(node, targetPath, out foundNode))
|
||||
return true;
|
||||
|
||||
foundNode = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryExpandToPath(TreeViewItem node, string targetPath, out TreeViewItem? foundNode)
|
||||
{
|
||||
if (node.Tag is WikiItem item && PathsEqual(item.Path, targetPath))
|
||||
{
|
||||
foundNode = node;
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var childRaw in node.Items)
|
||||
if (childRaw is TreeViewItem childNode &&
|
||||
TryExpandToPath(childNode, targetPath, out foundNode))
|
||||
{
|
||||
node.IsExpanded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
foundNode = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void OpenFolderButton_Click(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var path = Global._instance.wiki_storage_path;
|
||||
@@ -991,15 +1056,21 @@ public partial class MainWindow : Window
|
||||
private async void BtnWikiAddFile_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var result = await NamingWindow.Show(this);
|
||||
if (!result.EndsWith(".md")) result += ".md";
|
||||
if (string.IsNullOrWhiteSpace(result)) return;
|
||||
|
||||
result = result.Trim();
|
||||
if (!result.EndsWith(".md", StringComparison.OrdinalIgnoreCase)) result += ".md";
|
||||
|
||||
if (!Directory.Exists(Global._instance.wiki_storage_path))
|
||||
Directory.CreateDirectory(Global._instance.wiki_storage_path);
|
||||
|
||||
if (result != null)
|
||||
try
|
||||
{
|
||||
File.WriteAllText(Path.Combine(Global._instance.wiki_storage_path, result), "");
|
||||
PopulateNavTree();
|
||||
var targetDirectory = GetSelectedWikiTargetDirectory();
|
||||
Directory.CreateDirectory(targetDirectory);
|
||||
var newFilePath = Path.Combine(targetDirectory, result);
|
||||
File.WriteAllText(newFilePath, "");
|
||||
PopulateNavTree(targetDirectory, newFilePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1007,6 +1078,30 @@ public partial class MainWindow : Window
|
||||
}
|
||||
}
|
||||
|
||||
private async void BtnWikiAddFolder_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var result = await NamingWindow.Show(this);
|
||||
if (string.IsNullOrWhiteSpace(result)) return;
|
||||
|
||||
result = result.Trim();
|
||||
|
||||
if (!Directory.Exists(Global._instance.wiki_storage_path))
|
||||
Directory.CreateDirectory(Global._instance.wiki_storage_path);
|
||||
|
||||
try
|
||||
{
|
||||
var targetDirectory = GetSelectedWikiTargetDirectory();
|
||||
var newFolderPath = Path.Combine(targetDirectory, result);
|
||||
Directory.CreateDirectory(newFolderPath);
|
||||
PopulateNavTree(targetDirectory, newFolderPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(this, ex.StackTrace, "Fehler");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async void MnIAdSetRename_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
if (LstCustomerAdressSets.SelectedItems.Count > 0)
|
||||
|
||||
Reference in New Issue
Block a user