diff --git a/crates/engine/src/cmd/table.rs b/crates/engine/src/cmd/table.rs index 76be6bfb0..7ea66acfd 100644 --- a/crates/engine/src/cmd/table.rs +++ b/crates/engine/src/cmd/table.rs @@ -29,7 +29,7 @@ pub fn specs() -> Vec { "Insert Left", ["Table Layout", "Rows & Columns"], None, - "{col?, keepWidth?: bool (default true: the other columns shrink in proportion so the table keeps its width; false: the table grows by the new column), 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; a merged row that spans that column grows to span the new one", has_selection, |s, p| rows_cols(s, p, "colLeft", "table.insertColumnLeft") ), @@ -38,7 +38,7 @@ pub fn specs() -> Vec { "Insert Right", ["Table Layout", "Rows & Columns"], None, - "{col?, keepWidth?: bool (default true: the other columns shrink in proportion so the table keeps its width; false: the table grows by the new column), 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; a merged row that spans that column grows to span the new one", has_selection, |s, p| rows_cols(s, p, "colRight", "table.insertColumnRight") ), @@ -321,14 +321,14 @@ fn with_table(s: &mut Session, p: &Value, cmd: &str, f: impl Fn(&mut Table) -> R fn rows_cols(s: &mut Session, p: &Value, op: &str, name: &'static str) -> Result { let (r, c) = at(s, p, name)?; - // Absent: keep the table's width; present, it must be a bool, so a mistyped value is refused + // Absent: the table grows, as in PowerPoint; present, it must be a bool, so a mistyped value is refused // rather than silently taken as the default. let keep_width = match (op, p.get("keepWidth")) { - (_, None) | (_, Some(Value::Null)) => true, + (_, None) | (_, Some(Value::Null)) => false, (_, Some(Value::Bool(b))) => *b, ("colLeft", Some(_)) => return Err(bad("table.insertColumnLeft", "`keepWidth` must be true or false")), ("colRight", Some(_)) => return Err(bad("table.insertColumnRight", "`keepWidth` must be true or false")), - _ => true, + _ => false, }; let op = op.to_string(); with_table(s, p, name, move |t| { @@ -355,7 +355,7 @@ fn rows_cols(s: &mut Session, p: &Value, op: &str, name: &'static str) -> Result /// Insert a column before `at` that looks like column `neighbour` (the one it is inserted beside): /// its width, and in every row the neighbour cell's explicit formatting with no text. With /// `keep_width` the columns then shrink in proportion so the table keeps its total width; -/// otherwise the table grows by the new column. +/// otherwise (PowerPoint's behaviour) the table grows by the new column and the widths stay. fn insert_col_like(t: &mut Table, at: usize, neighbour: usize, keep_width: bool) { let total = t.width(); let neighbour = neighbour.min(t.cols.len().saturating_sub(1)); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index fc6548597..04d26951f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6483,65 +6483,6 @@ fn master_body_placeholder_format_keeps_per_level_formatting() { assert_eq!(shown(&s), vec![Some(28.0), Some(24.0)]); } -#[test] -fn table_insert_column_keeps_the_table_width_and_copies_the_neighbour_format() { - let mut s = session(); - let id = table_at(&mut s, &[280.0, 280.0, 280.0], json!([["a", "b", "c"], ["d", "e", "f"]])); - s.execute("table.cellFill", &json!({"id": id.0, "color": "#FF0000", "cells": [[0, 2], [1, 2]]})).unwrap(); - s.execute("table.cellMargins", &json!({"id": id.0, "left": 20, "right": 20, "top": 5, "bottom": 5, "cells": [[0, 2]]})).unwrap(); - s.execute("table.cellAlign", &json!({"id": id.0, "anchor": "bottom", "cells": [[0, 2]]})).unwrap(); - s.execute("edit.select", &json!({"ids": [id.0]})).unwrap(); - s.execute("table.selectCells", &json!({"from": [0, 2], "to": [1, 2]})).unwrap(); - s.execute("format.bold", &json!({"bold": true})).unwrap(); - // Right of the last column: four columns in the same 840 pt, the new one as wide as its - // neighbour, the others shrunk to make room. - s.execute("table.insertColumnRight", &json!({"id": id.0, "col": 2})).unwrap(); - let t = table_of(&s, id); - assert_eq!(t.n_cols(), 4); - assert!((t.width() - 840.0).abs() < 1e-6, "width {}", t.width()); - assert!(t.cols.iter().all(|w| (w - 210.0).abs() < 1e-6), "{:?}", t.cols); - let x = s.doc().unwrap().shape(id).unwrap().xfrm.unwrap(); - assert!((x.w - 840.0).abs() < 1e-6, "frame {}", x.w); - // The new cells take the neighbour's explicit formatting. - let new = t.cell(0, 3).unwrap(); - assert_eq!(new.fill, Some(deckcraft_model::Fill::solid(deckcraft_model::ColorRef::rgb(deckcraft_color::Rgba::rgb(255, 0, 0))))); - assert_eq!(new.margins, Some([20.0, 20.0, 5.0, 5.0])); - assert_eq!(new.anchor, Some(deckcraft_model::text::Anchor::Bottom)); - assert_eq!(new.text.text(), "", "the new cell is empty"); - let bold = new.text.paragraphs.first().map(|p| p.end_props.bold.or(p.def_props.bold)); - assert_eq!(bold, Some(Some(true)), "the neighbour's bold carries over: {:?}", new.text); - assert_eq!(t.cell(1, 3).unwrap().fill, t.cell(1, 2).unwrap().fill); - // Left of the first column: the neighbour is the old first column. - s.execute("table.insertColumnLeft", &json!({"id": id.0, "col": 0})).unwrap(); - let t = table_of(&s, id); - assert_eq!(t.n_cols(), 5); - assert!((t.width() - 840.0).abs() < 1e-6, "width {}", t.width()); - assert_eq!(t.cell(0, 1).unwrap().text.text(), "a"); - assert_eq!(t.cell(0, 0).unwrap().fill, None); - // keepWidth: false grows the table by the neighbour's width, as before. - s.execute("table.insertColumnRight", &json!({"id": id.0, "col": 4, "keepWidth": false})).unwrap(); - let t = table_of(&s, id); - assert_eq!(t.n_cols(), 6); - assert!((t.width() - 840.0 - 168.0).abs() < 1e-6, "width {}", t.width()); - // The insertion is journaled as called, and each one is a single undo step back to the table - // as it was: the columns, their widths and the frame. - let (jid, jp) = s.journal.last().cloned().unwrap(); - assert_eq!(jid, "table.insertColumnRight"); - assert_eq!(jp["keepWidth"], json!(false), "{jp}"); - s.execute("edit.undo", &json!({})).unwrap(); - let t = table_of(&s, id); - assert_eq!(t.n_cols(), 5); - assert!((t.width() - 840.0).abs() < 1e-6, "width {}", t.width()); - s.execute("edit.undo", &json!({})).unwrap(); - s.execute("edit.undo", &json!({})).unwrap(); - let t = table_of(&s, id); - assert_eq!(t.n_cols(), 3); - assert!(t.cols.iter().all(|w| (w - 280.0).abs() < 1e-6), "{:?}", t.cols); - assert_eq!(t.cell(0, 2).unwrap().text.text(), "c"); - let x = s.doc().unwrap().shape(id).unwrap().xfrm.unwrap(); - assert!((x.w - 840.0).abs() < 1e-6, "frame {}", x.w); -} - /// A `keepWidth` that is not a bool is refused, naming it, and the table is left alone; it used /// to be read as absent and the columns shrunk as if `true` had been asked. #[test] @@ -6595,6 +6536,94 @@ fn table_insert_column_left_of_an_overflowing_span_keeps_the_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(); + let id = table_at(&mut s, &[280.0, 280.0, 280.0], json!([["a", "b", "c"], ["d", "e", "f"]])); + s.execute("table.cellFill", &json!({"id": id.0, "color": "#FF0000", "cells": [[0, 2], [1, 2]]})).unwrap(); + s.execute("table.cellMargins", &json!({"id": id.0, "left": 20, "right": 20, "top": 5, "bottom": 5, "cells": [[0, 2]]})).unwrap(); + s.execute("table.cellAlign", &json!({"id": id.0, "anchor": "bottom", "cells": [[0, 2]]})).unwrap(); + s.execute("edit.select", &json!({"ids": [id.0]})).unwrap(); + s.execute("table.selectCells", &json!({"from": [0, 2], "to": [1, 2]})).unwrap(); + s.execute("format.bold", &json!({"on": true})).unwrap(); + // Right of the last column: four columns in the same 840 pt, the new one as wide as its + // neighbour, the others shrunk to make room. + s.execute("table.insertColumnRight", &json!({"id": id.0, "col": 2, "keepWidth": true})).unwrap(); + let t = table_of(&s, id); + assert_eq!(t.n_cols(), 4); + assert!((t.width() - 840.0).abs() < 1e-6, "width {}", t.width()); + assert!(t.cols.iter().all(|w| (w - 210.0).abs() < 1e-6), "{:?}", t.cols); + let x = s.doc().unwrap().shape(id).unwrap().xfrm.unwrap(); + assert!((x.w - 840.0).abs() < 1e-6, "frame {}", x.w); + // The new cells take the neighbour's explicit formatting. + let new = t.cell(0, 3).unwrap(); + assert_eq!(new.fill, Some(deckcraft_model::Fill::solid(deckcraft_model::ColorRef::rgb(deckcraft_color::Rgba::rgb(255, 0, 0))))); + assert_eq!(new.margins, Some([20.0, 20.0, 5.0, 5.0])); + assert_eq!(new.anchor, Some(deckcraft_model::text::Anchor::Bottom)); + assert_eq!(new.text.text(), "", "the new cell is empty"); + let bold = new.text.paragraphs.first().map(|p| p.end_props.bold.or(p.def_props.bold)); + assert_eq!(bold, Some(Some(true)), "the neighbour's bold carries over: {:?}", new.text); + assert_eq!(t.cell(1, 3).unwrap().fill, t.cell(1, 2).unwrap().fill); + // Left of the first column: the neighbour is the old first column. + s.execute("table.insertColumnLeft", &json!({"id": id.0, "col": 0, "keepWidth": true})).unwrap(); + let t = table_of(&s, id); + assert_eq!(t.n_cols(), 5); + assert!((t.width() - 840.0).abs() < 1e-6, "width {}", t.width()); + assert_eq!(t.cell(0, 1).unwrap().text.text(), "a"); + assert_eq!(t.cell(0, 0).unwrap().fill, None); + // keepWidth: false grows the table by the neighbour's width, as before. + s.execute("table.insertColumnRight", &json!({"id": id.0, "col": 4, "keepWidth": false})).unwrap(); + let t = table_of(&s, id); + assert_eq!(t.n_cols(), 6); + assert!((t.width() - 840.0 - 168.0).abs() < 1e-6, "width {}", t.width()); + // The insertion is journaled as called, and each one is a single undo step back to the table + // as it was: the columns, their widths and the frame. + let (jid, jp) = s.journal.last().cloned().unwrap(); + assert_eq!(jid, "table.insertColumnRight"); + assert_eq!(jp["keepWidth"], json!(false), "{jp}"); + s.execute("edit.undo", &json!({})).unwrap(); + let t = table_of(&s, id); + assert_eq!(t.n_cols(), 5); + assert!((t.width() - 840.0).abs() < 1e-6, "width {}", t.width()); + s.execute("edit.undo", &json!({})).unwrap(); + s.execute("edit.undo", &json!({})).unwrap(); + let t = table_of(&s, id); + assert_eq!(t.n_cols(), 3); + assert!(t.cols.iter().all(|w| (w - 280.0).abs() < 1e-6), "{:?}", t.cols); + assert_eq!(t.cell(0, 2).unwrap().text.text(), "c"); + let x = s.doc().unwrap().shape(id).unwrap().xfrm.unwrap(); + assert!((x.w - 840.0).abs() < 1e-6, "frame {}", x.w); +} + +/// PowerPoint inserts a column as wide as its neighbour and widens the table by that amount; the +/// other widths stay. Deleting a column narrows the table by its width, the others untouched. +#[test] +fn table_insert_and_delete_column_leave_other_widths_alone() { + let mut s = session(); + let id = table_at(&mut s, &[160.0, 234.0, 235.0, 235.0], json!([["a", "b", "c", "d"]])); + for (cmd, col, at, wide) in [("table.insertColumnRight", 0, 1, 160.0), ("table.insertColumnLeft", 3, 3, 235.0)] { + let before = table_of(&s, id).cols.clone(); + let w0 = table_of(&s, id).width(); + s.execute(cmd, &json!({"id": id.0, "col": col})).unwrap(); + let t = table_of(&s, id); + let mut want = before.clone(); + want.insert(at, wide); + assert_eq!(t.cols, want, "{cmd}"); + assert!((t.width() - w0 - wide).abs() < 1e-6, "{cmd} width {}", t.width()); + let x = s.doc().unwrap().shape(id).unwrap().xfrm.unwrap(); + assert!((x.w - t.width()).abs() < 1e-6, "{cmd} frame {}", x.w); + } + let before = table_of(&s, id).cols.clone(); + s.execute("table.deleteColumn", &json!({"id": id.0, "col": 1})).unwrap(); + let t = table_of(&s, id); + let mut want = before.clone(); + let gone = want.remove(1); + assert_eq!(t.cols, want); + assert!((t.width() - (before.iter().sum::() - gone)).abs() < 1e-6); + let x = s.doc().unwrap().shape(id).unwrap().xfrm.unwrap(); + assert!((x.w - t.width()).abs() < 1e-6, "frame {}", x.w); +} + #[test] fn slide_master_view_rejects_invalid_indices() { let mut s = Session::with_new();