2022-07-08 15:06:50 +08:00
|
|
|
use crate::services::cell::apply_cell_data_changeset;
|
2022-07-08 14:54:11 +08:00
|
|
|
use crate::services::field::select_option::SelectOptionCellChangeset;
|
2022-03-16 10:02:37 +08:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2022-06-15 15:13:50 +08:00
|
|
|
use flowy_grid_data_model::revision::{gen_row_id, CellRevision, FieldRevision, RowRevision, DEFAULT_ROW_HEIGHT};
|
2022-04-11 14:09:50 +08:00
|
|
|
use indexmap::IndexMap;
|
2022-03-14 17:24:25 +08:00
|
|
|
use std::collections::HashMap;
|
2022-07-01 10:36:07 +08:00
|
|
|
use std::sync::Arc;
|
2022-03-13 11:06:28 +08:00
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
pub struct CreateRowRevisionBuilder<'a> {
|
2022-07-01 10:36:07 +08:00
|
|
|
field_rev_map: HashMap<&'a String, &'a Arc<FieldRevision>>,
|
2022-06-20 10:24:43 +08:00
|
|
|
payload: CreateRowRevisionPayload,
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
impl<'a> CreateRowRevisionBuilder<'a> {
|
2022-07-01 10:36:07 +08:00
|
|
|
pub fn new(fields: &'a [Arc<FieldRevision>]) -> Self {
|
2022-06-15 15:13:50 +08:00
|
|
|
let field_rev_map = fields
|
2022-03-16 10:02:37 +08:00
|
|
|
.iter()
|
|
|
|
.map(|field| (&field.id, field))
|
2022-07-01 10:36:07 +08:00
|
|
|
.collect::<HashMap<&String, &Arc<FieldRevision>>>();
|
2022-03-16 10:02:37 +08:00
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
let payload = CreateRowRevisionPayload {
|
2022-04-11 15:27:03 +08:00
|
|
|
row_id: gen_row_id(),
|
2022-03-14 17:24:25 +08:00
|
|
|
cell_by_field_id: Default::default(),
|
|
|
|
height: DEFAULT_ROW_HEIGHT,
|
|
|
|
visibility: true,
|
|
|
|
};
|
2022-03-16 10:02:37 +08:00
|
|
|
|
2022-06-15 17:24:46 +08:00
|
|
|
Self { field_rev_map, payload }
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:02:37 +08:00
|
|
|
pub fn add_cell(&mut self, field_id: &str, data: String) -> FlowyResult<()> {
|
2022-06-15 15:13:50 +08:00
|
|
|
match self.field_rev_map.get(&field_id.to_owned()) {
|
2022-03-16 10:02:37 +08:00
|
|
|
None => {
|
|
|
|
let msg = format!("Invalid field_id: {}", field_id);
|
|
|
|
Err(FlowyError::internal().context(msg))
|
|
|
|
}
|
2022-06-15 15:13:50 +08:00
|
|
|
Some(field_rev) => {
|
2022-07-08 14:54:11 +08:00
|
|
|
let data = apply_cell_data_changeset(data, None, field_rev)?;
|
2022-06-15 15:13:50 +08:00
|
|
|
let cell = CellRevision::new(data);
|
2022-04-07 15:34:00 +08:00
|
|
|
self.payload.cell_by_field_id.insert(field_id.to_owned(), cell);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_select_option_cell(&mut self, field_id: &str, data: String) -> FlowyResult<()> {
|
2022-06-15 15:13:50 +08:00
|
|
|
match self.field_rev_map.get(&field_id.to_owned()) {
|
2022-04-07 15:34:00 +08:00
|
|
|
None => {
|
|
|
|
let msg = format!("Invalid field_id: {}", field_id);
|
|
|
|
Err(FlowyError::internal().context(msg))
|
|
|
|
}
|
2022-06-15 15:13:50 +08:00
|
|
|
Some(field_rev) => {
|
2022-07-08 14:54:11 +08:00
|
|
|
let cell_data = SelectOptionCellChangeset::from_insert(&data).to_str();
|
|
|
|
let data = apply_cell_data_changeset(cell_data, None, field_rev)?;
|
2022-06-15 15:13:50 +08:00
|
|
|
let cell = CellRevision::new(data);
|
2022-03-18 21:04:01 +08:00
|
|
|
self.payload.cell_by_field_id.insert(field_id.to_owned(), cell);
|
2022-03-16 10:02:37 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn height(mut self, height: i32) -> Self {
|
2022-03-18 21:04:01 +08:00
|
|
|
self.payload.height = height;
|
2022-03-14 17:24:25 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn visibility(mut self, visibility: bool) -> Self {
|
2022-03-18 21:04:01 +08:00
|
|
|
self.payload.visibility = visibility;
|
2022-03-13 11:06:28 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
pub fn build(self) -> CreateRowRevisionPayload {
|
2022-03-18 21:04:01 +08:00
|
|
|
self.payload
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-14 17:24:25 +08:00
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
pub fn make_row_rev_from_context(block_id: &str, payload: CreateRowRevisionPayload) -> RowRevision {
|
2022-06-15 15:13:50 +08:00
|
|
|
RowRevision {
|
2022-03-18 21:04:01 +08:00
|
|
|
id: payload.row_id,
|
2022-03-14 17:24:25 +08:00
|
|
|
block_id: block_id.to_owned(),
|
2022-04-09 07:35:35 +08:00
|
|
|
cells: payload.cell_by_field_id,
|
2022-03-18 21:04:01 +08:00
|
|
|
height: payload.height,
|
|
|
|
visibility: payload.visibility,
|
2022-03-14 17:24:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
pub struct CreateRowRevisionPayload {
|
2022-03-14 17:24:25 +08:00
|
|
|
pub row_id: String,
|
2022-06-15 15:13:50 +08:00
|
|
|
pub cell_by_field_id: IndexMap<String, CellRevision>,
|
2022-03-14 17:24:25 +08:00
|
|
|
pub height: i32,
|
|
|
|
pub visibility: bool,
|
|
|
|
}
|