| 1 | using CascadeIDE.Features.Markdown.Application; |
| 2 | using CommunityToolkit.Mvvm.Input; |
| 3 | |
| 4 | namespace CascadeIDE.ViewModels; |
| 5 | |
| 6 | /// <summary>Экспорт Markdown.</summary> |
| 7 | public partial class MainWindowViewModel |
| 8 | { |
| 9 | [RelayCommand(CanExecute = nameof(CanExportExpandedMarkdown))] |
| 10 | private async Task ExportExpandedMarkdownAsync() |
| 11 | { |
| 12 | if (!CanExportExpandedMarkdown()) |
| 13 | return; |
| 14 | |
| 15 | var sourcePath = CurrentFilePath!; |
| 16 | var raw = EditorText ?? ""; |
| 17 | |
| 18 | var outPath = RequestSaveMarkdownFile is not null |
| 19 | ? await RequestSaveMarkdownFile(sourcePath).ConfigureAwait(true) |
| 20 | : ExpandedMarkdownDefaultExportPath.Resolve(sourcePath); |
| 21 | |
| 22 | if (string.IsNullOrWhiteSpace(outPath)) |
| 23 | return; |
| 24 | |
| 25 | var outcome = ExpandedMarkdownFileExportProjection.TryExpandAndWriteAllText(sourcePath, raw, outPath); |
| 26 | if (outcome.Ok) |
| 27 | { |
| 28 | if (RequestShowInfoAsync is not null) |
| 29 | await RequestShowInfoAsync("Export expanded Markdown", $"Saved: {outPath}"); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | if (RequestShowInfoAsync is not null) |
| 34 | await RequestShowInfoAsync("Export expanded Markdown failed", outcome.ErrorMessage ?? "Unknown error"); |
| 35 | } |
| 36 | |
| 37 | private bool CanExportExpandedMarkdown() => |
| 38 | IsMarkdownFile && !string.IsNullOrWhiteSpace(CurrentFilePath); |
| 39 | |
| 40 | } |
| 41 | |
| 42 | |