mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-04 13:03:19 -05:00
Copy into Grid: say why the item is off, test the tool's refusals
- The undo label is "Copy field into grid" and the parity title "Copy a field into a grid", so no other product's wording is left. The parity notes and the tool text say "value" rather than "initial value", and the notes drop "actions", which no test checks. - form_add_field_table no longer repeats the text-or-check-box test: copy_edit refuses what field_table::copyable says no to, so the tool and the menu refuse the same fields with the same Uncopyable reason. The tool still checks the field and the widget exist. - A disabled Copy into Grid... row says why on hover: several fields are selected, or the field is not a text field or a check box with one export value. - New tests: the tool refuses source_widget without source_field, an unknown field, a radio group, source_widget 0 and a widget past the last, adding nothing; and the hostile-argument sweep sends every hostile value as source_widget, with and without source_field. Co-authored-by: Karim <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
54f1f92d32
commit
cbe3eb63cb
8 files changed
+71
-12
No files matched your search
@@ -551,12 +551,10 @@ impl Automation {
|
||||
let widget = a.opt_int("source_widget")?.unwrap_or(1);
|
||||
let widget = usize::try_from(widget).ok().and_then(|n| n.checked_sub(1)).ok_or_else(|| bad("source_widget must be at least 1"))?;
|
||||
let field = doc.form.iter().find(|f| f.name == source).ok_or_else(|| bad(format!("field {source:?} does not exist")))?;
|
||||
if !matches!(field.kind, pdfcraft_engine::FormFieldKind::Text | pdfcraft_engine::FormFieldKind::CheckBox) {
|
||||
return Err(bad("only text fields and check boxes can be copied into a grid"));
|
||||
}
|
||||
if field.widgets.get(widget).is_none() {
|
||||
return Err(bad("source_widget does not exist"));
|
||||
}
|
||||
// `copy_edit` refuses, with the reason, a field `field_table::copyable` says no to.
|
||||
field_table::copy_edit(&table, page, info, &doc.form, source, widget).map_err(|e| bad(e.to_string()))?
|
||||
} else {
|
||||
if a.get("source_widget").is_some() {
|
||||
|
||||
@@ -596,7 +596,7 @@ pub fn tools() -> Vec<ToolDef> {
|
||||
t(
|
||||
"form_add_field_table",
|
||||
"Prepare form: add a table of text fields",
|
||||
"Divide rect into rows and columns of fields, one per cell, as one undo step. By default these are new text fields; source_field copies an existing text field or check box with its properties and initial value into independent fields. rect in points from the top-left of the displayed page [x0, y0, x1, y1]; rows 1-100 from the top; columns 1-50, or as many as names or widths list. Each column names its cells from its pattern in names: an N on its own after _ - / or : (price_N), # or {row} is the row number, {col} the column number, {seq} counts every cell along the rows, starting at first_row; with none of them the row number goes at the end. Rows count from first_row (default 1). Without names the columns are Table1_N, Table1_c2_N, … using the first free TableK. widths are relative (default equal); row_gap and column_gap are points between cells (default 0). Nothing is added if a cell would be under 4 points or a name would contain a period, repeat, or already exist; the error says which. Returns the fields' names. Undoable.",
|
||||
"Divide rect into rows and columns of fields, one per cell, as one undo step. By default these are new text fields; source_field copies an existing text field or check box with its properties and value into independent fields. rect in points from the top-left of the displayed page [x0, y0, x1, y1]; rows 1-100 from the top; columns 1-50, or as many as names or widths list. Each column names its cells from its pattern in names: an N on its own after _ - / or : (price_N), # or {row} is the row number, {col} the column number, {seq} counts every cell along the rows, starting at first_row; with none of them the row number goes at the end. Rows count from first_row (default 1). Without names the columns are Table1_N, Table1_c2_N, … using the first free TableK. widths are relative (default equal); row_gap and column_gap are points between cells (default 0). Nothing is added if a cell would be under 4 points or a name would contain a period, repeat, or already exist; the error says which. Returns the fields' names. Undoable.",
|
||||
)
|
||||
.cmd("form.add.text_table")
|
||||
.with(schema(
|
||||
|
||||
@@ -6040,6 +6040,32 @@ fn copying_a_field_into_a_grid_through_tools() {
|
||||
assert_eq!(result["fields"].as_array().unwrap().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copying_into_a_grid_refuses_what_it_cannot_copy() {
|
||||
let dir = workdir("copy-field-grid-refusals");
|
||||
let mut a = auto(&dir);
|
||||
let doc = ok(&mut a, "doc_open", json!({"path": "a.pdf"}))["doc"].as_u64().unwrap();
|
||||
ok(&mut a, "form_add_field", json!({"doc": doc, "page": 1, "type": "text", "name": "source", "rect": [20, 20, 180, 50]}));
|
||||
ok(&mut a, "form_add_field", json!({"doc": doc, "page": 1, "type": "radio", "rect": [20, 100, 34, 114], "group": "size", "export": "S"}));
|
||||
ok(&mut a, "form_add_field", json!({"doc": doc, "page": 1, "type": "radio", "rect": [40, 100, 54, 114], "group": "size", "export": "L"}));
|
||||
let grid = |extra: Value| {
|
||||
let mut args = json!({"doc": doc, "page": 1, "rect": [20, 150, 180, 230], "rows": 2});
|
||||
args.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
|
||||
args
|
||||
};
|
||||
let refused = |a: &mut Automation, extra: Value, says: &str| match err(a, "form_add_field_table", grid(extra.clone())) {
|
||||
ToolError::InvalidArgs(m) => assert!(m.contains(says), "{extra}: {m}"),
|
||||
other => panic!("{extra}: expected InvalidArgs, got {other:?}"),
|
||||
};
|
||||
refused(&mut a, json!({"source_widget": 1}), "source_widget requires source_field");
|
||||
refused(&mut a, json!({"source_field": "nowhere"}), "\"nowhere\" does not exist");
|
||||
refused(&mut a, json!({"source_field": "size"}), "can't be copied into a grid");
|
||||
refused(&mut a, json!({"source_field": "source", "source_widget": 0}), "source_widget must be at least 1");
|
||||
refused(&mut a, json!({"source_field": "source", "source_widget": 2}), "source_widget does not exist");
|
||||
// Each was refused before anything was added.
|
||||
assert_eq!(ok(&mut a, "form_fields", json!({"doc": doc}))["fields"].as_array().unwrap().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_mark_respects_single_page_and_rejects_conflicting_selectors() {
|
||||
let dir = workdir("redact-single-page");
|
||||
|
||||
@@ -562,3 +562,29 @@ fn no_tool_panics_when_the_viewer_sees_fewer_pages_than_the_file_has() {
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
assert!(failures.is_empty(), "{} failure(s); the first is: {}", failures.len(), failures.first().map_or("", String::as_str));
|
||||
}
|
||||
|
||||
/// `source_widget` is 1-based and counted against the source's widgets: no value of it but the
|
||||
/// source's one widget (or null, which is the default), nor the key without `source_field`,
|
||||
/// panics or adds a field.
|
||||
#[test]
|
||||
fn hostile_source_widgets_are_refused_without_a_panic() {
|
||||
let mut a = Automation::new().with_root(tmp("grid-source-widget")).expect("root");
|
||||
let doc = blank_doc(&mut a);
|
||||
a.call("form_add_field", &json!({"doc": doc, "page": 1, "type": "text", "name": "source", "rect": [20, 20, 180, 50]})).expect("add the source");
|
||||
let mut failures = Vec::new();
|
||||
for value in zoo().into_iter().chain([json!(2), json!(usize::MAX)]).filter(|v| *v != json!(1) && !v.is_null()) {
|
||||
let rect = json!([20, 100, 180, 180]);
|
||||
for args in [
|
||||
json!({"doc": doc, "page": 1, "rect": rect, "rows": 2, "source_field": "source", "source_widget": value}),
|
||||
json!({"doc": doc, "page": 1, "rect": rect, "rows": 2, "source_widget": value}),
|
||||
] {
|
||||
match catch_unwind(AssertUnwindSafe(|| a.call("form_add_field_table", &args))) {
|
||||
Ok(Err(ToolError::InvalidArgs(_) | ToolError::Failed(_))) => {}
|
||||
Ok(Ok(_)) => failures.push(format!("{args} was accepted")),
|
||||
Ok(Err(e)) => failures.push(format!("{args}: {e:?}")),
|
||||
Err(_) => failures.push(format!("{args} panicked")),
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(failures.is_empty(), "{failures:#?}");
|
||||
}
|
||||
@@ -13,7 +13,7 @@ use crate::{Edit, FormField, FormFieldKind as FieldKind, NewField};
|
||||
pub const LABEL: &str = "Add text field table";
|
||||
|
||||
/// The history entry for a grid copied from an existing field.
|
||||
pub const COPY_LABEL: &str = "Create multiple field copies";
|
||||
pub const COPY_LABEL: &str = "Copy field into grid";
|
||||
|
||||
/// The edit adding `table`'s cells to `page` (0-based, shown as `info`) of a document whose
|
||||
/// fields are `form`. Refused, with nothing added, when a cell would be too small on the page or
|
||||
|
||||
@@ -267,10 +267,19 @@ fn field_rows(
|
||||
if MenuItem::new(tl!("Properties…").to_string()).show(ui).ran {
|
||||
pick.field = Some(FieldMenu::Properties);
|
||||
}
|
||||
// One field at a time, of a kind a grid can copy.
|
||||
let copyable = view.session.prepare.also.is_empty()
|
||||
&& form.iter().find(|f| f.name == name).is_some_and(|f| pdfcraft_engine::field_table::copyable(f, widget));
|
||||
if MenuItem::new(tl!("Copy into Grid…").to_string()).enabled(can_modify && copyable).show(ui).ran {
|
||||
// One field at a time, of a kind a grid can copy; a disabled row says which it is not.
|
||||
let why_not = if !view.session.prepare.also.is_empty() {
|
||||
Some(tl!("Select one field to copy it into a grid"))
|
||||
} else if !form.iter().find(|f| f.name == name).is_some_and(|f| pdfcraft_engine::field_table::copyable(f, widget)) {
|
||||
Some(tl!("Only text fields and check boxes with one export value can be copied into a grid"))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut copies = MenuItem::new(tl!("Copy into Grid…").to_string()).enabled(can_modify && why_not.is_none());
|
||||
if let Some(why) = why_not {
|
||||
copies = copies.tooltip(craft::ui::contract::TooltipSpec::new(why).disabled_text(why));
|
||||
}
|
||||
if copies.show(ui).ran {
|
||||
pick.field = Some(FieldMenu::Copies(name.clone(), widget));
|
||||
}
|
||||
if MenuItem::new(tl!("Duplicate…").to_string()).enabled(can_modify).show(ui).ran {
|
||||
|
||||
@@ -1090,7 +1090,7 @@ fn copying_a_field_into_a_grid_from_its_context_menu() {
|
||||
let f = doc.form.iter().find(|f| &f.name == name).unwrap();
|
||||
assert_eq!((f.flags, doc.field_look(name).unwrap()), styled, "{name} keeps the day's multi-line flag, background and border");
|
||||
}
|
||||
assert_eq!(doc.can_undo(), Some("Create multiple field copies"));
|
||||
assert_eq!(doc.can_undo(), Some("Copy field into grid"));
|
||||
h.execute("edit.undo");
|
||||
h.run_steps(2);
|
||||
assert_eq!(names(&h), before, "one undo takes every copy back");
|
||||
|
||||
@@ -5249,7 +5249,7 @@ notes = "right-click a field ▸ Duplicate: all pages or a range; one field, sha
|
||||
|
||||
[[feature]]
|
||||
id = "form.multiple-copies"
|
||||
title = "Create multiple copies of a field in a grid"
|
||||
title = "Copy a field into a grid"
|
||||
area = "F"
|
||||
tier = "P2"
|
||||
milestone = "M6"
|
||||
@@ -5266,7 +5266,7 @@ evidence = [
|
||||
"crates/automation/tests/it/automation.rs::copying_a_field_into_a_grid_through_tools",
|
||||
"crates/ui-egui/tests/it/prepare.rs::copying_a_field_into_a_grid_from_its_context_menu",
|
||||
]
|
||||
notes = "Prepare a form ▸ Text field table: drag an area, then set rows, columns, spacing, column widths (draggable dividers) and a name pattern per column in a movable window, with the table previewed on the page. Added as one undoable edit, or refused whole with the reason (a cell under 4 pt, a name with a period, a repeated or existing name). Copy into Grid in a text field or check box's menu uses the same window to repeat that field in rows and columns beside it: each copy is an independently named field with the source's flags, look, actions and initial value and appearance streams of its own, previewed on the page and added as one undoable edit; form_add_field_table does the same with source_field. Missing: copying radio groups, choice fields, buttons, signatures and check boxes with per-widget export values (refused with the reason)."
|
||||
notes = "Prepare a form ▸ Text field table: drag an area, then set rows, columns, spacing, column widths (draggable dividers) and a name pattern per column in a movable window, with the table previewed on the page. Added as one undoable edit, or refused whole with the reason (a cell under 4 pt, a name with a period, a repeated or existing name). Copy into Grid in a text field or check box's menu uses the same window to repeat that field in rows and columns beside it: each copy is an independently named field with the source's flags, look and value and appearance streams of its own, previewed on the page and added as one undoable edit; form_add_field_table does the same with source_field. Missing: copying radio groups, choice fields, buttons, signatures and check boxes with per-widget export values (refused with the reason)."
|
||||
|
||||
[[feature]]
|
||||
id = "form.rename-lock-fields"
|
||||
|
||||
Reference in new issue
Block a user