mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: get cell metas
This commit is contained in:
@ -1,10 +1,11 @@
|
||||
use crate::entities::revision::{md5, RepeatedRevision, Revision};
|
||||
use crate::errors::{CollaborateError, CollaborateResult};
|
||||
use crate::util::{cal_diff, make_delta_from_revisions};
|
||||
use flowy_grid_data_model::entities::{GridBlockMetaSerde, RowMeta, RowMetaChangeset};
|
||||
use flowy_grid_data_model::entities::{CellMeta, GridBlockMetaData, RowMeta, RowMetaChangeset};
|
||||
use lib_infra::uuid;
|
||||
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -25,12 +26,12 @@ impl GridBlockMetaPad {
|
||||
let s = delta.to_str()?;
|
||||
tracing::info!("delta: {}", delta);
|
||||
tracing::info!("{}", s);
|
||||
let block_meta: GridBlockMetaSerde = serde_json::from_str(&s).map_err(|e| {
|
||||
let meta_data: GridBlockMetaData = serde_json::from_str(&s).map_err(|e| {
|
||||
let msg = format!("Deserialize delta to block meta failed: {}", e);
|
||||
CollaborateError::internal().context(msg)
|
||||
})?;
|
||||
let block_id = block_meta.block_id;
|
||||
let rows = block_meta
|
||||
let block_id = meta_data.block_id;
|
||||
let rows = meta_data
|
||||
.row_metas
|
||||
.into_iter()
|
||||
.map(Arc::new)
|
||||
@ -78,7 +79,7 @@ impl GridBlockMetaPad {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_row_metas(&self, row_ids: Option<Vec<String>>) -> CollaborateResult<Vec<Arc<RowMeta>>> {
|
||||
pub fn get_row_metas(&self, row_ids: &Option<Vec<String>>) -> CollaborateResult<Vec<Arc<RowMeta>>> {
|
||||
match row_ids {
|
||||
None => Ok(self.row_metas.to_vec()),
|
||||
Some(row_ids) => {
|
||||
@ -102,6 +103,18 @@ impl GridBlockMetaPad {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_cell_metas(&self, field_id: &str, row_ids: &Option<Vec<String>>) -> CollaborateResult<Vec<CellMeta>> {
|
||||
let rows = self.get_row_metas(row_ids)?;
|
||||
let cell_metas = rows
|
||||
.iter()
|
||||
.flat_map(|row| {
|
||||
let cell_meta = row.cell_by_field_id.get(field_id)?;
|
||||
Some(cell_meta.clone())
|
||||
})
|
||||
.collect::<Vec<CellMeta>>();
|
||||
Ok(cell_metas)
|
||||
}
|
||||
|
||||
pub fn number_of_rows(&self) -> i32 {
|
||||
self.row_metas.len() as i32
|
||||
}
|
||||
@ -186,12 +199,12 @@ pub struct GridBlockMetaChange {
|
||||
pub md5: String,
|
||||
}
|
||||
|
||||
pub fn make_block_meta_delta(grid_block_meta_data: &GridBlockMetaSerde) -> GridBlockMetaDelta {
|
||||
pub fn make_block_meta_delta(grid_block_meta_data: &GridBlockMetaData) -> GridBlockMetaDelta {
|
||||
let json = serde_json::to_string(&grid_block_meta_data).unwrap();
|
||||
PlainTextDeltaBuilder::new().insert(&json).build()
|
||||
}
|
||||
|
||||
pub fn make_block_meta_revisions(user_id: &str, grid_block_meta_data: &GridBlockMetaSerde) -> RepeatedRevision {
|
||||
pub fn make_block_meta_revisions(user_id: &str, grid_block_meta_data: &GridBlockMetaData) -> RepeatedRevision {
|
||||
let delta = make_block_meta_delta(grid_block_meta_data);
|
||||
let bytes = delta.to_delta_bytes();
|
||||
let revision = Revision::initial_revision(user_id, &grid_block_meta_data.block_id, bytes);
|
||||
@ -200,7 +213,7 @@ pub fn make_block_meta_revisions(user_id: &str, grid_block_meta_data: &GridBlock
|
||||
|
||||
impl std::default::Default for GridBlockMetaPad {
|
||||
fn default() -> Self {
|
||||
let block_meta_data = GridBlockMetaSerde {
|
||||
let block_meta_data = GridBlockMetaData {
|
||||
block_id: uuid(),
|
||||
row_metas: vec![],
|
||||
};
|
||||
|
@ -41,7 +41,7 @@ fn check_rows(fields: &[FieldMeta], rows: &[RowMeta]) -> CollaborateResult<()> {
|
||||
mod tests {
|
||||
|
||||
use crate::client_grid::{make_block_meta_delta, make_grid_delta, GridBuilder};
|
||||
use flowy_grid_data_model::entities::{FieldMeta, FieldType, GridBlockMetaSerde, GridMeta};
|
||||
use flowy_grid_data_model::entities::{FieldMeta, FieldType, GridBlockMetaData, GridMeta};
|
||||
|
||||
#[test]
|
||||
fn create_default_grid_test() {
|
||||
@ -64,6 +64,6 @@ mod tests {
|
||||
let _: GridMeta = serde_json::from_str(&grid_meta_delta.to_str().unwrap()).unwrap();
|
||||
|
||||
let grid_block_meta_delta = make_block_meta_delta(&build_context.block_meta_data);
|
||||
let _: GridBlockMetaSerde = serde_json::from_str(&grid_block_meta_delta.to_str().unwrap()).unwrap();
|
||||
let _: GridBlockMetaData = serde_json::from_str(&grid_block_meta_delta.to_str().unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -212,7 +212,7 @@ impl GridMetaPad {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_block(&mut self, block: GridBlockMeta) -> CollaborateResult<Option<GridChangeset>> {
|
||||
pub fn create_block_meta(&mut self, block: GridBlockMeta) -> CollaborateResult<Option<GridChangeset>> {
|
||||
self.modify_grid(|grid| {
|
||||
if grid.block_metas.iter().any(|b| b.block_id == block.block_id) {
|
||||
tracing::warn!("Duplicate grid block");
|
||||
@ -235,11 +235,11 @@ impl GridMetaPad {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_blocks(&self) -> Vec<GridBlockMeta> {
|
||||
pub fn get_block_metas(&self) -> Vec<GridBlockMeta> {
|
||||
self.grid_meta.block_metas.clone()
|
||||
}
|
||||
|
||||
pub fn update_block(&mut self, changeset: GridBlockMetaChangeset) -> CollaborateResult<Option<GridChangeset>> {
|
||||
pub fn update_block_meta(&mut self, changeset: GridBlockMetaChangeset) -> CollaborateResult<Option<GridChangeset>> {
|
||||
let block_id = changeset.block_id.clone();
|
||||
self.modify_block(&block_id, |block| {
|
||||
let mut is_changed = None;
|
||||
|
Reference in New Issue
Block a user