Links: Unembed takes ids of one image as id

links.unembed now reads `ids` of one image as `id`, and `id` with
`folder` as `ids` of that image, as the other commands that name both
keys do. `id` with `ids`, and `path` with `folder`, are refused with the
shared "give `a` or `b`, not both" error, in a replayed step too, and the
catalog rows for the old combined message are removed.
This commit is contained in:
dotCooCoo
2026-11-16 09:15:37 -08:00
parent 7f72a7c46d
commit 5ffe485da1
28 changed files with 46 additions and 43 deletions
+19 -11
View File
@@ -108,7 +108,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Unembed",
[],
None,
"{id?: an embedded image (default: the one selected), path?, ids?: [image ids] (with folder; default: the selected embedded images), folder?} write the image's pixels to the file at path (PNG, JPEG, GIF or WebP as stored, other images as PNG; a path ending in .png/.jpg/.jpeg/.gif/.webp/.tif/.tiff/.bmp converts them) and link the image to it, keeping its bounds. One undo step → {id, path}; no path → {name, dataBase64} (nothing changes: there is no file to link to). Several images, with ids and folder: write each embedded image to a new file in folder, as stored (as PNG when that format is not one of VectorCraft's raster file formats), named after the image (characters a file name can't hold become _; Image when nothing is left), with Name 2 … Name 99 when the name is taken; a file already there is never replaced. Linked images among ids are left as they are. Each image links to its file and keeps its bounds. One undo step (undo does not remove the files); when a file can't be written, the files this call wrote are removed and nothing changes → {unembedded: [{id, path}], skipped: [ids of linked images]}; id or path with ids or folder is an error",
"{id?: an embedded image (default: the one selected), path?, ids?: [image ids] (instead of id; one id is the same as id), folder?} write the image's pixels to the file at path (PNG, JPEG, GIF or WebP as stored, other images as PNG; a path ending in .png/.jpg/.jpeg/.gif/.webp/.tif/.tiff/.bmp converts them) and link the image to it, keeping its bounds. One undo step → {id, path}; no path → {name, dataBase64} (nothing changes: there is no file to link to). With folder: write each embedded image of ids or id (default: the selected embedded images) to a new file in folder, as stored (as PNG when that format is not one of VectorCraft's raster file formats), named after the image (characters a file name can't hold become _; Image when nothing is left), with Name 2 … Name 99 when the name is taken; a file already there is never replaced. Linked images among ids are left as they are. Each image links to its file and keeps its bounds. One undo step (undo does not remove the files); when a file can't be written, the files this call wrote are removed and nothing changes → {unembedded: [{id, path}], skipped: [ids of linked images]}; id with ids, path with folder, and ids of more or fewer than one image without folder are errors",
has_doc,
unembed
),
@@ -2685,15 +2685,19 @@ fn unembed_bytes(blob: &vectorcraft_doc::ImageBlob, ext: &str) -> std::result::R
fn unembed(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "links.unembed";
let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null());
if given("ids") || given("folder") {
if given("id") || given("path") {
return Err(bad(C, "give id and path for one image, or ids and folder for several"));
}
return unembed_to_folder(s, p);
// `id` with `ids`, and `path` with `folder`, are refused, in a replayed step too.
super::params::check_exclusive_keys(C, p)?;
let ids = ids_param(p, "ids", C)?;
if given("folder") || ids.as_ref().is_some_and(|ids| ids.len() != 1) {
return unembed_to_folder(s, p, ids);
}
let st = s.doc()?;
let doc = with_brush_art(&st.doc);
let id = image_param(st, &doc, p, C)?;
// `ids` of one image is `id`.
let id = match ids.as_deref() {
Some(&[id]) => id,
_ => image_param(st, &doc, p, C)?,
};
let n = stored(&doc, id).ok_or(crate::EngineError::NoNode(id))?;
let im = image_of(&doc, id).ok_or_else(|| bad(C, format!("object {} is not an image", id.0)))?;
if im.link.is_some() {
@@ -2724,16 +2728,20 @@ fn unembed(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({ "id": id.0, "path": path }))
}
/// `links.unembed {ids?, folder}`: each embedded image of `ids` (default: the selected embedded
/// images) written to a new file in `folder` as stored (as PNG when that format is not one of
/// `links.unembed {ids?, folder}`: each embedded image of `ids` (`id` as `ids` of one; default: the
/// selected embedded images) written to a new file in `folder` as stored (as PNG when that format is not one of
/// VectorCraft's raster file formats) and linked to it, in one undo step. Linked images are skipped. When any image fails, the files written before it
/// are removed and the document does not change.
fn unembed_to_folder(s: &mut Session, p: &Value) -> Result<Value> {
fn unembed_to_folder(s: &mut Session, p: &Value, ids: Option<Vec<NodeId>>) -> Result<Value> {
const C: &str = "links.unembed";
let folder = text_param(p, "folder", C)?.filter(|f| !f.is_empty()).ok_or_else(|| bad(C, "missing `folder`"))?;
let ids = match ids {
Some(ids) => Some(ids),
None => object_param(p, "id", C)?.map(|id| vec![id]),
};
let st = s.doc()?;
let doc = with_brush_art(&st.doc);
let ids = ids_param(p, "ids", C)?.unwrap_or_else(|| selected_images(st, |im| im.link.is_none()));
let ids = ids.unwrap_or_else(|| selected_images(st, |im| im.link.is_none()));
let mut seen = BTreeSet::new();
if !ids.iter().all(|id| seen.insert(id.0)) {
return Err(bad(C, "`ids` names an object twice"));
+7 -2
View File
@@ -258,11 +258,16 @@ pub fn check_keys(id: &str, doc: &'static str, params: &Value) -> Result<()> {
/// Parameters a command takes instead of one another: (command, key, other key). Giving both,
/// neither null, is a parameter error ([`check_exclusive_keys`]).
const EXCLUSIVE_KEYS: &[(&str, &str, &str)] = &[("addon.pathtools.cornerSet", "items", "ids"), ("addon.pathtools.cornerRemove", "items", "ids")];
const EXCLUSIVE_KEYS: &[(&str, &str, &str)] = &[
("addon.pathtools.cornerSet", "items", "ids"),
("addon.pathtools.cornerRemove", "items", "ids"),
("links.unembed", "id", "ids"),
("links.unembed", "path", "folder"),
];
/// `params` of command `id` give at most one of each pair in [`EXCLUSIVE_KEYS`], or the parameter
/// error naming the pair.
fn check_exclusive_keys(id: &str, params: &Value) -> Result<()> {
pub(crate) fn check_exclusive_keys(id: &str, params: &Value) -> Result<()> {
let given = |key: &str| params.get(key).filter(|v| !v.is_null());
for (_, key, other) in EXCLUSIVE_KEYS.iter().filter(|(c, ..)| *c == id) {
if given(key).is_some() && given(other).is_some() {
+16 -4
View File
@@ -253,11 +253,20 @@ fn unembed_to_a_folder_writes_each_image_and_links_it_in_one_undo_step() {
run(&mut s, "edit.undo", json!({}));
assert!(image(&s, a).link.is_none() && image(&s, b).link.is_none(), "both embedded again");
assert_eq!(files_in(&out).len(), 3, "undo leaves the files");
// `ids` of one image without `folder` is `id`: the bytes, and nothing changes.
let one = run(&mut s, "links.unembed", json!({"ids": [a.0]}));
assert_eq!(one, run(&mut s, "links.unembed", json!({"id": a.0})));
assert!(one["dataBase64"].is_string(), "{one}");
assert!(image(&s, a).link.is_none());
// With no ids, the selected embedded images.
s.doc_mut().unwrap().selection.objects = vec![b, linked];
let r = run(&mut s, "links.unembed", json!({"folder": out}));
assert_eq!(r, json!({"unembedded": [{"id": b.0, "path": file_in(&out, "photo 4.png")}], "skipped": []}));
assert_eq!(std::fs::read(file_in(&out, "photo 4.png")).unwrap(), blue);
// `id` with `folder` is `ids` of that one image.
let r = run(&mut s, "links.unembed", json!({"id": a.0, "folder": out}));
assert_eq!(r, json!({"unembedded": [{"id": a.0, "path": file_in(&out, "photo 5.png")}], "skipped": []}));
assert_eq!(image(&s, a).link.unwrap().path, file_in(&out, "photo 5.png"));
}
/// A batch that fails part way writes no file and changes nothing; requests that don't fit the
@@ -285,11 +294,14 @@ fn unembed_to_a_folder_that_fails_leaves_no_file_and_no_change() {
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).images.insert(key, saved);
let refused = |s: &mut Session, p: Value| s.execute("links.unembed", &p).unwrap_err().to_string();
let both = "give id and path for one image, or ids and folder for several";
let both = "give `path` or `folder`, not both";
assert!(refused(&mut s, json!({"ids": [a.0, a.0], "folder": out})).ends_with("`ids` names an object twice"));
assert!(refused(&mut s, json!({"id": a.0, "folder": out})).ends_with(both));
assert!(refused(&mut s, json!({"ids": [a.0], "path": file_in(&out, "x.png")})).ends_with(both));
assert!(refused(&mut s, json!({"ids": [a.0]})).ends_with("missing `folder`"));
assert!(refused(&mut s, json!({"id": a.0, "path": file_in(&out, "x.png"), "folder": out})).ends_with(both));
assert!(refused(&mut s, json!({"ids": [a.0], "path": file_in(&out, "x.png"), "folder": out})).ends_with(both));
assert!(refused(&mut s, json!({"ids": [a.0, b.0]})).ends_with("missing `folder`"));
assert!(refused(&mut s, json!({"ids": []})).ends_with("missing `folder`"));
assert!(refused(&mut s, json!({"id": a.0, "ids": [b.0], "folder": out})).ends_with("give `id` or `ids`, not both"));
assert!(refused(&mut s, json!({"id": a.0, "ids": [b.0]})).ends_with("give `id` or `ids`, not both"));
assert!(refused(&mut s, json!({"ids": [a.0], "folder": 5})).ends_with("`folder` must be a string"));
assert!(refused(&mut s, json!({"ids": [], "folder": out})).ends_with("select an image"));
let file = dir.file("one.png");