From 810b95bf68449f46bb1a54805facd8272ac0cee0 Mon Sep 17 00:00:00 2001 From: alehel Date: Sat, 14 Nov 2026 10:54:29 +0100 Subject: [PATCH] Text defaults: a JSON workbook without sheets keeps them on open When GridCraft JSON has no sheets, the reader replaces the workbook with Workbook::new() and carries only the macros across, so its text defaults were dropped and a save lost them. They are carried across too. --- crates/engine/src/io.rs | 5 ++++- crates/engine/src/io/tests.rs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index 6dd96b042..c5a38b2f6 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -319,10 +319,13 @@ fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook, let mut wb: Workbook = serde_json::from_slice(bytes).map_err(|e| EngineError::Other(format!("not a GridCraft JSON workbook: {e}")))?; wb.styles.rebuild_index(); if wb.sheets.is_empty() { - // The file's macros are read after this (`macros::load`), so they come along. + // The file's macros are read after this (`macros::load`), so they come along, and + // so do its text defaults: a save would otherwise lose them. let macros = std::mem::take(&mut wb.macros_read); + let text_defaults = std::mem::take(&mut wb.text_defaults); wb = Workbook::new(); wb.macros_read = macros; + wb.text_defaults = text_defaults; } // The file's band placement and cell count are derived state: rebuild them from the // cells themselves, dropping what is outside the sheet grid (#1008). diff --git a/crates/engine/src/io/tests.rs b/crates/engine/src/io/tests.rs index ad8ca40f2..9533bc168 100644 --- a/crates/engine/src/io/tests.rs +++ b/crates/engine/src/io/tests.rs @@ -881,3 +881,20 @@ fn the_merge_index_holds_a_bounded_number_of_cells() { sh.merges = vec![RangeRef::new(CellRef::new(0, 0), CellRef::new(1_000_000, 0))]; assert!(merge_index(&sh, rect).is_none()); } + +#[test] +fn json_without_sheets_keeps_its_text_defaults() -> std::result::Result<(), Box> { + let mut saved = Workbook::new(); + saved.sheets.clear(); + saved.text_defaults.asian = gridcraft_model::ScriptDefault { + language: Some("ja".into()), + country: Some("JP".into()), + font: Some("Noto Sans CJK JP".into()), + ..Default::default() + }; + let json = serde_json::to_vec(&saved)?; + let (wb, _) = open_bytes("book.json", &json)?; + assert!(!wb.sheets.is_empty(), "a workbook without sheets opens as a new one"); + assert_eq!(wb.text_defaults, saved.text_defaults); + Ok(()) +}