mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-01 10:14:21 -06:00
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.
This commit is contained in:
@@ -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).
|
||||
|
||||
@@ -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<dyn std::error::Error>> {
|
||||
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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user