mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-19 00:27:11 +00:00
Merge pull request #2954 from makesomethingshit/fix/2694-idml-data-merge-counts
Data Merge: count fields after reopening IDML (#2694)
This commit is contained in:
3 files changed
+92
-3
No files matched your search
@@ -147,8 +147,27 @@ fn fields_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let Some(src) = st.doc.data_merge.sources.first() else {
|
||||
return Ok(json!([]));
|
||||
};
|
||||
let placeholders = st.doc.data_merge.placeholders.clone();
|
||||
Ok(fields_json(&src.fields, |name| placeholders.iter().filter(|ph| ph.field == name).count()))
|
||||
let doc = st.preview_stash.as_deref().unwrap_or(st.doc.as_ref());
|
||||
let placeholders = &doc.data_merge.placeholders;
|
||||
let mut marker_counts = std::collections::HashMap::<String, usize>::new();
|
||||
for (story_id, story) in &doc.stories {
|
||||
// The generated endnote frame repeats note text already counted in its owning story.
|
||||
if Some(*story_id) == doc.endnote_story {
|
||||
continue;
|
||||
}
|
||||
for (start, end, name) in fill::find_markers(&story.text) {
|
||||
// A bound placeholder already accounts for its marker in the usage count.
|
||||
let covered = placeholders.iter().any(|ph| {
|
||||
matches!((&ph.role, &ph.anchor), (PlaceholderRole::Text | PlaceholderRole::Image, PlaceholderAnchor::Text { story, start: a, end: b })
|
||||
if story == story_id && start < *b && end > *a)
|
||||
});
|
||||
if !covered {
|
||||
*marker_counts.entry(name).or_default() += 1;
|
||||
}
|
||||
}
|
||||
fill::count_nested_markers(story, &mut marker_counts, 0);
|
||||
}
|
||||
Ok(fields_json(&src.fields, |name| placeholders.iter().filter(|ph| ph.field == name).count() + marker_counts.get(name).copied().unwrap_or(0)))
|
||||
}
|
||||
|
||||
fn placeholder_add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -417,6 +417,26 @@ fn fill_inner(st: &mut Story, fields: &[DataField], row: &[String], depth: usize
|
||||
}
|
||||
}
|
||||
|
||||
/// Count markers in nested text to match the merge's depth limit.
|
||||
pub(super) fn count_nested_markers(st: &Story, counts: &mut HashMap<String, usize>, depth: usize) {
|
||||
if depth >= MAX_NESTED_DEPTH {
|
||||
return;
|
||||
}
|
||||
let mut count = |inner: &Story| {
|
||||
for (_, _, name) in find_markers(&inner.text) {
|
||||
*counts.entry(name).or_default() += 1;
|
||||
}
|
||||
count_nested_markers(inner, counts, depth + 1);
|
||||
};
|
||||
for table in st.tables.values() {
|
||||
for cell in &table.cells {
|
||||
count(&cell.text);
|
||||
}
|
||||
}
|
||||
for note in st.notes.iter().chain(st.endnotes.iter()) {
|
||||
count(¬e.text);
|
||||
}
|
||||
}
|
||||
/// Replace the markers of known fields in `st`'s own text with the record's values.
|
||||
fn fill_own(st: &mut Story, fields: &[DataField], row: &[String]) {
|
||||
let found: Vec<(usize, usize, String)> =
|
||||
@@ -426,7 +446,7 @@ fn fill_own(st: &mut Story, fields: &[DataField], row: &[String]) {
|
||||
}
|
||||
}
|
||||
|
||||
fn find_markers(text: &str) -> Vec<(usize, usize, String)> {
|
||||
pub(super) fn find_markers(text: &str) -> Vec<(usize, usize, String)> {
|
||||
let mut out = Vec::new();
|
||||
let mut i = 0;
|
||||
// The next `>>` at or after the `<<` being looked at: found once, kept until passed, so the
|
||||
|
||||
@@ -1654,8 +1654,10 @@ fn a_field_converted_into_a_table_is_filled_in_its_cell() {
|
||||
exec(&mut s, "text.select", json!({"story": made["story"], "anchor": 0, "focus": 13}));
|
||||
exec(&mut s, "table.convertFromText", json!({"columnSeparator": "tab"}));
|
||||
assert_eq!(cell_texts(&s), ["Name", "<<Name>>"]);
|
||||
assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 1);
|
||||
exec(&mut s, "data.preview", json!({"record": 1}));
|
||||
assert_eq!(cell_texts(&s), ["Name", "Ada"], "the field is filled in its cell");
|
||||
assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 1, "the count reads the unfilled template while previewing");
|
||||
}
|
||||
|
||||
/// A table `Who⇥Where` with the caret in its second cell, and a source with `Name` and `City`.
|
||||
@@ -1745,6 +1747,7 @@ fn a_field_in_an_endnote_is_filled() {
|
||||
s.documents()[s.active_index().unwrap()].doc.stories.values().flat_map(|st| st.endnotes.iter().map(|e| e.text.text.clone())).collect()
|
||||
};
|
||||
assert_eq!(endnotes(&s), ["From <<City>>"]);
|
||||
assert_eq!(exec(&mut s, "data.fields", json!({}))[1]["uses"], 1);
|
||||
exec(&mut s, "data.preview", json!({"record": 1}));
|
||||
assert_eq!(endnotes(&s), ["From London"]);
|
||||
}
|
||||
@@ -1799,3 +1802,50 @@ fn select_records_stops_expanding_once_the_limit_is_met_but_still_checks_every_r
|
||||
assert!(parse::select_records(10, "range", 1, "1-10,5-11", Some(2)).is_err());
|
||||
assert!(parse::select_records(10, "range", 1, "1-2,0", Some(2)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idml_reopen_relink_counts_existing_text_field() {
|
||||
let dir = scratch("idml-field-count");
|
||||
let csv = dir.join("people.csv");
|
||||
let idml = dir.join("template.idml");
|
||||
write(&csv, "Name\nAda\n");
|
||||
let mut s = Session::new();
|
||||
new_doc(&mut s, 1);
|
||||
text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "", None);
|
||||
exec(&mut s, "data.source.select", json!({"path": csv}));
|
||||
exec(&mut s, "data.placeholder.add", json!({"field": "Name"}));
|
||||
assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 1);
|
||||
exec(&mut s, "file.exportIdml", json!({"path": idml}));
|
||||
let mut reopened = Session::new();
|
||||
exec(&mut reopened, "file.openIdml", json!({"path": idml}));
|
||||
exec(&mut reopened, "data.source.select", json!({"path": csv}));
|
||||
let uses = exec(&mut reopened, "data.fields", json!({}))[0]["uses"].clone();
|
||||
exec(&mut reopened, "data.merge", json!({}));
|
||||
assert!(texts(&reopened).contains(&"Ada".to_string()));
|
||||
assert_eq!(uses, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fields_count_unbound_markers_without_double_counting_bound_placeholders() {
|
||||
let mut s = Session::new();
|
||||
new_doc(&mut s, 1);
|
||||
select_csv(&mut s, "Name\nAda\n");
|
||||
text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "<<Name>> <<Name>>", None);
|
||||
text_frame(&mut s, [36.0, 100.0, 240.0, 150.0], "", None);
|
||||
exec(&mut s, "data.placeholder.add", json!({"field": "Name"}));
|
||||
assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 3);
|
||||
assert_eq!(exec(&mut s, "data.fields", json!({"csv": "Name\nAda\n"}))[0]["uses"], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hyperlink_over_marker_does_not_hide_its_field_usage() {
|
||||
let mut s = Session::new();
|
||||
new_doc(&mut s, 1);
|
||||
select_csv(&mut s, "Name,Link\nAda,https://example.com\n");
|
||||
let made = text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "<<Name>>", None);
|
||||
exec(&mut s, "text.select", json!({"story": made["story"], "anchor": 0, "focus": 8}));
|
||||
exec(&mut s, "data.placeholder.add", json!({"field": "Link", "role": "hyperlink"}));
|
||||
let fields = exec(&mut s, "data.fields", json!({}));
|
||||
assert_eq!(fields[0]["uses"], 1);
|
||||
assert_eq!(fields[1]["uses"], 1);
|
||||
}
|
||||
Reference in new issue
Block a user