mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 05:22:58 -05:00
Scripts: a group holding an image cannot take the tracing's name
PageItem.name now refuses "Image Trace" for an unclipped group whose first object is an image and that is not already a tracing. That name would make the group a tracing, and expanding it would delete its first object. The snapshot of a tracing's objects is now taken only for a tracing saved without the record of the objects its trace made, and only the first time a run sees it. Retrace reads it only in that case. docs/target-app-parity.md now says that the view options only change the view, and docs/mcp.md lists the new rename error.
This commit is contained in:
1 parent
ac03057d2f
commit
bf56f4d062
6 files changed
+61
-12
No files matched your search
@@ -130,6 +130,8 @@ pub const NO_ACTIVE_DOCUMENT: &str = "No active document";
|
||||
pub const STALE_ITEM: &str = "Stale page item";
|
||||
/// The error for a new name given to a `PluginItem`.
|
||||
const TRACING_NAME: &str = "The name of a tracing cannot be changed";
|
||||
/// The error for the tracing's name given to a group that holds an image first.
|
||||
const NOT_A_TRACING: &str = "Only Image Trace can make a tracing";
|
||||
/// The error for a document that was closed, or that came from an earlier run.
|
||||
pub const STALE_DOCUMENT: &str = "Stale document";
|
||||
/// The error of `index` and `getByName` when nothing matches.
|
||||
@@ -2283,12 +2285,27 @@ fn item_set_name(h: &Host, a: &[Data]) -> Answer {
|
||||
if name.len() > MAX_TEXT_BYTES {
|
||||
return Err(HostError::Range("the name is too long".into()));
|
||||
}
|
||||
// An Image Trace object is known by its name, so renaming one would end the tracing.
|
||||
// An Image Trace object is known by its name, so renaming one would end the tracing, and giving
|
||||
// an ordinary group that holds an image first the tracing's name would make it one.
|
||||
let item = this_item(h, a)?;
|
||||
let tracing =
|
||||
with_doc(h, item.doc, |_, st| st.doc.node(item.id).is_some_and(|n| crate::cmd::is_trace_group(n) && n.name.as_deref() != Some(&**name)));
|
||||
if tracing {
|
||||
return Err(HostError::Error(TRACING_NAME.into()));
|
||||
let refused = with_doc(h, item.doc, |_, st| {
|
||||
st.doc.node(item.id).and_then(|n| {
|
||||
let tracing = crate::cmd::is_trace_group(n);
|
||||
let would_trace = !tracing
|
||||
&& name == crate::cmd::TRACE_NAME
|
||||
&& matches!(n.kind, crate::doc::NodeKind::Group { clip: false, .. })
|
||||
&& n.children().is_some_and(|c| c.first().is_some_and(|i| matches!(i.kind, crate::doc::NodeKind::Image(_))));
|
||||
if tracing && n.name.as_deref() != Some(&**name) {
|
||||
Some(TRACING_NAME)
|
||||
} else if would_trace {
|
||||
Some(NOT_A_TRACING)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
});
|
||||
if let Some(msg) = refused {
|
||||
return Err(HostError::Error(msg.into()));
|
||||
}
|
||||
set_props(h, a, |n| n.name.as_deref().unwrap_or("") == name, json!({"name": name}))
|
||||
}
|
||||
|
||||
@@ -174,8 +174,16 @@ fn replaced(h: &Host, uid: u64, old: NodeId, new: NodeId) {
|
||||
}
|
||||
|
||||
pub(super) fn remember_source(h: &Host, uid: u64, group: &Node) {
|
||||
let mut first = true;
|
||||
if let Some(source) = group.children().and_then(|c| c.first()) {
|
||||
h.dom.tracing.sources.borrow_mut().entry((uid, group.id.0)).or_insert(source.id);
|
||||
let mut sources = h.dom.tracing.sources.borrow_mut();
|
||||
first = !sources.contains_key(&(uid, group.id.0));
|
||||
sources.entry((uid, group.id.0)).or_insert(source.id);
|
||||
}
|
||||
// Only a tracing saved without the record of the objects its trace made needs the snapshot,
|
||||
// taken the first time this run sees it.
|
||||
if !first || group.trace_made().is_some() {
|
||||
return;
|
||||
}
|
||||
h.dom.tracing.descendants.borrow_mut().entry((uid, group.id.0)).or_insert_with(|| {
|
||||
let mut ids = HashSet::new();
|
||||
@@ -309,14 +317,22 @@ fn retrace(h: &Host, item: Item, mut params: Map<String, Value>) -> Result<(), H
|
||||
}
|
||||
read_group(h, item, |_, group| {
|
||||
remember_source(h, item.doc.uid, group);
|
||||
let descendants = h.dom.tracing.descendants.borrow();
|
||||
let ids = descendants.get(&(item.doc.uid, item.id.0)).ok_or_else(stale)?;
|
||||
// The objects the trace made, as the tracing records them; a tracing saved without that
|
||||
// record is checked against the objects it held when this run first saw it.
|
||||
let made = group.trace_made();
|
||||
let descendants = h.dom.tracing.descendants.borrow();
|
||||
let ids = match made {
|
||||
Some(_) => None,
|
||||
None => Some(descendants.get(&(item.doc.uid, item.id.0)).ok_or_else(stale)?),
|
||||
};
|
||||
let mut added = false;
|
||||
group.walk(&mut |n| {
|
||||
added |= n.id != group.id && made.as_ref().map_or(!ids.contains(&n.id), |m| !m.contains(n.id));
|
||||
added |= n.id != group.id
|
||||
&& match (&made, ids) {
|
||||
(Some(m), _) => !m.contains(n.id),
|
||||
(None, Some(ids)) => !ids.contains(&n.id),
|
||||
(None, None) => false,
|
||||
};
|
||||
});
|
||||
if added {
|
||||
return Err(HostError::Error(ADDED_ARTWORK.into()));
|
||||
|
||||
Reference in new issue
Block a user