|
@@ -1,6 +1,6 @@
|
|
|
use crate::entities::revision::{md5, RepeatedRevision, Revision};
|
|
|
use crate::errors::{CollaborateError, CollaborateResult};
|
|
|
-use crate::util::{cal_diff, make_delta_from_revisions};
|
|
|
+use crate::util::{cal_diff, make_delta_from_revisions, make_text_delta_from_revisions};
|
|
|
use flowy_grid_data_model::revision::{
|
|
|
gen_block_id, gen_row_id, CellRevision, GridBlockRevision, RowMetaChangeset, RowRevision,
|
|
|
};
|
|
@@ -9,27 +9,24 @@ use std::borrow::Cow;
|
|
|
use std::collections::HashMap;
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
-pub type GridBlockRevisionDelta = TextDelta;
|
|
|
-pub type GridBlockRevisionDeltaBuilder = TextDeltaBuilder;
|
|
|
-
|
|
|
#[derive(Debug, Clone)]
|
|
|
pub struct GridBlockRevisionPad {
|
|
|
- block_revision: GridBlockRevision,
|
|
|
- pub(crate) delta: GridBlockRevisionDelta,
|
|
|
+ block: GridBlockRevision,
|
|
|
+ delta: TextDelta,
|
|
|
}
|
|
|
|
|
|
impl std::ops::Deref for GridBlockRevisionPad {
|
|
|
type Target = GridBlockRevision;
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
- &self.block_revision
|
|
|
+ &self.block
|
|
|
}
|
|
|
}
|
|
|
|
|
|
impl GridBlockRevisionPad {
|
|
|
pub async fn duplicate_data(&self, duplicated_block_id: &str) -> GridBlockRevision {
|
|
|
let duplicated_rows = self
|
|
|
- .block_revision
|
|
|
+ .block
|
|
|
.rows
|
|
|
.iter()
|
|
|
.map(|row| {
|
|
@@ -45,18 +42,18 @@ impl GridBlockRevisionPad {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- pub fn from_delta(delta: GridBlockRevisionDelta) -> CollaborateResult<Self> {
|
|
|
+ pub fn from_delta(delta: TextDelta) -> CollaborateResult<Self> {
|
|
|
let s = delta.content()?;
|
|
|
- let block_revision: GridBlockRevision = serde_json::from_str(&s).map_err(|e| {
|
|
|
- let msg = format!("Deserialize delta to block meta failed: {}", e);
|
|
|
+ let revision: GridBlockRevision = serde_json::from_str(&s).map_err(|e| {
|
|
|
+ let msg = format!("Deserialize delta to GridBlockRevision failed: {}", e);
|
|
|
tracing::error!("{}", s);
|
|
|
CollaborateError::internal().context(msg)
|
|
|
})?;
|
|
|
- Ok(Self { block_revision, delta })
|
|
|
+ Ok(Self { block: revision, delta })
|
|
|
}
|
|
|
|
|
|
pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> {
|
|
|
- let block_delta: GridBlockRevisionDelta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
|
|
|
+ let block_delta: TextDelta = make_text_delta_from_revisions(revisions)?;
|
|
|
Self::from_delta(block_delta)
|
|
|
}
|
|
|
|
|
@@ -65,7 +62,7 @@ impl GridBlockRevisionPad {
|
|
|
&mut self,
|
|
|
row: RowRevision,
|
|
|
start_row_id: Option<String>,
|
|
|
- ) -> CollaborateResult<Option<GridBlockMetaChange>> {
|
|
|
+ ) -> CollaborateResult<Option<GridBlockRevisionChangeset>> {
|
|
|
self.modify(|rows| {
|
|
|
if let Some(start_row_id) = start_row_id {
|
|
|
if !start_row_id.is_empty() {
|
|
@@ -81,7 +78,10 @@ impl GridBlockRevisionPad {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn delete_rows(&mut self, row_ids: Vec<Cow<'_, String>>) -> CollaborateResult<Option<GridBlockMetaChange>> {
|
|
|
+ pub fn delete_rows(
|
|
|
+ &mut self,
|
|
|
+ row_ids: Vec<Cow<'_, String>>,
|
|
|
+ ) -> CollaborateResult<Option<GridBlockRevisionChangeset>> {
|
|
|
self.modify(|rows| {
|
|
|
rows.retain(|row| !row_ids.contains(&Cow::Borrowed(&row.id)));
|
|
|
Ok(Some(()))
|
|
@@ -93,10 +93,10 @@ impl GridBlockRevisionPad {
|
|
|
T: AsRef<str> + ToOwned + ?Sized,
|
|
|
{
|
|
|
match row_ids {
|
|
|
- None => Ok(self.block_revision.rows.clone()),
|
|
|
+ None => Ok(self.block.rows.clone()),
|
|
|
Some(row_ids) => {
|
|
|
let row_map = self
|
|
|
- .block_revision
|
|
|
+ .block
|
|
|
.rows
|
|
|
.iter()
|
|
|
.map(|row| (row.id.as_str(), row.clone()))
|
|
@@ -136,18 +136,18 @@ impl GridBlockRevisionPad {
|
|
|
}
|
|
|
|
|
|
pub fn number_of_rows(&self) -> i32 {
|
|
|
- self.block_revision.rows.len() as i32
|
|
|
+ self.block.rows.len() as i32
|
|
|
}
|
|
|
|
|
|
pub fn index_of_row(&self, row_id: &str) -> Option<i32> {
|
|
|
- self.block_revision
|
|
|
+ self.block
|
|
|
.rows
|
|
|
.iter()
|
|
|
.position(|row| row.id == row_id)
|
|
|
.map(|index| index as i32)
|
|
|
}
|
|
|
|
|
|
- pub fn update_row(&mut self, changeset: RowMetaChangeset) -> CollaborateResult<Option<GridBlockMetaChange>> {
|
|
|
+ pub fn update_row(&mut self, changeset: RowMetaChangeset) -> CollaborateResult<Option<GridBlockRevisionChangeset>> {
|
|
|
let row_id = changeset.row_id.clone();
|
|
|
self.modify_row(&row_id, |row| {
|
|
|
let mut is_changed = None;
|
|
@@ -172,7 +172,12 @@ impl GridBlockRevisionPad {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn move_row(&mut self, row_id: &str, from: usize, to: usize) -> CollaborateResult<Option<GridBlockMetaChange>> {
|
|
|
+ pub fn move_row(
|
|
|
+ &mut self,
|
|
|
+ row_id: &str,
|
|
|
+ from: usize,
|
|
|
+ to: usize,
|
|
|
+ ) -> CollaborateResult<Option<GridBlockRevisionChangeset>> {
|
|
|
self.modify(|row_revs| {
|
|
|
if let Some(position) = row_revs.iter().position(|row_rev| row_rev.id == row_id) {
|
|
|
debug_assert_eq!(from, position);
|
|
@@ -185,33 +190,36 @@ impl GridBlockRevisionPad {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn modify<F>(&mut self, f: F) -> CollaborateResult<Option<GridBlockMetaChange>>
|
|
|
+ pub fn modify<F>(&mut self, f: F) -> CollaborateResult<Option<GridBlockRevisionChangeset>>
|
|
|
where
|
|
|
F: for<'a> FnOnce(&'a mut Vec<Arc<RowRevision>>) -> CollaborateResult<Option<()>>,
|
|
|
{
|
|
|
let cloned_self = self.clone();
|
|
|
- match f(&mut self.block_revision.rows)? {
|
|
|
+ match f(&mut self.block.rows)? {
|
|
|
None => Ok(None),
|
|
|
Some(_) => {
|
|
|
- let old = cloned_self.to_json()?;
|
|
|
- let new = self.to_json()?;
|
|
|
+ let old = cloned_self.revision_json()?;
|
|
|
+ let new = self.revision_json()?;
|
|
|
match cal_diff::<PhantomAttributes>(old, new) {
|
|
|
None => Ok(None),
|
|
|
Some(delta) => {
|
|
|
- tracing::trace!("[GridBlockMeta] Composing delta {}", delta.json_str());
|
|
|
+ tracing::trace!("[GridBlockRevision] Composing delta {}", delta.json_str());
|
|
|
// tracing::debug!(
|
|
|
// "[GridBlockMeta] current delta: {}",
|
|
|
// self.delta.to_str().unwrap_or_else(|_| "".to_string())
|
|
|
// );
|
|
|
self.delta = self.delta.compose(&delta)?;
|
|
|
- Ok(Some(GridBlockMetaChange { delta, md5: self.md5() }))
|
|
|
+ Ok(Some(GridBlockRevisionChangeset {
|
|
|
+ delta,
|
|
|
+ md5: md5(&self.delta.json_bytes()),
|
|
|
+ }))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fn modify_row<F>(&mut self, row_id: &str, f: F) -> CollaborateResult<Option<GridBlockMetaChange>>
|
|
|
+ fn modify_row<F>(&mut self, row_id: &str, f: F) -> CollaborateResult<Option<GridBlockRevisionChangeset>>
|
|
|
where
|
|
|
F: FnOnce(&mut RowRevision) -> CollaborateResult<Option<()>>,
|
|
|
{
|
|
@@ -225,27 +233,23 @@ impl GridBlockRevisionPad {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub fn to_json(&self) -> CollaborateResult<String> {
|
|
|
- serde_json::to_string(&self.block_revision)
|
|
|
- .map_err(|e| CollaborateError::internal().context(format!("serial trash to json failed: {}", e)))
|
|
|
+ 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)))
|
|
|
}
|
|
|
|
|
|
- pub fn md5(&self) -> String {
|
|
|
- md5(&self.delta.json_bytes())
|
|
|
- }
|
|
|
-
|
|
|
- pub fn delta_str(&self) -> String {
|
|
|
+ pub fn json_str(&self) -> String {
|
|
|
self.delta.json_str()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-pub struct GridBlockMetaChange {
|
|
|
- pub delta: GridBlockRevisionDelta,
|
|
|
+pub struct GridBlockRevisionChangeset {
|
|
|
+ pub delta: TextDelta,
|
|
|
/// md5: the md5 of the grid after applying the change.
|
|
|
pub md5: String,
|
|
|
}
|
|
|
|
|
|
-pub fn make_grid_block_delta(block_rev: &GridBlockRevision) -> GridBlockRevisionDelta {
|
|
|
+pub fn make_grid_block_delta(block_rev: &GridBlockRevision) -> TextDelta {
|
|
|
let json = serde_json::to_string(&block_rev).unwrap();
|
|
|
TextDeltaBuilder::new().insert(&json).build()
|
|
|
}
|
|
@@ -265,14 +269,18 @@ impl std::default::Default for GridBlockRevisionPad {
|
|
|
};
|
|
|
|
|
|
let delta = make_grid_block_delta(&block_revision);
|
|
|
- GridBlockRevisionPad { block_revision, delta }
|
|
|
+ GridBlockRevisionPad {
|
|
|
+ block: block_revision,
|
|
|
+ delta,
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
|
mod tests {
|
|
|
- use crate::client_grid::{GridBlockRevisionDelta, GridBlockRevisionPad};
|
|
|
+ use crate::client_grid::GridBlockRevisionPad;
|
|
|
use flowy_grid_data_model::revision::{RowMetaChangeset, RowRevision};
|
|
|
+ use lib_ot::core::TextDelta;
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
#[test]
|
|
@@ -369,7 +377,7 @@ mod tests {
|
|
|
#[test]
|
|
|
fn block_meta_delete_row() {
|
|
|
let mut pad = test_pad();
|
|
|
- let pre_delta_str = pad.delta_str();
|
|
|
+ let pre_delta_str = pad.json_str();
|
|
|
let row = RowRevision {
|
|
|
id: "1".to_string(),
|
|
|
block_id: pad.block_id.clone(),
|
|
@@ -382,7 +390,7 @@ mod tests {
|
|
|
let change = pad.delete_rows(vec![Cow::Borrowed(&row.id)]).unwrap().unwrap();
|
|
|
assert_eq!(change.delta.json_str(), r#"[{"retain":24},{"delete":66},{"retain":2}]"#);
|
|
|
|
|
|
- assert_eq!(pad.delta_str(), pre_delta_str);
|
|
|
+ assert_eq!(pad.json_str(), pre_delta_str);
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
@@ -412,13 +420,13 @@ mod tests {
|
|
|
);
|
|
|
|
|
|
assert_eq!(
|
|
|
- pad.to_json().unwrap(),
|
|
|
+ pad.revision_json().unwrap(),
|
|
|
r#"{"block_id":"1","rows":[{"id":"1","block_id":"1","cells":[],"height":100,"visibility":true}]}"#
|
|
|
);
|
|
|
}
|
|
|
|
|
|
fn test_pad() -> GridBlockRevisionPad {
|
|
|
- let delta = GridBlockRevisionDelta::from_json(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
|
|
|
+ let delta = TextDelta::from_json(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
|
|
|
GridBlockRevisionPad::from_delta(delta).unwrap()
|
|
|
}
|
|
|
}
|