diff --git a/crates/engine/src/cmd/design/slide_setup.rs b/crates/engine/src/cmd/design/slide_setup.rs index ed91d3253..32534255a 100644 --- a/crates/engine/src/cmd/design/slide_setup.rs +++ b/crates/engine/src/cmd/design/slide_setup.rs @@ -176,12 +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) { - // Only a format the caller asked for. The dialog always sends - // `dateText`, so an Apply with no format selection reached here and - // rewrote a slide's own `datetime11` field to the deck's global - // format, which the caller never mentioned (noted in review). No - // format means the field keeps the one it has. - set_date(sh, &hf.date_text, if format_given { &hf.date_format } else { "" }); + // 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); } } _ => {} @@ -205,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()), @@ -214,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. @@ -329,6 +339,29 @@ mod header_footer_date_tests { 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.