Header & Footer: an automatic date's format comes back on import

`header_footer()` recorded a Date placeholder's text only when it held no field,
and never looked at the field itself, so an automatic date's format was dropped
on import: `date_format` stayed empty whatever the deck used.

With the dialog of the previous commit that is not only cosmetic. Opening a deck
whose dates are `datetime5` showed `datetime1` selected, and pressing Apply then
sent that back and reformatted the deck's dates — a silent change to a file the
user only opened to look at.

The reader now remembers which of the thirteen formats the field is, so the
dialog opens on the deck's own choice. An unknown spelling (`datetime`,
`datetimeFigureOut`, which both render as the short date) is left as no choice
rather than guessed at, since the command only accepts the thirteen and would
refuse it. A fixed date still comes back as its text, as before.

`crates/pptx` gains a dependency on `crates/text` for `datetime::DATE_KINDS`,
which layer 3 may take on layer 2; `cargo xtask layers` passes.
This commit is contained in:
Hamza committed 2026-10-28 20:29:33 +05:00
1 parent 6d09d3770f
commit 64d3127fe8
4 files changed
+69 -5

No files matched your search

Generated
+1
View File
@@ -1217,6 +1217,7 @@ dependencies = [
"deckcraft-engine",
"deckcraft-geom",
"deckcraft-model",
"deckcraft-text",
"image",
"log",
"quick-xml 0.41.0",
+1
View File
@@ -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 }
+22 -5
View File
@@ -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) => {
+45
View File
@@ -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, "");
}