Tables: cell widths start after a row's skipped grid columns (gridBefore), now that Insert Column syncs them (#4120)

This commit is contained in:
aiongg
2026-10-24 21:05:19 +00:00
parent 1dbbade6fa
commit 29c48a7eae
2 changed files with 19 additions and 2 deletions
+3 -2
View File
@@ -243,14 +243,15 @@ fn autofit(s: &mut Session, v: &Value) -> CmdResult {
pub(crate) fn sync_cell_widths(t: &mut Table) {
let grid = t.grid.clone();
for row in &mut t.rows {
let mut g = 0usize;
// A row's cells start after the grid columns it skips (`w:gridBefore`).
let mut g = row.first_grid();
for c in &mut row.cells {
let span = c.span();
let w: f32 = grid.get(g..g.saturating_add(span).min(grid.len())).unwrap_or(&[]).iter().sum();
if w > 0.0 {
c.props.width = Some(w);
}
g += span;
g = g.saturating_add(span);
}
}
}
+16
View File
@@ -1808,6 +1808,22 @@ fn inserting_a_column_keeps_cell_widths_in_step_with_the_grid() {
}
}
/// A row that skips grid columns (`w:gridBefore`) gives its cells the widths of the columns
/// they sit in, not of the first ones.
#[test]
fn cell_widths_follow_the_grid_after_skipped_columns() {
let mut s = s();
run(&mut s, "insert.table", json!({"rows": 2, "cols": 3}));
let i = s.doc.body.iter().position(|b| b.as_table().is_some()).unwrap();
let wordcraft_doc::Block::Table(t) = std::sync::Arc::make_mut(&mut s.doc.body[i]) else { panic!("no table") };
t.grid = vec![100.0, 200.0, 300.0];
t.rows[1].props.grid_before = 1;
t.rows[1].cells.pop();
crate::cmd::table::sync_cell_widths(t);
let widths: Vec<f32> = t.rows[1].cells.iter().map(|c| c.props.width.unwrap_or(0.0)).collect();
assert_eq!(widths, [200.0, 300.0]);
}
#[test]
fn a_rejected_version_restore_saves_nothing() {
let mut s = Session::new(wordcraft_doc::Document::from_text("Original text."));