2022-03-13 23:16:52 +08:00
|
|
|
use crate::errors::{CollaborateError, CollaborateResult};
|
2022-11-08 21:13:28 +08:00
|
|
|
use crate::util::{cal_diff, make_operations_from_revisions};
|
2022-12-30 11:16:47 +08:00
|
|
|
use flowy_http_model::revision::Revision;
|
2022-11-08 21:13:28 +08:00
|
|
|
use flowy_http_model::util::md5;
|
2022-11-08 13:51:12 +08:00
|
|
|
use grid_rev_model::{gen_block_id, gen_row_id, CellRevision, GridBlockRevision, RowChangeset, RowRevision};
|
2022-10-10 11:56:48 +08:00
|
|
|
use lib_ot::core::{DeltaBuilder, DeltaOperations, EmptyAttributes, OperationTransform};
|
2022-04-10 08:25:01 +08:00
|
|
|
use std::borrow::Cow;
|
2022-03-12 09:30:13 +08:00
|
|
|
use std::collections::HashMap;
|
2022-03-10 17:14:10 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-10-10 11:56:48 +08:00
|
|
|
pub type GridBlockOperations = DeltaOperations<EmptyAttributes>;
|
|
|
|
pub type GridBlockOperationsBuilder = DeltaBuilder;
|
|
|
|
|
2022-06-26 15:14:24 +08:00
|
|
|
#[derive(Debug, Clone)]
|
2022-06-15 15:13:50 +08:00
|
|
|
pub struct GridBlockRevisionPad {
|
2022-08-14 23:01:53 +08:00
|
|
|
block: GridBlockRevision,
|
2022-10-10 11:56:48 +08:00
|
|
|
operations: GridBlockOperations,
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:44:15 +08:00
|
|
|
impl std::ops::Deref for GridBlockRevisionPad {
|
|
|
|
type Target = GridBlockRevision;
|
2022-03-10 21:43:23 +08:00
|
|
|
|
2022-06-29 13:44:15 +08:00
|
|
|
fn deref(&self) -> &Self::Target {
|
2022-08-14 23:01:53 +08:00
|
|
|
&self.block
|
2022-06-29 13:44:15 +08:00
|
|
|
}
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
impl GridBlockRevisionPad {
|
2022-06-26 15:14:24 +08:00
|
|
|
pub async fn duplicate_data(&self, duplicated_block_id: &str) -> GridBlockRevision {
|
2022-06-06 20:06:08 +08:00
|
|
|
let duplicated_rows = self
|
2022-08-14 23:01:53 +08:00
|
|
|
.block
|
2022-06-06 20:06:08 +08:00
|
|
|
.rows
|
|
|
|
.iter()
|
|
|
|
.map(|row| {
|
|
|
|
let mut duplicated_row = row.as_ref().clone();
|
|
|
|
duplicated_row.id = gen_row_id();
|
|
|
|
duplicated_row.block_id = duplicated_block_id.to_string();
|
2022-06-26 15:14:24 +08:00
|
|
|
Arc::new(duplicated_row)
|
2022-06-06 20:06:08 +08:00
|
|
|
})
|
2022-06-26 15:14:24 +08:00
|
|
|
.collect::<Vec<Arc<RowRevision>>>();
|
|
|
|
GridBlockRevision {
|
2022-06-06 20:06:08 +08:00
|
|
|
block_id: duplicated_block_id.to_string(),
|
|
|
|
rows: duplicated_rows,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:56:48 +08:00
|
|
|
pub fn from_operations(operations: GridBlockOperations) -> CollaborateResult<Self> {
|
|
|
|
let s = operations.content()?;
|
2022-08-14 23:01:53 +08:00
|
|
|
let revision: GridBlockRevision = serde_json::from_str(&s).map_err(|e| {
|
2022-10-10 11:56:48 +08:00
|
|
|
let msg = format!("Deserialize operations to GridBlockRevision failed: {}", e);
|
2022-04-09 07:35:35 +08:00
|
|
|
tracing::error!("{}", s);
|
2022-03-18 17:14:46 +08:00
|
|
|
CollaborateError::internal().context(msg)
|
2022-03-10 17:14:10 +08:00
|
|
|
})?;
|
2022-10-10 11:56:48 +08:00
|
|
|
Ok(Self {
|
|
|
|
block: revision,
|
|
|
|
operations,
|
|
|
|
})
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> {
|
2022-10-10 11:56:48 +08:00
|
|
|
let operations: GridBlockOperations = make_operations_from_revisions(revisions)?;
|
|
|
|
Self::from_operations(operations)
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
|
2022-03-20 17:17:06 +08:00
|
|
|
#[tracing::instrument(level = "trace", skip(self, row), err)]
|
2022-06-15 15:13:50 +08:00
|
|
|
pub fn add_row_rev(
|
2022-03-16 16:10:35 +08:00
|
|
|
&mut self,
|
2022-06-15 15:13:50 +08:00
|
|
|
row: RowRevision,
|
2022-03-17 17:25:43 +08:00
|
|
|
start_row_id: Option<String>,
|
2022-08-14 23:01:53 +08:00
|
|
|
) -> CollaborateResult<Option<GridBlockRevisionChangeset>> {
|
2022-03-10 21:43:23 +08:00
|
|
|
self.modify(|rows| {
|
2022-03-20 17:17:06 +08:00
|
|
|
if let Some(start_row_id) = start_row_id {
|
2022-04-10 14:24:12 +08:00
|
|
|
if !start_row_id.is_empty() {
|
|
|
|
if let Some(index) = rows.iter().position(|row| row.id == start_row_id) {
|
|
|
|
rows.insert(index + 1, Arc::new(row));
|
|
|
|
return Ok(Some(()));
|
|
|
|
}
|
2022-03-16 16:10:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 21:43:23 +08:00
|
|
|
rows.push(Arc::new(row));
|
2022-03-10 17:14:10 +08:00
|
|
|
Ok(Some(()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-14 23:01:53 +08:00
|
|
|
pub fn delete_rows(
|
|
|
|
&mut self,
|
|
|
|
row_ids: Vec<Cow<'_, String>>,
|
|
|
|
) -> CollaborateResult<Option<GridBlockRevisionChangeset>> {
|
2022-03-10 21:43:23 +08:00
|
|
|
self.modify(|rows| {
|
2022-04-10 08:25:01 +08:00
|
|
|
rows.retain(|row| !row_ids.contains(&Cow::Borrowed(&row.id)));
|
2022-03-10 17:14:10 +08:00
|
|
|
Ok(Some(()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-26 21:28:08 +08:00
|
|
|
pub fn get_row_rev(&self, row_id: &str) -> Option<(usize, Arc<RowRevision>)> {
|
|
|
|
for (index, row) in self.block.rows.iter().enumerate() {
|
|
|
|
if row.id == row_id {
|
|
|
|
return Some((index, row.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
pub fn get_row_revs<T>(&self, row_ids: Option<Vec<Cow<'_, T>>>) -> CollaborateResult<Vec<Arc<RowRevision>>>
|
2022-04-10 08:25:01 +08:00
|
|
|
where
|
|
|
|
T: AsRef<str> + ToOwned + ?Sized,
|
|
|
|
{
|
2022-03-15 11:07:18 +08:00
|
|
|
match row_ids {
|
2022-08-14 23:01:53 +08:00
|
|
|
None => Ok(self.block.rows.clone()),
|
2022-03-15 11:07:18 +08:00
|
|
|
Some(row_ids) => {
|
|
|
|
let row_map = self
|
2022-08-14 23:01:53 +08:00
|
|
|
.block
|
2022-04-10 10:31:55 +08:00
|
|
|
.rows
|
2022-03-15 11:07:18 +08:00
|
|
|
.iter()
|
2022-04-10 08:25:01 +08:00
|
|
|
.map(|row| (row.id.as_str(), row.clone()))
|
2022-06-15 15:13:50 +08:00
|
|
|
.collect::<HashMap<&str, Arc<RowRevision>>>();
|
2022-03-12 09:30:13 +08:00
|
|
|
|
2022-03-15 11:07:18 +08:00
|
|
|
Ok(row_ids
|
|
|
|
.iter()
|
2022-04-10 08:25:01 +08:00
|
|
|
.flat_map(|row_id| {
|
|
|
|
let row_id = row_id.as_ref().as_ref();
|
|
|
|
match row_map.get(row_id) {
|
|
|
|
None => {
|
|
|
|
tracing::error!("Can't find the row with id: {}", row_id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Some(row) => Some(row.clone()),
|
2022-03-15 11:07:18 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>())
|
|
|
|
}
|
|
|
|
}
|
2022-03-13 23:16:52 +08:00
|
|
|
}
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
pub fn get_cell_revs(
|
2022-04-10 08:25:01 +08:00
|
|
|
&self,
|
|
|
|
field_id: &str,
|
|
|
|
row_ids: Option<Vec<Cow<'_, String>>>,
|
2022-06-15 15:13:50 +08:00
|
|
|
) -> CollaborateResult<Vec<CellRevision>> {
|
|
|
|
let rows = self.get_row_revs(row_ids)?;
|
|
|
|
let cell_revs = rows
|
2022-04-02 10:54:01 +08:00
|
|
|
.iter()
|
|
|
|
.flat_map(|row| {
|
2022-06-15 15:13:50 +08:00
|
|
|
let cell_rev = row.cells.get(field_id)?;
|
|
|
|
Some(cell_rev.clone())
|
2022-04-02 10:54:01 +08:00
|
|
|
})
|
2022-06-15 15:13:50 +08:00
|
|
|
.collect::<Vec<CellRevision>>();
|
|
|
|
Ok(cell_revs)
|
2022-04-02 10:54:01 +08:00
|
|
|
}
|
|
|
|
|
2022-03-13 11:06:28 +08:00
|
|
|
pub fn number_of_rows(&self) -> i32 {
|
2022-08-14 23:01:53 +08:00
|
|
|
self.block.rows.len() as i32
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
|
2022-08-16 15:49:54 +08:00
|
|
|
pub fn index_of_row(&self, row_id: &str) -> Option<usize> {
|
|
|
|
self.block.rows.iter().position(|row| row.id == row_id)
|
2022-04-10 14:24:12 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:02:44 +08:00
|
|
|
pub fn update_row(&mut self, changeset: RowChangeset) -> CollaborateResult<Option<GridBlockRevisionChangeset>> {
|
2022-03-10 21:43:23 +08:00
|
|
|
let row_id = changeset.row_id.clone();
|
|
|
|
self.modify_row(&row_id, |row| {
|
|
|
|
let mut is_changed = None;
|
|
|
|
if let Some(height) = changeset.height {
|
|
|
|
row.height = height;
|
|
|
|
is_changed = Some(());
|
|
|
|
}
|
2022-03-10 17:14:10 +08:00
|
|
|
|
2022-03-10 21:43:23 +08:00
|
|
|
if let Some(visibility) = changeset.visibility {
|
|
|
|
row.visibility = visibility;
|
|
|
|
is_changed = Some(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if !changeset.cell_by_field_id.is_empty() {
|
|
|
|
is_changed = Some(());
|
|
|
|
changeset.cell_by_field_id.into_iter().for_each(|(field_id, cell)| {
|
2022-04-09 07:35:35 +08:00
|
|
|
row.cells.insert(field_id, cell);
|
2022-03-10 21:43:23 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(is_changed)
|
|
|
|
})
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
|
2022-08-14 23:01:53 +08:00
|
|
|
pub fn move_row(
|
|
|
|
&mut self,
|
|
|
|
row_id: &str,
|
|
|
|
from: usize,
|
|
|
|
to: usize,
|
|
|
|
) -> CollaborateResult<Option<GridBlockRevisionChangeset>> {
|
2022-06-15 15:13:50 +08:00
|
|
|
self.modify(|row_revs| {
|
|
|
|
if let Some(position) = row_revs.iter().position(|row_rev| row_rev.id == row_id) {
|
2022-04-16 09:25:12 +08:00
|
|
|
debug_assert_eq!(from, position);
|
2022-06-15 15:13:50 +08:00
|
|
|
let row_rev = row_revs.remove(position);
|
2022-08-17 19:29:14 +08:00
|
|
|
if to > row_revs.len() {
|
2022-08-17 20:15:10 +08:00
|
|
|
Err(CollaborateError::out_of_bound())
|
2022-08-17 19:29:14 +08:00
|
|
|
} else {
|
|
|
|
row_revs.insert(to, row_rev);
|
|
|
|
Ok(Some(()))
|
|
|
|
}
|
2022-04-16 09:25:12 +08:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-14 23:01:53 +08:00
|
|
|
pub fn modify<F>(&mut self, f: F) -> CollaborateResult<Option<GridBlockRevisionChangeset>>
|
2022-03-10 17:14:10 +08:00
|
|
|
where
|
2022-06-15 15:13:50 +08:00
|
|
|
F: for<'a> FnOnce(&'a mut Vec<Arc<RowRevision>>) -> CollaborateResult<Option<()>>,
|
2022-03-10 17:14:10 +08:00
|
|
|
{
|
2022-03-10 21:43:23 +08:00
|
|
|
let cloned_self = self.clone();
|
2022-08-14 23:01:53 +08:00
|
|
|
match f(&mut self.block.rows)? {
|
2022-03-10 17:14:10 +08:00
|
|
|
None => Ok(None),
|
|
|
|
Some(_) => {
|
2022-08-14 23:01:53 +08:00
|
|
|
let old = cloned_self.revision_json()?;
|
|
|
|
let new = self.revision_json()?;
|
2022-09-12 10:44:33 +08:00
|
|
|
match cal_diff::<EmptyAttributes>(old, new) {
|
2022-03-10 17:14:10 +08:00
|
|
|
None => Ok(None),
|
2022-10-10 11:56:48 +08:00
|
|
|
Some(operations) => {
|
|
|
|
tracing::trace!("[GridBlockRevision] Composing operations {}", operations.json_str());
|
|
|
|
self.operations = self.operations.compose(&operations)?;
|
2022-08-14 23:01:53 +08:00
|
|
|
Ok(Some(GridBlockRevisionChangeset {
|
2022-10-10 11:56:48 +08:00
|
|
|
operations,
|
|
|
|
md5: md5(&self.operations.json_bytes()),
|
2022-08-14 23:01:53 +08:00
|
|
|
}))
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-10 21:43:23 +08:00
|
|
|
|
2022-08-14 23:01:53 +08:00
|
|
|
fn modify_row<F>(&mut self, row_id: &str, f: F) -> CollaborateResult<Option<GridBlockRevisionChangeset>>
|
2022-03-10 21:43:23 +08:00
|
|
|
where
|
2022-06-15 15:13:50 +08:00
|
|
|
F: FnOnce(&mut RowRevision) -> CollaborateResult<Option<()>>,
|
2022-03-10 21:43:23 +08:00
|
|
|
{
|
|
|
|
self.modify(|rows| {
|
2022-06-15 15:13:50 +08:00
|
|
|
if let Some(row_rev) = rows.iter_mut().find(|row_rev| row_id == row_rev.id) {
|
|
|
|
f(Arc::make_mut(row_rev))
|
2022-03-10 21:43:23 +08:00
|
|
|
} else {
|
|
|
|
tracing::warn!("[BlockMetaPad]: Can't find any row with id: {}", row_id);
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-14 23:01:53 +08:00
|
|
|
pub fn revision_json(&self) -> CollaborateResult<String> {
|
|
|
|
serde_json::to_string(&self.block)
|
|
|
|
.map_err(|e| CollaborateError::internal().context(format!("serial block to json failed: {}", e)))
|
2022-03-10 21:43:23 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 11:56:48 +08:00
|
|
|
pub fn operations_json_str(&self) -> String {
|
|
|
|
self.operations.json_str()
|
2022-03-10 21:43:23 +08:00
|
|
|
}
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
|
2022-08-14 23:01:53 +08:00
|
|
|
pub struct GridBlockRevisionChangeset {
|
2022-10-10 11:56:48 +08:00
|
|
|
pub operations: GridBlockOperations,
|
2022-03-10 17:14:10 +08:00
|
|
|
/// md5: the md5 of the grid after applying the change.
|
|
|
|
pub md5: String,
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:56:48 +08:00
|
|
|
pub fn make_grid_block_operations(block_rev: &GridBlockRevision) -> GridBlockOperations {
|
2022-06-26 15:14:24 +08:00
|
|
|
let json = serde_json::to_string(&block_rev).unwrap();
|
2022-10-10 11:56:48 +08:00
|
|
|
GridBlockOperationsBuilder::new().insert(&json).build()
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
|
2022-12-30 11:16:47 +08:00
|
|
|
pub fn make_grid_block_revisions(_user_id: &str, grid_block_meta_data: &GridBlockRevision) -> Vec<Revision> {
|
2022-10-10 11:56:48 +08:00
|
|
|
let operations = make_grid_block_operations(grid_block_meta_data);
|
|
|
|
let bytes = operations.json_bytes();
|
2022-11-02 17:15:27 +08:00
|
|
|
let revision = Revision::initial_revision(&grid_block_meta_data.block_id, bytes);
|
2022-12-30 11:16:47 +08:00
|
|
|
vec![revision]
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
impl std::default::Default for GridBlockRevisionPad {
|
2022-03-10 17:14:10 +08:00
|
|
|
fn default() -> Self {
|
2022-06-26 15:14:24 +08:00
|
|
|
let block_revision = GridBlockRevision {
|
2022-04-11 15:27:03 +08:00
|
|
|
block_id: gen_block_id(),
|
2022-04-10 07:10:15 +08:00
|
|
|
rows: vec![],
|
2022-03-10 17:14:10 +08:00
|
|
|
};
|
2022-03-10 21:43:23 +08:00
|
|
|
|
2022-10-10 11:56:48 +08:00
|
|
|
let operations = make_grid_block_operations(&block_revision);
|
2022-08-14 23:01:53 +08:00
|
|
|
GridBlockRevisionPad {
|
|
|
|
block: block_revision,
|
2022-10-10 11:56:48 +08:00
|
|
|
operations,
|
2022-08-14 23:01:53 +08:00
|
|
|
}
|
2022-03-10 17:14:10 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-10 21:43:23 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-10-10 11:56:48 +08:00
|
|
|
use crate::client_grid::{GridBlockOperations, GridBlockRevisionPad};
|
2022-11-08 13:51:12 +08:00
|
|
|
use grid_rev_model::{RowChangeset, RowRevision};
|
2022-10-10 11:56:48 +08:00
|
|
|
|
2022-04-10 08:25:01 +08:00
|
|
|
use std::borrow::Cow;
|
2022-03-10 21:43:23 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_meta_add_row() {
|
|
|
|
let mut pad = test_pad();
|
2022-06-15 15:13:50 +08:00
|
|
|
let row = RowRevision {
|
2022-03-10 21:43:23 +08:00
|
|
|
id: "1".to_string(),
|
|
|
|
block_id: pad.block_id.clone(),
|
2022-04-09 07:35:35 +08:00
|
|
|
cells: Default::default(),
|
2022-03-10 21:43:23 +08:00
|
|
|
height: 0,
|
|
|
|
visibility: false,
|
|
|
|
};
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
let change = pad.add_row_rev(row.clone(), None).unwrap().unwrap();
|
2022-04-11 14:09:50 +08:00
|
|
|
assert_eq!(pad.rows.first().unwrap().as_ref(), &row);
|
2022-03-10 21:43:23 +08:00
|
|
|
assert_eq!(
|
2022-10-10 11:56:48 +08:00
|
|
|
change.operations.json_str(),
|
2022-04-12 11:52:14 +08:00
|
|
|
r#"[{"retain":24},{"insert":"{\"id\":\"1\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
2022-03-10 21:43:23 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:10:35 +08:00
|
|
|
#[test]
|
|
|
|
fn block_meta_insert_row() {
|
|
|
|
let mut pad = test_pad();
|
2022-06-15 15:13:50 +08:00
|
|
|
let row_1 = test_row_rev("1", &pad);
|
|
|
|
let row_2 = test_row_rev("2", &pad);
|
|
|
|
let row_3 = test_row_rev("3", &pad);
|
2022-03-16 16:10:35 +08:00
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
let change = pad.add_row_rev(row_1.clone(), None).unwrap().unwrap();
|
2022-03-16 16:10:35 +08:00
|
|
|
assert_eq!(
|
2022-10-10 11:56:48 +08:00
|
|
|
change.operations.json_str(),
|
2022-04-12 11:52:14 +08:00
|
|
|
r#"[{"retain":24},{"insert":"{\"id\":\"1\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
2022-03-16 16:10:35 +08:00
|
|
|
);
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
let change = pad.add_row_rev(row_2.clone(), None).unwrap().unwrap();
|
2022-03-16 16:10:35 +08:00
|
|
|
assert_eq!(
|
2022-10-10 11:56:48 +08:00
|
|
|
change.operations.json_str(),
|
2022-04-12 11:52:14 +08:00
|
|
|
r#"[{"retain":90},{"insert":",{\"id\":\"2\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
2022-03-16 16:10:35 +08:00
|
|
|
);
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
let change = pad.add_row_rev(row_3.clone(), Some("2".to_string())).unwrap().unwrap();
|
2022-03-16 16:10:35 +08:00
|
|
|
assert_eq!(
|
2022-10-10 11:56:48 +08:00
|
|
|
change.operations.json_str(),
|
2022-04-12 11:52:14 +08:00
|
|
|
r#"[{"retain":157},{"insert":",{\"id\":\"3\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
2022-03-16 16:10:35 +08:00
|
|
|
);
|
|
|
|
|
2022-04-10 10:31:55 +08:00
|
|
|
assert_eq!(*pad.rows[0], row_1);
|
|
|
|
assert_eq!(*pad.rows[1], row_2);
|
|
|
|
assert_eq!(*pad.rows[2], row_3);
|
2022-03-16 16:10:35 +08:00
|
|
|
}
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
fn test_row_rev(id: &str, pad: &GridBlockRevisionPad) -> RowRevision {
|
|
|
|
RowRevision {
|
2022-03-16 16:10:35 +08:00
|
|
|
id: id.to_string(),
|
|
|
|
block_id: pad.block_id.clone(),
|
2022-04-09 07:35:35 +08:00
|
|
|
cells: Default::default(),
|
2022-03-16 16:10:35 +08:00
|
|
|
height: 0,
|
|
|
|
visibility: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_meta_insert_row2() {
|
|
|
|
let mut pad = test_pad();
|
2022-06-15 15:13:50 +08:00
|
|
|
let row_1 = test_row_rev("1", &pad);
|
|
|
|
let row_2 = test_row_rev("2", &pad);
|
|
|
|
let row_3 = test_row_rev("3", &pad);
|
2022-03-16 16:10:35 +08:00
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
let _ = pad.add_row_rev(row_1.clone(), None).unwrap().unwrap();
|
|
|
|
let _ = pad.add_row_rev(row_2.clone(), None).unwrap().unwrap();
|
|
|
|
let _ = pad.add_row_rev(row_3.clone(), Some("1".to_string())).unwrap().unwrap();
|
2022-03-16 16:10:35 +08:00
|
|
|
|
2022-04-10 14:33:34 +08:00
|
|
|
assert_eq!(*pad.rows[0], row_1);
|
|
|
|
assert_eq!(*pad.rows[1], row_3);
|
2022-04-10 10:31:55 +08:00
|
|
|
assert_eq!(*pad.rows[2], row_2);
|
2022-03-16 16:10:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_meta_insert_row3() {
|
|
|
|
let mut pad = test_pad();
|
2022-06-15 15:13:50 +08:00
|
|
|
let row_1 = test_row_rev("1", &pad);
|
|
|
|
let row_2 = test_row_rev("2", &pad);
|
|
|
|
let row_3 = test_row_rev("3", &pad);
|
2022-03-16 16:10:35 +08:00
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
let _ = pad.add_row_rev(row_1.clone(), None).unwrap().unwrap();
|
|
|
|
let _ = pad.add_row_rev(row_2.clone(), None).unwrap().unwrap();
|
|
|
|
let _ = pad.add_row_rev(row_3.clone(), Some("".to_string())).unwrap().unwrap();
|
2022-03-16 16:10:35 +08:00
|
|
|
|
2022-04-10 14:33:34 +08:00
|
|
|
assert_eq!(*pad.rows[0], row_1);
|
|
|
|
assert_eq!(*pad.rows[1], row_2);
|
|
|
|
assert_eq!(*pad.rows[2], row_3);
|
2022-03-16 16:10:35 +08:00
|
|
|
}
|
|
|
|
|
2022-03-10 21:43:23 +08:00
|
|
|
#[test]
|
|
|
|
fn block_meta_delete_row() {
|
|
|
|
let mut pad = test_pad();
|
2022-10-10 11:56:48 +08:00
|
|
|
let pre_json_str = pad.operations_json_str();
|
2022-06-15 15:13:50 +08:00
|
|
|
let row = RowRevision {
|
2022-03-10 21:43:23 +08:00
|
|
|
id: "1".to_string(),
|
|
|
|
block_id: pad.block_id.clone(),
|
2022-04-09 07:35:35 +08:00
|
|
|
cells: Default::default(),
|
2022-03-10 21:43:23 +08:00
|
|
|
height: 0,
|
|
|
|
visibility: false,
|
|
|
|
};
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
let _ = pad.add_row_rev(row.clone(), None).unwrap().unwrap();
|
2022-04-10 08:25:01 +08:00
|
|
|
let change = pad.delete_rows(vec![Cow::Borrowed(&row.id)]).unwrap().unwrap();
|
2022-10-10 11:56:48 +08:00
|
|
|
assert_eq!(
|
|
|
|
change.operations.json_str(),
|
|
|
|
r#"[{"retain":24},{"delete":66},{"retain":2}]"#
|
|
|
|
);
|
2022-03-10 21:43:23 +08:00
|
|
|
|
2022-10-10 11:56:48 +08:00
|
|
|
assert_eq!(pad.operations_json_str(), pre_json_str);
|
2022-03-10 21:43:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_meta_update_row() {
|
|
|
|
let mut pad = test_pad();
|
2022-06-15 15:13:50 +08:00
|
|
|
let row = RowRevision {
|
2022-03-10 21:43:23 +08:00
|
|
|
id: "1".to_string(),
|
|
|
|
block_id: pad.block_id.clone(),
|
2022-04-09 07:35:35 +08:00
|
|
|
cells: Default::default(),
|
2022-03-10 21:43:23 +08:00
|
|
|
height: 0,
|
|
|
|
visibility: false,
|
|
|
|
};
|
|
|
|
|
2022-08-18 21:43:05 +08:00
|
|
|
let changeset = RowChangeset {
|
2022-03-10 21:43:23 +08:00
|
|
|
row_id: row.id.clone(),
|
|
|
|
height: Some(100),
|
|
|
|
visibility: Some(true),
|
|
|
|
cell_by_field_id: Default::default(),
|
|
|
|
};
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
let _ = pad.add_row_rev(row, None).unwrap().unwrap();
|
2022-03-10 21:43:23 +08:00
|
|
|
let change = pad.update_row(changeset).unwrap().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-10-10 11:56:48 +08:00
|
|
|
change.operations.json_str(),
|
2022-04-10 10:31:55 +08:00
|
|
|
r#"[{"retain":69},{"insert":"10"},{"retain":15},{"insert":"tru"},{"delete":4},{"retain":4}]"#
|
2022-03-10 21:43:23 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-08-14 23:01:53 +08:00
|
|
|
pad.revision_json().unwrap(),
|
2022-04-11 14:09:50 +08:00
|
|
|
r#"{"block_id":"1","rows":[{"id":"1","block_id":"1","cells":[],"height":100,"visibility":true}]}"#
|
2022-03-10 21:43:23 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-15 15:13:50 +08:00
|
|
|
fn test_pad() -> GridBlockRevisionPad {
|
2022-10-10 11:56:48 +08:00
|
|
|
let operations = GridBlockOperations::from_json(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
|
|
|
|
GridBlockRevisionPad::from_operations(operations).unwrap()
|
2022-03-10 21:43:23 +08:00
|
|
|
}
|
|
|
|
}
|