Move growing a merge at its edge on column insertion to its own change

The review asked for it separately: this PR keeps the neighbour's width and
cell formatting for an inserted column, and an optional keepWidth.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Zach Berry
2026-11-04 19:50:27 -06:00
co-authored by Claude Opus 5.5
parent 54a9f741d6
commit 8b6fdce1ba
3 changed files with 5 additions and 81 deletions
+4 -10
View File
@@ -29,7 +29,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Insert Left",
["Table Layout", "Rows & Columns"],
None,
"{col?, keepWidth?: bool (default false, as in PowerPoint: the table grows by the new column and the other widths stay; true: the other columns shrink in proportion so the table keeps its width), id?} the new column takes the width and cell formatting (fill, margins, anchor, text properties) of the column it is inserted beside; a merged row that spans that column grows to span the new one",
"{col?, keepWidth?: bool (default false, as in PowerPoint: the table grows by the new column and the other widths stay; true: the other columns shrink in proportion so the table keeps its width), id?} the new column takes the width and cell formatting (fill, margins, anchor, text properties) of the column it is inserted beside",
has_selection,
|s, p| rows_cols(s, p, "colLeft", "table.insertColumnLeft")
),
@@ -38,7 +38,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Insert Right",
["Table Layout", "Rows & Columns"],
None,
"{col?, keepWidth?: bool (default false, as in PowerPoint: the table grows by the new column and the other widths stay; true: the other columns shrink in proportion so the table keeps its width), id?} the new column takes the width and cell formatting (fill, margins, anchor, text properties) of the column it is inserted beside; a merged row that spans that column grows to span the new one",
"{col?, keepWidth?: bool (default false, as in PowerPoint: the table grows by the new column and the other widths stay; true: the other columns shrink in proportion so the table keeps its width), id?} the new column takes the width and cell formatting (fill, margins, anchor, text properties) of the column it is inserted beside",
has_selection,
|s, p| rows_cols(s, p, "colRight", "table.insertColumnRight")
),
@@ -361,19 +361,13 @@ fn insert_col_like(t: &mut Table, at: usize, neighbour: usize, keep_width: bool)
let neighbour = neighbour.min(t.cols.len().saturating_sub(1));
let w = t.cols.get(neighbour).copied();
let templates: Vec<Option<Cell>> = t.rows.iter().map(|r| r.cells.get(neighbour).cloned()).collect();
t.insert_col_beside(at, Some(neighbour));
t.insert_col(at);
let at = at.min(t.cols.len().saturating_sub(1));
if let (Some(w), Some(slot)) = (w, t.cols.get_mut(at)) {
*slot = w;
}
for (row, tpl) in t.rows.iter_mut().zip(templates) {
let Some(tpl) = tpl else { continue };
// Inserted left of a merge's anchor, the merge now starts at the new cell, which took the
// anchor's cell whole, text and formatting (Table::insert_col_beside); the old anchor,
// covered now, is the empty cell that gets the neighbour's formatting. Read off the table
// as it is now: a span the file lets run past the last column is no merge to the model.
let anchor_moved = row.cells.get(at).is_some_and(|c| c.grid_span > 1 && !c.is_covered());
if let Some(cell) = row.cells.get_mut(if anchor_moved { at + 1 } else { at }) {
if let (Some(cell), Some(tpl)) = (row.cells.get_mut(at), tpl) {
copy_cell_format(cell, &tpl);
}
}
-38
View File
@@ -6498,44 +6498,6 @@ fn table_insert_column_refuses_a_keep_width_that_is_not_a_bool() {
}
}
#[test]
fn table_insert_column_extends_a_merge_that_spans_the_neighbour() {
let mut s = session();
let id = table_at(&mut s, &[100.0, 100.0, 100.0], json!([["title", "", ""], ["a", "b", "c"]]));
s.execute("table.merge", &json!({"id": id.0, "from": [0, 0], "to": [0, 2]})).unwrap();
s.execute("table.insertColumnRight", &json!({"id": id.0, "col": 2})).unwrap();
let t = table_of(&s, id);
assert_eq!(t.cell(0, 0).unwrap().grid_span, 4, "the title now spans the new column");
assert!(t.cell(0, 3).unwrap().h_merge);
assert_eq!(t.cell(1, 0).unwrap().grid_span, 1);
s.execute("table.insertColumnLeft", &json!({"id": id.0, "col": 0})).unwrap();
let t = table_of(&s, id);
assert_eq!(t.cell(0, 0).unwrap().grid_span, 5, "left of the first column too");
assert_eq!(t.cell(0, 0).unwrap().text.text(), "title");
}
/// A span a file lets run past the last column is no merge to the model, so inserting left of
/// that cell adds a plain column: the cell keeps its text and the new one gets its formatting.
#[test]
fn table_insert_column_left_of_an_overflowing_span_keeps_the_text() {
let mut s = session();
let id = table_at(&mut s, &[100.0, 100.0, 100.0], json!([["a", "b", "wide"], ["d", "e", "f"]]));
{
let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
let slide = std::sync::Arc::make_mut(&mut doc.slides[0]);
let sh = deckcraft_model::find_shape_mut(&mut slide.shapes, id).unwrap();
let deckcraft_model::ShapeKind::Table(t) = &mut sh.kind else { panic!("a table") };
t.cell_mut(0, 2).unwrap().grid_span = 4;
}
s.execute("table.cellFill", &json!({"id": id.0, "color": "#FF0000", "cells": [[0, 2]]})).unwrap();
s.execute("table.insertColumnLeft", &json!({"id": id.0, "col": 2})).unwrap();
let t = table_of(&s, id);
assert_eq!(t.n_cols(), 4);
assert_eq!(t.cell(0, 3).unwrap().text.text(), "wide", "the cell keeps its text");
assert_eq!(t.cell(0, 2).unwrap().text.text(), "");
assert_eq!(t.cell(0, 2).unwrap().fill, t.cell(0, 3).unwrap().fill, "the new cell takes the neighbour's fill");
}
#[test]
fn table_insert_column_with_keep_width_keeps_the_table_width_and_copies_the_neighbour_format() {
let mut s = session();
+1 -33
View File
@@ -122,12 +122,6 @@ impl Table {
}
/// Insert an empty column before `at`. A merge the new column falls inside grows to cover it.
pub fn insert_col(&mut self, at: usize) {
self.insert_col_beside(at, None);
}
/// Like [`Table::insert_col`], for a column inserted beside column `neighbour` (as before the
/// insertion): a horizontal merge that spans the neighbour also grows over the new column, even when the
/// insertion point is at its edge.
pub fn insert_col_beside(&mut self, at: usize, neighbour: Option<usize>) {
let at = at.min(self.cols.len());
let merges = self.merges();
let w = self.cols.get(at.saturating_sub(1)).copied().unwrap_or(80.0);
@@ -136,13 +130,7 @@ impl Table {
let i = at.min(r.cells.len());
r.cells.insert(i, Cell::new(""));
}
let grows = |m: &Merge| matches!(neighbour, Some(n) if m.gs > 1 && m.c <= n && n < m.c + m.gs && (at == m.c || at == m.c + m.gs));
// A merge that grows to the left starts at the new cell, which takes the anchor's text and
// formatting; the old anchor becomes a covered, empty cell.
for m in merges.iter().filter(|m| grows(m) && at == m.c) {
self.move_anchor((m.r, at + 1), (m.r, at));
}
self.set_merges(merges.into_iter().map(|m| if grows(&m) { Merge { gs: m.gs + 1, ..m } } else { m.insert_col(at) }));
self.set_merges(merges.into_iter().map(|m| m.insert_col(at)));
}
/// Delete row `at`. Merges through it shrink; a merge anchored on it moves its anchor (and
/// text) to the next row it covers.
@@ -656,24 +644,4 @@ mod tests {
assert!(t.split(0, 0));
assert_eq!(t.cell(0, 0).map(|c| (c.grid_span, c.row_span)), Some((1, 1)));
}
/// A column inserted left of a merge's anchor grows the merge, which now starts at the new
/// cell: that cell holds the text, and the old anchor is a covered, empty cell.
#[test]
fn a_column_inserted_left_of_a_merge_keeps_the_text_in_its_anchor() {
let mut t = merged();
t.insert_col_beside(1, Some(1));
assert_topology(&t);
let a = t.cell(1, 1).unwrap();
assert_eq!((a.grid_span, a.row_span), (3, 2));
assert_eq!(anchor_text(&t, 1, 1), "M");
assert!(t.cell(1, 2).unwrap().is_covered());
assert_eq!(anchor_text(&t, 1, 2), "");
// Right of the merge, the anchor stays where it is.
let mut t = merged();
t.insert_col_beside(3, Some(2));
assert_topology(&t);
assert_eq!(t.cell(1, 1).map(|c| c.grid_span), Some(3));
assert_eq!(anchor_text(&t, 1, 1), "M");
}
}