mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 20:43:36 +00:00
Merge pull request #7217 from dotCooCoo/fix/brush-repeated-names-numbered
Brushes: repeated brush names are numbered when a document loads
This commit is contained in:
@@ -721,3 +721,24 @@ fn other_documents_brushes_load_and_unreadable_files_are_errors() {
|
||||
let bytes = vectorcraft_format::base64_decode(r["dataBase64"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(vectorcraft_brush::library(&vectorcraft_format::load(&bytes).unwrap()).len(), 1);
|
||||
}
|
||||
|
||||
/// A library file whose brushes repeat a name loads with the later brush numbered, and each one
|
||||
/// can be added to a document by its own name.
|
||||
#[test]
|
||||
fn a_library_file_that_repeats_a_brush_name_loads_with_the_later_one_numbered() {
|
||||
use vectorcraft_brush::{Brush, Calligraphic};
|
||||
let nib = |size: f64| Brush { name: "Nib".into(), kind: BrushKind::Calligraphic(Calligraphic { size, ..Default::default() }) };
|
||||
let mut file = vectorcraft_doc::Document::new(100.0, 100.0);
|
||||
vectorcraft_brush::store(&mut file, &[nib(1.0), nib(2.0)]);
|
||||
let b64 = vectorcraft_format::base64_encode(&vectorcraft_format::save_file(&file));
|
||||
let mut s = session();
|
||||
let r = run(&mut s, "brush.loadLibrary", json!({"dataBase64": b64, "name": "Nibs.vectorcraft"}));
|
||||
let library = r["library"].as_str().unwrap().to_string();
|
||||
let listed = run(&mut s, "brush.library", json!({ "library": library }));
|
||||
let names: Vec<&str> = listed["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap()).collect();
|
||||
assert_eq!(names, ["Nib", "Nib 2"]);
|
||||
let r = run(&mut s, "brush.addFromLibrary", json!({"library": library, "name": "Nib 2"}));
|
||||
assert_eq!(strs(&r["added"]), ["Nib 2"]);
|
||||
let size = |b: Brush| if let BrushKind::Calligraphic(c) = b.kind { c.size } else { 0.0 };
|
||||
assert_eq!(size(brush(&s, "Nib 2")), 2.0);
|
||||
}
|
||||
|
||||
@@ -631,6 +631,37 @@ fn brush_options_rename_duplicate_and_delete_update_strokes() {
|
||||
assert!(s.execute("brush.delete", &json!({"name": "5 pt. Round"})).is_err());
|
||||
}
|
||||
|
||||
/// A document whose brushes repeat a name opens with the later brush numbered: strokes of the name
|
||||
/// keep the first brush, and renaming or deleting one brush leaves the other and its strokes alone.
|
||||
#[test]
|
||||
fn a_repeated_brush_name_is_numbered_when_the_document_opens() {
|
||||
use vectorcraft_brush::{Brush, BrushKind, Calligraphic};
|
||||
let nib = |size: f64| Brush { name: "Nib".into(), kind: BrushKind::Calligraphic(Calligraphic { size, ..Default::default() }) };
|
||||
let mut s = session();
|
||||
vectorcraft_brush::store(std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc), &[nib(1.0), nib(2.0)]);
|
||||
let a = rect(&mut s, 10.0, 10.0, 50.0, 50.0);
|
||||
s.execute("brush.apply", &json!({"name": "Nib", "ids": [a.0]})).unwrap();
|
||||
let saved = s.execute("document.serialize", &json!({})).unwrap()["dataBase64"].clone();
|
||||
let mut o = Session::new();
|
||||
o.execute("document.open", &json!({"name": "a.vectorcraft", "dataBase64": saved})).unwrap();
|
||||
let names = |o: &mut Session| -> Vec<String> {
|
||||
let list = o.execute("brush.list", &json!({})).unwrap();
|
||||
list["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap().to_string()).collect()
|
||||
};
|
||||
assert_eq!(names(&mut o), ["Nib", "Nib 2"]);
|
||||
assert_eq!(brush_of(&o, a).as_deref(), Some("Nib"));
|
||||
assert_eq!(o.execute("brush.get", &json!({"name": "Nib"})).unwrap()["size"], json!(1.0));
|
||||
assert_eq!(o.execute("brush.get", &json!({"name": "Nib 2"})).unwrap()["size"], json!(2.0));
|
||||
let b = rect(&mut o, 80.0, 10.0, 50.0, 50.0);
|
||||
o.execute("brush.apply", &json!({"name": "Nib 2", "ids": [b.0]})).unwrap();
|
||||
o.execute("brush.options", &json!({"name": "Nib", "newName": "Pen"})).unwrap();
|
||||
assert_eq!(names(&mut o), ["Pen", "Nib 2"]);
|
||||
assert_eq!((brush_of(&o, a).as_deref(), brush_of(&o, b).as_deref()), (Some("Pen"), Some("Nib 2")));
|
||||
o.execute("brush.delete", &json!({"name": "Nib 2"})).unwrap();
|
||||
assert_eq!(names(&mut o), ["Pen"]);
|
||||
assert_eq!((brush_of(&o, a).as_deref(), brush_of(&o, b)), (Some("Pen"), None));
|
||||
}
|
||||
|
||||
/// `brush.move`, which the Brushes panel runs when a brush is dragged, reorders brushes among those of
|
||||
/// their type in one undo step and refuses to move one into another type.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user