From cf57c2fdbc17890ebb1fe1fb23a4ae30a1561f77 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 20:18:23 -0800 Subject: [PATCH] Keep Text Export selection options with their opening artwork --- crates/ui-egui/src/dialogs/export_host.rs | 4 +- crates/ui-egui/src/dialogs/text_export.rs | 67 ++++++++++++++++++++++- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/export_host.rs b/crates/ui-egui/src/dialogs/export_host.rs index fb68f64c51..dd4c63d169 100644 --- a/crates/ui-egui/src/dialogs/export_host.rs +++ b/crates/ui-egui/src/dialogs/export_host.rs @@ -52,7 +52,7 @@ pub(crate) enum Export { /// Save for Office Documents. Office, /// Text Export Options. - Text, + Text { source: Option }, /// The options a save or open asks for before it goes on, for `action`. Save { /// The action the options belong to. @@ -137,7 +137,7 @@ fn confirm(app: &mut VectorcraftApp, open: &Open, fields: &DialogFields) -> Resu Export::Dxf => dxf_options::confirm(app, open, fields), Export::Hpgl { .. } => hpgl_options::confirm(app, open, fields), Export::Office => office_export::confirm(app, open, fields), - Export::Text => text_export::confirm(app, open, fields), + Export::Text { source } => text_export::confirm(app, open, source.as_ref(), fields), Export::Save { .. } => save_options::confirm(app, open, fields), } } diff --git a/crates/ui-egui/src/dialogs/text_export.rs b/crates/ui-egui/src/dialogs/text_export.rs index 0e4b8d262a..ab33e24e2c 100644 --- a/crates/ui-egui/src/dialogs/text_export.rs +++ b/crates/ui-egui/src/dialogs/text_export.rs @@ -6,6 +6,7 @@ use craft::ui::contract::Bind; use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind}; use craft::ui::runtime::env::Label; use serde_json::Value; +use vectorcraft_doc::NodeId; use super::export_host::{self, After, Export, Open, options}; use super::form; @@ -32,11 +33,28 @@ fn params(can_select: bool) -> Vec { ] } +/// The drawing and selected objects a Selection Only export started with. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct SelectionSource { + drawing: (u64, u64), + objects: Vec, +} + +impl SelectionSource { + fn of(app: &VectorcraftApp) -> Option { + let state = app.session.active()?; + let mut objects = state.selection.objects.clone(); + objects.sort_unstable(); + Some(Self { drawing: (state.uid, state.reverts), objects }) + } +} + /// Open the options for exporting the document's text to `path`; `selection` when Export Selection opened them. pub fn open(app: &mut VectorcraftApp, path: &str, selection: bool) -> Result<(), String> { let ctx = app.ui_ctx.clone().ok_or("no window")?; // Export Selection keeps Selection Only on. - let can_select = !selection && app.session.active().is_some_and(|st| !st.selection.objects.is_empty()); + let source = SelectionSource::of(app); + let can_select = !selection && source.as_ref().is_some_and(|source| !source.objects.is_empty()); // The platform's own line endings by default. let eol = if cfg!(windows) { LINE_ENDINGS[1] } else { LINE_ENDINGS[0] }; let mut fields = DialogFields::from_pairs([("format", "txt"), ("path", path), ("encoding", ENCODINGS[0]), ("lineEndings", eol)]); @@ -44,12 +62,15 @@ pub fn open(app: &mut VectorcraftApp, path: &str, selection: bool) -> Result<(), let dialog = HostedParamDialog::new(&ctx, KIND, fields, move |fields| { ParamDialog::new(KIND, tl!("Text Export Options"), params(can_select)).values(Bind::bound(fields)).ok_label(Label::from("Export")) }); - export_host::open(app, Export::Text, selection, dialog).map(|_| ()).ok_or_else(|| "no window".into()) + export_host::open(app, Export::Text { source }, selection, dialog).map(|_| ()).ok_or_else(|| "no window".into()) } /// Write the text file (the command runs first, so a bad option keeps the dialog open). Opened by /// Export Selection, it writes `document.exportSelection`'s text. -pub(super) fn confirm(app: &mut VectorcraftApp, open: &Open, fields: &DialogFields) -> Result { +pub(super) fn confirm(app: &mut VectorcraftApp, open: &Open, source: Option<&SelectionSource>, fields: &DialogFields) -> Result { + if (open.selection || fields.bool("selectionOnly") == Some(true)) && SelectionSource::of(app).as_ref() != source { + return Err(tl!("Select the original artwork to export it, or reopen Text Export Options.").into()); + } let mut params = Value::Object(form::json_of(¶ms(true), fields)); let id = if open.selection { if let Some(o) = params.as_object_mut() { @@ -62,3 +83,43 @@ pub(super) fn confirm(app: &mut VectorcraftApp, open: &Open, fields: &DialogFiel io::save_command_output(app, id, "txt", params, |_, _| Ok(Value::Null))?; Ok(None) } + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::super::tests_export::{app_saving, frame, kind, set, try_confirm}; + + #[test] + fn selection_only_text_export_stays_on_the_opening_artwork() { + for export_selection in [true, false] { + let (mut app, written) = app_saving(1, |_| "/output/selection.txt".into()); + let first = app.run("text.create", json!({"x": 5, "y": 10, "text": "First text"})).unwrap()["id"].clone(); + let second = app.run("text.create", json!({"x": 5, "y": 25, "text": "Second text"})).unwrap()["id"].clone(); + app.run("select.set", json!({"ids": [first]})).unwrap(); + if export_selection { + app.run("document.exportSelection", json!({})).unwrap(); + frame(&mut app); + } else { + app.run("file.exportAs", json!({"format": "txt"})).unwrap(); + try_confirm(&mut app).unwrap(); + set(&mut app, "selectionOnly", json!(true)); + } + assert_eq!(kind(&app).as_deref(), Some("txtOptions")); + + app.run("select.set", json!({"ids": [second]})).unwrap(); + let why = try_confirm(&mut app).expect_err("the original selection is required"); + assert!(why.contains("original artwork")); + assert_eq!(kind(&app).as_deref(), Some("txtOptions")); + assert!(written.borrow().is_empty()); + + app.run("select.set", json!({"ids": [first]})).unwrap(); + try_confirm(&mut app).unwrap(); + let output = written.borrow(); + assert_eq!(output.len(), 1); + let contents = std::str::from_utf8(&output[0].1).unwrap(); + assert!(contents.contains("First text")); + assert!(!contents.contains("Second text")); + } + } +}