Skip to content

Commit a2b5d6f

Browse files
authored
fix: number cell format (#1623)
Co-authored-by: nathan <[email protected]>
1 parent 5c1b084 commit a2b5d6f

File tree

24 files changed

+134
-1003
lines changed

24 files changed

+134
-1003
lines changed

frontend/rust-lib/flowy-grid/src/event_handler.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,14 @@ pub(crate) async fn move_field_handler(
229229

230230
/// The [FieldRevision] contains multiple data, each of them belongs to a specific FieldType.
231231
async fn get_type_option_data(field_rev: &FieldRevision, field_type: &FieldType) -> FlowyResult<Vec<u8>> {
232-
let s = field_rev.get_type_option_str(field_type).unwrap_or_else(|| {
233-
default_type_option_builder_from_type(field_type)
234-
.serializer()
235-
.json_str()
236-
});
232+
let s = field_rev
233+
.get_type_option_str(field_type)
234+
.map(|value| value.to_owned())
235+
.unwrap_or_else(|| {
236+
default_type_option_builder_from_type(field_type)
237+
.serializer()
238+
.json_str()
239+
});
237240
let field_type: FieldType = field_rev.ty.into();
238241
let builder = type_option_builder_from_json_str(&s, &field_type);
239242
let type_option_data = builder.serializer().protobuf_bytes().to_vec();

frontend/rust-lib/flowy-grid/src/services/cell/cell_operation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub trait CellDataChangeset: TypeOption {
4141
&self,
4242
changeset: <Self as TypeOption>::CellChangeset,
4343
type_cell_data: Option<TypeCellData>,
44-
) -> FlowyResult<<Self as TypeOption>::CellData>;
44+
) -> FlowyResult<(String, <Self as TypeOption>::CellData)>;
4545
}
4646

4747
/// changeset: It will be deserialized into specific data base on the FieldType.
@@ -65,13 +65,13 @@ pub fn apply_cell_data_changeset<C: ToCellChangesetString, T: AsRef<FieldRevisio
6565
Err(_) => None,
6666
});
6767

68-
let cell_data = match TypeOptionCellExt::new_with_cell_data_cache(field_rev, cell_data_cache)
68+
let cell_str = match TypeOptionCellExt::new_with_cell_data_cache(field_rev, cell_data_cache)
6969
.get_type_option_cell_data_handler(&field_type)
7070
{
7171
None => "".to_string(),
7272
Some(handler) => handler.handle_cell_changeset(changeset, type_cell_data, field_rev)?,
7373
};
74-
Ok(TypeCellData::new(cell_data, field_type).to_json())
74+
Ok(TypeCellData::new(cell_str, field_type).to_json())
7575
}
7676

