mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 15:21:17 -05:00
Scripts: textFrames.removeAll() refuses type on a locked artboard
- textFrames.removeAll() deleted a text frame on a locked artboard, although its editable property reads false. It now throws "cannot delete objects on a locked artboard" with the ids before anything is deleted, as pathItems.removeAll() and remove() do. - add(contents, relativeObject) with a text range of a removed frame threw a type error. It now throws "Stale text range"; only an argument of another class gets the type error. - The hidden or locked edit test starts each case from a fresh frame, and checks run sizes and justification after each refused edit. The add test also checks the text of the frame the script edits.
This commit is contained in:
@@ -48,9 +48,9 @@ use vectorcraft_doc::{CharStyle, Justify, TextKind, TextObject};
|
||||
use vectorcraft_js::{Data, HostClass, HostError, HostFunction, HostItem, HostMember, Realm, RunError, host_ref};
|
||||
|
||||
use super::{
|
||||
Answer, Doc, Host, Item, Link, NOT_FOUND, STALE_ITEM, all_editable, arg, boolean, class, colors, command, doc_of, doc_ref, edit_item, editable,
|
||||
float, frame, function, get, handle_of, id_parts, item_of, item_ref, method, named, num, number, on_doc, page_items, point, property, text,
|
||||
typename_of, whole, with_doc,
|
||||
Answer, Doc, Host, Item, Link, NOT_FOUND, ON_LOCKED_ARTBOARD, STALE_ITEM, all_editable, arg, boolean, class, colors, command, doc_of, doc_ref,
|
||||
edit_item, editable, float, frame, function, get, handle_of, id_parts, item_of, item_ref, method, named, num, number, on_doc, page_items, point,
|
||||
property, text, typename_of, whole, with_doc,
|
||||
};
|
||||
use crate::{DocState, Document, NodeId, NodeKind};
|
||||
|
||||
@@ -744,8 +744,10 @@ fn part_add(h: &Host, a: &[Data], before: bool) -> Answer {
|
||||
let rel = arg(a, 2);
|
||||
let target = if handle_of(rel, "TextFrame").is_some() {
|
||||
whole_span(h, item_of(h, rel)?)?.1
|
||||
} else if handle_of(rel, "TextRange").is_some() {
|
||||
span_of(h, rel, &["TextRange"])?
|
||||
} else {
|
||||
span_of(h, rel, &["TextRange"]).map_err(|_| HostError::Type("relativeObject must be a TextRange or a TextFrame".into()))?
|
||||
return Err(HostError::Type("relativeObject must be a TextRange or a TextFrame".into()));
|
||||
};
|
||||
if target.item.doc.uid != r.item.doc.uid || target.item.id != r.item.id {
|
||||
return Err(HostError::Error("relativeObject must be text of the same text frame".into()));
|
||||
@@ -1381,19 +1383,26 @@ fn frames_path_text(h: &Host, a: &[Data]) -> Answer {
|
||||
in_path(h, a, true, &[2, 3, 5, 6])
|
||||
}
|
||||
|
||||
/// `removeAll()`: deletes every type object in one command; a hidden or locked one stops it first.
|
||||
/// `removeAll()`: deletes every type object in one command; a hidden or locked one, or one on a
|
||||
/// locked artboard, stops it first.
|
||||
fn frames_remove_all(h: &Host, a: &[Data]) -> Answer {
|
||||
let doc = frames_doc(h, a)?;
|
||||
let (ids, refused) = with_doc(h, doc, |_, st| {
|
||||
let (ids, refused, on_locked) = with_doc(h, doc, |_, st| {
|
||||
let ids = frame_ids(h, st);
|
||||
let ok = |id: &NodeId| editable(&st.doc, *id) && st.doc.node(*id).is_some_and(all_editable);
|
||||
let refused: Vec<u64> = ids.iter().filter(|i| !ok(i)).map(|i| i.0).take(20).collect();
|
||||
(ids, refused)
|
||||
let locks = st.doc.locked_artboards();
|
||||
let on_locked: Vec<u64> = ids.iter().filter(|i| locks.holds_id(**i)).map(|i| i.0).take(20).collect();
|
||||
(ids, refused, on_locked)
|
||||
});
|
||||
if !refused.is_empty() {
|
||||
let list = refused.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
|
||||
return Err(HostError::Error(format!("cannot delete hidden or locked type: {list}")));
|
||||
}
|
||||
if !on_locked.is_empty() {
|
||||
let list = on_locked.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
|
||||
return Err(HostError::Error(format!("{ON_LOCKED_ARTBOARD}: {list}")));
|
||||
}
|
||||
if !ids.is_empty() {
|
||||
on_doc(h, doc.uid, true, |h| command(h, "edit.clear", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})))?;
|
||||
}
|
||||
|
||||
@@ -299,8 +299,10 @@ fn bad_text_paint_is_refused_without_a_change() {
|
||||
|
||||
#[test]
|
||||
fn hidden_or_locked_type_is_not_edited() {
|
||||
let (mut s, id) = doc_with("abc");
|
||||
let (runs, text) = (paints(&s, id), plain(&s, id));
|
||||
let state = |s: &Session, id: NodeId| match &s.active().unwrap().doc.node(id).unwrap().kind {
|
||||
NodeKind::Text(t) => (paints(s, id), plain(s, id), sizes(s, id), t.para_at(0).justify),
|
||||
_ => panic!("not text"),
|
||||
};
|
||||
for hide in ["t.locked = true;", "t.hidden = true;"] {
|
||||
for edit in [
|
||||
"r.characters[1].fillColor = red;",
|
||||
@@ -309,14 +311,17 @@ fn hidden_or_locked_type_is_not_edited() {
|
||||
"r.characters.add('!');",
|
||||
"r.justification = Justification.CENTER;",
|
||||
] {
|
||||
// Each case starts from a fresh frame, so the hidden cases test a frame that is only hidden.
|
||||
let (mut s, id) = doc_with("abc");
|
||||
let before = state(&s, id);
|
||||
let e = failure(
|
||||
&mut s,
|
||||
&format!("var t = app.activeDocument.textFrames[0], r = t.textRange, red = new RGBColor(); red.red = 255; {hide} {edit}"),
|
||||
);
|
||||
assert!(e.contains("Cannot edit TextFrame"), "{hide} {edit}: {e}");
|
||||
assert_eq!(state(&s, id), before, "{hide} {edit}");
|
||||
}
|
||||
}
|
||||
assert_eq!((paints(&s, id), plain(&s, id)), (runs, text));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -605,7 +610,7 @@ fn collections_of_text_add_words_characters_and_paragraphs() {
|
||||
}
|
||||
// Empty contents add nothing; bad arguments throw before anything changes.
|
||||
let (mut s, id) = doc_with("ab");
|
||||
s.execute("text.create", &json!({"x": 20, "y": 200, "text": "other", "size": 12})).unwrap();
|
||||
let other = NodeId(s.execute("text.create", &json!({"x": 20, "y": 200, "text": "other", "size": 12})).unwrap()["id"].as_u64().unwrap());
|
||||
let before = undo_steps(&s);
|
||||
let out = log(&mut s, "var n = app.activeDocument.textFrames[0].words.add(''); vc.log([n.length, n.start]);");
|
||||
assert_eq!(out, vec![json!([0, 5])]);
|
||||
@@ -620,7 +625,35 @@ fn collections_of_text_add_words_characters_and_paragraphs() {
|
||||
assert!(e.contains(want), "{call}: {e}");
|
||||
}
|
||||
assert_eq!(undo_steps(&s), before);
|
||||
assert_eq!(plain(&s, other), "other");
|
||||
assert_eq!(plain(&s, id), "ab");
|
||||
// A range of a frame that was removed is stale, not of the wrong class.
|
||||
let e = failure(&mut s, "var f = app.activeDocument.textFrames, b = f[0], a = f[1], r = b.textRange; b.remove(); a.characters.add('x', r);");
|
||||
assert!(e.contains("Stale text range"), "{e}");
|
||||
assert_eq!(plain(&s, id), "ab");
|
||||
}
|
||||
|
||||
/// Type on a locked artboard stays: `removeAll()` throws and deletes nothing, as
|
||||
/// `pathItems.removeAll()` does.
|
||||
#[test]
|
||||
fn text_frames_remove_all_refuses_type_on_a_locked_artboard() {
|
||||
let (mut s, id) = doc_with("ab");
|
||||
let lock = |s: &mut Session, locked: bool| {
|
||||
s.edit("Lock Artboard", |d, _| {
|
||||
d.artboards[0].locked = locked;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
};
|
||||
lock(&mut s, true);
|
||||
let steps = undo_steps(&s);
|
||||
let e = failure(&mut s, "app.activeDocument.textFrames.removeAll();");
|
||||
assert!(e.contains("cannot delete objects on a locked artboard") && e.contains(&id.0.to_string()), "{e}");
|
||||
assert_eq!(plain(&s, id), "ab");
|
||||
assert_eq!(undo_steps(&s), steps);
|
||||
lock(&mut s, false);
|
||||
log(&mut s, "app.activeDocument.textFrames.removeAll();");
|
||||
assert_eq!(log(&mut s, "vc.log(app.activeDocument.textFrames.length);"), vec![json!(0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user