From 4bed43a4e895c18ca77ac7e9799efa638291c3c7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 21:54:23 -0800 Subject: [PATCH 1/2] Scripts: undoing a point write selects the anchors it dropped again Writing a script's point changes drops the selected anchors and segments that the new points no longer have before it runs path.setAnchors, so the undo step of the edit kept the trimmed selection. Undoing the edit, with app.undo() in the run or Edit > Undo after it, put the points back but left them unselected. The step now keeps the selection from before the trim. The trim was added in #9304. --- crates/engine/src/script/dom/paths.rs | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/script/dom/paths.rs b/crates/engine/src/script/dom/paths.rs index cb23b429b0..4ce08f8116 100644 --- a/crates/engine/src/script/dom/paths.rs +++ b/crates/engine/src/script/dom/paths.rs @@ -342,7 +342,12 @@ pub(super) fn flush(h: &super::Host) -> std::result::Result<(), HostError> { let subpaths: Vec = subs.iter().map(|sp| json!({"anchors": sp.anchors.iter().map(anchor_json).collect::>(), "closed": sp.closed})).collect(); super::atomically(h, |h| { + // The anchors and segments the new points no longer have leave the selection before the + // points are written. The undo step of the edit keeps the selection from before that, so + // undoing it selects them again. + let mut kept = None; if let Some(st) = h.session.borrow_mut().document_mut(item.doc.uid) { + kept = Some((st.doc.clone(), st.selection.clone())); let selection = &mut st.selection; if let Some(anchors) = selection.anchors.get_mut(&item.id) { anchors.retain(|&(s, i)| subs.get(s).is_some_and(|sp| i < sp.anchors.len())); @@ -361,8 +366,18 @@ pub(super) fn flush(h: &super::Host) -> std::result::Result<(), HostError> { { selection.remove(item.id); } + if kept.as_ref().is_some_and(|(_, before)| *before == st.selection) { + kept = None; + } } - edit_item(h, item, "path.setAnchors", &json!({"id": item.id.0, "subpaths": subpaths})) + edit_item(h, item, "path.setAnchors", &json!({"id": item.id.0, "subpaths": subpaths}))?; + if let Some((doc, selection)) = kept + && let Some(st) = h.session.borrow_mut().document_mut(item.doc.uid) + && let Some(step) = st.history.undo.last_mut().filter(|step| Arc::ptr_eq(&step.doc, &doc)) + { + step.selection = Arc::new(selection); + } + Ok(()) })?; Ok(()) } @@ -1992,6 +2007,21 @@ mod tests { LOCATE_STEPS.with(|steps| assert!(steps.get() <= 512, "subpath visits: {}", steps.get())); } + #[test] + fn undoing_a_point_write_selects_the_anchors_it_removed_again() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + let id = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap()["id"].clone(); + s.execute("select.anchors", &json!({"id": id, "anchors": [[0, 1], [0, 3]]})).unwrap(); + let source = "var p = app.activeDocument.pathItems[0]; p.setEntirePath([[0, 0], [10, 0], [10, 10]]); \ + vc.log(p.selectedPathPoints.length); app.undo();"; + let out = s.execute("script.run", &json!({"source": source, "name": "t.js"})).unwrap(); + assert_eq!(out["log"], json!([1])); + let id = NodeId(id.as_u64().unwrap()); + let anchors: Vec<(usize, usize)> = s.active().unwrap().selection.anchors.get(&id).map(|a| a.iter().copied().collect()).unwrap_or_default(); + assert_eq!(anchors, vec![(0, 1), (0, 3)], "the undo puts back the selection the write changed"); + } + #[test] fn a_made_id_above_two_to_the_53_stays_exact() { let made = |v: Value| made_id(&(Data::Undefined, v)); From f4ee5e7be3b0103de986d7eefc46a508a30b66be Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 23:26:40 -0800 Subject: [PATCH 2/2] Scripts: match the point write's undo step after the switch to its document Making a background document active can measure its type again and replace the document, so the undo step is now found by the document read after the switch. A test covers a background document whose text is measured again. --- crates/engine/src/script/dom/paths.rs | 39 ++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/script/dom/paths.rs b/crates/engine/src/script/dom/paths.rs index 4ce08f8116..6ea8fabfc3 100644 --- a/crates/engine/src/script/dom/paths.rs +++ b/crates/engine/src/script/dom/paths.rs @@ -347,7 +347,7 @@ pub(super) fn flush(h: &super::Host) -> std::result::Result<(), HostError> { // undoing it selects them again. let mut kept = None; if let Some(st) = h.session.borrow_mut().document_mut(item.doc.uid) { - kept = Some((st.doc.clone(), st.selection.clone())); + kept = Some(st.selection.clone()); let selection = &mut st.selection; if let Some(anchors) = selection.anchors.get_mut(&item.id) { anchors.retain(|&(s, i)| subs.get(s).is_some_and(|sp| i < sp.anchors.len())); @@ -366,12 +366,18 @@ pub(super) fn flush(h: &super::Host) -> std::result::Result<(), HostError> { { selection.remove(item.id); } - if kept.as_ref().is_some_and(|(_, before)| *before == st.selection) { + if kept.as_ref().is_some_and(|before| *before == st.selection) { kept = None; } } - edit_item(h, item, "path.setAnchors", &json!({"id": item.id.0, "subpaths": subpaths}))?; - if let Some((doc, selection)) = kept + // The document as the edit's step keeps it, read after the switch to it, which can measure + // its type again. + let doc = super::edit_item_with(h, item, |h| { + let doc = h.session.borrow().active().map(|st| st.doc.clone()); + super::command(h, "path.setAnchors", &json!({"id": item.id.0, "subpaths": subpaths}))?; + Ok(doc) + })?; + if let Some((doc, selection)) = doc.zip(kept) && let Some(st) = h.session.borrow_mut().document_mut(item.doc.uid) && let Some(step) = st.history.undo.last_mut().filter(|step| Arc::ptr_eq(&step.doc, &doc)) { @@ -2022,6 +2028,31 @@ mod tests { assert_eq!(anchors, vec![(0, 1), (0, 3)], "the undo puts back the selection the write changed"); } + #[test] + fn undoing_a_point_write_in_a_background_document_selects_its_anchors_again() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + let id = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap()["id"].clone(); + let text = s.execute("text.create", &json!({"x": 20, "y": 40, "text": "Text"})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.anchors", &json!({"id": id, "anchors": [[0, 1], [0, 3]]})).unwrap(); + let uid = s.active().unwrap().uid; + s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + let st = s.document_mut(uid).unwrap(); + let NodeKind::Text(t) = &mut Arc::make_mut(&mut st.doc).node_mut(NodeId(text)).unwrap().kind else { panic!("not a text object") }; + t.cached_bounds = None; + let before_activation = st.doc.clone(); + let source = "var d; for (var i = 0; i < app.documents.length; i++) { if (app.documents[i].pathItems.length) d = app.documents[i]; } \ + d.pathItems[0].setEntirePath([[0, 0], [10, 0], [10, 10]]); app.activeDocument = d; app.undo();"; + s.execute("script.run", &json!({"source": source, "name": "t.js"})).unwrap(); + let id = NodeId(id.as_u64().unwrap()); + let st = s.documents().iter().find(|d| d.uid == uid).unwrap(); + assert!(!Arc::ptr_eq(&st.doc, &before_activation)); + assert!(matches!(&st.doc.node(NodeId(text)).unwrap().kind, NodeKind::Text(t) if t.cached_bounds.is_some())); + assert_eq!(st.doc.node(id).and_then(|n| n.path_data()).map(|pd| pd.subpaths[0].anchors.len()), Some(4)); + let anchors: Vec<(usize, usize)> = st.selection.anchors.get(&id).map(|a| a.iter().copied().collect()).unwrap_or_default(); + assert_eq!(anchors, vec![(0, 1), (0, 3)], "the undo puts back the selection the write changed"); + } + #[test] fn a_made_id_above_two_to_the_53_stays_exact() { let made = |v: Value| made_id(&(Data::Undefined, v));