@inject IJSRuntime JS @inject ISettingsService SettingsService @inject IProjectService ProjectService
@if (showCreateDialog) { } @code { [Parameter] public string Text { get; set; } = string.Empty; [Parameter] public string? DefaultFileName { get; set; } [Parameter] public EventCallback OnFileCreated { get; set; } private bool showCreateDialog; private string newFileName = string.Empty; private int? _projectId; private string? _projectPath; private bool _isCopyDisabled; protected override async Task OnInitializedAsync() { _projectId = SettingsService.CurrentProjectId; if (_projectId.HasValue && _projectId > 0) { var project = await ProjectService.GetProjectAsync(_projectId.Value); _projectPath = project?.RootPath; } if (!string.IsNullOrEmpty(DefaultFileName)) newFileName = DefaultFileName; } private async Task CopyToClipboard() { if (string.IsNullOrEmpty(Text) || _isCopyDisabled) return; _isCopyDisabled = true; try { await JS.InvokeVoidAsync("navigator.clipboard.writeText", Text); } catch { // Fallback for older browsers try { var textArea = System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(Text); await JS.InvokeVoidAsync("eval", $"navigator.clipboard.writeText('{textArea}')"); } catch { } } finally { _isCopyDisabled = false; } } private void ShowCreateDialog() { newFileName = DefaultFileName ?? string.Empty; showCreateDialog = true; } private void CancelCreate() { showCreateDialog = false; newFileName = string.Empty; } private async Task CreateFile() { if (string.IsNullOrWhiteSpace(newFileName) || string.IsNullOrEmpty(Text)) return; try { var fileName = newFileName.Trim(); if (!fileName.EndsWith(".md", StringComparison.OrdinalIgnoreCase)) fileName += ".md"; string savePath; if (!string.IsNullOrEmpty(_projectPath) && Directory.Exists(_projectPath)) { savePath = Path.Combine(_projectPath, fileName); } else { savePath = Path.Combine(Directory.GetCurrentDirectory(), fileName); } if (File.Exists(savePath)) { var dir = Path.GetDirectoryName(savePath)!; var name = Path.GetFileNameWithoutExtension(savePath); var ext = Path.GetExtension(savePath); var counter = 1; do { savePath = Path.Combine(dir, $"{name}_{counter}{ext}"); counter++; } while (File.Exists(savePath)); } await File.WriteAllTextAsync(savePath, Text); showCreateDialog = false; await OnFileCreated.InvokeAsync(Path.GetFileName(savePath)); } catch { } } }