util.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use collab_database::fields::Field;
  2. use flowy_database2::entities::{CreateFieldParams, FieldType};
  3. use flowy_database2::services::field::{
  4. type_option_to_pb, DateCellChangeset, DateFormat, DateTypeOption, FieldBuilder,
  5. RichTextTypeOption, SelectOption, SingleSelectTypeOption, TimeFormat,
  6. };
  7. pub fn create_text_field(grid_id: &str) -> (CreateFieldParams, Field) {
  8. let field_type = FieldType::RichText;
  9. let type_option = RichTextTypeOption::default();
  10. let text_field = FieldBuilder::new(field_type.clone(), type_option.clone())
  11. .name("Name")
  12. .visibility(true)
  13. .primary(true)
  14. .build();
  15. let type_option_data = type_option_to_pb(type_option.into(), &field_type).to_vec();
  16. let params = CreateFieldParams {
  17. view_id: grid_id.to_owned(),
  18. field_type,
  19. type_option_data: Some(type_option_data),
  20. };
  21. (params, text_field)
  22. }
  23. pub fn create_single_select_field(grid_id: &str) -> (CreateFieldParams, Field) {
  24. let field_type = FieldType::SingleSelect;
  25. let mut type_option = SingleSelectTypeOption::default();
  26. type_option.options.push(SelectOption::new("Done"));
  27. type_option.options.push(SelectOption::new("Progress"));
  28. let single_select_field = FieldBuilder::new(field_type.clone(), type_option.clone())
  29. .name("Name")
  30. .visibility(true)
  31. .build();
  32. let type_option_data = type_option_to_pb(type_option.into(), &field_type).to_vec();
  33. let params = CreateFieldParams {
  34. view_id: grid_id.to_owned(),
  35. field_type,
  36. type_option_data: Some(type_option_data),
  37. };
  38. (params, single_select_field)
  39. }
  40. pub fn create_date_field(grid_id: &str, field_type: FieldType) -> (CreateFieldParams, Field) {
  41. let date_type_option = DateTypeOption {
  42. date_format: DateFormat::US,
  43. time_format: TimeFormat::TwentyFourHour,
  44. timezone_id: "Etc/UTC".to_owned(),
  45. field_type: field_type.clone(),
  46. };
  47. let field: Field = match field_type {
  48. FieldType::DateTime => FieldBuilder::new(field_type.clone(), date_type_option.clone())
  49. .name("Date")
  50. .visibility(true)
  51. .build(),
  52. FieldType::LastEditedTime => FieldBuilder::new(field_type.clone(), date_type_option.clone())
  53. .name("Updated At")
  54. .visibility(true)
  55. .build(),
  56. FieldType::CreatedTime => FieldBuilder::new(field_type.clone(), date_type_option.clone())
  57. .name("Created At")
  58. .visibility(true)
  59. .build(),
  60. _ => panic!("Unsupported group field type"),
  61. };
  62. let type_option_data = type_option_to_pb(date_type_option.into(), &field_type).to_vec();
  63. let params = CreateFieldParams {
  64. view_id: grid_id.to_owned(),
  65. field_type,
  66. type_option_data: Some(type_option_data),
  67. };
  68. (params, field)
  69. }
  70. // The grid will contains all existing field types and there are three empty rows in this grid.
  71. pub fn make_date_cell_string(s: &str) -> String {
  72. serde_json::to_string(&DateCellChangeset {
  73. date: Some(s.to_string()),
  74. time: None,
  75. include_time: Some(false),
  76. })
  77. .unwrap()
  78. }