[chore:] better file- and folder creating
This commit is contained in:
+6
-2
@@ -327,8 +327,12 @@
|
|||||||
<Grid ColumnDefinitions="300,*">
|
<Grid ColumnDefinitions="300,*">
|
||||||
<Border Grid.Column="0" Background="#FFF" BorderBrush="#DDD" BorderThickness="0,0,1,0">
|
<Border Grid.Column="0" Background="#FFF" BorderBrush="#DDD" BorderThickness="0,0,1,0">
|
||||||
<StackPanel>
|
<StackPanel>
|
||||||
<Button Content="+ Hinzufügen" Margin="10" x:Name="BtnWikiAddFile"
|
<StackPanel Spacing="10" Orientation="Horizontal" Margin="10">
|
||||||
Click="BtnWikiAddFile_OnClick" />
|
<Button Content="+ Datei" x:Name="BtnWikiAddFile"
|
||||||
|
Click="BtnWikiAddFile_OnClick" />
|
||||||
|
<Button Content="+ Ordner" x:Name="BtnWikiAddFolder"
|
||||||
|
Click="BtnWikiAddFolder_OnClick" />
|
||||||
|
</StackPanel>
|
||||||
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
|
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
|
||||||
<TreeView Name="NavTree" Margin="10" />
|
<TreeView Name="NavTree" Margin="10" />
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
|
|||||||
+108
-13
@@ -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 roots = _wikiService.GetRootItems();
|
||||||
var nodes = new List<TreeViewItem>();
|
var nodes = new List<TreeViewItem>();
|
||||||
foreach (var r in roots) nodes.Add(BuildNode(r));
|
foreach (var r in roots) nodes.Add(BuildNode(r));
|
||||||
NavTree.ItemsSource = nodes;
|
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)
|
private TreeViewItem BuildNode(WikiItem item)
|
||||||
@@ -278,6 +286,63 @@ public partial class MainWindow : Window
|
|||||||
return node;
|
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)
|
private void OpenFolderButton_Click(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var path = Global._instance.wiki_storage_path;
|
var path = Global._instance.wiki_storage_path;
|
||||||
@@ -991,21 +1056,51 @@ public partial class MainWindow : Window
|
|||||||
private async void BtnWikiAddFile_OnClick(object? sender, RoutedEventArgs e)
|
private async void BtnWikiAddFile_OnClick(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var result = await NamingWindow.Show(this);
|
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))
|
if (!Directory.Exists(Global._instance.wiki_storage_path))
|
||||||
Directory.CreateDirectory(Global._instance.wiki_storage_path);
|
Directory.CreateDirectory(Global._instance.wiki_storage_path);
|
||||||
|
|
||||||
if (result != null)
|
try
|
||||||
try
|
{
|
||||||
{
|
var targetDirectory = GetSelectedWikiTargetDirectory();
|
||||||
File.WriteAllText(Path.Combine(Global._instance.wiki_storage_path, result), "");
|
Directory.CreateDirectory(targetDirectory);
|
||||||
PopulateNavTree();
|
var newFilePath = Path.Combine(targetDirectory, result);
|
||||||
}
|
File.WriteAllText(newFilePath, "");
|
||||||
catch (Exception ex)
|
PopulateNavTree(targetDirectory, newFilePath);
|
||||||
{
|
}
|
||||||
MessageBox.Show(this, ex.StackTrace, "Fehler");
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
|
MessageBox.Show(this, ex.StackTrace, "Fehler");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
private async void MnIAdSetRename_OnClick(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user