diff --git a/Cargo.lock b/Cargo.lock index 01a92e4a5..5aa455924 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1217,6 +1217,7 @@ dependencies = [ "deckcraft-engine", "deckcraft-geom", "deckcraft-model", + "deckcraft-text", "image", "log", "quick-xml 0.41.0", diff --git a/crates/pptx/Cargo.toml b/crates/pptx/Cargo.toml index 0ea79d6b4..8d1e0fc34 100644 --- a/crates/pptx/Cargo.toml +++ b/crates/pptx/Cargo.toml @@ -10,6 +10,7 @@ workspace = true [dependencies] deckcraft-model = { workspace = true } +deckcraft-text = { workspace = true } deckcraft-geom = { workspace = true } deckcraft-color = { workspace = true } quick-xml = { workspace = true } diff --git a/crates/pptx/src/read.rs b/crates/pptx/src/read.rs index 18e8eb92f..412bc8f25 100644 --- a/crates/pptx/src/read.rs +++ b/crates/pptx/src/read.rs @@ -1332,11 +1332,28 @@ fn header_footer(p: &Presentation) -> HeaderFooter { Some(PhType::SlideNum) => hf.slide_number = true, Some(PhType::Date) => { hf.date = true; - if let Some(t) = &sh.text - && !t.paragraphs.iter().flat_map(|p| &p.runs).any(|r| matches!(r.kind, deckcraft_model::text::RunKind::Field { .. })) - && hf.date_text.is_empty() - { - hf.date_text = t.text(); + let field = sh.text.iter().flat_map(|t| t.paragraphs.iter()).flat_map(|p| p.runs.iter()).find_map(|r| match &r.kind { + deckcraft_model::text::RunKind::Field { field } if deckcraft_text::datetime::is_date_field(field) => Some(field.as_str()), + _ => None, + }); + match field { + // An automatic date: remember which of the thirteen formats it is, so the + // Header and Footer dialog opens on the deck's own choice instead of the + // default. An unknown spelling (`datetime`, `datetimeFigureOut`) is left + // as no choice rather than guessed at. + Some(kind) => { + if hf.date_format.is_empty() && deckcraft_text::datetime::DATE_KINDS.contains(&kind) { + hf.date_format = kind.to_string(); + } + } + // A fixed date: its text, as before. + None => { + if hf.date_text.is_empty() + && let Some(t) = &sh.text + { + hf.date_text = t.text(); + } + } } } Some(PhType::Footer) => { diff --git a/crates/pptx/tests/header_footer_date.rs b/crates/pptx/tests/header_footer_date.rs new file mode 100644 index 000000000..99b263082 --- /dev/null +++ b/crates/pptx/tests/header_footer_date.rs @@ -0,0 +1,45 @@ +//! The Header & Footer date through PPTX: an automatic date's format comes back on import, so the +//! dialog opens on the deck's own choice; a fixed date still comes back as its text. + +use deckcraft_model::{Presentation, defaults}; +use serde_json::json; + +/// A deck whose Date placeholders are set up by the engine, exported and imported again. +fn round_trip(args: serde_json::Value) -> Presentation { + let mut s = deckcraft_engine::Session::with_new(); + s.execute("design.headerFooter", &args).expect("headerFooter"); + let p = s.doc().expect("doc").doc.clone(); + let bytes = deckcraft_pptx::export(&p).expect("export"); + deckcraft_pptx::import(&bytes).expect("import") +} + +#[test] +fn an_automatic_dates_format_survives_a_round_trip() { + // A chosen format comes back as the remembered choice. + let q = round_trip(json!({"date": true, "dateFormat": "datetime5", "all": true})); + assert!(q.header_footer.date, "the date placeholder is there"); + assert_eq!(q.header_footer.date_format, "datetime5", "the format is remembered"); + assert_eq!(q.header_footer.date_text, "", "an automatic date has no fixed text"); + + // A different one, so the test cannot pass by always reading datetime5. + let q = round_trip(json!({"date": true, "dateFormat": "datetime11", "all": true})); + assert_eq!(q.header_footer.date_format, "datetime11"); + + // The default: no format asked for, so nothing to remember beyond the field itself. + let q = round_trip(json!({"date": true, "all": true})); + assert_eq!(q.header_footer.date_format, "datetime1", "the placeholder's own field is datetime1"); + + // A fixed date is text, not a field: its text comes back and no format is claimed. + let q = round_trip(json!({"date": true, "dateText": "4 July 1776", "all": true})); + assert_eq!(q.header_footer.date_text, "4 July 1776"); + assert_eq!(q.header_footer.date_format, "", "a fixed date has no format"); +} + +#[test] +fn a_deck_without_a_date_placeholder_claims_no_format() { + let p = defaults::new_presentation(None); + let bytes = deckcraft_pptx::export(&p).expect("export"); + let q = deckcraft_pptx::import(&bytes).expect("import"); + assert!(!q.header_footer.date); + assert_eq!(q.header_footer.date_format, ""); +}