@using LANCommander.Server.Extensions; @foreach (var file in OpenFiles) { } @code { [Parameter] public string WorkingDirectory { get; set; } StandaloneCodeEditor? Editor; IEnumerable Files; HashSet FileTree { get; set; } = new HashSet(); string CurrentFile = ""; List OpenFiles = new List(); string ActiveFile; private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor) { return new StandaloneEditorConstructionOptions { AutomaticLayout = true, Theme = "vs-dark" }; } protected override async Task OnParametersSetAsync() { var root = new FileTreeNode { Name = "/", FullName = WorkingDirectory, IsExpanded = true }; root.PopulateChildren(WorkingDirectory); foreach (var child in root.Children) { FileTree.Add(child); } } private void OpenFile(FileTreeNode node) { if (!OpenFiles.Contains(node.FullName)) OpenFiles.Add(node.FullName); ActiveFile = node.FullName; StateHasChanged(); } private void OnTabClose(string key) { OpenFiles.Remove(key); StateHasChanged(); } public class FileTreeNode { public string Name { get; set; } public string FullName { get; set; } public bool IsExpanded { get; set; } = false; public bool IsDirectory { get; set; } = false; public bool HasChildren => Children != null && Children.Count > 0; public HashSet Children { get; set; } = new HashSet(); public void PopulateChildren(string path) { if (Directory.Exists(path)) { try { foreach (var file in Directory.GetFiles(path)) { var fileInfo = new FileInfo(file); Children.Add(new FileTreeNode { Name = fileInfo.Name, FullName = fileInfo.FullName, IsDirectory = false }); } } catch { } try { foreach (var directory in Directory.GetDirectories(path)) { var directoryInfo = new DirectoryInfo(directory); var child = new FileTreeNode { Name = directoryInfo.Name, FullName = directoryInfo.FullName, IsDirectory = true }; child.PopulateChildren(directoryInfo.FullName); Children.Add(child); } } catch { } } } public void PopulateChildren(IEnumerable paths) { var childPaths = paths.Where(p => p.StartsWith(FullName) && p.EndsWith(Path.PathSeparator)); var directChildren = childPaths.Where(p => p != FullName && p.Substring(FullName.Length + 1).TrimEnd(Path.PathSeparator).Split(Path.PathSeparator).Length == 1); foreach (var directChild in directChildren) { var child = new FileTreeNode() { FullName = directChild, Name = directChild.Substring(FullName.Length).TrimEnd(Path.PathSeparator) }; child.PopulateChildren(paths); Children.Add(child); } } } }