diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
index 2eb4f55..80a3334 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
@@ -111,6 +111,13 @@
+
+
+
+
+
+
+
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs
index 3875826..36a00f2 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs
@@ -111,6 +111,10 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
OpenImageCommand = new DelegateCommand(OpenImage, () => !IsCapturing);
CaptureStandardVideoImageCommand = new DelegateCommand(async () => await CaptureStandardVideoImageAsync(), () => !IsCapturing);
InitializeAgentCommands();
+ PropertyChanged += (_, e) =>
+ {
+ if (e.PropertyName is nameof(IsAgentBusy) or nameof(IsCapturing)) RaisePropertyChanged(nameof(CanTransferSteps));
+ };
}
///
@@ -286,6 +290,7 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
foreach (var warning in saved.Warnings) Warnings.Add(warning);
SaveStatus = $"已保存 {Path.GetFileName(_path)}";
+ StepTransferStatus = "";
_onSaved?.Invoke(Path.GetFileName(_path));
}
catch (Exception ex)
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs
new file mode 100644
index 0000000..e48c9f4
--- /dev/null
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs
@@ -0,0 +1,86 @@
+using Avalonia.Controls;
+using Avalonia.Interactivity;
+using Avalonia.Platform.Storage;
+using SopHelper.Engine.Sop;
+using System.IO;
+using System.Text;
+
+namespace SopHelper.VVMs.RecipeEditor;
+
+public partial class RecipeEditor
+{
+ private bool _stepTransferInProgress;
+ private async void ImportStepsFile(object? sender, RoutedEventArgs args)
+ {
+ if (_stepTransferInProgress || _model is not { CanTransferSteps: true } model || TopLevel.GetTopLevel(this) is not Window owner) return;
+ _stepTransferInProgress = true;
+ try
+ {
+ var files = await owner.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
+ {
+ Title = "导入步骤或配方文件", AllowMultiple = false,
+ FileTypeFilter = [new("SOP步骤 / 配方") { Patterns = ["*.yaml", "*.yml", "*.json"] }]
+ });
+ if (files.Count == 0 || _closing || !ReferenceEquals(model, _model)) return;
+ var path = files[0].TryGetLocalPath() ?? throw new IOException("请选择本地步骤文件");
+ if (new FileInfo(path).Length > 8 * 1024 * 1024) throw new IOException("步骤文件超过8MB");
+ var incoming = SopStepTransfer.Read(await File.ReadAllTextAsync(path));
+ var mode = model.Recipe.Steps.Count == 0 ? "replace" : await ChooseStepImportMode(incoming.Sop.Steps.Count, model.Recipe.Steps.Count);
+ if (mode is null || _closing || !ReferenceEquals(model, _model)) return;
+ model.ImportSteps(incoming, mode == "append");
+ }
+ catch (Exception error) { await ShowNotice("导入步骤失败", error.Message, false); }
+ finally { _stepTransferInProgress = false; }
+ }
+
+ private async void ExportStepsFile(object? sender, RoutedEventArgs args)
+ {
+ if (_stepTransferInProgress || _model is not { CanTransferSteps: true } model || TopLevel.GetTopLevel(this) is not Window owner) return;
+ _stepTransferInProgress = true;
+ string? temporary = null;
+ try
+ {
+ var text = model.ExportStepsText();
+ var count = model.Recipe.Steps.Count;
+ var name = string.Concat(model.Recipe.Name.Select(c => Path.GetInvalidFileNameChars().Contains(c) ? '_' : c));
+ var file = await owner.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
+ {
+ Title = "导出全部步骤", SuggestedFileName = name + ".steps.yaml", DefaultExtension = "yaml", ShowOverwritePrompt = true,
+ FileTypeChoices = [new("SOP步骤文件") { Patterns = ["*.yaml"] }]
+ });
+ if (file is null || _closing || !ReferenceEquals(model, _model)) return;
+ var path = file.TryGetLocalPath() ?? throw new IOException("请选择本地保存位置");
+ if (model.IsCurrentRecipeFile(path)) throw new IOException("步骤导出不能覆盖当前配方文件,请选择其他文件名");
+ temporary = Path.Combine(Path.GetDirectoryName(path)!, ".steps-export-" + Guid.NewGuid().ToString("N") + ".tmp");
+ await File.WriteAllTextAsync(temporary, text, new UTF8Encoding(false));
+ File.Move(temporary, path, overwrite: true);
+ model.StepTransferStatus = $"已导出 {count} 个步骤:{Path.GetFileName(path)}";
+ }
+ catch (Exception error) { await ShowNotice("导出步骤失败", error.Message, false); }
+ finally
+ {
+ try { if (temporary is not null && File.Exists(temporary)) File.Delete(temporary); }
+ catch (Exception cleanup) when (cleanup is IOException or UnauthorizedAccessException) { }
+ _stepTransferInProgress = false;
+ }
+ }
+
+ private async Task ChooseStepImportMode(int incoming, int current)
+ {
+ if (_closing || _dialog is not null || TopLevel.GetTopLevel(this) is not Window owner) return null;
+ var actions = new StackPanel { Orientation = Avalonia.Layout.Orientation.Horizontal, HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right, Spacing = 8 };
+ var dialog = new Window { Title = "导入步骤", Width = 500, SizeToContent = SizeToContent.Height, CanResize = false, WindowStartupLocation = WindowStartupLocation.CenterOwner,
+ Content = new StackPanel { Margin = new Avalonia.Thickness(20), Spacing = 18, Children =
+ {
+ new TextBlock { Text = $"文件含 {incoming} 个步骤,当前有 {current} 个步骤。\n请选择追加或替换。关联区域将一并导入,同名但坐标不同的区域会自动改名。导入后可继续编辑,点击“保存”才写入配方。", TextWrapping = Avalonia.Media.TextWrapping.Wrap }, actions
+ } } };
+ foreach (var (label, mode) in new[] { ("取消", ""), ("追加步骤", "append"), ("替换步骤", "replace") })
+ {
+ var button = new Button { Content = label, IsCancel = mode.Length == 0, IsDefault = mode.Length == 0 };
+ button.Click += (_, _) => dialog.Close(mode); actions.Children.Add(button);
+ }
+ _dialog = dialog;
+ try { var result = await dialog.ShowDialog