Skip to content

Commit da1b094

Browse files
0xkelvinLucasXu0
authored andcommitted
feat: handling same as Notion when user fills in the mixing of number and text (#1650)
* feat: handling same as Notion when user fills in the mixing of number and text * feat: remove debug log * feat: using lazy_static to initialized lazily the regex
1 parent 48940ef commit da1b094

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ use crate::services::field::{
77
TypeOptionCellDataCompare, TypeOptionCellDataFilter, TypeOptionTransform,
88
};
99
use bytes::Bytes;
10+
use fancy_regex::Regex;
1011
use flowy_derive::ProtoBuf;
1112
use flowy_error::FlowyResult;
1213
use grid_rev_model::{FieldRevision, TypeOptionDataDeserializer, TypeOptionDataSerializer};
14+
use lazy_static::lazy_static;
1315
use rust_decimal::Decimal;
1416
use serde::{Deserialize, Serialize};
1517
use std::cmp::Ordering;
@@ -97,10 +99,13 @@ impl NumberTypeOptionPB {
9799

98100
pub(crate) fn format_cell_data(&self, s: &str) -> FlowyResult<NumberCellData> {
99101
match self.format {
100-
NumberFormat::Num => match Decimal::from_str(s) {
101-
Ok(value, ..) => Ok(NumberCellData::from_decimal(value)),
102-
Err(_) => Ok(NumberCellData::new()),
103-
},
102+
NumberFormat::Num => {
103+
let strnum = NUM_REGEX.replace_all(s, "");
104+
match Decimal::from_str(&strnum) {
105+
Ok(value, ..) => Ok(NumberCellData::from_decimal(value)),
106+
Err(_) => Ok(NumberCellData::new()),
107+
}
108+
}
104109
_ => NumberCellData::from_format_str(s, self.sign_positive, &self.format),
105110
}
106111
}
@@ -158,7 +163,11 @@ impl CellDataChangeset for NumberTypeOptionPB {
158163
) -> FlowyResult<(String, <Self as TypeOption>::CellData)> {
159164
let data = changeset.trim().to_string();
160165
let number_cell_data = self.format_cell_data(&data)?;
161-
Ok((data, number_cell_data.to_string().into()))
166+
167+
match self.format {
168+
NumberFormat::Num => Ok((number_cell_data.to_string().into(), number_cell_data.to_string().into())),
169+
_ => Ok((data, number_cell_data.to_string().into())),
170+
}
162171
}
163172
}
164173

@@ -201,3 +210,7 @@ impl std::default::Default for NumberTypeOptionPB {
201210
}
202211
}
203212
}
213+
214+
lazy_static! {
215+
static ref NUM_REGEX: Regex = Regex::new(r"[^\d\.]").unwrap();
216+
}

0 commit comments

Comments
 (0)