Merge pull request #9432 from dotCooCoo/fix/script-handles-after-revert

Scripts: invalidate page-item handles after file.revert
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-27 23:34:31 -08:00
commit 8f9fda6a7d
4 files changed
+51 -12

No files matched your search

+25 -11
View File
@@ -384,12 +384,27 @@ fn doc_of(h: &Host, d: &Data, class: &str) -> std::result::Result<Doc, HostError
}
}
fn node_handle(h: &Host, st: &DocState, id: NodeId) -> [f64; 5] {
let [high, low] = id_parts(id);
[float(h.run), float(st.uid), high, low, float(st.reverts)]
}
fn node_of(h: &Host, handle: &[f64]) -> Option<(Doc, NodeId)> {
let &[run, uid, high, low, reverts] = handle else { return None };
let doc = find_doc(h, run, uid)?;
if h.session.borrow().documents().get(doc.index)?.reverts != whole(reverts)? {
return None;
}
Some((doc, id_of(high, low)?))
}
/// The page item `d` names, which must still be in its document.
fn item_of(h: &Host, d: &Data) -> std::result::Result<Item, HostError> {
let Some(handle) = handle_of(d, "PageItem") else { return Err(HostError::Type("this is not a page item".into())) };
let [run, uid, high, low] = handle.as_slice() else { return Err(HostError::Type("this is not a page item".into())) };
let doc = find_doc(h, *run, *uid).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
let id = id_of(*high, *low).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
if handle.len() != 5 {
return Err(HostError::Type("this is not a page item".into()));
}
let (doc, id) = node_of(h, &handle).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
let s = h.session.borrow();
let st = &s.documents()[doc.index];
if st.doc.node(id).is_none_or(|n| typename(n).is_none()) {
@@ -426,8 +441,8 @@ fn doc_ref(h: &Host, uid: u64) -> Data {
fn item_ref(h: &Host, st: &DocState, id: NodeId) -> Data {
match st.doc.node(id) {
Some(n) => match typename(n) {
Some(t) => host_ref(t, &[float(h.run), float(st.uid), id_parts(id)[0], id_parts(id)[1]]),
None => host_ref("Layer", &[float(h.run), float(st.uid), id_parts(id)[0], id_parts(id)[1]]),
Some(t) => host_ref(t, &node_handle(h, st, id)),
None => host_ref("Layer", &node_handle(h, st, id)),
},
None => Data::Null,
}
@@ -1974,17 +1989,16 @@ fn destination(h: &Host, item: Item, target: &Data, how: &Data) -> std::result::
let how = placement_of(how)?;
let beside = matches!(how, Placement::Above | Placement::Below);
let handle = handle_of(target, "PageItem").or_else(|| handle_of(target, "Layer"));
let Some(&[run, uid, high, low]) = handle.as_deref() else {
let Some(handle) = handle.filter(|parts| parts.len() == 5) else {
if handle_of(target, "Document").is_some() {
return Err(HostError::Error("a document as the relative object is not available yet".into()));
}
return Err(HostError::Type("the relative object must be a page item or a layer".into()));
};
let doc = find_doc(h, run, uid).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
let (doc, target) = node_of(h, &handle).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
if doc.uid != item.doc.uid {
return Err(HostError::Error(CROSS_DOCUMENT.into()));
}
let target = id_of(high, low).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
with_doc(h, doc, |_, st| {
let d = &st.doc;
let t = d.node(target).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
@@ -2150,9 +2164,9 @@ fn item_z_order(h: &Host, a: &[Data]) -> Answer {
fn layer_name(h: &Host, a: &[Data]) -> Answer {
let Some(handle) = handle_of(arg(a, 0), "Layer") else { return Err(HostError::Type("this is not a Layer".into())) };
let [run, uid, high, low] = handle.as_slice() else { return Err(HostError::Type("this is not a Layer".into())) };
let doc = find_doc(h, *run, *uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?;
let id = id_of(*high, *low).unwrap_or(NodeId(u64::MAX));
let [run, uid, _, _, _] = handle.as_slice() else { return Err(HostError::Type("this is not a Layer".into())) };
find_doc(h, *run, *uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?;
let (doc, id) = node_of(h, &handle).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
with_doc(h, doc, |_, st| st.doc.node(id).map(|n| text(n.name.clone().unwrap_or_default())).ok_or_else(|| HostError::Error(STALE_ITEM.into())))
}
+22
View File
@@ -1265,6 +1265,28 @@ fn wrappers_go_stale_after_delete_and_undo() {
assert!(e.contains("Stale page item"), "{e}");
}
#[test]
fn wrappers_go_stale_after_revert() {
let (mut s, ..) = doc();
let folder = vectorcraft_testkit::TempDir::new("script-dom-revert");
let path = folder.join("saved's art.vectorcraft");
s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap();
let held = "var d = app.activeDocument, i = d.pageItems[0], l = i.parent; i.name = 'changed'; vc.run('file.revert');";
let out = log(&mut s, &format!("{held} vc.log(d.pageItems.length); vc.log(d.pageItems[0] === i); vc.log(d.pageItems[0].parent === l);"));
assert_eq!(out, vec![json!(2), json!(false), json!(false)]);
for operation in [
"i.name",
"i.name = 'wrong'",
"l.name",
"i.move(d.pageItems[1], ElementPlacement.PLACEBEFORE)",
"d.pageItems[1].duplicate(i, ElementPlacement.PLACEBEFORE)",
"d.pageItems[1].move(l, ElementPlacement.PLACEATEND)",
] {
let source = format!("{held} try {{ {operation}; vc.log('ran'); }} catch (e) {{ vc.log(e.message); }} vc.log(d.pageItems.length);");
assert_eq!(log(&mut s, &source), vec![json!("Stale page item"), json!(2)], "{operation}");
}
}
#[test]
fn remove_all_refuses_locked_art_before_deleting_anything() {
let (mut s, a, b) = doc();
+2 -1
View File
@@ -2870,7 +2870,8 @@ other files through `$`:
changed document. A value that changes nothing runs no command, and a change to another document leaves the active
tab as it was. A command that selects the objects it changes leaves the current layer and the highlighted Layers
panel rows as they were before the change. A bad value throws an error and changes nothing. An object that was
deleted, or that a named engine kept from an earlier run, throws `Stale page item`. `remove()` on an object that
deleted, that a named engine kept from an earlier run, or whose document was reverted with `file.revert`, throws
`Stale page item`. Layer objects also become stale after their document is reverted. `remove()` on an object that
is hidden or locked, is inside a hidden or locked layer or group, or is a group that holds one, and `removeAll()`
when any object is, throw `cannot delete hidden or locked objects` and delete nothing. `remove()` on an object on
a locked artboard, and `removeAll()` when any object is on one, throw `cannot delete objects on a locked artboard`
+2
View File
@@ -57,6 +57,8 @@ History panel row context menus (2026-11-20, G12): Right-clicking any state offe
Character-style edits keep manual leading overrides when Dynamic Text's Block cannot fit the frame. When Block can fit the frame, its generated leading follows the edited style. A regression in `crates/engine/src/cmd/typecmd/block_tests.rs` covers a frame whose inset leaves no room.
Script page-item and layer objects become stale after `file.revert`, including when the file reuses node IDs. The document object and its collections remain available and return new objects for the reverted content. `crates/engine/src/tests/script_dom.rs` covers reads, assignments, and relative targets for moves and copies.
Script appearance and geometry setters reject objects on locked artboards. Assigning the current opacity remains allowed when it changes no graph styling. Position, size and stacking-order setters also reject objects excluded by selection filtering, including locked guide paths. `crates/engine/src/tests/script_dom.rs` covers refused edits, unchanged documents and undo history, and editing a guide after unlocking it.
Variables panel row context menus (2026-11-20, G12): Right-click highlights the clicked variable without changing the canvas selection and offers Select Bound Object, Variable Options…, Unbind Variable, the four Make Dynamic actions, and Delete Variable. Shift+F10 opens the same menu for the focused row; Escape dismisses it. Row actions target that variable; dynamic actions use the canvas selection. Lock and selection checks disable unavailable actions. Headless pointer/accessibility tests cover opening the menu, highlighting, bound-object selection, deletion in one undo step and restoration, and disabled actions. Scores and estimates unchanged.