chore: create grid view when create grid

This commit is contained in:
appflowy 2022-08-15 23:17:29 +08:00
parent 0514b005ca
commit 461160094c
6 changed files with 56 additions and 22 deletions

View File

@ -31,7 +31,7 @@ class BoardPluginBuilder implements PluginBuilder {
class BoardPluginConfig implements PluginConfig {
@override
bool get creatable => true;
bool get creatable => false;
}
class BoardPlugin extends Plugin {

View File

@ -1,5 +1,6 @@
use crate::services::block_editor::GridBlockRevisionCompactor;
use crate::services::grid_editor::{GridRevisionCompactor, GridRevisionEditor};
use crate::services::grid_view_manager::make_grid_view_rev_manager;
use crate::services::persistence::block_index::BlockIndexCache;
use crate::services::persistence::kv::GridKVPersistence;
use crate::services::persistence::migration::GridMigration;
@ -9,10 +10,10 @@ use bytes::Bytes;
use dashmap::DashMap;
use flowy_database::ConnectionPool;
use flowy_error::{FlowyError, FlowyResult};
use flowy_grid_data_model::revision::{BuildGridContext, GridRevision};
use flowy_grid_data_model::revision::{BuildGridContext, GridRevision, GridViewRevision};
use flowy_revision::disk::{SQLiteGridBlockRevisionPersistence, SQLiteGridRevisionPersistence};
use flowy_revision::{RevisionManager, RevisionPersistence, RevisionWebSocket, SQLiteRevisionSnapshotPersistence};
use flowy_sync::client_grid::{make_grid_block_delta, make_grid_delta};
use flowy_sync::client_grid::{make_grid_block_delta, make_grid_delta, make_grid_view_delta};
use flowy_sync::entities::revision::{RepeatedRevision, Revision};
use std::sync::Arc;
use tokio::sync::RwLock;
@ -70,6 +71,15 @@ impl GridManager {
let db_pool = self.grid_user.db_pool()?;
let rev_manager = self.make_grid_rev_manager(grid_id, db_pool)?;
let _ = rev_manager.reset_object(revisions).await?;
Ok(())
}
#[tracing::instrument(level = "debug", skip_all, err)]
async fn create_grid_view<T: AsRef<str>>(&self, view_id: T, revisions: RepeatedRevision) -> FlowyResult<()> {
let view_id = view_id.as_ref();
let rev_manager = make_grid_view_rev_manager(&self.grid_user, view_id).await?;
let _ = rev_manager.reset_object(revisions).await?;
Ok(())
}
@ -198,14 +208,23 @@ pub async fn make_grid_view_data(
let _ = grid_manager.create_grid_block(&block_id, repeated_revision).await?;
}
let grid_rev = GridRevision::from_build_context(view_id, build_context);
let grid_id = view_id.to_owned();
let grid_rev = GridRevision::from_build_context(&grid_id, build_context);
// Create grid
let grid_meta_delta = make_grid_delta(&grid_rev);
let grid_delta_data = grid_meta_delta.json_bytes();
let grid_rev_delta = make_grid_delta(&grid_rev);
let grid_rev_delta_bytes = grid_rev_delta.json_bytes();
let repeated_revision: RepeatedRevision =
Revision::initial_revision(user_id, view_id, grid_delta_data.clone()).into();
let _ = grid_manager.create_grid(view_id, repeated_revision).await?;
Revision::initial_revision(user_id, &grid_id, grid_rev_delta_bytes.clone()).into();
let _ = grid_manager.create_grid(&grid_id, repeated_revision).await?;
Ok(grid_delta_data)
// Create grid view
let grid_view = GridViewRevision::new(view_id.to_owned(), view_id.to_owned());
let grid_view_delta = make_grid_view_delta(&grid_view);
let grid_view_delta_bytes = grid_view_delta.json_bytes();
let repeated_revision: RepeatedRevision =
Revision::initial_revision(user_id, view_id, grid_view_delta_bytes).into();
let _ = grid_manager.create_grid_view(view_id, repeated_revision).await?;
Ok(grid_rev_delta_bytes)
}

View File

@ -140,18 +140,31 @@ where
DataSource: GridViewRevisionDataSource,
{
tracing::trace!("Open view:{} editor", view_id);
let rev_manager = make_grid_view_rev_manager(user, view_id).await?;
let user_id = user.user_id()?;
let token = user.token()?;
let view_id = view_id.to_owned();
GridViewRevisionEditor::new(&user_id, &token, view_id, delegate, data_source, scheduler, rev_manager).await
}
pub async fn make_grid_view_rev_manager(user: &Arc<dyn GridUser>, view_id: &str) -> FlowyResult<RevisionManager> {
tracing::trace!("Open view:{} editor", view_id);
let user_id = user.user_id()?;
let pool = user.db_pool()?;
let view_id = view_id.to_owned();
let disk_cache = SQLiteGridViewRevisionPersistence::new(&user_id, pool.clone());
let rev_persistence = RevisionPersistence::new(&user_id, &view_id, disk_cache);
let rev_persistence = RevisionPersistence::new(&user_id, view_id, disk_cache);
let rev_compactor = GridViewRevisionCompactor();
let snapshot_persistence = SQLiteRevisionSnapshotPersistence::new(&view_id, pool);
let rev_manager = RevisionManager::new(&user_id, &view_id, rev_persistence, rev_compactor, snapshot_persistence);
GridViewRevisionEditor::new(&user_id, &token, view_id, delegate, data_source, scheduler, rev_manager).await
let snapshot_persistence = SQLiteRevisionSnapshotPersistence::new(view_id, pool);
Ok(RevisionManager::new(
&user_id,
view_id,
rev_persistence,
rev_compactor,
snapshot_persistence,
))
}
pub struct GridViewRevisionCompactor();

View File

@ -22,9 +22,9 @@ pub struct GridViewRevision {
}
impl GridViewRevision {
pub fn new(grid_id: String) -> Self {
pub fn new(grid_id: String, view_id: String) -> Self {
GridViewRevision {
view_id: grid_id.clone(),
view_id,
grid_id,
setting: Default::default(),
row_orders: vec![],

View File

@ -6,8 +6,5 @@ fn grid_default_serde_test() {
let grid = GridRevision::new(&grid_id);
let json = serde_json::to_string(&grid).unwrap();
assert_eq!(
json,
r#"{"grid_id":"1","fields":[],"blocks":[],"setting":{"layout":0,"filters":[],"groups":[]}}"#
)
assert_eq!(json, r#"{"grid_id":"1","fields":[],"blocks":[]}"#)
}

View File

@ -25,8 +25,8 @@ impl std::ops::Deref for GridViewRevisionPad {
}
impl GridViewRevisionPad {
pub fn new(grid_id: String) -> Self {
let view = Arc::new(GridViewRevision::new(grid_id));
pub fn new(grid_id: String, view_id: String) -> Self {
let view = Arc::new(GridViewRevision::new(grid_id, view_id));
let json = serde_json::to_string(&view).unwrap();
let delta = TextDeltaBuilder::new().insert(&json).build();
Self { view, delta }
@ -244,3 +244,8 @@ pub fn make_grid_view_rev_json_str(grid_revision: &GridViewRevision) -> Collabor
.map_err(|err| internal_error(format!("Serialize grid view to json str failed. {:?}", err)))?;
Ok(json)
}
pub fn make_grid_view_delta(grid_view: &GridViewRevision) -> TextDelta {
let json = serde_json::to_string(grid_view).unwrap();
TextDeltaBuilder::new().insert(&json).build()
}