mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Scripts: tracing keeps added images and direct selections; app.open logs warnings
- Expanding a tracing removes only its source image; other images a script moved or duplicated into the group stay. - A TracingObject goes stale when its original source image is gone, even when another image now sits first in the group. - Tracing, retracing, expanding and releasing restore the whole selection, anchors and segments included, with only the replaced object remapped. - app.open writes the open's warnings to the script console, as saveAs does. - embed() checks the image's link state after the command instead of a returned id rounded to a JavaScript number. - The placed-image tests pass ordinary drive paths to File on Windows.
This commit is contained in:
1 parent
64972e8a91
commit
db7fe4d38b
7 files changed
+163
-34
No files matched your search
@@ -159,14 +159,6 @@ fn field<'a>(d: &'a Data, k: &str) -> Option<&'a Data> {
|
||||
}
|
||||
}
|
||||
|
||||
fn id_value(d: Option<&Data>) -> Option<NodeId> {
|
||||
match d {
|
||||
Some(Data::Number(n)) => whole(*n).map(NodeId),
|
||||
Some(Data::Array(a)) => id_value(a.first()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// ---- PlacedItems and RasterItems ----
|
||||
|
||||
fn collection(placed: bool) -> &'static str {
|
||||
@@ -422,11 +414,8 @@ fn placed_embed(h: &Host, a: &[Data]) -> Answer {
|
||||
if matches!(field(&info, "status"), Some(Data::String(s)) if s == "missing") {
|
||||
return Err(HostError::Error(MISSING_LINK.into()));
|
||||
}
|
||||
let r = edit_item(h, item, "links.embed", &json!({"ids": [item.id.0]}))?;
|
||||
let embedded = match field(&r, "embedded") {
|
||||
Some(Data::Array(ids)) => ids.iter().any(|d| id_value(Some(d)) == Some(item.id)),
|
||||
_ => false,
|
||||
};
|
||||
edit_item(h, item, "links.embed", &json!({"ids": [item.id.0]}))?;
|
||||
let embedded = with_doc(h, item.doc, |_, st| matches!(st.doc.node(item.id).map(|n| &n.kind), Some(NodeKind::Image(im)) if im.link.is_none()));
|
||||
if !embedded {
|
||||
return Err(HostError::Error(MISSING_LINK.into()));
|
||||
}
|
||||
|
||||
@@ -767,7 +767,10 @@ fn app_open(h: &Host, a: &[Data]) -> Answer {
|
||||
}
|
||||
let real = h.disk.borrow().open_target(path).map_err(|m| refused(m, path))?;
|
||||
p.insert("path".into(), json!(plain(real)));
|
||||
let r = exactly(h, || command(h, "document.open", &Value::Object(p)))?;
|
||||
let (r, value) = exactly(h, || command_value(h, "document.open", &Value::Object(p)))?;
|
||||
for w in value.get("warnings").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str) {
|
||||
super::super::each_line(&format!("open: {w}"), |line, cut| h.line(super::super::console::Entry::Log(line), cut));
|
||||
}
|
||||
let index = match &r {
|
||||
Data::Object(items) => items.iter().find_map(|(k, v)| match (k.as_str(), v) {
|
||||
("index", Data::Number(n)) => whole(*n).and_then(|i| usize::try_from(i).ok()),
|
||||
|
||||
@@ -115,6 +115,7 @@ const NOT_IMPLEMENTED: &[&str] = &[
|
||||
#[derive(Default)]
|
||||
pub(super) struct State {
|
||||
next: RefCell<HashMap<(u64, u64), u64>>,
|
||||
sources: RefCell<HashMap<(u64, u64), NodeId>>,
|
||||
}
|
||||
|
||||
/// `id` while the document `uid` holds it (a run never gives an id out twice, so an undo that put
|
||||
@@ -185,8 +186,11 @@ fn tracing_of(h: &Host, d: &Data, cls: &str) -> Result<(Item, [f64; 5]), HostErr
|
||||
};
|
||||
let &[run, uid, high, low, reverts] = handle.as_slice() else { return Err(HostError::Type(format!("this is not a {c}"))) };
|
||||
let (doc, id) = node_of(h, &handle).ok_or_else(stale)?;
|
||||
let source = h.dom.tracing.sources.borrow().get(&(doc.uid, id.0)).copied().ok_or_else(stale)?;
|
||||
let id = follow(h, doc.uid, id);
|
||||
let live = with_doc(h, doc, |_, st| st.doc.node(id).is_some_and(crate::cmd::is_trace_group));
|
||||
let live = with_doc(h, doc, |_, st| {
|
||||
st.doc.node(id).is_some_and(|g| crate::cmd::is_trace_group(g) && g.children().and_then(|c| c.first()).is_some_and(|n| n.id == source))
|
||||
});
|
||||
if !live {
|
||||
return Err(stale());
|
||||
}
|
||||
@@ -218,27 +222,24 @@ fn read_group<T>(h: &Host, item: Item, f: impl FnOnce(&crate::DocState, &Node) -
|
||||
/// Runs command `id` with `params` on `item`'s document → the object its result names under `key`.
|
||||
/// The selection stays as it was, with that object in place of `item`.
|
||||
fn replace(h: &Host, item: Item, id: &str, params: &Value, key: &str) -> Result<NodeId, HostError> {
|
||||
let before = with_doc(h, item.doc, |_, st| st.selection.objects.clone());
|
||||
let mut before = with_doc(h, item.doc, |_, st| st.selection.clone());
|
||||
on_doc(h, item.doc.uid, false, |h| {
|
||||
let (_, r) = command_value(h, id, params)?;
|
||||
let new = result_id(&r, key).ok_or_else(|| HostError::Error(format!("{id} gave no object")))?;
|
||||
let (want, now) = {
|
||||
let remap = |i: NodeId| if i == item.id { new } else { i };
|
||||
before.objects = before.objects.into_iter().map(remap).collect();
|
||||
let mut seen = HashSet::new();
|
||||
before.objects.retain(|i| seen.insert(*i));
|
||||
before.key = before.key.map(remap);
|
||||
before.target = before.target.map(remap);
|
||||
before.on_path_member = before.on_path_member.map(remap);
|
||||
let restore = {
|
||||
let s = h.session.borrow();
|
||||
let st = s.documents().iter().find(|d| d.uid == item.doc.uid).ok_or_else(|| HostError::Error(super::STALE_DOCUMENT.into()))?;
|
||||
let mut want: Vec<NodeId> = Vec::new();
|
||||
for i in before.iter().map(|i| if *i == item.id { new } else { *i }) {
|
||||
if st.doc.node(i).is_some() && !want.contains(&i) {
|
||||
want.push(i);
|
||||
}
|
||||
}
|
||||
(want, st.selection.objects.clone())
|
||||
(st.selection != before).then(|| st.doc.clone())
|
||||
};
|
||||
if now != want {
|
||||
if want.is_empty() {
|
||||
command(h, "select.none", &json!({}))?;
|
||||
} else {
|
||||
command(h, "select.set", &json!({"ids": want.iter().map(|i| i.0).collect::<Vec<_>>()}))?;
|
||||
}
|
||||
if let Some(doc) = restore {
|
||||
super::restore_selection(h, &doc, &before)?;
|
||||
}
|
||||
Ok(new)
|
||||
})
|
||||
@@ -299,7 +300,10 @@ fn plugin_is_tracing(h: &Host, a: &[Data]) -> Answer {
|
||||
}
|
||||
|
||||
fn plugin_tracing(h: &Host, a: &[Data]) -> Answer {
|
||||
let (_, handle) = plugin_of(h, a)?;
|
||||
let (item, handle) = plugin_of(h, a)?;
|
||||
let source = read_group(h, item, |_, g| g.children().and_then(|c| c.first()).map(|n| n.id).ok_or_else(stale))?;
|
||||
let (_, original) = node_of(h, &handle).ok_or_else(stale)?;
|
||||
h.dom.tracing.sources.borrow_mut().entry((item.doc.uid, original.0)).or_insert(source);
|
||||
Ok(host_ref("TracingObject", &handle))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user