7777
pub fn decode_type_cell_data<T: TryInto<TypeCellData, Error = FlowyError> + Debug>(

frontend/rust-lib/flowy-grid/src/services/cell/type_cell_data.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ impl std::convert::TryFrom<CellRevision> for TypeCellData {
8282
}
8383

8484
impl TypeCellData {
85-
pub fn new(content: String, field_type: FieldType) -> Self {
86-
TypeCellData {
87-
cell_str: content,
88-
field_type,
89-
}
85+
pub fn new(cell_str: String, field_type: FieldType) -> Self {
86+
TypeCellData { cell_str, field_type }
9087
}
9188

9289
pub fn to_json(&self) -> String {

frontend/rust-lib/flowy-grid/src/services/field/type_options/checkbox_type_option/checkbox_type_option.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ impl CellDataChangeset for CheckboxTypeOptionPB {
8787
&self,
8888
changeset: <Self as TypeOption>::CellChangeset,
8989
_type_cell_data: Option<TypeCellData>,
90-
) -> FlowyResult<<Self as TypeOption>::CellData> {
90+
) -> FlowyResult<(String, <Self as TypeOption>::CellData)> {
9191
let checkbox_cell_data = CheckboxCellData::from_str(&changeset)?;
92-
Ok(checkbox_cell_data)
92+
Ok((checkbox_cell_data.to_string(), checkbox_cell_data))
9393
}
9494
}
9595

frontend/rust-lib/flowy-grid/src/services/field/type_options/date_type_option/date_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,17 @@ mod tests {
152152
time: include_time_str,
153153
is_utc: false,
154154
};
155-
let encoded_data = type_option.apply_changeset(changeset, None).unwrap();
155+
let (cell_str, _) = type_option.apply_changeset(changeset, None).unwrap();
156156

157157
assert_eq!(
158-
decode_cell_data(encoded_data.to_string(), type_option, field_rev),
158+
decode_cell_data(cell_str, type_option, field_rev),
159159
expected_str.to_owned(),
160160
);
161161
}
162162

163-
fn decode_cell_data(encoded_data: String, type_option: &DateTypeOptionPB, field_rev: &FieldRevision) -> String {
163+
fn decode_cell_data(cell_str: String, type_option: &DateTypeOptionPB, field_rev: &FieldRevision) -> String {
164164
let decoded_data = type_option
165-
.decode_cell_str(encoded_data, &FieldType::DateTime, field_rev)
165+
.decode_cell_str(cell_str, &FieldType::DateTime, field_rev)
166166
.unwrap();
167167
let decoded_data = type_option.convert_to_protobuf(decoded_data);
168168
if type_option.include_time {

frontend/rust-lib/flowy-grid/src/services/field/type_options/date_type_option/date_type_option.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl CellDataChangeset for DateTypeOptionPB {
157157
&self,
158158
changeset: <Self as TypeOption>::CellChangeset,
159159
_type_cell_data: Option<TypeCellData>,
160-
) -> FlowyResult<<Self as TypeOption>::CellData> {
160+
) -> FlowyResult<(String, <Self as TypeOption>::CellData)> {
161161
let cell_data = match changeset.date_timestamp() {
162162
None => 0,
163163
Some(date_timestamp) => match (self.include_time, changeset.time) {
@@ -171,8 +171,8 @@ impl CellDataChangeset for DateTypeOptionPB {
171171
_ => date_timestamp,
172172
},
173173
};
174-
175-
Ok(DateCellData(Some(cell_data)))
174+
let date_cell_data = DateCellData(Some(cell_data));
175+
Ok((date_cell_data.to_string(), date_cell_data))
176176
}
177177
}
178178

frontend/rust-lib/flowy-grid/src/services/field/type_options/date_type_option/date_type_option_entities.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl ToCellChangesetString for DateCellChangeset {
7070
}
7171
}
7272

73-
#[derive(Default, Clone)]
73+
#[derive(Default, Clone, Debug)]
7474
pub struct DateCellData(pub Option<i64>);
7575

7676
impl std::convert::From<DateCellData> for i64 {

frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_type_option.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ impl CellDataChangeset for NumberTypeOptionPB {
155155
&self,
156156
changeset: <Self as TypeOption>::CellChangeset,
157157
_type_cell_data: Option<TypeCellData>,
158-
) -> FlowyResult<<Self as TypeOption>::CellData> {
158+
) -> FlowyResult<(String, <Self as TypeOption>::CellData)> {
159159
let data = changeset.trim().to_string();
160-
let _ = self.format_cell_data(&data)?;
161-
Ok(StrCellData(data))
160+
let number_cell_data = self.format_cell_data(&data)?;
161+
Ok((data, number_cell_data.to_string().into()))
162162
}
163163
}
164164

frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/checklist_type_option.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ impl CellDataChangeset for ChecklistTypeOptionPB {
6060
&self,
6161
changeset: <Self as TypeOption>::CellChangeset,
6262
type_cell_data: Option<TypeCellData>,
63-
) -> FlowyResult<<Self as TypeOption>::CellData> {
63+
) -> FlowyResult<(String, <Self as TypeOption>::CellData)> {
6464
let insert_option_ids = changeset
6565
.insert_option_ids
6666
.into_iter()
6767
.filter(|insert_option_id| self.options.iter().any(|option| &option.id == insert_option_id))
6868
.collect::<Vec<String>>();
6969

70-
match type_cell_data {
71-
None => Ok(SelectOptionIds::from(insert_option_ids)),
70+
let select_option_ids = match type_cell_data {
71+
None => SelectOptionIds::from(insert_option_ids),
7272
Some(type_cell_data) => {
7373
let mut select_ids: SelectOptionIds = type_cell_data.cell_str.into();
7474
for insert_option_id in insert_option_ids {
@@ -81,9 +81,10 @@ impl CellDataChangeset for ChecklistTypeOptionPB {
8181
select_ids.retain(|id| id != &delete_option_id);
8282
}
8383

84-
Ok(select_ids)
84+
select_ids
8585
}
86-
}
86+
};
87+
Ok((select_option_ids.to_string(), select_option_ids))
8788
}
8889
}
8990
impl TypeOptionCellDataFilter for ChecklistTypeOptionPB {

frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/multi_select_type_option.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ impl CellDataChangeset for MultiSelectTypeOptionPB {
6161
&self,
6262
changeset: <Self as TypeOption>::CellChangeset,
6363
type_cell_data: Option<TypeCellData>,
64-
) -> FlowyResult<<Self as TypeOption>::CellData> {
64+
) -> FlowyResult<(String, <Self as TypeOption>::CellData)> {
6565
let insert_option_ids = changeset
6666
.insert_option_ids
6767
.into_iter()
6868
.filter(|insert_option_id| self.options.iter().any(|option| &option.id == insert_option_id))
6969
.collect::<Vec<String>>();
7070

71-
match type_cell_data {
72-
None => Ok(SelectOptionIds::from(insert_option_ids)),
71+
let select_option_ids = match type_cell_data {
72+
None => SelectOptionIds::from(insert_option_ids),
7373
Some(type_cell_data) => {
7474
let mut select_ids: SelectOptionIds = type_cell_data.cell_str.into();
7575
for insert_option_id in insert_option_ids {
@@ -83,9 +83,10 @@ impl CellDataChangeset for MultiSelectTypeOptionPB {
8383
}
8484

8585
tracing::trace!("Multi-select cell data: {}", select_ids.to_string());
86-
Ok(select_ids)
86+
select_ids
8787
}
88-
}
88+
};
89+
Ok((select_option_ids.to_string(), select_option_ids))
8990
}
9091
}
9192

@@ -210,7 +211,7 @@ mod tests {
210211
let type_option = MultiSelectTypeOptionPB::from(&field_rev);
211212
let option_ids = vec![google.id, facebook.id];
212213
let changeset = SelectOptionCellChangeset::from_insert_options(option_ids.clone());
213-
let select_option_ids: SelectOptionIds = type_option.apply_changeset(changeset, None).unwrap();
214+
let select_option_ids: SelectOptionIds = type_option.apply_changeset(changeset, None).unwrap().1;
214215

215216
assert_eq!(&*select_option_ids, &option_ids);
216217
}
@@ -229,12 +230,12 @@ mod tests {
229230

230231
// insert
231232
let changeset = SelectOptionCellChangeset::from_insert_options(option_ids.clone());
232-
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap();
233+
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap().1;
233234
assert_eq!(&*select_option_ids, &option_ids);
234235

235236
// delete
236237
let changeset = SelectOptionCellChangeset::from_delete_options(option_ids);
237-
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap();
238+
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap().1;
238239
assert!(select_option_ids.is_empty());
239240
}
240241

@@ -250,7 +251,7 @@ mod tests {
250251

251252
let type_option = MultiSelectTypeOptionPB::from(&field_rev);
252253
let changeset = SelectOptionCellChangeset::from_insert_option_id(&google.id);
253-
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap();
254+
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap().1;
254255
assert_eq!(select_option_ids.to_string(), google.id);
255256
}
256257

@@ -265,7 +266,7 @@ mod tests {
265266

266267
let type_option = MultiSelectTypeOptionPB::from(&field_rev);
267268
let changeset = SelectOptionCellChangeset::from_insert_option_id(&google.id);
268-
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap();
269+
let (_, select_option_ids) = type_option.apply_changeset(changeset, None).unwrap();
269270
assert!(select_option_ids.is_empty());
270271
}
271272

@@ -283,11 +284,11 @@ mod tests {
283284

284285
// empty option id string
285286
let changeset = SelectOptionCellChangeset::from_insert_option_id("");
286-
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap();
287-
assert_eq!(select_option_ids.to_string(), "");
287+
let (cell_str, _) = type_option.apply_changeset(changeset, None).unwrap();
288+
assert_eq!(cell_str, "");
288289

289290
let changeset = SelectOptionCellChangeset::from_insert_option_id("123,456");
290-
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap();
291+
let select_option_ids = type_option.apply_changeset(changeset, None).unwrap().1;
291292
assert!(select_option_ids.is_empty());
292293
}
293294
}

0 commit comments

Comments
 (0)