mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Keep File Place import options attached to their drawing
This commit is contained in:
1 parent
86d00a6b04
commit
3299ae6fce
3 files changed
+59
-2
No files matched your search
@@ -84,6 +84,7 @@ pub fn offer(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: Option<St
|
||||
fields.set("hasSlices", FieldValue::Bool(has_slices));
|
||||
let summary = format!("{} × {} px · {} {}", info.width, info.height, crate::i18n::colon(tl!("Layers")), info.layers);
|
||||
let placing = place.is_some();
|
||||
let drawing = place.and_then(|params| crate::place::import_options_drawing(app, params));
|
||||
let source = Source {
|
||||
place: place.map(|p| other_place_params(p, &["layers", "hiddenLayers", "importSlices"])),
|
||||
name: name.to_string(),
|
||||
@@ -93,7 +94,11 @@ pub fn offer(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: Option<St
|
||||
};
|
||||
let ctx = app.ui_ctx.clone().unwrap_or_default();
|
||||
let dialog = HostedParamDialog::new(&ctx, KIND, fields, move |fields| build(fields, placing, &summary)).held_ok();
|
||||
open_held(app, dialog, move |app, result| confirm(app, &source, result)).is_some()
|
||||
open_held(app, dialog, move |app, result| {
|
||||
crate::place::check_import_options_drawing(app, drawing)?;
|
||||
confirm(app, &source, result)
|
||||
})
|
||||
.is_some()
|
||||
}
|
||||
|
||||
/// `file.place` with params `p` asks first (true) when they embed a PSD or PSB file (`link: false`,
|
||||
@@ -269,6 +274,25 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normal_psd_place_options_wait_for_the_original_drawing() {
|
||||
let mut app = host::app();
|
||||
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
|
||||
let encoded = vectorcraft_format::base64_encode(&layered());
|
||||
let params = json!({"name": "poster.psd", "dataBase64": encoded, "at": [50, 50]});
|
||||
assert_eq!(crate::place::run_for_user(&mut app, ¶ms).unwrap()["dialog"], KIND);
|
||||
set(&app, "convert", json!("flatten"));
|
||||
app.run("file.new", json!({"width": 300, "height": 150})).unwrap();
|
||||
let error = host::confirm(&mut app, KIND).expect_err("the other drawing must not receive the file");
|
||||
assert!(error.contains("original drawing"), "{error}");
|
||||
assert!(app.session.active().unwrap().history.undo.is_empty());
|
||||
assert_eq!(host::fields(&app, KIND)["convert"], "flatten");
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
host::confirm(&mut app, KIND).unwrap();
|
||||
let st = app.session.active().unwrap();
|
||||
assert!(matches!(st.doc.layers[0].children().map(Vec::as_slice), Some([node]) if matches!(node.kind, NodeKind::Image(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placing_without_link_asks_and_places_the_layers_as_a_group() {
|
||||
let b64 = vectorcraft_format::base64_encode(&layered());
|
||||
|
||||
@@ -97,8 +97,12 @@ fn starting_fields(app: &VectorcraftApp) -> DialogFields {
|
||||
pub fn open(app: &mut VectorcraftApp, params: &Value) -> Value {
|
||||
let ctx = app.ui_ctx.clone().unwrap_or_default();
|
||||
let dialog = HostedParamDialog::new(&ctx, KIND, starting_fields(app), build).held_ok();
|
||||
let drawing = crate::place::import_options_drawing(app, params);
|
||||
let params = params.clone();
|
||||
open_held(app, dialog, move |app, result| confirm(app, ¶ms, result));
|
||||
open_held(app, dialog, move |app, result| {
|
||||
crate::place::check_import_options_drawing(app, drawing)?;
|
||||
confirm(app, ¶ms, result)
|
||||
});
|
||||
json!({ "dialog": KIND })
|
||||
}
|
||||
|
||||
@@ -244,6 +248,23 @@ mod tests {
|
||||
assert_eq!(app.ui.text_import["removeLineReturns"], true, "remembered for the next file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normal_text_place_options_wait_for_the_original_drawing() {
|
||||
let mut app = host::app();
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
let file = json!({"name": "notes.txt", "dataBase64": vectorcraft_format::base64_encode(b"one\r\ntwo")});
|
||||
assert_eq!(app.run("file.place", file).unwrap()["dialog"], KIND);
|
||||
host::set(&app, KIND, "removeLineReturns", json!(true));
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
let error = host::confirm(&mut app, KIND).expect_err("the other drawing must not receive the file");
|
||||
assert!(error.contains("original drawing"), "{error}");
|
||||
assert!(placed_text(&app).is_none());
|
||||
assert_eq!(host::fields(&app, KIND)["removeLineReturns"], true);
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
host::confirm(&mut app, KIND).unwrap();
|
||||
assert_eq!(placed_text(&app).as_deref(), Some("one two"));
|
||||
}
|
||||
|
||||
/// Every character set is a choice: one confirmed last comes back on the next text file and
|
||||
/// decodes it, an unknown remembered value falls back to Unicode, and with Show Import Options
|
||||
/// off the remembered set is the one the file is placed with.
|
||||
|
||||
@@ -121,6 +121,18 @@ const ASKED: &str = "__placeCursorAsk";
|
||||
/// document; hidden from the dialog's fields and removed before the file is placed.
|
||||
const DROPPED: &str = "__dropTarget";
|
||||
|
||||
/// Normal Place import options wait for their opening drawing. Canvas drops already name their destination.
|
||||
pub(crate) fn import_options_drawing(app: &VectorcraftApp, params: &Value) -> Option<u64> {
|
||||
app.session.active().filter(|_| params.get(DROPPED).is_none()).map(|st| st.uid)
|
||||
}
|
||||
|
||||
pub(crate) fn check_import_options_drawing(app: &VectorcraftApp, drawing: Option<u64>) -> Result<(), String> {
|
||||
if drawing.is_some_and(|drawing| app.session.active().is_none_or(|st| st.uid != drawing)) {
|
||||
return Err(tl!("Return to the original drawing to place the file").into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The `file.place` param that holds the import options a Shift-click of the place cursor set
|
||||
/// aside ([`set_aside_import_options`]): the file is placed with them when no import options dialog
|
||||
/// opens for it. Removed before any dialog opens.
|
||||
|
||||
Reference in new issue
Block a user