util.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, TimestampTypeOption,
  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. #[allow(dead_code)]
  41. pub fn create_date_field(grid_id: &str) -> (CreateFieldParams, Field) {
  42. let date_type_option = DateTypeOption {
  43. date_format: DateFormat::US,
  44. time_format: TimeFormat::TwentyFourHour,
  45. timezone_id: "Etc/UTC".to_owned(),
  46. };
  47. let field = FieldBuilder::new(FieldType::DateTime, date_type_option.clone())
  48. .name("Date")
  49. .visibility(true)
  50. .build();
  51. let type_option_data = type_option_to_pb(date_type_option.into(), &FieldType::DateTime).to_vec();
  52. let params = CreateFieldParams {
  53. view_id: grid_id.to_owned(),
  54. field_type: FieldType::DateTime,
  55. type_option_data: Some(type_option_data),
  56. };
  57. (params, field)
  58. }
  59. pub fn create_timestamp_field(grid_id: &str, field_type: FieldType) -> (CreateFieldParams, Field) {
  60. let timestamp_type_option = TimestampTypeOption {
  61. date_format: DateFormat::US,
  62. time_format: TimeFormat::TwentyFourHour,
  63. include_time: true,
  64. field_type: field_type.clone(),
  65. };
  66. let field: Field = match field_type {
  67. FieldType::LastEditedTime => {
  68. FieldBuilder::new(field_type.clone(), timestamp_type_option.clone())
  69. .name("Updated At")
  70. .visibility(true)
  71. .build()
  72. },
  73. FieldType::CreatedTime => FieldBuilder::new(field_type.clone(), timestamp_type_option.clone())
  74. .name("Created At")
  75. .visibility(true)
  76. .build(),
  77. _ => panic!("Unsupported group field type"),
  78. };
  79. let type_option_data = type_option_to_pb(timestamp_type_option.into(), &field_type).to_vec();
  80. let params = CreateFieldParams {
  81. view_id: grid_id.to_owned(),
  82. field_type,
  83. type_option_data: Some(type_option_data),
  84. };
  85. (params, field)
  86. }
  87. // The grid will contains all existing field types and there are three empty rows in this grid.
  88. pub fn make_date_cell_string(timestamp: i64) -> String {
  89. serde_json::to_string(&DateCellChangeset {
  90. date: Some(timestamp),
  91. time: None,
  92. include_time: Some(false),
  93. clear_flag: None,
  94. })
  95. .unwrap()
  96. }