diff --git a/crates/ui-egui/src/dialogs/smart_punctuation.rs b/crates/ui-egui/src/dialogs/smart_punctuation.rs index 9e68c0507b..c3c1549d2a 100644 --- a/crates/ui-egui/src/dialogs/smart_punctuation.rs +++ b/crates/ui-egui/src/dialogs/smart_punctuation.rs @@ -6,6 +6,8 @@ //! Fields: the switches `fiLigatures`, `flLigatures`, `quotes`, `spaces`, `dashes`, `ellipsis` and //! `fractions`, `scope` (`selection` or `document`) and `report`. +use std::collections::HashSet; + use craft::ui::contract::{Bind, SaveKey}; use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult}; use craft::ui::runtime::env::Label; @@ -51,6 +53,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result { let source = app.session.active().ok_or("no document")?; let document = source.uid; let selected = !source.selection.is_empty(); + let selection = source.selection.objects.iter().copied().collect::>(); let mut params: Vec = options() .into_iter() .map(|(key, _, label)| Param::new(key, label, ParamKind::bool(matches!(key, "quotes" | "dashes" | "ellipsis")))) @@ -72,10 +75,18 @@ pub fn open(app: &mut VectorcraftApp) -> Result { ctx.data_mut(|data| data.insert_temp(owner, document)); let ok = move |app: &mut VectorcraftApp, result: &ParamResult| -> Result<(), String> { let document = ctx.data(|data| data.get_temp::(owner)); - if app.session.active().is_none_or(|st| Some(st.uid) != document) { + let Some(active) = app.session.active().filter(|st| Some(st.uid) == document) else { return Err(tl!("Return to the original drawing to replace its punctuation").into()); + }; + let params = params_of(result); + if params["scope"] == "selection" + && (active.selection.is_empty() + || active.selection.objects.len() != selection.len() + || active.selection.objects.iter().any(|id| !selection.contains(id))) + { + return Err(tl!("The selected text changed. Restore the selection to continue.").into()); } - let r = app.run(COMMAND, params_of(result))?; + let r = app.run(COMMAND, params)?; result.remember(&ctx); if result.fields.bool("report") == Some(true) { let lines: Vec = options() @@ -208,4 +219,47 @@ mod tests { app.run("document.activate", json!({"index": 0})).unwrap(); assert_eq!(plain(&app, source), "a--b"); } + + fn selected_text_pair() -> (VectorcraftApp, u64, u64) { + let (mut app, selected) = app_with_text("a--b"); + let other = app.run("text.create", json!({"x": 10, "y": 70, "text": "c--d", "font": "Source Sans 3"})).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", json!({"ids": [selected]})).unwrap(); + open(&mut app).unwrap(); + assert_eq!(entry(&app, COMMAND).unwrap().fields.unwrap()["scope"], "selection"); + (app, selected, other) + } + + #[test] + fn selected_text_only_does_not_expand_to_the_document_after_clearing_selection() { + let (mut app, selected, other) = selected_text_pair(); + let undo = app.session.active().unwrap().history.undo.len(); + app.run("select.none", json!({})).unwrap(); + assert_eq!(entry(&app, COMMAND).unwrap().fields.unwrap()["scope"], "selection"); + + let answer = super::super::tests_export::try_confirm(&mut app); + assert_eq!([plain(&app, selected), plain(&app, other)], ["a--b", "c--d"], "Selected Text Only must not expand to the document"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert_eq!(answer, Err("The selected text changed. Restore the selection to continue.".into())); + + app.run("select.set", json!({"ids": [selected]})).unwrap(); + ok(&mut app); + assert_eq!([plain(&app, selected), plain(&app, other)], ["a–b", "c--d"], "retry changes only the original selection"); + } + + #[test] + fn selected_text_only_refuses_later_art_but_entire_document_still_works() { + let (mut app, selected, other) = selected_text_pair(); + app.run("select.set", json!({"ids": [other]})).unwrap(); + let undo = app.session.active().unwrap().history.undo.len(); + + let answer = super::super::tests_export::try_confirm(&mut app); + assert_eq!([plain(&app, selected), plain(&app, other)], ["a--b", "c--d"], "Selected Text Only must not change later artwork"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert_eq!(answer, Err("The selected text changed. Restore the selection to continue.".into())); + + set(&app, "scope", json!("document")); + ok(&mut app); + assert_eq!([plain(&app, selected), plain(&app, other)], ["a–b", "c–d"], "an explicit Entire Document still works"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1); + } }