159 lines
4.8 KiB
Rust
Raw Normal View History

2022-03-05 10:59:44 +08:00
use crate::entities::revision::{md5, RepeatedRevision, Revision};
2022-03-04 18:11:12 +08:00
use crate::errors::{internal_error, CollaborateError, CollaborateResult};
use crate::util::{cal_diff, make_delta_from_revisions};
2022-03-10 12:01:31 +08:00
use flowy_grid_data_model::entities::{Field, FieldOrder, Grid, GridMeta, RowMeta, RowOrder};
2022-03-04 18:11:12 +08:00
use lib_infra::uuid;
2022-03-06 09:03:02 +08:00
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
2022-03-04 18:11:12 +08:00
use std::sync::Arc;
pub type GridDelta = PlainTextDelta;
pub type GridDeltaBuilder = PlainTextDeltaBuilder;
2022-03-10 12:01:31 +08:00
pub struct GridMetaPad {
pub(crate) grid_meta: Arc<GridMeta>,
2022-03-04 18:11:12 +08:00
pub(crate) delta: GridDelta,
}
2022-03-10 12:01:31 +08:00
impl GridMetaPad {
2022-03-04 18:11:12 +08:00
pub fn from_delta(delta: GridDelta) -> CollaborateResult<Self> {
let s = delta.to_str()?;
2022-03-10 12:01:31 +08:00
let grid: GridMeta = serde_json::from_str(&s)
2022-03-04 18:11:12 +08:00
.map_err(|e| CollaborateError::internal().context(format!("Deserialize delta to grid failed: {}", e)))?;
Ok(Self {
2022-03-10 12:01:31 +08:00
grid_meta: Arc::new(grid),
2022-03-04 18:11:12 +08:00
delta,
})
}
2022-03-05 10:59:44 +08:00
pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> {
2022-03-10 12:01:31 +08:00
let grid_delta: GridDelta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
Self::from_delta(grid_delta)
2022-03-04 18:11:12 +08:00
}
2022-03-10 12:01:31 +08:00
pub fn create_row(&mut self, row: RowMeta) -> CollaborateResult<Option<GridChange>> {
2022-03-04 18:11:12 +08:00
self.modify_grid(|grid| {
2022-03-10 12:01:31 +08:00
grid.rows.push(row);
2022-03-04 18:11:12 +08:00
Ok(Some(()))
})
}
2022-03-10 12:01:31 +08:00
pub fn create_field(&mut self, field: Field) -> CollaborateResult<Option<GridChange>> {
2022-03-04 18:11:12 +08:00
self.modify_grid(|grid| {
2022-03-10 12:01:31 +08:00
grid.fields.push(field);
2022-03-04 18:11:12 +08:00
Ok(Some(()))
})
}
2022-03-06 09:03:02 +08:00
pub fn delete_rows(&mut self, row_ids: &[String]) -> CollaborateResult<Option<GridChange>> {
2022-03-04 21:26:32 +08:00
self.modify_grid(|grid| {
2022-03-10 12:01:31 +08:00
grid.rows.retain(|row| !row_ids.contains(&row.id));
2022-03-04 21:26:32 +08:00
Ok(Some(()))
})
2022-03-04 18:11:12 +08:00
}
pub fn delete_field(&mut self, field_id: &str) -> CollaborateResult<Option<GridChange>> {
2022-03-10 12:01:31 +08:00
self.modify_grid(|grid| match grid.fields.iter().position(|field| field.id == field_id) {
None => Ok(None),
Some(index) => {
grid.fields.remove(index);
Ok(Some(()))
2022-03-04 18:11:12 +08:00
}
})
}
pub fn md5(&self) -> String {
md5(&self.delta.to_bytes())
}
2022-03-05 10:59:44 +08:00
pub fn grid_data(&self) -> Grid {
2022-03-10 12:01:31 +08:00
let field_orders = self
.grid_meta
.fields
.iter()
.map(FieldOrder::from)
.collect::<Vec<FieldOrder>>();
let row_orders = self
.grid_meta
.rows
.iter()
.map(RowOrder::from)
.collect::<Vec<RowOrder>>();
Grid {
id: "".to_string(),
field_orders,
row_orders,
}
2022-03-05 10:59:44 +08:00
}
2022-03-05 22:30:42 +08:00
pub fn delta_str(&self) -> String {
self.delta.to_delta_str()
}
2022-03-10 12:01:31 +08:00
pub fn fields(&self) -> &[Field] {
&self.grid_meta.fields
2022-03-05 17:52:25 +08:00
}
2022-03-04 18:11:12 +08:00
pub fn modify_grid<F>(&mut self, f: F) -> CollaborateResult<Option<GridChange>>
where
2022-03-10 12:01:31 +08:00
F: FnOnce(&mut GridMeta) -> CollaborateResult<Option<()>>,
2022-03-04 18:11:12 +08:00
{
2022-03-10 12:01:31 +08:00
let cloned_grid = self.grid_meta.clone();
match f(Arc::make_mut(&mut self.grid_meta))? {
2022-03-04 18:11:12 +08:00
None => Ok(None),
Some(_) => {
let old = json_from_grid(&cloned_grid)?;
2022-03-10 12:01:31 +08:00
let new = json_from_grid(&self.grid_meta)?;
2022-03-04 18:11:12 +08:00
match cal_diff::<PlainTextAttributes>(old, new) {
None => Ok(None),
Some(delta) => {
self.delta = self.delta.compose(&delta)?;
Ok(Some(GridChange { delta, md5: self.md5() }))
}
}
}
}
}
}
2022-03-10 12:01:31 +08:00
fn json_from_grid(grid: &Arc<GridMeta>) -> CollaborateResult<String> {
2022-03-04 18:11:12 +08:00
let json = serde_json::to_string(grid)
.map_err(|err| internal_error(format!("Serialize grid to json str failed. {:?}", err)))?;
Ok(json)
}
pub struct GridChange {
pub delta: GridDelta,
/// md5: the md5 of the grid after applying the change.
pub md5: String,
}
2022-03-10 12:01:31 +08:00
pub fn make_grid_delta(grid_meta: &GridMeta) -> GridDelta {
let json = serde_json::to_string(&grid_meta).unwrap();
2022-03-04 18:11:12 +08:00
PlainTextDeltaBuilder::new().insert(&json).build()
}
2022-03-10 12:01:31 +08:00
pub fn make_grid_revisions(user_id: &str, grid_meta: &GridMeta) -> RepeatedRevision {
let delta = make_grid_delta(grid_meta);
2022-03-05 10:59:44 +08:00
let bytes = delta.to_bytes();
2022-03-10 12:01:31 +08:00
let revision = Revision::initial_revision(user_id, &grid_meta.grid_id, bytes);
2022-03-05 10:59:44 +08:00
revision.into()
}
2022-03-10 12:01:31 +08:00
impl std::default::Default for GridMetaPad {
2022-03-04 18:11:12 +08:00
fn default() -> Self {
2022-03-10 12:01:31 +08:00
let grid = GridMeta {
grid_id: uuid(),
fields: vec![],
rows: vec![],
2022-03-04 18:11:12 +08:00
};
2022-03-05 10:59:44 +08:00
let delta = make_grid_delta(&grid);
2022-03-10 12:01:31 +08:00
GridMetaPad {
grid_meta: Arc::new(grid),
2022-03-04 18:11:12 +08:00
delta,
}
}
}