2022-05-11 11:34:13 +08:00
|
|
|
use crate::services::field::SelectOptionCellContentChangeset;
|
2022-04-11 15:27:03 +08:00
|
|
|
use crate::services::row::apply_cell_data_changeset;
|
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-03-13 11:06:28 +08:00
|
|
|
|
2022-03-18 21:04:01 +08:00
|
|
|
pub struct CreateRowMetaBuilder<'a> {
|
2022-06-15 15:13:50 +08:00
|
|
|
field_rev_map: HashMap<&'a String, &'a FieldRevision>,
|
2022-03-18 21:04:01 +08:00
|
|
|
payload: CreateRowMetaPayload,
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 21:04:01 +08:00
|
|
|
impl<'a> CreateRowMetaBuilder<'a> {
|
2022-06-15 15:13:50 +08:00
|
|
|
pub fn new(fields: &'a [FieldRevision]) -> Self {
|
|
|
|
let field_rev_map = fields
|
2022-03-16 10:02:37 +08:00
|
|
|
.iter()
|
|
|
|
.map(|field| (&field.id, field))
|
2022-06-15 15:13:50 +08:00
|
|
|
.collect::<HashMap<&String, &FieldRevision>>();
|
2022-03-16 10:02:37 +08:00
|
|
|
|
2022-03-18 21:04:01 +08:00
|
|
|
let payload = CreateRowMetaPayload {
|
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-03-18 21:04:01 +08:00
|
|
|
Self {
|
2022-06-15 15:13:50 +08:00
|
|
|
field_rev_map: field_rev_map,
|
2022-03-18 21:04:01 +08:00
|
|
|
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) => {
|
|
|
|
let data = apply_cell_data_changeset(&data, None, field_rev)?;
|
|
|
|
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-05-11 11:34:13 +08:00
|
|
|
let cell_data = SelectOptionCellContentChangeset::from_insert(&data).to_str();
|
2022-06-15 15:13:50 +08:00
|
|
|
let data = apply_cell_data_changeset(&cell_data, None, field_rev)?;
|
|
|
|
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-03-18 21:04:01 +08:00
|
|
|
pub fn build(self) -> CreateRowMetaPayload {
|
|
|
|
self.payload
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-14 17:24:25 +08:00
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
pub fn make_row_rev_from_context(block_id: &str, payload: CreateRowMetaPayload) -> RowRevision {
|
|
|
|
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-03-18 21:04:01 +08:00
|
|
|
pub struct CreateRowMetaPayload {
|
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,
|
|
|
|
}
|