diff --git a/frontend/rust-lib/flowy-grid/src/services/cell/cell_operation.rs b/frontend/rust-lib/flowy-grid/src/services/cell/cell_operation.rs index b47e82db6b..46aa411338 100644 --- a/frontend/rust-lib/flowy-grid/src/services/cell/cell_operation.rs +++ b/frontend/rust-lib/flowy-grid/src/services/cell/cell_operation.rs @@ -137,16 +137,25 @@ pub fn try_decode_cell_str( /// If the cell data of the `FieldType` doesn't support displaying in String then will return an /// empty string. For example, The string of the Multi-Select cell will be a list of the option's name /// separated by a comma. +/// +/// # Arguments +/// +/// * `cell_str`: the opaque cell string that can be decoded by corresponding structs that implement the +/// `FromCellString` trait. +/// * `decoded_field_type`: the field_type of the cell_str +/// * `field_type`: use this field type's `TypeOption` to stringify this cell_str +/// * `field_rev`: used to get the corresponding TypeOption for the specified field type. +/// +/// returns: String pub fn stringify_cell_data( cell_str: String, - from_field_type: &FieldType, - to_field_type: &FieldType, + decoded_field_type: &FieldType, + field_type: &FieldType, field_rev: &FieldRevision, ) -> String { - match TypeOptionCellExt::new_with_cell_data_cache(field_rev, None).get_type_option_cell_data_handler(to_field_type) - { + match TypeOptionCellExt::new_with_cell_data_cache(field_rev, None).get_type_option_cell_data_handler(field_type) { None => "".to_string(), - Some(handler) => handler.stringify_cell_str(cell_str, from_field_type, field_rev), + Some(handler) => handler.stringify_cell_str(cell_str, decoded_field_type, field_rev), } } diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs index d3a8bbad44..69ec27055c 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs @@ -7,8 +7,8 @@ use crate::services::cell::{ use crate::services::field::selection_type_option::type_option_transform::SelectOptionTypeOptionTransformHelper; use crate::services::field::{ - CheckboxCellData, ChecklistTypeOptionPB, MultiSelectTypeOptionPB, SingleSelectTypeOptionPB, TypeOption, - TypeOptionCellData, TypeOptionTransform, + CheckboxCellData, ChecklistTypeOptionPB, MultiSelectTypeOptionPB, SingleSelectTypeOptionPB, StrCellData, + TypeOption, TypeOptionCellData, TypeOptionTransform, }; use bytes::Bytes; use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; @@ -152,10 +152,10 @@ where fn transform_type_option_cell_str( &self, cell_str: &str, - _decoded_field_type: &FieldType, + decoded_field_type: &FieldType, _field_rev: &FieldRevision, ) -> Option<::CellData> { - match _decoded_field_type { + match decoded_field_type { FieldType::SingleSelect | FieldType::MultiSelect | FieldType::Checklist => None, FieldType::Checkbox => match CheckboxCellData::from_cell_str(cell_str) { Ok(checkbox_cell_data) => { @@ -169,6 +169,7 @@ where } Err(_) => None, }, + FieldType::RichText => SelectOptionIds::from_cell_str(cell_str).ok(), _ => Some(SelectOptionIds::from(vec![])), } } diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_tests.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_tests.rs index a4138589ed..aa73f8c6f7 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_tests.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_tests.rs @@ -1,24 +1,20 @@ #[cfg(test)] mod tests { use crate::entities::FieldType; + use crate::services::cell::stringify_cell_data; use crate::services::cell::CellDataDecoder; use crate::services::field::FieldBuilder; - use crate::services::field::*; // Test parser the cell data which field's type is FieldType::Date to cell data // which field's type is FieldType::Text #[test] fn date_type_to_text_type() { - let type_option = RichTextTypeOptionPB::default(); let field_type = FieldType::DateTime; let field_rev = FieldBuilder::from_field_type(&field_type).build(); assert_eq!( - type_option - .decode_cell_str(1647251762.to_string(), &field_type, &field_rev) - .unwrap() - .as_str(), + stringify_cell_data(1647251762.to_string(), &FieldType::RichText, &field_type, &field_rev), "Mar 14,2022" ); } @@ -27,8 +23,6 @@ mod tests { // which field's type is FieldType::Text #[test] fn single_select_to_text_type() { - let type_option = RichTextTypeOptionPB::default(); - let field_type = FieldType::SingleSelect; let done_option = SelectOptionPB::new("Done"); let option_id = done_option.id.clone(); @@ -36,10 +30,7 @@ mod tests { let field_rev = FieldBuilder::new(single_select).build(); assert_eq!( - type_option - .decode_cell_str(option_id, &field_type, &field_rev) - .unwrap() - .to_string(), + stringify_cell_data(option_id, &FieldType::RichText, &field_type, &field_rev), done_option.name, ); } @@ -49,7 +40,6 @@ mod tests { */ #[test] fn multiselect_to_text_type() { - let text_type_option = RichTextTypeOptionPB::default(); let field_type = FieldType::MultiSelect; let france = SelectOptionPB::new("france"); @@ -65,14 +55,12 @@ mod tests { let field_rev = FieldBuilder::new(multi_select).build(); assert_eq!( - text_type_option - .decode_cell_str( - format!("{},{}", france_option_id, argentina_option_id), - &field_type, - &field_rev - ) - .unwrap() - .to_string(), + stringify_cell_data( + format!("{},{}", france_option_id, argentina_option_id), + &FieldType::RichText, + &field_type, + &field_rev + ), format!("{},{}", france.name, argentina.name) ); } diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_type_option.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_type_option.rs index 25d9d9bff2..1d404fb92e 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_type_option.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_type_option.rs @@ -48,7 +48,31 @@ impl TypeOption for RichTextTypeOptionPB { type CellFilter = TextFilterPB; } -impl TypeOptionTransform for RichTextTypeOptionPB {} +impl TypeOptionTransform for RichTextTypeOptionPB { + fn transformable(&self) -> bool { + true + } + + fn transform_type_option(&mut self, _old_type_option_field_type: FieldType, _old_type_option_data: String) {} + + fn transform_type_option_cell_str( + &self, + cell_str: &str, + decoded_field_type: &FieldType, + field_rev: &FieldRevision, + ) -> Option<::CellData> { + if decoded_field_type.is_date() + || decoded_field_type.is_single_select() + || decoded_field_type.is_multi_select() + || decoded_field_type.is_number() + || decoded_field_type.is_url() + { + Some(stringify_cell_data(cell_str.to_owned(), decoded_field_type, decoded_field_type, field_rev).into()) + } else { + StrCellData::from_cell_str(&cell_str).ok() + } + } +} impl TypeOptionCellData for RichTextTypeOptionPB { fn convert_to_protobuf(&self, cell_data: ::CellData) -> ::CellProtobufType { @@ -64,19 +88,10 @@ impl CellDataDecoder for RichTextTypeOptionPB { fn decode_cell_str( &self, cell_str: String, - decoded_field_type: &FieldType, - field_rev: &FieldRevision, + _decoded_field_type: &FieldType, + _field_rev: &FieldRevision, ) -> FlowyResult<::CellData> { - if decoded_field_type.is_date() - || decoded_field_type.is_single_select() - || decoded_field_type.is_multi_select() - || decoded_field_type.is_number() - || decoded_field_type.is_url() - { - Ok(stringify_cell_data(cell_str, decoded_field_type, decoded_field_type, field_rev).into()) - } else { - StrCellData::from_cell_str(&cell_str) - } + StrCellData::from_cell_str(&cell_str) } fn decode_cell_data_to_str(&self, cell_data: ::CellData) -> String { diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/type_option_cell.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/type_option_cell.rs index a0d5b6a7cf..02713b6e38 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/type_option_cell.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/type_option_cell.rs @@ -47,7 +47,8 @@ pub trait TypeOptionCellDataHandler { /// Decode the cell_str to corresponding cell data, and then return the display string of the /// cell data. - fn stringify_cell_str(&self, cell_str: String, field_type: &FieldType, field_rev: &FieldRevision) -> String; + fn stringify_cell_str(&self, cell_str: String, decoded_field_type: &FieldType, field_rev: &FieldRevision) + -> String; } struct CellDataCacheKey(u64); @@ -222,9 +223,14 @@ where perform_filter().unwrap_or(true) } - fn stringify_cell_str(&self, cell_str: String, field_type: &FieldType, field_rev: &FieldRevision) -> String { + fn stringify_cell_str( + &self, + cell_str: String, + decoded_field_type: &FieldType, + field_rev: &FieldRevision, + ) -> String { if self.transformable() { - let cell_data = self.transform_type_option_cell_str(&cell_str, field_type, field_rev); + let cell_data = self.transform_type_option_cell_str(&cell_str, decoded_field_type, field_rev); if let Some(cell_data) = cell_data { return self.decode_cell_data_to_str(cell_data); } diff --git a/frontend/rust-lib/flowy-grid/tests/grid/field_test/test.rs b/frontend/rust-lib/flowy-grid/tests/grid/field_test/test.rs index 37de685993..77c8921873 100644 --- a/frontend/rust-lib/flowy-grid/tests/grid/field_test/test.rs +++ b/frontend/rust-lib/flowy-grid/tests/grid/field_test/test.rs @@ -208,25 +208,25 @@ async fn grid_switch_from_multi_select_to_text_test() { let mut multi_select_type_option = test.get_multi_select_type_option(&field_rev.id); - let script_switchfield = vec![SwitchToField { + let script_switch_field = vec![SwitchToField { field_id: field_rev.id.clone(), new_field_type: FieldType::RichText, }]; - test.run_scripts(script_switchfield).await; + test.run_scripts(script_switch_field).await; - let script_assertfield = vec![AssertCellContent { + let script_assert_field = vec![AssertCellContent { field_id: field_rev.id.clone(), row_index: 0, from_field_type: FieldType::MultiSelect, expected_content: format!( "{},{}", - multi_select_type_option.get_mut(0).unwrap().id.to_string(), - multi_select_type_option.get_mut(1).unwrap().id.to_string() + multi_select_type_option.get_mut(0).unwrap().name.to_string(), + multi_select_type_option.get_mut(1).unwrap().name.to_string() ), }]; - test.run_scripts(script_assertfield).await; + test.run_scripts(script_assert_field).await; } // Test when switching the current field from Checkbox to Text test @@ -276,4 +276,28 @@ async fn grid_switch_from_date_to_text_test() {} // input: // $1 -> "$1"(This string will be different base on current data setting) #[tokio::test] -async fn grid_switch_from_number_to_text_test() {} +async fn grid_switch_from_number_to_text_test() { + let mut test = GridFieldTest::new().await; + let field_rev = test.get_first_field_rev(FieldType::Number).clone(); + + let scripts = vec![ + SwitchToField { + field_id: field_rev.id.clone(), + new_field_type: FieldType::RichText, + }, + AssertCellContent { + field_id: field_rev.id.clone(), + row_index: 0, + from_field_type: FieldType::Number, + expected_content: "$1".to_string(), + }, + AssertCellContent { + field_id: field_rev.id.clone(), + row_index: 4, + from_field_type: FieldType::Number, + expected_content: "".to_string(), + }, + ]; + + test.run_scripts(scripts).await; +}