530 lines
20 KiB
Rust
Raw Normal View History

2022-03-24 17:09:05 +08:00
use crate::dart_notification::{send_dart_notification, GridNotification};
2022-03-05 10:59:44 +08:00
use crate::manager::GridUser;
2022-04-07 15:34:00 +08:00
use crate::services::block_meta_manager::GridBlockMetaEditorManager;
2022-04-07 20:15:00 +08:00
use crate::services::cell::CellIdentifier;
2022-04-05 21:25:59 +08:00
use crate::services::field::{default_type_option_builder_from_type, type_option_builder_from_bytes, FieldBuilder};
2022-04-05 14:25:07 +08:00
use crate::services::persistence::block_index::BlockIndexPersistence;
2022-03-24 17:09:05 +08:00
use crate::services::row::*;
2022-03-11 21:36:00 +08:00
use bytes::Bytes;
2022-03-27 09:35:10 +08:00
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
2022-03-24 17:09:05 +08:00
use flowy_grid_data_model::entities::*;
use flowy_revision::{RevisionCloudService, RevisionCompactor, RevisionManager, RevisionObjectBuilder};
2022-04-07 08:33:10 +08:00
use flowy_sync::client_grid::{GridChangeset, GridMetaPad, JsonDeserializer};
2022-03-19 16:52:28 +08:00
use flowy_sync::entities::revision::Revision;
use flowy_sync::errors::CollaborateResult;
2022-03-19 16:52:28 +08:00
use flowy_sync::util::make_delta_from_revisions;
2022-03-04 18:11:12 +08:00
use lib_infra::future::FutureResult;
2022-03-12 09:30:13 +08:00
use lib_ot::core::PlainTextAttributes;
2022-03-24 17:09:05 +08:00
use std::collections::HashMap;
2022-03-04 18:11:12 +08:00
use std::sync::Arc;
2022-03-05 10:59:44 +08:00
use tokio::sync::RwLock;
2022-03-04 18:11:12 +08:00
pub struct ClientGridEditor {
2022-03-04 22:09:16 +08:00
grid_id: String,
2022-03-05 10:59:44 +08:00
user: Arc<dyn GridUser>,
2022-03-17 17:25:43 +08:00
pad: Arc<RwLock<GridMetaPad>>,
2022-03-04 18:11:12 +08:00
rev_manager: Arc<RevisionManager>,
2022-03-11 21:36:00 +08:00
block_meta_manager: Arc<GridBlockMetaEditorManager>,
2022-03-04 18:11:12 +08:00
}
impl ClientGridEditor {
pub async fn new(
2022-03-04 22:09:16 +08:00
grid_id: &str,
2022-03-05 10:59:44 +08:00
user: Arc<dyn GridUser>,
mut rev_manager: RevisionManager,
2022-04-05 14:25:07 +08:00
persistence: Arc<BlockIndexPersistence>,
2022-03-04 22:09:16 +08:00
) -> FlowyResult<Arc<Self>> {
2022-03-05 10:59:44 +08:00
let token = user.token()?;
let cloud = Arc::new(GridRevisionCloudService { token });
2022-03-12 21:06:15 +08:00
let grid_pad = rev_manager.load::<GridPadBuilder>(Some(cloud)).await?;
2022-03-04 18:11:12 +08:00
let rev_manager = Arc::new(rev_manager);
2022-03-17 17:25:43 +08:00
let pad = Arc::new(RwLock::new(grid_pad));
2022-04-05 21:25:59 +08:00
let blocks = pad.read().await.get_block_metas();
2022-03-12 09:30:13 +08:00
2022-04-05 14:25:07 +08:00
let block_meta_manager = Arc::new(GridBlockMetaEditorManager::new(grid_id, &user, blocks, persistence).await?);
2022-03-04 22:09:16 +08:00
Ok(Arc::new(Self {
2022-03-05 10:59:44 +08:00
grid_id: grid_id.to_owned(),
user,
2022-03-17 17:25:43 +08:00
pad,
2022-03-04 18:11:12 +08:00
rev_manager,
2022-03-11 21:36:00 +08:00
block_meta_manager,
2022-03-04 22:09:16 +08:00
}))
2022-03-04 18:11:12 +08:00
}
pub async fn insert_field(&self, params: InsertFieldParams) -> FlowyResult<()> {
let InsertFieldParams {
2022-03-25 20:55:56 +08:00
field,
type_option_data,
start_field_id,
2022-03-31 22:51:46 +08:00
grid_id,
2022-03-25 20:55:56 +08:00
} = params;
let field_id = field.id.clone();
2022-04-14 13:29:42 +08:00
if self.contain_field(&field_id).await {
let _ = self
.modify(|grid| {
2022-04-07 08:33:10 +08:00
let deserializer = TypeOptionJsonDeserializer(field.field_type.clone());
2022-03-31 22:51:46 +08:00
let changeset = FieldChangesetParams {
field_id: field.id,
grid_id,
name: Some(field.name),
desc: Some(field.desc),
field_type: Some(field.field_type),
frozen: Some(field.frozen),
visibility: Some(field.visibility),
width: Some(field.width),
type_option_data: Some(type_option_data),
};
2022-04-14 13:29:42 +08:00
Ok(grid.update_field_meta(changeset, deserializer)?)
})
.await?;
let _ = self.notify_grid_did_update_field(&field_id).await?;
} else {
let _ = self
.modify(|grid| {
2022-04-01 16:38:51 +08:00
let builder = type_option_builder_from_bytes(type_option_data, &field.field_type);
let field_meta = FieldBuilder::from_field(field, builder).build();
2022-04-14 13:29:42 +08:00
Ok(grid.create_field_meta(field_meta, start_field_id)?)
})
.await?;
let _ = self.notify_grid_did_insert_field(&field_id).await?;
}
2022-03-04 21:26:32 +08:00
Ok(())
}
2022-04-01 22:46:01 +08:00
pub async fn create_next_field_meta(&self, field_type: &FieldType) -> FlowyResult<FieldMeta> {
2022-03-26 20:27:32 +08:00
let name = format!("Property {}", self.pad.read().await.fields().len() + 1);
let field_meta = FieldBuilder::from_field_type(field_type).name(&name).build();
2022-03-25 15:02:43 +08:00
Ok(field_meta)
2022-03-24 17:09:05 +08:00
}
pub async fn contain_field(&self, field_id: &str) -> bool {
self.pad.read().await.contain_field(field_id)
2022-03-15 19:00:28 +08:00
}
2022-04-01 22:46:01 +08:00
pub async fn update_field(&self, params: FieldChangesetParams) -> FlowyResult<()> {
2022-04-03 22:22:54 +08:00
let field_id = params.field_id.clone();
2022-04-14 13:29:42 +08:00
let json_deserializer = match self.pad.read().await.get_field_meta(params.field_id.as_str()) {
None => return Err(ErrorCode::FieldDoesNotExist.into()),
2022-04-14 13:29:42 +08:00
Some((_, field_meta)) => TypeOptionJsonDeserializer(field_meta.field_type.clone()),
};
2022-03-27 09:35:10 +08:00
2022-04-07 08:33:10 +08:00
let _ = self
2022-04-14 13:29:42 +08:00
.modify(|grid| Ok(grid.update_field_meta(params, json_deserializer)?))
2022-04-07 08:33:10 +08:00
.await?;
2022-04-14 13:29:42 +08:00
let _ = self.notify_grid_did_update_field(&field_id).await?;
2022-03-12 22:52:24 +08:00
Ok(())
}
2022-04-07 08:33:10 +08:00
pub async fn replace_field(&self, field_meta: FieldMeta) -> FlowyResult<()> {
2022-04-08 21:21:42 +08:00
let field_id = field_meta.id.clone();
2022-04-14 13:29:42 +08:00
let _ = self.modify(|pad| Ok(pad.replace_field_meta(field_meta)?)).await?;
let _ = self.notify_grid_did_update_field(&field_id).await?;
2022-04-07 08:33:10 +08:00
Ok(())
}
2022-03-12 21:06:15 +08:00
pub async fn delete_field(&self, field_id: &str) -> FlowyResult<()> {
2022-04-14 13:29:42 +08:00
let _ = self.modify(|grid| Ok(grid.delete_field_meta(field_id)?)).await?;
let field_order = FieldOrder::from(field_id);
let notified_changeset = GridFieldChangeset::delete(&self.grid_id, vec![field_order]);
let _ = self.notify_did_update_grid(notified_changeset).await?;
2022-03-04 21:26:32 +08:00
Ok(())
}
2022-04-01 22:46:01 +08:00
pub async fn switch_to_field_type(&self, field_id: &str, field_type: &FieldType) -> FlowyResult<()> {
2022-04-02 21:17:20 +08:00
// let block_ids = self
// .get_block_metas()
// .await?
// .into_iter()
// .map(|block_meta| block_meta.block_id)
// .collect();
// let cell_metas = self
// .block_meta_manager
// .get_cell_metas(block_ids, field_id, None)
// .await?;
2022-04-02 10:54:01 +08:00
let type_option_json_builder = |field_type: &FieldType| -> String {
return default_type_option_builder_from_type(field_type).entry().json_str();
};
2022-04-01 22:46:01 +08:00
let _ = self
.modify(|grid| Ok(grid.switch_to_field(field_id, field_type.clone(), type_option_json_builder)?))
.await?;
2022-04-14 13:29:42 +08:00
2022-04-14 21:57:00 +08:00
let _ = self.notify_grid_did_update_field(field_id).await?;
2022-04-14 13:29:42 +08:00
2022-04-01 22:46:01 +08:00
Ok(())
}
2022-03-27 11:14:21 +08:00
pub async fn duplicate_field(&self, field_id: &str) -> FlowyResult<()> {
2022-04-14 13:29:42 +08:00
let duplicated_field_id = gen_field_id();
2022-04-13 21:26:27 +08:00
let _ = self
2022-04-14 13:29:42 +08:00
.modify(|grid| Ok(grid.duplicate_field_meta(field_id, &duplicated_field_id)?))
2022-04-13 21:26:27 +08:00
.await?;
2022-04-14 13:29:42 +08:00
let _ = self.notify_grid_did_insert_field(field_id).await?;
2022-03-27 11:14:21 +08:00
Ok(())
}
2022-04-07 08:33:10 +08:00
pub async fn get_field_meta(&self, field_id: &str) -> Option<FieldMeta> {
2022-04-14 13:29:42 +08:00
let field_meta = self.pad.read().await.get_field_meta(field_id)?.1.clone();
2022-04-07 15:34:00 +08:00
Some(field_meta)
2022-04-07 08:33:10 +08:00
}
pub async fn get_field_metas<T>(&self, field_ids: Option<Vec<T>>) -> FlowyResult<Vec<FieldMeta>>
2022-04-05 21:25:59 +08:00
where
T: Into<FieldOrder>,
{
2022-04-07 08:33:10 +08:00
if field_ids.is_none() {
2022-04-05 21:25:59 +08:00
let field_metas = self.pad.read().await.get_field_metas(None)?;
return Ok(field_metas);
}
let to_field_orders = |item: Vec<T>| item.into_iter().map(|data| data.into()).collect();
2022-04-07 08:33:10 +08:00
let field_orders = field_ids.map_or(vec![], to_field_orders);
2022-04-05 21:25:59 +08:00
let expected_len = field_orders.len();
let field_metas = self.pad.read().await.get_field_metas(Some(field_orders))?;
if expected_len != 0 && field_metas.len() != expected_len {
tracing::error!(
"This is a bug. The len of the field_metas should equal to {}",
expected_len
);
debug_assert!(field_metas.len() == expected_len);
2022-03-31 22:51:46 +08:00
}
2022-04-05 21:25:59 +08:00
Ok(field_metas)
2022-03-31 22:51:46 +08:00
}
2022-03-17 17:25:43 +08:00
pub async fn create_block(&self, grid_block: GridBlockMeta) -> FlowyResult<()> {
2022-04-02 10:54:01 +08:00
let _ = self.modify(|grid| Ok(grid.create_block_meta(grid_block)?)).await?;
2022-03-13 11:06:28 +08:00
Ok(())
}
2022-03-17 17:25:43 +08:00
pub async fn update_block(&self, changeset: GridBlockMetaChangeset) -> FlowyResult<()> {
2022-04-02 10:54:01 +08:00
let _ = self.modify(|grid| Ok(grid.update_block_meta(changeset)?)).await?;
2022-03-13 11:06:28 +08:00
Ok(())
}
2022-03-18 21:04:01 +08:00
pub async fn create_row(&self, start_row_id: Option<String>) -> FlowyResult<RowOrder> {
2022-03-17 17:25:43 +08:00
let field_metas = self.pad.read().await.get_field_metas(None)?;
let block_id = self.block_id().await?;
// insert empty row below the row whose id is upper_row_id
2022-03-18 21:04:01 +08:00
let row_meta_ctx = CreateRowMetaBuilder::new(&field_metas).build();
let row_meta = make_row_meta_from_context(&block_id, row_meta_ctx);
let row_order = RowOrder::from(&row_meta);
// insert the row
let row_count = self
.block_meta_manager
2022-03-17 17:25:43 +08:00
.create_row(&block_id, row_meta, start_row_id)
.await?;
// update block row count
2022-03-17 17:25:43 +08:00
let changeset = GridBlockMetaChangeset::from_row_count(&block_id, row_count);
2022-03-14 17:24:25 +08:00
let _ = self.update_block(changeset).await?;
2022-03-18 21:04:01 +08:00
Ok(row_order)
2022-03-14 17:24:25 +08:00
}
2022-03-13 23:16:52 +08:00
2022-03-18 21:04:01 +08:00
pub async fn insert_rows(&self, contexts: Vec<CreateRowMetaPayload>) -> FlowyResult<Vec<RowOrder>> {
2022-03-17 17:25:43 +08:00
let block_id = self.block_id().await?;
2022-03-14 17:24:25 +08:00
let mut rows_by_block_id: HashMap<String, Vec<RowMeta>> = HashMap::new();
2022-03-18 21:04:01 +08:00
let mut row_orders = vec![];
2022-03-14 17:24:25 +08:00
for ctx in contexts {
2022-03-18 21:04:01 +08:00
let row_meta = make_row_meta_from_context(&block_id, ctx);
row_orders.push(RowOrder::from(&row_meta));
2022-03-14 17:24:25 +08:00
rows_by_block_id
.entry(block_id.clone())
2022-03-16 10:02:37 +08:00
.or_insert_with(Vec::new)
2022-03-14 17:24:25 +08:00
.push(row_meta);
}
2022-03-17 17:25:43 +08:00
let changesets = self.block_meta_manager.insert_row(rows_by_block_id).await?;
2022-03-14 17:24:25 +08:00
for changeset in changesets {
let _ = self.update_block(changeset).await?;
}
2022-03-18 21:04:01 +08:00
Ok(row_orders)
2022-03-11 21:36:00 +08:00
}
2022-03-05 17:52:25 +08:00
2022-03-14 17:24:25 +08:00
pub async fn update_row(&self, changeset: RowMetaChangeset) -> FlowyResult<()> {
self.block_meta_manager.update_row(changeset).await
}
2022-03-18 17:14:46 +08:00
pub async fn get_rows(&self, block_id: &str) -> FlowyResult<RepeatedRow> {
let block_ids = vec![block_id.to_owned()];
2022-04-02 10:54:01 +08:00
let mut grid_block_snapshot = self.grid_block_snapshots(Some(block_ids)).await?;
// For the moment, we only support one block.
// We can save the rows into multiple blocks and load them asynchronously in the future.
debug_assert_eq!(grid_block_snapshot.len(), 1);
if grid_block_snapshot.len() == 1 {
let snapshot = grid_block_snapshot.pop().unwrap();
2022-04-05 21:25:59 +08:00
let field_metas = self.get_field_metas::<FieldOrder>(None).await?;
2022-04-02 10:54:01 +08:00
let rows = make_rows_from_row_metas(&field_metas, &snapshot.row_metas);
2022-03-18 17:14:46 +08:00
Ok(rows.into())
} else {
Ok(vec![].into())
}
}
2022-04-05 14:25:07 +08:00
pub async fn get_row(&self, row_id: &str) -> FlowyResult<Option<Row>> {
match self.block_meta_manager.get_row_meta(row_id).await? {
2022-03-18 17:14:46 +08:00
None => Ok(None),
2022-04-05 14:25:07 +08:00
Some(row_meta) => {
2022-04-05 21:25:59 +08:00
let field_metas = self.get_field_metas::<FieldOrder>(None).await?;
2022-04-05 14:25:07 +08:00
let row_metas = vec![row_meta];
2022-03-18 17:14:46 +08:00
let mut rows = make_rows_from_row_metas(&field_metas, &row_metas);
debug_assert!(rows.len() == 1);
Ok(rows.pop())
}
}
}
2022-04-09 22:42:42 +08:00
pub async fn delete_row(&self, row_id: &str) -> FlowyResult<()> {
let _ = self.block_meta_manager.delete_row(row_id).await?;
Ok(())
2022-04-09 22:42:42 +08:00
}
pub async fn duplicate_row(&self, _row_id: &str) -> FlowyResult<()> {
Ok(())
2022-04-09 22:42:42 +08:00
}
2022-03-18 17:14:46 +08:00
2022-04-07 20:15:00 +08:00
pub async fn get_cell(&self, params: &CellIdentifier) -> Option<Cell> {
let field_meta = self.get_field_meta(&params.field_id).await?;
let row_meta = self.block_meta_manager.get_row_meta(&params.row_id).await.ok()??;
make_cell(&params.field_id, &field_meta, &row_meta)
}
2022-04-05 14:25:07 +08:00
pub async fn get_cell_meta(&self, row_id: &str, field_id: &str) -> FlowyResult<Option<CellMeta>> {
let row_meta = self.block_meta_manager.get_row_meta(row_id).await?;
match row_meta {
None => Ok(None),
Some(row_meta) => {
2022-04-09 07:35:35 +08:00
let cell_meta = row_meta.cells.get(field_id).cloned();
2022-04-05 14:25:07 +08:00
Ok(cell_meta)
}
}
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub async fn update_cell(&self, mut changeset: CellChangeset) -> FlowyResult<()> {
2022-04-05 21:25:59 +08:00
if changeset.data.as_ref().is_none() {
return Ok(());
2022-03-16 10:02:37 +08:00
}
2022-04-05 21:25:59 +08:00
let cell_data_changeset = changeset.data.unwrap();
let cell_meta = self.get_cell_meta(&changeset.row_id, &changeset.field_id).await?;
tracing::trace!("{}: {:?}", &changeset.field_id, cell_meta);
2022-04-14 13:29:42 +08:00
match self.pad.read().await.get_field_meta(&changeset.field_id) {
2022-04-05 21:25:59 +08:00
None => {
let msg = format!("Field not found with id: {}", &changeset.field_id);
2022-04-07 15:34:00 +08:00
Err(FlowyError::internal().context(msg))
2022-04-05 21:25:59 +08:00
}
2022-04-14 13:29:42 +08:00
Some((_, field_meta)) => {
2022-04-05 21:25:59 +08:00
// Update the changeset.data property with the return value.
changeset.data = Some(apply_cell_data_changeset(cell_data_changeset, cell_meta, field_meta)?);
2022-04-06 10:32:08 +08:00
let _ = self.block_meta_manager.update_cell(changeset).await?;
2022-04-05 21:25:59 +08:00
Ok(())
}
}
2022-03-05 17:52:25 +08:00
}
2022-03-18 17:14:46 +08:00
pub async fn get_blocks(&self, block_ids: Option<Vec<String>>) -> FlowyResult<RepeatedGridBlock> {
2022-04-02 10:54:01 +08:00
let block_snapshots = self.grid_block_snapshots(block_ids.clone()).await?;
make_grid_blocks(block_ids, block_snapshots)
2022-03-13 23:16:52 +08:00
}
2022-03-18 17:14:46 +08:00
pub async fn get_block_metas(&self) -> FlowyResult<Vec<GridBlockMeta>> {
2022-04-02 10:54:01 +08:00
let grid_blocks = self.pad.read().await.get_block_metas();
2022-03-18 17:14:46 +08:00
Ok(grid_blocks)
2022-03-14 17:24:25 +08:00
}
2022-03-13 11:06:28 +08:00
2022-03-18 21:04:01 +08:00
pub async fn delete_rows(&self, row_orders: Vec<RowOrder>) -> FlowyResult<()> {
let changesets = self.block_meta_manager.delete_rows(row_orders).await?;
2022-03-14 17:24:25 +08:00
for changeset in changesets {
let _ = self.update_block(changeset).await?;
}
2022-03-13 11:06:28 +08:00
Ok(())
2022-03-05 17:52:25 +08:00
}
2022-03-15 11:07:18 +08:00
pub async fn grid_data(&self) -> FlowyResult<Grid> {
2022-04-10 08:25:01 +08:00
let pad_read_guard = self.pad.read().await;
let field_orders = pad_read_guard.get_field_orders();
let mut block_orders = vec![];
for block_order in pad_read_guard.get_block_metas() {
let row_orders = self.block_meta_manager.get_row_orders(&block_order.block_id).await?;
let block_order = GridBlockOrder {
block_id: block_order.block_id,
row_orders,
};
block_orders.push(block_order);
}
2022-03-15 11:07:18 +08:00
Ok(Grid {
id: self.grid_id.clone(),
field_orders,
2022-03-18 17:14:46 +08:00
block_orders,
2022-03-15 11:07:18 +08:00
})
2022-03-11 21:36:00 +08:00
}
2022-04-02 10:54:01 +08:00
pub async fn grid_block_snapshots(&self, block_ids: Option<Vec<String>>) -> FlowyResult<Vec<GridBlockSnapshot>> {
2022-04-02 21:17:20 +08:00
let block_ids = match block_ids {
2022-04-02 23:48:56 +08:00
None => self
.pad
2022-04-02 10:54:01 +08:00
.read()
.await
.get_block_metas()
.into_iter()
.map(|block_meta| block_meta.block_id)
.collect::<Vec<String>>(),
2022-04-02 21:17:20 +08:00
Some(block_ids) => block_ids,
};
2022-04-02 10:54:01 +08:00
let snapshots = self.block_meta_manager.make_block_snapshots(block_ids).await?;
Ok(snapshots)
2022-03-13 11:06:28 +08:00
}
2022-04-13 14:24:54 +08:00
pub async fn move_item(&self, params: MoveItemParams) -> FlowyResult<()> {
match params.ty {
MoveItemType::MoveField => {
self.move_field(params.from_index, params.to_index, &params.item_id)
.await
}
MoveItemType::MoveRow => self.move_row(params.from_index, params.to_index, &params.item_id).await,
}
}
pub async fn move_field(&self, from: i32, to: i32, field_id: &str) -> FlowyResult<()> {
2022-04-13 21:26:27 +08:00
// GridFieldChangeset
2022-04-13 14:24:54 +08:00
todo!()
}
pub async fn move_row(&self, from: i32, to: i32, row_id: &str) -> FlowyResult<()> {
2022-04-13 21:26:27 +08:00
// GridRowsChangeset
2022-04-13 14:24:54 +08:00
todo!()
}
2022-03-15 19:00:28 +08:00
pub async fn delta_bytes(&self) -> Bytes {
2022-03-17 17:25:43 +08:00
self.pad.read().await.delta_bytes()
2022-03-05 22:30:42 +08:00
}
2022-03-04 21:26:32 +08:00
async fn modify<F>(&self, f: F) -> FlowyResult<()>
2022-03-04 18:11:12 +08:00
where
2022-03-16 21:19:51 +08:00
F: for<'a> FnOnce(&'a mut GridMetaPad) -> FlowyResult<Option<GridChangeset>>,
2022-03-04 18:11:12 +08:00
{
2022-03-17 17:25:43 +08:00
let mut write_guard = self.pad.write().await;
2022-04-08 15:13:53 +08:00
if let Some(changeset) = f(&mut *write_guard)? {
let _ = self.apply_change(changeset).await?;
2022-03-04 18:11:12 +08:00
}
Ok(())
}
2022-03-16 21:19:51 +08:00
async fn apply_change(&self, change: GridChangeset) -> FlowyResult<()> {
let GridChangeset { delta, md5 } = change;
2022-03-05 10:59:44 +08:00
let user_id = self.user.user_id()?;
2022-03-04 18:11:12 +08:00
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
2022-03-15 19:00:28 +08:00
let delta_data = delta.to_delta_bytes();
2022-03-04 18:11:12 +08:00
let revision = Revision::new(
&self.rev_manager.object_id,
base_rev_id,
rev_id,
delta_data,
2022-03-05 10:59:44 +08:00
&user_id,
2022-03-04 18:11:12 +08:00
md5,
);
let _ = self
.rev_manager
2022-03-11 21:36:00 +08:00
.add_local_revision(&revision, Box::new(GridRevisionCompactor()))
2022-03-04 18:11:12 +08:00
.await?;
Ok(())
}
2022-03-14 17:24:25 +08:00
2022-03-17 17:25:43 +08:00
async fn block_id(&self) -> FlowyResult<String> {
2022-04-02 10:54:01 +08:00
match self.pad.read().await.get_block_metas().last() {
2022-03-14 17:24:25 +08:00
None => Err(FlowyError::internal().context("There is no grid block in this grid")),
2022-03-17 17:25:43 +08:00
Some(grid_block) => Ok(grid_block.block_id.clone()),
2022-03-14 17:24:25 +08:00
}
}
2022-03-16 21:19:51 +08:00
2022-04-14 13:29:42 +08:00
#[tracing::instrument(level = "trace", skip_all, err)]
async fn notify_grid_did_insert_field(&self, field_id: &str) -> FlowyResult<()> {
if let Some((index, field_meta)) = self.pad.read().await.get_field_meta(field_id) {
let index_field = IndexField::from_field_meta(field_meta, index);
let notified_changeset = GridFieldChangeset::insert(&self.grid_id, vec![index_field]);
let _ = self.notify_did_update_grid(notified_changeset).await?;
}
2022-04-13 21:26:27 +08:00
Ok(())
}
#[tracing::instrument(level = "trace", skip_all, err)]
2022-04-14 13:29:42 +08:00
async fn notify_grid_did_update_field(&self, field_id: &str) -> FlowyResult<()> {
2022-04-05 21:25:59 +08:00
let mut field_metas = self.get_field_metas(Some(vec![field_id])).await?;
2022-04-03 22:22:54 +08:00
debug_assert!(field_metas.len() == 1);
if let Some(field_meta) = field_metas.pop() {
2022-04-14 13:29:42 +08:00
let updated_field = Field::from(field_meta);
let notified_changeset = GridFieldChangeset::update(&self.grid_id, vec![updated_field.clone()]);
let _ = self.notify_did_update_grid(notified_changeset).await?;
send_dart_notification(field_id, GridNotification::DidUpdateField)
2022-04-14 13:29:42 +08:00
.payload(updated_field)
2022-04-03 22:22:54 +08:00
.send();
}
Ok(())
}
2022-04-14 13:29:42 +08:00
async fn notify_did_update_grid(&self, changeset: GridFieldChangeset) -> FlowyResult<()> {
2022-04-14 21:16:29 +08:00
send_dart_notification(&self.grid_id, GridNotification::DidUpdateGridField)
2022-04-14 13:29:42 +08:00
.payload(changeset)
.send();
Ok(())
}
2022-03-04 18:11:12 +08:00
}
2022-03-12 21:06:15 +08:00
#[cfg(feature = "flowy_unit_test")]
impl ClientGridEditor {
pub fn rev_manager(&self) -> Arc<RevisionManager> {
self.rev_manager.clone()
}
}
pub struct GridPadBuilder();
2022-03-04 18:11:12 +08:00
impl RevisionObjectBuilder for GridPadBuilder {
2022-03-10 12:01:31 +08:00
type Output = GridMetaPad;
2022-03-04 18:11:12 +08:00
2022-03-05 10:59:44 +08:00
fn build_object(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output> {
2022-03-10 12:01:31 +08:00
let pad = GridMetaPad::from_revisions(object_id, revisions)?;
2022-03-04 18:11:12 +08:00
Ok(pad)
}
}
struct GridRevisionCloudService {
#[allow(dead_code)]
token: String,
}
impl RevisionCloudService for GridRevisionCloudService {
#[tracing::instrument(level = "trace", skip(self))]
fn fetch_object(&self, _user_id: &str, _object_id: &str) -> FutureResult<Vec<Revision>, FlowyError> {
FutureResult::new(async move { Ok(vec![]) })
}
}
2022-03-11 21:36:00 +08:00
struct GridRevisionCompactor();
impl RevisionCompactor for GridRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
2022-03-15 19:00:28 +08:00
Ok(delta.to_delta_bytes())
2022-03-11 21:36:00 +08:00
}
}
2022-04-07 08:33:10 +08:00
struct TypeOptionJsonDeserializer(FieldType);
impl JsonDeserializer for TypeOptionJsonDeserializer {
fn deserialize(&self, type_option_data: Vec<u8>) -> CollaborateResult<String> {
2022-04-07 08:33:10 +08:00
// The type_option_data sent from Dart is serialized by protobuf.
2022-04-01 16:38:51 +08:00
let builder = type_option_builder_from_bytes(type_option_data, &self.0);
let json = builder.entry().json_str();
tracing::trace!("Deserialize type option data to: {}", json);
Ok(json)
}
}