From 8a54f45d75ae33e4c2921c0b730257c3e730028b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Hatcher?= Date: Mon, 3 Feb 2025 23:31:04 +0100 Subject: [PATCH] UPDATE: Add clear formatting Fixes #267 --- base/src/test/user_model/test_styles.rs | 44 +++++++++++++++++++ base/src/user_model/common.rs | 48 +++++++++++++++++++++ base/src/user_model/history.rs | 6 +++ bindings/nodejs/src/user_model.rs | 22 ++++++++++ bindings/wasm/src/lib.rs | 21 +++++++++ webapp/IronCalc/src/components/toolbar.tsx | 15 +++++++ webapp/IronCalc/src/components/workbook.tsx | 14 ++++++ webapp/IronCalc/src/locale/en_us.json | 1 + 8 files changed, 171 insertions(+) diff --git a/base/src/test/user_model/test_styles.rs b/base/src/test/user_model/test_styles.rs index 7482f62..34267ab 100644 --- a/base/src/test/user_model/test_styles.rs +++ b/base/src/test/user_model/test_styles.rs @@ -436,3 +436,47 @@ fn false_removes_value() { let style = model.get_cell_style(0, 1, 1).unwrap(); assert!(!style.font.b); } + +#[test] +fn cell_clear_formatting() { + let mut model = UserModel::new_empty("model", "en", "UTC").unwrap(); + let range = Area { + sheet: 0, + row: 1, + column: 1, + width: 1, + height: 1, + }; + + // bold + model.update_range_style(&range, "font.b", "true").unwrap(); + model + .update_range_style(&range, "alignment.horizontal", "centerContinuous") + .unwrap(); + + let style = model.get_cell_style(0, 1, 1).unwrap(); + assert!(style.font.b); + assert_eq!( + style.alignment.unwrap().horizontal, + HorizontalAlignment::CenterContinuous + ); + + model.range_clear_all(&range).unwrap(); + let style = model.get_cell_style(0, 1, 1).unwrap(); + assert!(!style.font.b); + assert_eq!(style.alignment, None); + + model.undo().unwrap(); + + let style = model.get_cell_style(0, 1, 1).unwrap(); + assert!(style.font.b); + assert_eq!( + style.alignment.unwrap().horizontal, + HorizontalAlignment::CenterContinuous + ); + model.redo().unwrap(); + + let style = model.get_cell_style(0, 1, 1).unwrap(); + assert!(!style.font.b); + assert_eq!(style.alignment, None); +} diff --git a/base/src/user_model/common.rs b/base/src/user_model/common.rs index 385ef99..8a2eebe 100644 --- a/base/src/user_model/common.rs +++ b/base/src/user_model/common.rs @@ -566,6 +566,34 @@ impl UserModel { Ok(()) } + /// Removes cells styles and formatting, but keeps the content + /// + /// See also: + /// * [UserModel::range_clear_all] + /// * [UserModel::range_clear_contents] + pub fn range_clear_formatting(&mut self, range: &Area) -> Result<(), String> { + let sheet = range.sheet; + let mut diff_list = Vec::new(); + for row in range.row..range.row + range.height { + for column in range.column..range.column + range.width { + let old_style = self.model.get_style_for_cell(sheet, row, column)?; + // We can always assume that style with style_index 0 exists and it is the default + self.model + .workbook + .worksheet_mut(sheet)? + .set_cell_style(row, column, 0)?; + diff_list.push(Diff::CellClearFormatting { + sheet, + row, + column, + old_style: Box::new(old_style), + }); + } + } + self.push_diff_list(diff_list); + Ok(()) + } + /// Inserts a row /// /// See also: @@ -2006,6 +2034,15 @@ impl UserModel { old_value, new_value: _, } => self.model.set_sheet_state(*index, old_value.clone())?, + Diff::CellClearFormatting { + sheet, + row, + column, + old_style, + } => { + self.model + .set_cell_style(*sheet, *row, *column, old_style)?; + } } } if needs_evaluation { @@ -2160,6 +2197,17 @@ impl UserModel { old_value: _, new_value, } => self.model.set_sheet_state(*index, new_value.clone())?, + Diff::CellClearFormatting { + sheet, + row, + column, + old_style: _, + } => { + self.model + .workbook + .worksheet_mut(*sheet)? + .set_cell_style(*row, *column, 0)?; + } } } diff --git a/base/src/user_model/history.rs b/base/src/user_model/history.rs index b9f8c75..7516e30 100644 --- a/base/src/user_model/history.rs +++ b/base/src/user_model/history.rs @@ -39,6 +39,12 @@ pub(crate) enum Diff { old_value: Box>, old_style: Box