From c4b269319c2ab83f6b0d61e710d7610b4f9e38ef Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 13 Nov 2026 17:59:10 +0100 Subject: [PATCH] Allow pasted values to map to a subset of selected layers --- crates/midra-layout/src/gui.rs | 43 +++++++++++++++++++++++------ crates/midra-layout/src/lib.rs | 8 +++--- crates/midra-layout/tests/import.rs | 6 +--- docs/midra-layout.md | 12 ++++---- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index e1464ea..eb69eef 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -152,9 +152,12 @@ impl Paste { ); } let count = self.ignored.iter().filter(|v| !**v).count(); - if count != self.targets.len() { + if count == 0 { + return Err("No rectangles to import. Include at least one rectangle.".into()); + } + if count > self.targets.len() { return Err(format!( - "Layer count mismatch: {} selected, {} detected ({} excluded). Select {} destination layer(s), or supply {} rectangle(s).", + "Layer count mismatch: {} selected, {} detected ({} excluded). Choose at least {} destination layer(s), or supply at most {} rectangle(s).", self.targets.len(), count, self.shapes.len() - count, @@ -183,7 +186,7 @@ impl Paste { let target = self.mapping[row].ok_or("Choose a destination for each detected rectangle.")?; if !self.targets.contains(&target) || values.iter().any(|(i, _)| *i == target) { - return Err("Each selected layer must be mapped exactly once; choose a different destination for duplicate mappings.".into()); + return Err("Each imported rectangle needs a different selected destination; choose a different destination for duplicate mappings.".into()); } values.push((target, self.geometry(shape, layout.layers()[target])?)); } @@ -734,6 +737,7 @@ impl Editor { if !p.include_size {ui.small("Outline adjustment requires Format. Position uses the source top-left when enabled.");} ui.separator(); ui.strong("Detected rectangles / destination layers"); + ui.small("Map each rectangle to one selected layer. Unmapped selected layers stay unchanged."); let mut reset_mapping=false; for row in 0..p.shapes.len() { let shape=p.shapes[row].clone(); @@ -3512,6 +3516,32 @@ mod tests { assert_eq!(*app.layout.layers(), before); } #[test] + fn fewer_pasted_rectangles_can_map_to_either_selected_layer() { + for target in 0..2 { + let mut app = Editor::default(); + app.layout.set_selection([true; 2]); + let before = app.layout.layers().clone(); + let formats = app.layout.formats().clone(); + app.open_paste(include_str!("../tests/fixtures/drawio-rectangle.txt").into()); + let paste = app.paste.as_mut().unwrap(); + paste.mapping[0] = Some(target); + paste.shapes[0].zoom = Some(2.0); + paste.apply(&mut app.layout).unwrap(); + assert_eq!(app.layout.layers()[target].width, 945.0); + assert_eq!(app.layout.layers()[1 - target], before[1 - target]); + assert_eq!( + app.layout.formats().advanced[1 - target], + formats.advanced[1 - target] + ); + assert_eq!(app.layout.selected_indices(), vec![0, 1]); + app.layout.undo(); + assert_eq!(*app.layout.layers(), before); + assert_eq!(*app.layout.formats(), formats); + app.layout.redo(); + assert_eq!(app.layout.layers()[target].width, 945.0); + } + } + #[test] fn paste_opens_review_without_editing_and_escape_cancels() { let mut app = Editor::default(); let ctx = egui::Context::default(); @@ -4206,11 +4236,6 @@ mod tests { assert_eq!(*app.layout.layers(), before); app.open_paste(include_str!("../tests/fixtures/drawio-rectangle.txt").into()); let paste = app.paste.as_ref().unwrap(); - assert!( - paste - .values(&app.layout) - .unwrap_err() - .contains("Layer count mismatch") - ); + assert_eq!(paste.values(&app.layout).unwrap().len(), 1); } } diff --git a/crates/midra-layout/src/lib.rs b/crates/midra-layout/src/lib.rs index ab0ad6d..cbf3926 100644 --- a/crates/midra-layout/src/lib.rs +++ b/crates/midra-layout/src/lib.rs @@ -878,12 +878,12 @@ impl Layout { next[index] = geometry; self.commit(next) } - /// Apply a one-to-one import to the complete selection, with one undo record. + /// Apply a one-to-one import to a subset of the selection, with one undo record. pub fn apply_import(&mut self, values: &[(usize, Geometry)]) -> Result<(), String> { let targets = self.selected_indices(); - if values.len() != targets.len() || targets.is_empty() { + if values.is_empty() || values.len() > targets.len() { return Err(format!( - "Layer count mismatch: {} selected, {} imported. Select exactly one destination for each imported rectangle.", + "Layer count mismatch: {} selected, {} imported. Map each imported rectangle to a different selected destination.", targets.len(), values.len() )); @@ -892,7 +892,7 @@ impl Layout { let mut next = self.layers.clone(); for (i, g) in values { if !targets.contains(i) || seen.contains(i) || !self.selectable(*i) { - return Err("Map every selected layer exactly once; duplicate or unselected targets are not allowed.".into()); + return Err("Map each imported rectangle to a different selected layer; duplicate or unselected targets are not allowed.".into()); } g.validate()?; seen.push(*i); diff --git a/crates/midra-layout/tests/import.rs b/crates/midra-layout/tests/import.rs index f952b72..d0493f9 100644 --- a/crates/midra-layout/tests/import.rs +++ b/crates/midra-layout/tests/import.rs @@ -147,11 +147,7 @@ fn batch_import_is_one_transaction_and_refuses_count_duplicates_and_invalid_geom width: 300.0, height: 250.0, }; - assert!( - l.apply_import(&[(0, a)]) - .unwrap_err() - .contains("count mismatch") - ); + assert!(l.apply_import(&[]).unwrap_err().contains("count mismatch")); assert!(l.apply_import(&[(0, a), (0, b)]).is_err()); assert!( l.apply_import(&[(0, a), (1, Geometry { width: -1.0, ..b })]) diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 1f266cc..d244fb6 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -21,9 +21,10 @@ cargo run --locked -p midra-layout same review window; raw SVG XML can also be pasted. **Import → Clipboard values** reads the clipboard directly and opens the mapping review. 3. Review each detected rectangle and choose its destination layer. Initial - mapping follows document order and the selected layer list. Every selected - destination must receive exactly one rectangle: count mismatches and duplicate - mappings block Apply and explain what needs changing. Nothing changes yet. + mapping follows document order and the selected layer list. Each rectangle must map to a different + selected destination. Fewer rectangles than selected layers are allowed; + unmapped layers stay unchanged. Too many rectangles or duplicate mappings + block Apply and explain what needs changing. Nothing changes yet. 4. Choose independently what to paste: **Format (width / height)**, **Position**, and **Crop zoom**. Zoom is enabled only when present in the data (new Midra clipboard values include it; older copies, draw.io and SVG do not). All @@ -585,8 +586,9 @@ You can choose layers without returning to the viewport: - **Import → Choose destination layers…**, and the same picker inside the clipboard/SVG review, select the destinations. Paste and SVG import can open with nothing selected. Then map each detected rectangle using its destination - dropdown. The number of chosen destinations must match the imported rectangles - after ignoring an SVG background; duplicate mappings remain invalid. + dropdown. There must be at least as many chosen destinations as imported rectangles + after ignoring an SVG background. Map to any subset of them; other layers stay + unchanged. Duplicate mappings and empty imports remain invalid. - **Project → Export SVG → Choose layers to export…** selects the group for **Selected layers…**. **Specific layer…** remains available for individual layers, including hidden layers.