diff --git a/Cargo.lock b/Cargo.lock index 20fc1c938..6f758557e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1358,6 +1358,7 @@ dependencies = [ "deckcraft-engine", "deckcraft-geom", "deckcraft-model", + "deckcraft-text", "image", "log", "quick-xml 0.41.0", diff --git a/crates/engine/src/cmd/design/slide_setup.rs b/crates/engine/src/cmd/design/slide_setup.rs index 86d9317b7..32534255a 100644 --- a/crates/engine/src/cmd/design/slide_setup.rs +++ b/crates/engine/src/cmd/design/slide_setup.rs @@ -176,7 +176,10 @@ pub(super) fn header_footer(s: &mut Session, p: &Value) -> Result { } (Some(pos), true) if k == PhType::Date && (date_given || format_given) => { if let Some(sh) = sl.shapes.get_mut(pos) { - set_date(sh, &hf.date_text, &hf.date_format); + // The deck's format always travels; whether the caller asked for it + // is what decides if it may rewrite a field the slide already has + // (see `set_date`). + set_date(sh, &hf.date_text, &hf.date_format, format_given); } } _ => {} @@ -200,8 +203,19 @@ fn date_now(format: &str) -> String { } /// Point a slide's date placeholder at a fixed date (its own plain text) or, for an empty -/// `fixed`, at an automatic date field (one it already has keeps its format). -fn set_date(sh: &mut Shape, fixed: &str, format: &str) { +/// `fixed`, at an automatic date field. +/// +/// `format_given` is whether the caller named a format. The dialog always sends `dateText`, so an +/// Apply with no format selection reached here carrying the deck's global format and rewrote a +/// slide's own `datetime11` field to it -- a format the caller never mentioned. A field the slide +/// already has therefore keeps its own format unless one was asked for. +/// +/// That holds only where there *is* a field to keep. A placeholder showing a fixed date has no +/// date field and no format of its own, so clearing `dateText` without naming a format used to +/// build a `datetime1`: a slide that went from `datetime11` to fixed text and back came away on +/// the short date. With nothing to preserve, the deck's remembered format is the right one to +/// take (noted in review). +fn set_date(sh: &mut Shape, fixed: &str, format: &str, format_given: bool) { let runs = || sh.text.iter().flat_map(|t| &t.paragraphs).flat_map(|p| &p.runs); let field_now = runs().find_map(|r| match &r.kind { RunKind::Field { field } if deckcraft_text::datetime::is_date_field(field) => Some(field.clone()), @@ -209,6 +223,7 @@ fn set_date(sh: &mut Shape, fixed: &str, format: &str) { }); let has_field = field_now.is_some(); let props = runs().next().map(|r| r.props.clone()).unwrap_or_default(); + let format = if format_given || !has_field { format } else { "" }; let want = date_kind(format); let (text, kind) = match fixed { // Automatic: keep the field, but follow a format that has changed. @@ -298,6 +313,55 @@ mod header_footer_date_tests { got[0].0.clone() } + /// The dialog always sends `dateText`, so an Apply with no format selection reached + /// `set_date` with the deck's global format and rewrote a slide's own date field to it. The + /// format the caller did not mention is not a format the caller asked for (noted in review). + #[test] + fn an_apply_with_no_format_leaves_each_slides_own_format_alone() { + let mut s = Session::with_new(); + s.execute("slide.new", &json!({})).unwrap(); + s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime11", "all": true})).unwrap(); + let kinds = |s: &Session| dates(s).into_iter().map(|(_, k)| k).collect::>(); + assert_eq!(kinds(&s), vec![Some("datetime11".to_string()); 2], "both slides start on the time format"); + + // One slide alone takes a different format, which also becomes the deck's global one. + s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime5", "all": false})).unwrap(); + assert_eq!(kinds(&s), vec![Some("datetime11".to_string()), Some("datetime5".to_string())]); + assert_eq!(s.doc().unwrap().doc.header_footer.date_format, "datetime5"); + + // Apply with no `dateFormat`: the first slide keeps datetime11 rather than being rewritten + // to the global datetime5. + s.execute("design.headerFooter", &json!({"date": true, "dateText": "", "all": true})).unwrap(); + assert_eq!(kinds(&s), vec![Some("datetime11".to_string()), Some("datetime5".to_string())], "neither slide was rewritten"); + + // And a format that *is* given still reaches every slide, so the guard is not a block. + s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime2", "all": true})).unwrap(); + assert_eq!(kinds(&s), vec![Some("datetime2".to_string()); 2], "a format the caller asked for still applies"); + } + + /// A fixed date is plain text, so the placeholder has no date field and no format of its + /// own. Clearing `dateText` without naming a format therefore has nothing to preserve, and + /// built a `datetime1`: a slide that went from `datetime11` to fixed text and back came away + /// on the short date (noted in review). + #[test] + fn a_fixed_date_turned_automatic_again_comes_back_on_the_remembered_format() { + let mut s = Session::with_new(); + run_automatic(&mut s, json!({"date": true, "dateFormat": "datetime11", "all": true}), "datetime11"); + + s.execute("design.headerFooter", &json!({"date": true, "dateText": "4 July 1776", "all": true})).unwrap(); + assert_eq!(dates(&s), vec![("4 July 1776".to_string(), None)], "fixed: plain text, no field"); + + // No `dateFormat` in this call, which is what the dialog sends on an Apply. + run_automatic(&mut s, json!({"date": true, "dateText": "", "all": true}), "datetime11"); + + // And the guard this must not undo: where there *is* a field, an Apply naming no format + // still leaves the slide's own format alone, although the deck's format now reaches + // `set_date` either way. + s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime5", "all": false})).unwrap(); + s.execute("design.headerFooter", &json!({"date": true, "dateText": "", "all": true})).unwrap(); + assert_eq!(dates(&s)[0].1.as_deref(), Some("datetime5"), "the slide's own format survives"); + } + /// Header & Footer's automatic date takes the format it is given. `HeaderFooter::date_format` /// existed for this and was never read: the placeholder was always `datetime1`, and a /// `dateFormat` parameter was accepted and silently dropped. diff --git a/crates/pptx/Cargo.toml b/crates/pptx/Cargo.toml index 6be4dd23e..f023baae5 100644 --- a/crates/pptx/Cargo.toml +++ b/crates/pptx/Cargo.toml @@ -15,6 +15,7 @@ craft-cfb.workspace = true craft-drawingml.workspace = true craft-drawingml-table.workspace = true 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 2c0ec8810..ebf41f2d6 100644 --- a/crates/pptx/src/read.rs +++ b/crates/pptx/src/read.rs @@ -1611,11 +1611,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, ""); +} diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs index 7378a6a8b..452ff079c 100644 --- a/crates/ui-egui/src/dialogs/tests.rs +++ b/crates/ui-egui/src/dialogs/tests.rs @@ -28,7 +28,7 @@ fn header_footer_apply_keeps_an_existing_date_format() { use crate::SlideApp; let mut session = deckcraft_engine::Session::with_new(); session.execute("design.headerFooter", &json!({"date": true, "dateText": "", "dateFormat": "datetime4", "all": true})).unwrap(); - // A file whose document-level format is unset, as an imported deck's is. + // A file whose document-level format is unset, as an imported deck's is when its date field is not one of the thirteen kinds. Arc::make_mut(&mut session.doc_mut().unwrap().doc).header_footer.date_format = String::new(); let date_fields = |app: &SlideApp| -> Vec { app.session