2023-04-28 14:08:53 +08:00
|
|
|
use collab_database::fields::Field;
|
|
|
|
use flowy_database2::entities::{CreateFieldParams, FieldType};
|
|
|
|
use flowy_database2::services::field::{
|
2023-05-26 14:04:17 +03:30
|
|
|
type_option_to_pb, DateCellChangeset, DateFormat, DateTypeOption, FieldBuilder,
|
|
|
|
RichTextTypeOption, SelectOption, SingleSelectTypeOption, TimeFormat,
|
2023-04-28 14:08:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn create_text_field(grid_id: &str) -> (CreateFieldParams, Field) {
|
|
|
|
let field_type = FieldType::RichText;
|
|
|
|
let type_option = RichTextTypeOption::default();
|
|
|
|
let text_field = FieldBuilder::new(field_type.clone(), type_option.clone())
|
|
|
|
.name("Name")
|
|
|
|
.visibility(true)
|
|
|
|
.primary(true)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let type_option_data = type_option_to_pb(type_option.into(), &field_type).to_vec();
|
|
|
|
let params = CreateFieldParams {
|
|
|
|
view_id: grid_id.to_owned(),
|
|
|
|
field_type,
|
|
|
|
type_option_data: Some(type_option_data),
|
|
|
|
};
|
|
|
|
(params, text_field)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_single_select_field(grid_id: &str) -> (CreateFieldParams, Field) {
|
|
|
|
let field_type = FieldType::SingleSelect;
|
|
|
|
let mut type_option = SingleSelectTypeOption::default();
|
|
|
|
type_option.options.push(SelectOption::new("Done"));
|
|
|
|
type_option.options.push(SelectOption::new("Progress"));
|
|
|
|
let single_select_field = FieldBuilder::new(field_type.clone(), type_option.clone())
|
|
|
|
.name("Name")
|
|
|
|
.visibility(true)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let type_option_data = type_option_to_pb(type_option.into(), &field_type).to_vec();
|
|
|
|
let params = CreateFieldParams {
|
|
|
|
view_id: grid_id.to_owned(),
|
|
|
|
field_type,
|
|
|
|
type_option_data: Some(type_option_data),
|
|
|
|
};
|
|
|
|
(params, single_select_field)
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:04:17 +03:30
|
|
|
pub fn create_date_field(grid_id: &str, field_type: FieldType) -> (CreateFieldParams, Field) {
|
|
|
|
let date_type_option = DateTypeOption {
|
|
|
|
date_format: DateFormat::US,
|
|
|
|
time_format: TimeFormat::TwentyFourHour,
|
2023-05-31 16:52:37 +08:00
|
|
|
timezone_id: "Etc/UTC".to_owned(),
|
2023-05-26 14:04:17 +03:30
|
|
|
field_type: field_type.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let field: Field = match field_type {
|
|
|
|
FieldType::DateTime => FieldBuilder::new(field_type.clone(), date_type_option.clone())
|
|
|
|
.name("Date")
|
|
|
|
.visibility(true)
|
|
|
|
.build(),
|
2023-05-31 17:34:41 +08:00
|
|
|
FieldType::LastEditedTime => FieldBuilder::new(field_type.clone(), date_type_option.clone())
|
2023-05-26 14:04:17 +03:30
|
|
|
.name("Updated At")
|
|
|
|
.visibility(true)
|
|
|
|
.build(),
|
2023-05-31 17:34:41 +08:00
|
|
|
FieldType::CreatedTime => FieldBuilder::new(field_type.clone(), date_type_option.clone())
|
2023-05-26 14:04:17 +03:30
|
|
|
.name("Created At")
|
|
|
|
.visibility(true)
|
|
|
|
.build(),
|
|
|
|
_ => panic!("Unsupported group field type"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let type_option_data = type_option_to_pb(date_type_option.into(), &field_type).to_vec();
|
|
|
|
|
|
|
|
let params = CreateFieldParams {
|
|
|
|
view_id: grid_id.to_owned(),
|
|
|
|
field_type,
|
|
|
|
type_option_data: Some(type_option_data),
|
|
|
|
};
|
|
|
|
(params, field)
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:08:53 +08:00
|
|
|
// The grid will contains all existing field types and there are three empty rows in this grid.
|
|
|
|
|
2023-09-02 11:50:16 +08:00
|
|
|
pub fn make_date_cell_string(timestamp: i64) -> String {
|
2023-04-28 14:08:53 +08:00
|
|
|
serde_json::to_string(&DateCellChangeset {
|
2023-09-02 11:50:16 +08:00
|
|
|
date: Some(timestamp),
|
2023-04-28 14:08:53 +08:00
|
|
|
time: None,
|
|
|
|
include_time: Some(false),
|
2023-07-31 04:23:20 +02:00
|
|
|
clear_flag: None,
|
2023-04-28 14:08:53 +08:00
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|