Skip to content

Commit e75d8f2

Browse files
committed
chore: add edit field type option helper
1 parent f192f89 commit e75d8f2

File tree

27 files changed

+158
-75
lines changed

27 files changed

+158
-75
lines changed

frontend/rust-lib/flowy-grid/src/entities/group_entities/group_changeset.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ pub struct GroupViewChangesetPB {
133133
#[pb(index = 2)]
134134
pub inserted_groups: Vec<InsertedGroupPB>,
135135

136-
#[pb(index = 2)]
136+
#[pb(index = 3)]
137137
pub new_groups: Vec<GroupPB>,
138138

139-
#[pb(index = 3)]
139+
#[pb(index = 4)]
140140
pub deleted_groups: Vec<String>,
141141

142-
#[pb(index = 4)]
142+
#[pb(index = 5)]
143143
pub update_groups: Vec<GroupPB>,
144144
}
145145

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,14 @@ pub(crate) async fn move_field_handler(
203203

204204
/// The FieldMeta contains multiple data, each of them belongs to a specific FieldType.
205205
async fn get_type_option_data(field_rev: &FieldRevision, field_type: &FieldType) -> FlowyResult<Vec<u8>> {
206-
let s = field_rev
207-
.get_type_option_str(field_type)
208-
.unwrap_or_else(|| default_type_option_builder_from_type(field_type).entry().json_str());
206+
let s = field_rev.get_type_option_str(field_type).unwrap_or_else(|| {
207+
default_type_option_builder_from_type(field_type)
208+
.data_format()
209+
.json_str()
210+
});
209211
let field_type: FieldType = field_rev.ty.into();
210212
let builder = type_option_builder_from_json_str(&s, &field_type);
211-
let type_option_data = builder.entry().protobuf_bytes().to_vec();
213+
let type_option_data = builder.data_format().protobuf_bytes().to_vec();
212214

213215
Ok(type_option_data)
214216
}
@@ -337,7 +339,7 @@ pub(crate) async fn update_select_option_handler(
337339
type_option.delete_option(option);
338340
}
339341

340-
mut_field_rev.insert_type_option_entry(&*type_option);
342+
mut_field_rev.insert_type_option(&*type_option);
341343
let _ = editor.replace_field(field_rev).await?;
342344

343345
if let Some(cell_content_changeset) = cell_content_changeset {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! impl_type_option {
3030
($target: ident, $field_type:expr) => {
3131
impl std::convert::From<&FieldRevision> for $target {
3232
fn from(field_rev: &FieldRevision) -> $target {
33-
match field_rev.get_type_option_entry::<$target>($field_type.into()) {
33+
match field_rev.get_type_option::<$target>($field_type.into()) {
3434
None => $target::default(),
3535
Some(target) => target,
3636
}
@@ -39,7 +39,7 @@ macro_rules! impl_type_option {
3939

4040
impl std::convert::From<&std::sync::Arc<FieldRevision>> for $target {
4141
fn from(field_rev: &std::sync::Arc<FieldRevision>) -> $target {
42-
match field_rev.get_type_option_entry::<$target>($field_type.into()) {
42+
match field_rev.get_type_option::<$target>($field_type.into()) {
4343
None => $target::default(),
4444
Some(target) => target,
4545
}
@@ -52,7 +52,7 @@ macro_rules! impl_type_option {
5252
}
5353
}
5454

55-
impl TypeOptionDataEntry for $target {
55+
impl TypeOptionDataFormat for $target {
5656
fn json_str(&self) -> String {
5757
match serde_json::to_string(&self) {
5858
Ok(s) => s,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,25 @@ pub fn try_decode_cell_data(
101101
let field_type: FieldTypeRevision = t_field_type.into();
102102
let data = match t_field_type {
103103
FieldType::RichText => field_rev
104-
.get_type_option_entry::<RichTextTypeOptionPB>(field_type)?
104+
.get_type_option::<RichTextTypeOptionPB>(field_type)?
105105
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
106106
FieldType::Number => field_rev
107-
.get_type_option_entry::<NumberTypeOptionPB>(field_type)?
107+
.get_type_option::<NumberTypeOptionPB>(field_type)?
108108
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
109109
FieldType::DateTime => field_rev
110-
.get_type_option_entry::<DateTypeOptionPB>(field_type)?
110+
.get_type_option::<DateTypeOptionPB>(field_type)?
111111
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
112112
FieldType::SingleSelect => field_rev
113-
.get_type_option_entry::<SingleSelectTypeOptionPB>(field_type)?
113+
.get_type_option::<SingleSelectTypeOptionPB>(field_type)?
114114
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
115115
FieldType::MultiSelect => field_rev
116-
.get_type_option_entry::<MultiSelectTypeOptionPB>(field_type)?
116+
.get_type_option::<MultiSelectTypeOptionPB>(field_type)?
117117
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
118118
FieldType::Checkbox => field_rev
119-
.get_type_option_entry::<CheckboxTypeOptionPB>(field_type)?
119+
.get_type_option::<CheckboxTypeOptionPB>(field_type)?
120120
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
121121
FieldType::URL => field_rev
122-
.get_type_option_entry::<URLTypeOptionPB>(field_type)?
122+
.get_type_option::<URLTypeOptionPB>(field_type)?
123123
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
124124
};
125125
Some(data)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::entities::{FieldPB, FieldType};
22
use crate::services::field::type_options::*;
33
use bytes::Bytes;
4-
use flowy_grid_data_model::revision::{FieldRevision, TypeOptionDataEntry};
4+
use flowy_grid_data_model::revision::{FieldRevision, TypeOptionDataFormat};
55
use indexmap::IndexMap;
66

77
pub struct FieldBuilder {
@@ -78,14 +78,14 @@ impl FieldBuilder {
7878

7979
pub fn build(self) -> FieldRevision {
8080
let mut field_rev = self.field_rev;
81-
field_rev.insert_type_option_entry(self.type_option_builder.entry());
81+
field_rev.insert_type_option(self.type_option_builder.data_format());
8282
field_rev
8383
}
8484
}
8585

8686
pub trait TypeOptionBuilder {
8787
fn field_type(&self) -> FieldType;
88-
fn entry(&self) -> &dyn TypeOptionDataEntry;
88+
fn data_format(&self) -> &dyn TypeOptionDataFormat;
8989
}
9090

9191
pub fn default_type_option_builder_from_type(field_type: &FieldType) -> Box<dyn TypeOptionBuilder> {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::entities::{FieldChangesetParams, FieldType};
2+
use crate::services::field::{select_option_operation, SelectOptionPB};
3+
use crate::services::grid_editor::GridRevisionEditor;
4+
use flowy_error::FlowyResult;
5+
use flowy_grid_data_model::revision::{FieldRevision, TypeOptionDataDeserializer, TypeOptionDataFormat};
6+
use std::sync::Arc;
7+
8+
pub async fn edit_field<T>(
9+
field_id: &str,
10+
editor: Arc<GridRevisionEditor>,
11+
action: impl FnOnce(&mut T) -> bool,
12+
) -> FlowyResult<()>
13+
where
14+
T: TypeOptionDataDeserializer + TypeOptionDataFormat,
15+
{
16+
let get_type_option = async {
17+
let field_rev = editor.get_field_rev(field_id).await?;
18+
field_rev.get_type_option::<T>(field_rev.ty)
19+
};
20+
21+
if let Some(mut type_option) = get_type_option.await {
22+
if action(&mut type_option) {
23+
let changeset = FieldChangesetParams { ..Default::default() };
24+
let _ = editor.update_field(changeset).await?;
25+
}
26+
}
27+
28+
Ok(())
29+
}
30+
31+
pub fn insert_single_select_option(field_rev: &mut FieldRevision, options: Vec<SelectOptionPB>) -> FlowyResult<()> {
32+
if options.is_empty() {
33+
return Ok(());
34+
}
35+
let mut type_option = select_option_operation(field_rev)?;
36+
options.into_iter().for_each(|option| type_option.insert_option(option));
37+
Ok(())
38+
}
39+
40+
pub fn insert_multi_select_option(field_rev: &mut FieldRevision, options: Vec<SelectOptionPB>) -> FlowyResult<()> {
41+
if options.is_empty() {
42+
return Ok(());
43+
}
44+
let mut type_option = select_option_operation(field_rev)?;
45+
options.into_iter().for_each(|option| type_option.insert_option(option));
46+
Ok(())
47+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod field_builder;
2+
mod field_operation;
23
pub(crate) mod type_options;
34

45
pub use field_builder::*;
6+
pub use field_operation::*;
57
pub use type_options::*;

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
@@ -5,7 +5,7 @@ use crate::services::field::{BoxTypeOptionBuilder, CheckboxCellData, TypeOptionB
55
use bytes::Bytes;
66
use flowy_derive::ProtoBuf;
77
use flowy_error::{FlowyError, FlowyResult};
8-
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, TypeOptionDataDeserializer, TypeOptionDataEntry};
8+
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, TypeOptionDataDeserializer, TypeOptionDataFormat};
99
use serde::{Deserialize, Serialize};
1010
use std::str::FromStr;
1111

@@ -26,7 +26,7 @@ impl TypeOptionBuilder for CheckboxTypeOptionBuilder {
2626
FieldType::Checkbox
2727
}
2828

29-
fn entry(&self) -> &dyn TypeOptionDataEntry {
29+
fn data_format(&self) -> &dyn TypeOptionDataFormat {
3030
&self.0
3131
}
3232
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use chrono::format::strftime::StrftimeItems;
99
use chrono::{NaiveDateTime, Timelike};
1010
use flowy_derive::ProtoBuf;
1111
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
12-
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, TypeOptionDataDeserializer, TypeOptionDataEntry};
12+
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, TypeOptionDataDeserializer, TypeOptionDataFormat};
1313
use serde::{Deserialize, Serialize};
1414

1515
// Date
@@ -189,7 +189,7 @@ impl TypeOptionBuilder for DateTypeOptionBuilder {
189189
FieldType::DateTime
190190
}
191191

192-
fn entry(&self) -> &dyn TypeOptionDataEntry {
192+
fn data_format(&self) -> &dyn TypeOptionDataFormat {
193193
&self.0
194194
}
195195
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::services::field::{BoxTypeOptionBuilder, NumberCellData, TypeOptionBui
66
use bytes::Bytes;
77
use flowy_derive::ProtoBuf;
88
use flowy_error::{FlowyError, FlowyResult};
9-
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, TypeOptionDataDeserializer, TypeOptionDataEntry};
9+
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, TypeOptionDataDeserializer, TypeOptionDataFormat};
1010

1111
use rust_decimal::Decimal;
1212

@@ -45,7 +45,7 @@ impl TypeOptionBuilder for NumberTypeOptionBuilder {
4545
FieldType::Number
4646
}
4747

48-
fn entry(&self) -> &dyn TypeOptionDataEntry {
48+
fn data_format(&self) -> &dyn TypeOptionDataFormat {
4949
&self.0
5050
}
5151
}

0 commit comments

Comments
 (0)